RTC Toolkit  2.0.0
utils.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_UTILS_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_UTILS_HPP
14 
15 #include <type_traits>
16 
17 namespace rtctk::componentFramework {
18 
19 // template function defines an option with its default value
20 #define RTCTK_DEFINE_OPTION(Name, DefaultValue) \
21  \
22  template <typename T = DefaultValue> struct Name : T {} \
23 
24 
25 #define RTCTK_APPLY_OPTION_ED(Options, Name) \
26  \
27  std::conditional_t<is_present_v<Name<Enabled>, Options...>, Name<Enabled>, \
28  std::conditional_t<is_present_v<Name<Disabled>, Options...>, Name<Disabled>, Name<>>> \
29 
30 
31 // convenience macro to check options in an if or switch statement
32 #define RTCTK_IS_OPTION(Name, Value) \
33  \
34  std::is_base_of_v<Name<Value>, AppliedOptions> \
35 
36 
37 // assert that applied option is either enabled or disabled
38 #define RTCTK_ASSERT_OPTION_ED(Name) \
39  \
40  static_assert(RTCTK_IS_OPTION(Name, Enabled) or \
41  RTCTK_IS_OPTION(Name, Disabled), \
42  "Option " #Name " must either be Enabled or Disabled") \
43 
44 
45 // Default definitions for enabling and disabling options
46 using Enabled = std::true_type;
47 using Disabled = std::false_type;
48 
49 
50 // This is to check for dependencies between feature layers
51 
52 template <template <typename...> class Base, typename Derived>
54  template <typename... Ts>
55  static constexpr std::true_type test(const Base<Ts...> *);
56  static constexpr std::false_type test(...);
57  using type = decltype(test(std::declval<Derived *>()));
58 };
59 
60 template <template <typename...> class Base, typename Derived>
62 
63 template <template <typename...> class Base, typename Derived>
65 
66 
67 // This is to check if options are present or not
68 
69 template <typename What, typename... Args>
70 struct is_present {
71  static constexpr bool value{(std::is_same_v<What, Args> || ...)};
72 };
73 
74 template <typename What, typename... Args>
75 inline constexpr bool is_present_v = is_present<What, Args...>::value;
76 
77 
78 // This is for optional implementation of interfaces
79 
80 template <class>
81 struct empty_base {
82  empty_base() = default;
83 
84  template <class... Args>
85  constexpr empty_base(Args &&...) {
86  }
87 };
88 
89 template <class T, bool Condition>
90 using optional_base = std::conditional_t<Condition, T, empty_base<T>>;
91 
92 } // namespace rtctk::componentFramework
93 
94 #endif // RTCTK_COMPONENTFRAMEWORK_UTILS_HPP
rtctk::componentFramework::empty_base
Definition: utils.hpp:81
rtctk::componentFramework::is_base_of_template
typename is_base_of_template_impl< Base, Derived >::type is_base_of_template
Definition: utils.hpp:61
rtctk::componentFramework::empty_base::empty_base
empty_base()=default
rtctk::componentFramework::Args
detail::Args Args
Definition: rtcComponentMain.hpp:37
rtctk::componentFramework::is_base_of_template_impl::type
decltype(test(std::declval< Derived * >())) type
Definition: utils.hpp:57
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::is_present::value
static constexpr bool value
Definition: utils.hpp:71
rtctk::componentFramework::detail::Args
Definition: rtcComponent.hpp:38
rtctk::componentFramework::empty_base::empty_base
constexpr empty_base(Args &&...)
Definition: utils.hpp:85
rtctk::componentFramework::is_base_of_template_impl
Definition: utils.hpp:53
rtctk::componentFramework::optional_base
std::conditional_t< Condition, T, empty_base< T > > optional_base
Definition: utils.hpp:90
rtctk::componentFramework::is_present
Definition: utils.hpp:70
rtctk::componentFramework::is_base_of_template_v
constexpr bool is_base_of_template_v
Definition: utils.hpp:64
rtctk::componentFramework::is_base_of_template_impl::test
static constexpr std::true_type test(const Base< Ts... > *)
rtctk::componentFramework::is_base_of_template_impl::test
static constexpr std::false_type test(...)
rtctk::componentFramework::Disabled
std::false_type Disabled
Definition: utils.hpp:47
rtctk::componentFramework::is_present_v
constexpr bool is_present_v
Definition: utils.hpp:75
rtctk::componentFramework::Enabled
std::true_type Enabled
Definition: utils.hpp:46