You.i Engine
YiAny.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2017. All rights reserved.
2 #ifndef _YI_ANY_H_
3 #define _YI_ANY_H_
4 
5 #include "framework/YiPredef.h"
6 #include "utility/YiError.h"
7 
8 #ifdef YI_DEBUG
9 #include <glm/fwd.hpp>
10 
11 #if !defined(YI_ROKU) && !defined(YI_TIZEN_NACL)
12 #define YI_ANY_PROVIDE_TYPE_NAME
13 #endif
14 
15 #endif
16 
17 class CYIUrl;
18 
19 // The internal storage size of a CYIAny for small types. The size is expanded
20 // to the nearest multiple of the pointer size.
21 //
22 // Objects this size or smaller are stored internally within the CYIAny. Objects
23 // larger than this are allocated on the heap.
24 //
25 // This define is set to the size of a glm::vec3, but expanded to avoid needing
26 // to include the glm headers.
27 #define YI_ANY_STORAGE_SIZE (sizeof(float) * 3)
28 
77 class CYIAny
78 {
79 public:
80 
84  CYIAny();
85 
90  template <typename T>
91  CYIAny(const T &value);
92 
97  CYIAny(const CYIAny &other);
98 
105  ~CYIAny();
106 
110  CYIAny &Swap(CYIAny &other);
111 
116  CYIAny &operator=(const CYIAny &other);
117 
127  template <typename T>
128  CYIAny &operator=(const T &value);
129 
136  bool Empty() const;
137 
144  void Clear();
145 
160  template <typename T>
161  bool ContainsType() const;
162 
173  bool MatchesType(const CYIAny &rOther) const;
174 
197  template <typename T>
198  T &Get();
199 
213  template <typename T>
214  const T &Get() const;
215 
226  template<typename T>
227  static size_t GetRequiredHeapMemory();
228 
234  CYIString ToString() const;
235 
236 private:
237  friend class CYIAnyTest;
238 
239  // We wrap the template functions in a struct to allow partial specialization on bAllocate.
240  template <typename T, bool bAllocate>
241  struct TypeTableFunctions
242  {
243  // Creates a copy of the given value within the CYIAny (allocating storage if necessary.)
244  static void Create(CYIAny *pAny, const void *pOther);
245 
246  // Gets a pointer to the value contained in this CYIAny (dereferencing to the storage as necessary.)
247  static void *GetValue(CYIAny *pAny);
248 
249  // Destroys the content of this CYIAny (de-allocating storage if necessary.)
250  static void Destroy(CYIAny *pAny);
251 
252  // Converts the content of this CYIAny to a string (dereferencing to the storage as necessary.)
253  static CYIString ToString(const CYIAny *pAny);
254  };
255 
256  // The function pointer table implementing operations on a specific static type.
257  struct TypeTable
258  {
259  void (*Create)(CYIAny *pAny, const void *pOther);
260  void *(*GetValue)(CYIAny *pAny);
261  void (*Destroy)(CYIAny *pAny);
262  CYIString (*ToString)(const CYIAny *pAny);
263  };
264 
265  // Gets the dynamic TypeTable for the given static type.
266  template <typename T>
267  static TypeTable *GetTypeTable();
268 
269  // The table of functions for the type stored in the CYIAny, or nullptr if
270  // the CYIAny is empty.
271  TypeTable *m_pTypeTable;
272 
273 #ifdef YI_ANY_PROVIDE_TYPE_NAME
274  // The type stored in the CYIAny, as a char array
275  const char *m_pTypeName;
276 
277  // Returns the demangled type name of the templated type. The function retains ownership of the returned pointer
278  template<typename T>
279  static const char *GetDemangledTypeName();
280 #endif
281 
282  // The storage for the contained value. If the value fits, it
283  // is allocated within the storage via placement new; otherwise
284  // a pointer to the value is stored.
285  union Storage
286  {
287  public:
288  void *GetVoid() const;
289 
290  template <typename T>
291  T *GetCast() const;
292 
293  template <typename T>
294  void Destroy();
295 
296  private:
297  friend class CYIAnyTest;
298 
299  void *m_data[(YI_ANY_STORAGE_SIZE + sizeof(void *) - 1) / sizeof(void *)]; // round up to multiple of sizeof(void *)
300 
301 #ifdef YI_DEBUG
302  struct GlmVec3
303  {
304  float x, y, z;
305  };
306  struct GlmVec4
307  {
308  float x, y, z, w;
309  };
310 
311  bool m_bool;
312  char m_char;
313  uint8_t m_uint8;
314  int8_t m_int8;
315  uint16_t m_uint16;
316  int16_t m_int16;
317  uint32_t m_uint32;
318  int32_t m_int32;
319  uint64_t m_uint64;
320  int64_t m_int64;
321  float m_float;
322  double m_double;
323  GlmVec3 m_glmVec3;
324  GlmVec4 m_glmVec4;
325 
326  void *m_voidPointer;
327  char *m_charArray;
328  CYIString *m_string;
329  CYIUrl *m_url;
330  glm::mat3 *m_glmMat3;
331  glm::mat4 *m_glmMat4;
332 
333  public:
334  Storage()
335  {
336  memset(this, 0xFE, sizeof(Storage));
337  }
338 #endif
339  } m_storage;
340 };
341 
344 #include "framework/YiAny.inl"
345 
346 #endif // _YI_ANY_H_
CYIAny & operator=(const CYIAny &other)
bool Empty() const
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:35
void Clear()
A class that holds instances of any type that is copy-constructible.
Definition: YiAny.h:77
CYIString ToString() const
T & Get()
bool ContainsType() const
#define YI_ANY_STORAGE_SIZE
Definition: YiAny.h:27
static size_t GetRequiredHeapMemory()
CYIAny & Swap(CYIAny &other)
bool MatchesType(const CYIAny &rOther) const
A class used to encapsulate an URL.
Definition: YiUrl.h:24
friend class CYIAnyTest
Definition: YiAny.h:237