Any.hpp
Go to the documentation of this file.
1 //
3 // Aurora C++ Library
4 // Copyright (c) 2012-2016 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef AURORA_ANY_HPP
30 #define AURORA_ANY_HPP
31 
32 #include <Aurora/Config.hpp>
33 #include <Aurora/Tools/Swap.hpp>
36 
37 #include <typeindex>
38 
39 
40 namespace aurora
41 {
42 
45 
60 class Any
61 {
62  // ---------------------------------------------------------------------------------------------------------------------------
63  // Public member functions
64  public:
67  Any()
68  : mPointer()
69  , mType(typeid(void))
70  {
71  }
72 
75  template <typename T>
76  Any(const T& value)
77  : mPointer(makeCopied<T>(value))
78  , mType(typeid(T))
79  {
80  }
81 
84  Any(const Any& origin)
85  : mPointer(origin.mPointer)
86  , mType(origin.mType)
87  {
88  }
89 
92  Any(Any&& source)
93  : mPointer(std::move(source.mPointer))
94  , mType(source.mType)
95  {
96  }
97 
100  template <typename T>
101  Any& operator= (const T& value)
102  {
103  Any(value).swap(*this);
104  return *this;
105  }
106 
109  Any& operator= (const Any& origin)
110  {
111  Any(origin).swap(*this);
112  return *this;
113  }
114 
117  Any& operator= (Any&& source)
118  {
119  Any(std::move(source)).swap(*this);
120  return *this;
121  }
122 
126  {
127  }
128 
131  void swap(Any& other)
132  {
133  adlSwap(mPointer, other.mPointer);
134  adlSwap(mType, other.mType);
135  }
136 
141  template <typename T>
142  T& get()
143  {
144  assert(mType == typeid(T));
145  return *static_cast<T*>(mPointer.get());
146  }
147 
152  template <typename T>
153  T* check()
154  {
155  if (mType == typeid(T))
156  return static_cast<T*>(mPointer.get());
157  else
158  return nullptr;
159  }
160 
163  operator SafeBool() const
164  {
165  return mPointer;
166  }
167 
168  // ---------------------------------------------------------------------------------------------------------------------------
169  // Private variables
170  private:
171  CopiedPtr<void> mPointer;
172  std::type_index mType;
173 };
174 
178 
179 
181 } // namespace aurora
182 
183 #endif // AURORA_ANY_HPP
Any()
Construct empty value.
Definition: Any.hpp:67
#define AURORA_GLOBAL_SWAP(Class)
Macro to implement a global overload of swap(lhs, rhs) to allow argument-dependent lookup...
Definition: Swap.hpp:40
Class template aurora::CopiedPtr.
void(detail::SafeBoolHolder::* SafeBool)()
SafeBool type.
Definition: SafeBool.hpp:55
Any(const Any &origin)
Copy constructor.
Definition: Any.hpp:84
Configuration header of the library.
Any(const T &value)
Construct from arbitrary value.
Definition: Any.hpp:76
Any & operator=(const T &value)
Assignment operator from value.
Definition: Any.hpp:101
Type aurora::SafeBool and corresponding functionality.
Helpers to declare and invoke swap() functions.
void swap(Any &other)
Swaps the Any with another Any of the same type.
Definition: Any.hpp:131
Any(Any &&source)
Move constructor.
Definition: Any.hpp:92
T * get() const
Permits access to the internal pointer. Designed for rare use.
Definition: CopiedPtr.hpp:252
T * check()
Returns a pointer to the contained value.
Definition: Any.hpp:153
void adlSwap(T &lhs, T &rhs)
swap() function with argument-dependent lookup
Definition: Swap.hpp:49
Type-erased class holding any value.
Definition: Any.hpp:60
Definition: DispatchTraits.hpp:39
~Any()
Destructor.
Definition: Any.hpp:125