Turi Create  4.0
capi_wrapper_structs.hpp
1 /* Copyright © 2018 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 TURI_CAPI_WRAPPER_STRUCTS_HPP_
7 #define TURI_CAPI_WRAPPER_STRUCTS_HPP_
8 
9 #include <capi/TuriCreate.h>
10 #include <model_server/lib/variant.hpp>
11 #include <model_server/lib/variant_converter.hpp>
12 #include <core/data/flexible_type/flexible_type.hpp>
13 #include <model_server/lib/extensions/model_base.hpp>
14 #include <core/data/sframe/gl_sarray.hpp>
15 #include <core/data/sframe/gl_sframe.hpp>
16 #include <utility>
17 
18 struct capi_struct_type_info {
19  virtual const char* name() const = 0;
20  virtual void free(const void* v) = 0;
21 
22  // TODO: memory pool implementations, etc.
23 };
24 
25 // Each C API wrapper struct is intended to match a c++ container type that
26 // allows it to interface to pure C easily. As such, each wrapper struct has the
27 // an instance of the wrapping type stored as the value field, and type_info to hold
28 // some type information associated with it for error checking and, in the
29 // future, memory management.
30 //
31 // Creation of a wrapping struct should be done with new_* methods.
32 
33 #define DECLARE_CAPI_WRAPPER_STRUCT(struct_name, wrapping_type) \
34  struct capi_struct_type_info_##struct_name; \
35  \
36  /* The typeinfo executor needs to have a singleton instance. */ \
37  extern capi_struct_type_info_##struct_name \
38  capi_struct_type_info_##struct_name##_inst; \
39  \
40  extern "C" { \
41  \
42  struct struct_name##_struct { \
43  capi_struct_type_info* type_info = nullptr; \
44  wrapping_type value; \
45  }; \
46  \
47  typedef struct struct_name##_struct struct_name; \
48  } \
49  \
50  struct capi_struct_type_info_##struct_name : public capi_struct_type_info { \
51  const char* name() const { return #struct_name; } \
52  void free(const void* v) { \
53  if (UNLIKELY(v == nullptr)) { \
54  return; \
55  } \
56  const struct_name* vv = static_cast<const struct_name*>(v); \
57  ASSERT_TRUE(vv->type_info == this); \
58  ASSERT_TRUE(this == &(capi_struct_type_info_##struct_name##_inst)); \
59  delete vv; \
60  } \
61  }; \
62  \
63  static inline struct_name* new_##struct_name() { \
64  struct_name* ret = new struct_name##_struct(); \
65  ret->type_info = &(capi_struct_type_info_##struct_name##_inst); \
66  return ret; \
67  } \
68  \
69  template <typename... Args> \
70  static inline struct_name* new_##struct_name(Args&&... args) { \
71  struct_name* ret = new_##struct_name(); \
72  ret->value = wrapping_type(std::forward<Args>(args)...); \
73  return ret; \
74  }
75 
76 typedef std::map<std::string, turi::aggregate::groupby_descriptor_type> groupby_aggregator_map_type;
77 
78 DECLARE_CAPI_WRAPPER_STRUCT(tc_error, std::string);
79 DECLARE_CAPI_WRAPPER_STRUCT(tc_datetime, turi::flex_date_time);
80 DECLARE_CAPI_WRAPPER_STRUCT(tc_flex_dict, turi::flex_dict);
81 DECLARE_CAPI_WRAPPER_STRUCT(tc_flex_list, turi::flex_list);
82 DECLARE_CAPI_WRAPPER_STRUCT(tc_flex_image, turi::flex_image);
83 DECLARE_CAPI_WRAPPER_STRUCT(tc_ndarray, turi::flex_nd_vec);
84 DECLARE_CAPI_WRAPPER_STRUCT(tc_flexible_type, turi::flexible_type);
85 DECLARE_CAPI_WRAPPER_STRUCT(tc_flex_enum_list, std::vector<turi::flex_type_enum>);
86 DECLARE_CAPI_WRAPPER_STRUCT(tc_sarray, turi::gl_sarray);
87 DECLARE_CAPI_WRAPPER_STRUCT(tc_sframe, turi::gl_sframe);
88 DECLARE_CAPI_WRAPPER_STRUCT(tc_variant, turi::variant_type);
89 DECLARE_CAPI_WRAPPER_STRUCT(tc_parameters, turi::variant_map_type);
90 DECLARE_CAPI_WRAPPER_STRUCT(tc_model, std::shared_ptr<turi::model_base>);
91 DECLARE_CAPI_WRAPPER_STRUCT(tc_groupby_aggregator, groupby_aggregator_map_type);
92 DECLARE_CAPI_WRAPPER_STRUCT(tc_plot, std::shared_ptr<turi::visualization::Plot>);
93 
94 #endif
boost::make_recursive_variant< flexible_type, std::shared_ptr< unity_sgraph_base >, dataframe_t, std::shared_ptr< model_base >, std::shared_ptr< unity_sframe_base >, std::shared_ptr< unity_sarray_base >, std::map< std::string, boost::recursive_variant_ >, std::vector< boost::recursive_variant_ >, boost::recursive_wrapper< function_closure_info > >::type variant_type
Definition: variant.hpp:24
std::vector< std::pair< flexible_type, flexible_type > > flex_dict
std::vector< flexible_type > flex_list