summaryrefslogtreecommitdiffstatshomepage
path: root/examples/usercmodule/cppexample/example.cpp
diff options
context:
space:
mode:
authorstijn <stijn@ignitron.net>2020-10-21 11:13:47 +0200
committerDamien George <damien@micropython.org>2020-10-29 15:30:42 +1100
commit25c4563f26c2270cde01b01dca0e8b801c9c8282 (patch)
tree893f7160c558bceb288f06642d2fae440d51014d /examples/usercmodule/cppexample/example.cpp
parentfad4079778f46bc21dd19a674b31b4c3c7eb6a91 (diff)
downloadmicropython-25c4563f26c2270cde01b01dca0e8b801c9c8282.tar.gz
micropython-25c4563f26c2270cde01b01dca0e8b801c9c8282.zip
examples: Add example code for user C modules, both C and C++.
Add working example code to provide a starting point for users with files that they can just copy, and include the modules in the coverage test to verify the complete user C module build functionality. The cexample module uses the code originally found in cmodules.rst, which has been updated to reflect this and partially rewritten with more complete information.
Diffstat (limited to 'examples/usercmodule/cppexample/example.cpp')
-rw-r--r--examples/usercmodule/cppexample/example.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/examples/usercmodule/cppexample/example.cpp b/examples/usercmodule/cppexample/example.cpp
new file mode 100644
index 0000000000..06809732a4
--- /dev/null
+++ b/examples/usercmodule/cppexample/example.cpp
@@ -0,0 +1,17 @@
+extern "C" {
+#include <examplemodule.h>
+
+// Here we implement the function using C++ code, but since it's
+// declaration has to be compatible with C everything goes in extern "C" scope.
+mp_obj_t cppfunc(mp_obj_t a_obj, mp_obj_t b_obj) {
+ // Prove we have (at least) C++11 features.
+ const auto a = mp_obj_get_int(a_obj);
+ const auto b = mp_obj_get_int(b_obj);
+ const auto sum = [&]() {
+ return mp_obj_new_int(a + b);
+ } ();
+ // Prove we're being scanned for QSTRs.
+ mp_obj_t tup[] = {sum, MP_ROM_QSTR(MP_QSTR_hellocpp)};
+ return mp_obj_new_tuple(2, tup);
+}
+}