This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new d2e923cf6 docs: add basic example for cpp binding (#3108)
d2e923cf6 is described below

commit d2e923cf628ebcd16562df6ff1442eae393f9078
Author: Mingzhuo Yin <[email protected]>
AuthorDate: Mon Sep 18 13:37:28 2023 +0800

    docs: add basic example for cpp binding (#3108)
    
    * chore: add basic example for cpp binding
    
    Signed-off-by: silver-ymz <[email protected]>
    
    * typo
    
    Signed-off-by: silver-ymz <[email protected]>
    
    * typo
    
    Signed-off-by: silver-ymz <[email protected]>
    
    * use exist commit
    
    Signed-off-by: silver-ymz <[email protected]>
    
    ---------
    
    Signed-off-by: silver-ymz <[email protected]>
---
 examples/README.md          |  1 +
 examples/cpp/CMakeLists.txt | 21 +++++++++++++++++++++
 examples/cpp/README.md      | 13 +++++++++++++
 examples/cpp/basic.cpp      | 25 +++++++++++++++++++++++++
 4 files changed, 60 insertions(+)

diff --git a/examples/README.md b/examples/README.md
index f1c0b202f..a072e882b 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -7,3 +7,4 @@ The goal of these documents is to provide you with everything 
you need to start
 ## Examples
 
 - [Rust](rust/README.md)
+- [C++](cpp/README.md)
\ No newline at end of file
diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt
new file mode 100644
index 000000000..ba6948e29
--- /dev/null
+++ b/examples/cpp/CMakeLists.txt
@@ -0,0 +1,21 @@
+cmake_minimum_required(VERSION 3.10)
+project(opendal-cpp-examples)
+
+set(CMAKE_CXX_STANDARD 17)
+
+include(FetchContent)
+FetchContent_Declare(
+  opendal-cpp
+  GIT_REPOSITORY https://github.com/apache/incubator-opendal.git
+  GIT_TAG        4b02228f7fe9a7c0f21a1660fee95716910c7a0a
+  SOURCE_SUBDIR  bindings/cpp
+)
+FetchContent_MakeAvailable(opendal-cpp)
+
+add_executable(basic-example basic.cpp)
+target_link_libraries(basic-example opendal_cpp)
+include_directories(basic-example opendal_cpp)
+
+if(APPLE)
+    target_link_libraries(basic-example "-framework CoreFoundation -framework 
Security")
+endif()
\ No newline at end of file
diff --git a/examples/cpp/README.md b/examples/cpp/README.md
new file mode 100644
index 000000000..2dfdcdad7
--- /dev/null
+++ b/examples/cpp/README.md
@@ -0,0 +1,13 @@
+# OpenDAL Cpp Examples
+
+Thank you for using OpenDAL Cpp Core!
+
+## Run Examples
+
+```bash
+mkdir build
+cd build
+cmake ..
+make
+./basic-example
+```
\ No newline at end of file
diff --git a/examples/cpp/basic.cpp b/examples/cpp/basic.cpp
new file mode 100644
index 000000000..2ebb1504f
--- /dev/null
+++ b/examples/cpp/basic.cpp
@@ -0,0 +1,25 @@
+#include "opendal.hpp"
+
+#include <string>
+#include <vector>
+#include <iostream>
+
+int main() {
+  char s[] = "memory";
+  std::vector<uint8_t> data = {'a', 'b', 'c'};
+
+  // Init operator
+  opendal::Operator op = opendal::Operator(s);
+
+  // Write data to operator
+  op.write("test", data);
+
+  // Read data from operator
+  auto res = op.read("test"); // res == data
+
+  // Using reader
+  auto reader = op.reader("test");
+  opendal::ReaderStream stream(reader);
+  std::string res2;
+  stream >> res2; // res2 == "abc"
+}
\ No newline at end of file

Reply via email to