MyGUI  3.2.1
MyGUI_Any.h
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 // -- Based on boost::any, original copyright information follows --
00008 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
00009 //
00010 // Distributed under the Boost Software License, Version 1.0.
00011 // (See at http://www.boost.org/LICENSE_1_0.txt)
00012 // -- End original copyright --
00013 
00014 #ifndef __MYGUI_ANY_H__
00015 #define __MYGUI_ANY_H__
00016 
00017 #include "MyGUI_Prerequest.h"
00018 #include "MyGUI_Diagnostic.h"
00019 #include <algorithm>
00020 
00021 #include <typeinfo>
00022 
00023 namespace MyGUI
00024 {
00025 
00063     class MYGUI_EXPORT Any
00064     {
00065     public:
00066         struct AnyEmpty { };
00067         static AnyEmpty Null;
00068 
00069         Any();
00070         Any(const Any::AnyEmpty& value);
00071         Any(const Any& other);
00072 
00073         template<typename ValueType>
00074         Any(const ValueType& value) :
00075             mContent(new Holder<ValueType>(value))
00076         {
00077         }
00078 
00079         ~Any();
00080 
00081         Any& swap(Any& rhs);
00082 
00083         template<typename ValueType>
00084         Any& operator = (const ValueType& rhs)
00085         {
00086             Any(rhs).swap(*this);
00087             return *this;
00088         }
00089 
00090         Any& operator = (const Any::AnyEmpty& rhs);
00091         Any& operator = (const Any& rhs);
00092 
00093         bool empty() const;
00094 
00095         const std::type_info& getType() const;
00096 
00097         template<typename ValueType>
00098         ValueType* castType(bool _throw = true) const
00099         {
00100             if (this->getType() == typeid(ValueType))
00101                 return &static_cast<Any::Holder<ValueType> *>(this->mContent)->held;
00102             MYGUI_ASSERT(!_throw, "Bad cast from type '" << getType().name() << "' to '" << typeid(ValueType).name() << "'");
00103             return nullptr;
00104         }
00105 
00106         void* castUnsafe() const;
00107 
00108     private:
00109         class Placeholder
00110         {
00111         public:
00112             virtual ~Placeholder() { }
00113 
00114         public:
00115             virtual const std::type_info& getType() const = 0;
00116             virtual Placeholder* clone() const = 0;
00117         };
00118 
00119         template<typename ValueType>
00120         class Holder :
00121             public Placeholder
00122         {
00123         public:
00124             Holder(const ValueType& value) :
00125                 held(value)
00126             {
00127             }
00128 
00129         public:
00130             virtual const std::type_info& getType() const
00131             {
00132                 return typeid(ValueType);
00133             }
00134 
00135             virtual Placeholder* clone() const
00136             {
00137                 return new Holder(held);
00138             }
00139 
00140         public:
00141             ValueType held;
00142 
00143         private:
00144             Holder& operator=(const Holder&);
00145         };
00146 
00147     private:
00148         Placeholder* mContent;
00149     };
00150 
00151 } // namespace MyGUI
00152 
00153 #endif // __MYGUI_ANY_H__