MyGUI  3.2.0
MyGUI_Any.h
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 // -- Based on boost::any, original copyright information follows --
24 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
25 //
26 // Distributed under the Boost Software License, Version 1.0.
27 // (See at http://www.boost.org/LICENSE_1_0.txt)
28 // -- End original copyright --
29 
30 #ifndef __MYGUI_ANY_H__
31 #define __MYGUI_ANY_H__
32 
33 #include "MyGUI_Prerequest.h"
34 #include "MyGUI_Diagnostic.h"
35 #include <algorithm>
36 
37 #ifndef MYGUI_RTTI_DISABLE_TYPE_INFO
38 #include <typeinfo>
39 #endif
40 
41 namespace MyGUI
42 {
43 
82  {
83  public:
84  struct AnyEmpty { };
85  static AnyEmpty Null;
86 
87  Any();
88  Any(const Any::AnyEmpty& value);
89  Any(const Any& other);
90 
91  template<typename ValueType>
92  Any(const ValueType& value) :
93  mContent(new Holder<ValueType>(value))
94  {
95  }
96 
97  ~Any();
98 
99  Any& swap(Any& rhs);
100 
101  template<typename ValueType>
102  Any& operator = (const ValueType& rhs)
103  {
104  Any(rhs).swap(*this);
105  return *this;
106  }
107 
108  Any& operator = (const Any::AnyEmpty& rhs);
109  Any& operator = (const Any& rhs);
110 
111  bool empty() const;
112 
113 #ifndef MYGUI_RTTI_DISABLE_TYPE_INFO
114  const std::type_info& getType() const;
115 
116  template<typename ValueType>
117  ValueType* castType(bool _throw = true) const
118  {
119  if (this->getType() == typeid(ValueType))
120  return &static_cast<Any::Holder<ValueType> *>(this->mContent)->held;
121  MYGUI_ASSERT(!_throw, "Bad cast from type '" << getType().name() << "' to '" << typeid(ValueType).name() << "'");
122  return nullptr;
123  }
124 #else
125  template<typename ValueType>
126  ValueType* castType(bool _throw = true) const
127  {
128  Any::Holder<ValueType>* data = dynamic_cast<Any::Holder<ValueType> *>(this->mContent);
129  if (data != nullptr)
130  return &data->held;
131  MYGUI_ASSERT(!_throw, "Bad cast any");
132  return nullptr;
133  }
134 #endif
135 
136  void* castUnsafe() const;
137 
138  private:
139  class Placeholder
140  {
141  public:
142  virtual ~Placeholder() { }
143 
144  public:
145 #ifndef MYGUI_RTTI_DISABLE_TYPE_INFO
146  virtual const std::type_info& getType() const = 0;
147 #endif
148  virtual Placeholder* clone() const = 0;
149  };
150 
151  template<typename ValueType>
152  class Holder :
153  public Placeholder
154  {
155  public:
156  Holder(const ValueType& value) :
157  held(value)
158  {
159  }
160 
161  public:
162 #ifndef MYGUI_RTTI_DISABLE_TYPE_INFO
163  virtual const std::type_info& getType() const
164  {
165  return typeid(ValueType);
166  }
167 #endif
168 
169  virtual Placeholder* clone() const
170  {
171  return new Holder(held);
172  }
173 
174  public:
175  ValueType held;
176 
177  private:
178  Holder& operator=(const Holder&);
179  };
180 
181  private:
182  Placeholder* mContent;
183  };
184 
185 } // namespace MyGUI
186 
187 #endif // __MYGUI_ANY_H__