siplasplas
myclass.hpp
1 #ifndef SIPLASPLAS_EXAMPLES_REFLECTION_MYCLASS_HPP
2 #define SIPLASPLAS_EXAMPLES_REFLECTION_MYCLASS_HPP
3 
4 #include <siplasplas/reflection/api.hpp>
5 #include <siplasplas/reflection/dynamic/attributes/contract.hpp>
6 
7 #include <siplasplas/utility/meta.hpp>
8 
9 namespace meta = cpp::meta;
10 
12 {
13 public:
14  template<typename... Args>
15  Bind(Args&&... args) :
16  _bindedArgs{std::make_pair(std::is_placeholder<std::decay_t<Args>>::value, std::forward<Args>(args))...}
17  {}
18 
19  std::vector<cpp::dynamic_reflection::Object> processArguments(const std::vector<cpp::dynamic_reflection::Object>& args)
20  {
21  const std::size_t function_arity = _bindedArgs.size();
22 
23  std::vector<cpp::dynamic_reflection::Object> finalArgs;
24  finalArgs.reserve(function_arity);
25 
26  for(auto&& arg : _bindedArgs)
27  {
28  if(arg.first > 0)
29  {
30  finalArgs.emplace_back(args[arg.first - 1]);
31  }
32  else
33  {
34  finalArgs.emplace_back(arg.second);
35  }
36  }
37 
38  return finalArgs;
39  }
40 
41 private:
42  std::vector<std::pair<int, cpp::dynamic_reflection::Object>> _bindedArgs;
43 };
44 
45 namespace attr
46 {
47  template<typename... Args>
48  auto bind(Args&&... args)
49  {
50  return std::make_shared<Bind>(std::forward<Args>(args)...);
51  }
52 }
53 
54 
55 using namespace std::placeholders;
56 
57 $(enable_reflection)
58 class MyClass
59 {
60 public:
61  $(cpp::drfl::attributes::contract(
62  [](int a, int b){ return a > 0; }, "a must be greater than zero",
63  [](int r){ return true; }, "this must pass always"
64  ))
65  int f(int a, int b)
66  {
67  return a + b;
68  }
69 
70  int g(int a, int b, int c, int d)
71  {
72  return a + b + c + d;
73  }
74 
75  $(enable_reflection)
76  class InnerClass
77  {
78  public:
79  int innerMember = 0;
80  };
81 
82  enum class Enum
83  {
84  ENUM_VALUE_1 = 1,
85  ENUM_VALUE_2 = 2,
86  FOO,
87  BAR,
88  QUUX
89  };
90 
91  int field = 0;
92  bool otherField;
93  InnerClass objectOfInnerClass;
94  std::string strField = "hello";
95 
96  std::vector<std::string> vector = {"hello", ", ", "world!"};
97  std::unordered_map<std::string, int> answers = {
98  {"everything", 42},
99  {"everything squared", 42*42}
100  };
101  std::tuple<int, std::string, InnerClass> tuple = std::make_tuple(
102  42,
103  "everything",
104  InnerClass()
105  );
106 };
107 
108 #include <reflection/examples/reflection/myclass.hpp>
109 
110 #endif // SIPLASPLAS_EXAMPLES_REFLECTION_MYCLASS_HPP
Definition: myclass.hpp:58
Definition: meta.hpp:10
Definition: myclass.hpp:11
Definition: myclass.hpp:76
Definition: myclass.hpp:45