Turi Create  4.0
registration_macros.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef CPPIPC_REGISTRATION_MACROS_HPP
7 #define CPPIPC_REGISTRATION_MACROS_HPP
8 
9 /**
10  * \ingroup cppipc
11  * Macros which help member function registration for IPC objects.
12  * For instance, given the following base class which we would like to export.
13  * \code
14  * class object_base: public cppipc::ipc_object_base {
15  * public:
16  * virtual ~object_base() { }
17  * virtual std::string ping(std::string) = 0;
18  * virtual int add_one(int) = 0;
19  * virtual int add_one(int, int) = 0;
20  * };
21  * \endcode
22  *
23  * We simply introduce a REGISTRATION_BEGIN inside the public section of the
24  * object, and call REGISTER once for each member function to export.
25  * The result object definition is as follows:
26  * \code
27  * class object_base {
28  * public:
29  * virtual ~object_base() { }
30  * virtual std::string ping(std::string) = 0;
31  * virtual int add_one(int) = 0;
32  * virtual int add_one(int, int) = 0;
33  *
34  *
35  * REGISTRATION_BEGIN(object_base) // argument is a name which must
36  * // uniquely identify the object
37  * REGISTER(object_base::ping)
38  * REGISTER(object_base::add)
39  * REGISTER(object_base::add_one)
40  * REGISTRATION_END
41  * };
42  * \endcode
43  *
44  * The registration macros adds 2 static functions. The first function is
45  * \code
46  * static inline std::string __get_type_name__() {
47  * return name;
48  * }
49  * \endcode
50  * Where "name" is the argument provided to REGISTRATION_BEGIN macro. This
51  * name is used for object creation and must uniquely identify the type of the
52  * object.
53  *
54  * The second function is
55  * \code
56  * template <typename Registry>
57  * static inline void __register__(Registry& reg) {
58  * reg.register_function(&object_base::ping, "object_base::ping");
59  * reg.register_function(&object_base::add, "object_base::add");
60  * reg.register_function(&object_base::add_one, "object_base::add_one");
61  * }
62  * \endcode
63  * and is used by both the client side and the server side to identify and name
64  * the member functions available.
65  *
66  * \see GENERATE_INTERFACE
67  * \see GENERATE_INTERFACE_AND_PROXY
68  */
69 #define REGISTRATION_BEGIN(name) \
70  static inline std::string __get_type_name__() { \
71  return #name; \
72  } \
73  template <typename Registry> \
74  static inline void __register__(Registry& reg) {
75 
76 #define XSTRINGIFY(s) STRINGIFY(s)
77 #define STRINGIFY(s) #s
78 
79  /*
80 #define REGISTER(FN) { \
81  int status; \
82  char* demangled_name; \
83  demangled_name = abi::__cxa_demangle(typeid(decltype(&FN)).name(), 0, 0, &status); \
84  reg.register_function(&FN, std::string(XSTRINGIFY(FN)) + " " + demangled_name); \
85  free(demangled_name); \
86  }
87 */
88 #define REGISTER(FN) { \
89  reg.register_function(&FN, std::string(XSTRINGIFY(FN))); \
90 }
91 
92 
93 
94 #define REGISTRATION_END }
95 
96 #endif //CPPIPC_REGISTRATION_MACROS_HPP