From aa14d78a3a64289583e3bebc0315365aed6e03a4 Mon Sep 17 00:00:00 2001 From: Stefanus Du Toit Date: Wed, 17 Sep 2014 09:32:21 -0400 Subject: [PATCH] Add code for "Hourglass Interfaces for C++ APIs". --- .../code/CMakeLists.txt | 10 ++ .../code/README.md | 10 ++ .../code/enum_example.h | 6 + .../code/hairpoll.cpp | 134 ++++++++++++++++++ .../code/hairpoll.h | 47 ++++++ .../code/hairpoll.py | 36 +++++ .../code/poller.cpp | 133 +++++++++++++++++ .../code/visibility.h | 23 +++ 8 files changed, 399 insertions(+) create mode 100644 Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/CMakeLists.txt create mode 100644 Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/README.md create mode 100644 Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/enum_example.h create mode 100644 Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/hairpoll.cpp create mode 100644 Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/hairpoll.h create mode 100644 Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/hairpoll.py create mode 100644 Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/poller.cpp create mode 100644 Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/visibility.h diff --git a/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/CMakeLists.txt b/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/CMakeLists.txt new file mode 100644 index 0000000..ae33adf --- /dev/null +++ b/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.0.0) +project(hairpoll) + +add_library(hairpoll SHARED hairpoll.cpp) +target_compile_options(hairpoll PRIVATE -std=c++1y) +target_compile_options(hairpoll PRIVATE -fvisibility=hidden) + +add_executable(poller poller.cpp) +target_compile_options(poller PRIVATE -std=c++1y) +target_link_libraries(poller hairpoll) diff --git a/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/README.md b/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/README.md new file mode 100644 index 0000000..2596ac7 --- /dev/null +++ b/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/README.md @@ -0,0 +1,10 @@ +This folder contains the example code for the CppCon 2014 presentation +"Hourglass interfaces for C++ APIs". + +The example code in this folder is placed in the public domain and is +therefore free of copyright restrictions. + +The code has been built and run under Mac OS X using the Clang compiler, +and can be built with `cmake . && make`. + +-- Stefanus Du Toit, September 2014 diff --git a/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/enum_example.h b/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/enum_example.h new file mode 100644 index 0000000..8d6560d --- /dev/null +++ b/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/enum_example.h @@ -0,0 +1,6 @@ +typedef enum motu_alignment { + heroic_warrior = 0, + evil_warrior = 1, +} motu_alignment; + +int32_t motu_count(motu_alignment a); diff --git a/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/hairpoll.cpp b/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/hairpoll.cpp new file mode 100644 index 0000000..ad7ebe8 --- /dev/null +++ b/Presentations/Hourglass Interfaces for C++ APIs - Stefanus Du Toit - CppCon 2014/code/hairpoll.cpp @@ -0,0 +1,134 @@ +#include "hairpoll.h" + +#include +#include +#include +#include + +extern "C" { + +struct error { + std::string message; +}; + +const char* error_message(error_t error) +{ + return error->message.c_str(); +} + +void error_destruct(error_t error) +{ + delete error; +} + +} + +/// Returns true if fn executed without throwing an error, false otherwise. +/// If calling fn threw an error, capture it in *out_error. +template +bool translateExceptions(error_t* out_error, Fn&& fn) +{ + try { + fn(); + } catch (const std::exception& e) { + *out_error = new error{e.what()}; + return false; + } catch (...) { + *out_error = new error{"Unknown internal error"}; + return false; + } + return true; +} + +/// +/// hairpoll-related bits +/// + +class Poll { +public: + Poll(std::string person) + : person(person) + { + } + + struct option { + option(std::string name, std::string url) + : name(name) + , url(url) + , votes(0) + { + } + + std::string name; + std::string url; + int votes; + }; + + std::string person; + std::vector