density
C++11 library for paged memory management, function queues, heterogeneous queues and lifo memory management
dynamic_reference.h
1 
2 // Copyright Giuseppe Campana (giu.campana@gmail.com) 2016-2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #pragma once
9 #include <density/runtime_type.h>
10 #include <type_traits>
11 
12 namespace density
13 {
15  enum class cv_qual
16  {
17  no_qual = 0,
18  const_qual = 1,
19  volatile_qual = 2,
21  };
22 
27  template <typename TYPE> struct cv_qual_of
28  {
29  static constexpr cv_qual value = cv_qual::no_qual;
30  };
31  template <typename TYPE> struct cv_qual_of<const TYPE>
32  {
33  static constexpr cv_qual value = cv_qual::const_qual;
34  };
35  template <typename TYPE> struct cv_qual_of<volatile TYPE>
36  {
37  static constexpr cv_qual value = cv_qual::volatile_qual;
38  };
39  template <typename TYPE> struct cv_qual_of<const volatile TYPE>
40  {
41  static constexpr cv_qual value = cv_qual::const_volatile_qual;
42  };
43 
57  constexpr bool is_less_or_equal_cv_qualified(cv_qual i_first, cv_qual i_second) noexcept
58  {
59  return (static_cast<unsigned>(i_first) & ~static_cast<unsigned>(i_second)) == 0;
60  }
61 
71  constexpr bool is_less_cv_qualified(cv_qual i_first, cv_qual i_second) noexcept
72  {
73  return i_first != i_second && is_less_or_equal_cv_qualified(i_first, i_second);
74  }
75 
83  template <typename TYPE, cv_qual CV> struct add_cv_qual
84  {
85  using type = TYPE;
86  };
87  template <typename TYPE> struct add_cv_qual<TYPE, cv_qual::const_qual>
88  {
89  using type = const TYPE;
90  };
91  template <typename TYPE> struct add_cv_qual<TYPE, cv_qual::volatile_qual>
92  {
93  using type = volatile TYPE;
94  };
95  template <typename TYPE> struct add_cv_qual<TYPE, cv_qual::const_volatile_qual>
96  {
97  using type = const volatile TYPE;
98  };
99 
104  template <typename TYPE, cv_qual CV> using add_cv_qual_t = typename add_cv_qual<TYPE, CV>::type;
105 
106  template <typename RUNTIME_TYPE = runtime_type<>, cv_qual = cv_qual::no_qual>
108 
110  template <typename RUNTIME_TYPE = runtime_type<>>
112 
114  template <typename RUNTIME_TYPE = runtime_type<>>
116 
118  template <typename RUNTIME_TYPE = runtime_type<>>
121 
125  template <typename> struct is_dynamic_reference : std::false_type
126  {
127  };
128  template <typename RUNTIME_TYPE, cv_qual CV_QUAL>
129  struct is_dynamic_reference<dynamic_reference<RUNTIME_TYPE, CV_QUAL>> : std::true_type
130  {
131  };
132 
144  template <typename RUNTIME_TYPE, cv_qual CV_QUALIFIER> class dynamic_reference
145  {
146  public:
148  dynamic_reference() = delete;
149 
161 #ifdef DOXYGEN_DOC_GENERATION
162  template <typename TARGET_TYPE>
163 #else
164  template <
165  typename TARGET_TYPE,
166  typename std::enable_if<
169  nullptr>
170 #endif
171  dynamic_reference(TARGET_TYPE & i_target_object)
172  : dynamic_reference(
173  RUNTIME_TYPE::template make<typename std::remove_cv<TARGET_TYPE>::type>(),
174  &i_target_object)
175  {
176  }
177 
185  constexpr dynamic_reference(
186  const RUNTIME_TYPE & i_target_type, add_cv_qual_t<void, CV_QUALIFIER> * i_target_address)
187  : m_address(i_target_address), m_type(i_target_type)
188  {
189  }
190 
203 #ifdef DOXYGEN_DOC_GENERATION
204  template <cv_qual OTHER_CV>
205 #else
206  template <
207  cv_qual OTHER_CV,
208  typename std::enable_if<is_less_or_equal_cv_qualified(OTHER_CV, CV_QUALIFIER)>::type * =
209  nullptr>
210 #endif
212  : dynamic_reference(i_source.type(), i_source.address())
213 
214  {
215  }
216 
218  dynamic_reference & operator=(const dynamic_reference & i_source) = delete;
219 
223  constexpr const RUNTIME_TYPE & type() const noexcept { return m_type; }
224 
229  constexpr add_cv_qual_t<void, CV_QUALIFIER> * address() const noexcept { return m_address; }
230 
238  template <typename TYPE> constexpr bool is() const noexcept
239  {
241  m_type.template is<TYPE>();
242  }
243 
255  template <typename TYPE>
256  DENSITY_CPP14_CONSTEXPR add_cv_qual_t<TYPE, CV_QUALIFIER> & as() const noexcept
257  {
258  assert(m_type.template is<TYPE>());
259  return *static_cast<add_cv_qual_t<TYPE, CV_QUALIFIER> *>(m_address);
260  }
261 
262  private:
263  add_cv_qual_t<void, CV_QUALIFIER> * const m_address = nullptr;
264  RUNTIME_TYPE const m_type;
265  };
266 
267 } // namespace density
Definition: conc_function_queue.h:11
constexpr bool is() const noexcept
Definition: dynamic_reference.h:238
Definition: dynamic_reference.h:83
Definition: runtime_type.h:1061
cv_qual
Definition: dynamic_reference.h:15
constexpr dynamic_reference(const dynamic_reference< RUNTIME_TYPE, OTHER_CV > &i_source)
Definition: dynamic_reference.h:211
constexpr bool is_less_cv_qualified(cv_qual i_first, cv_qual i_second) noexcept
Definition: dynamic_reference.h:71
typename add_cv_qual< TYPE, CV >::type add_cv_qual_t
Definition: dynamic_reference.h:104
constexpr bool is_less_or_equal_cv_qualified(cv_qual i_first, cv_qual i_second) noexcept
Definition: dynamic_reference.h:57
constexpr const RUNTIME_TYPE & type() const noexcept
Definition: dynamic_reference.h:223
dynamic_reference(TARGET_TYPE &i_target_object)
Definition: dynamic_reference.h:171
constexpr add_cv_qual_t< void, CV_QUALIFIER > * address() const noexcept
Definition: dynamic_reference.h:229
constexpr dynamic_reference(const RUNTIME_TYPE &i_target_type, add_cv_qual_t< void, CV_QUALIFIER > *i_target_address)
Definition: dynamic_reference.h:185
Definition: dynamic_reference.h:27
Definition: dynamic_reference.h:107
Definition: dynamic_reference.h:125
DENSITY_CPP14_CONSTEXPR add_cv_qual_t< TYPE, CV_QUALIFIER > & as() const noexcept
Definition: dynamic_reference.h:256