00001
00002
00003 #ifndef BOOST_ANY_INCLUDED
00004 #define BOOST_ANY_INCLUDED
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <algorithm>
00014 #include <typeinfo>
00015 #include <ostream>
00016
00017
00018
00019 namespace boost
00020 {
00021 class any
00022 {
00023 public:
00024
00025 any()
00026 : content(0)
00027 {
00028 }
00029
00030 template<typename ValueType>
00031 any(const ValueType & value)
00032 : content(new holder<ValueType>(value))
00033 {
00034 }
00035
00036 any(const any & other)
00037 : content(other.content ? other.content->clone() : 0)
00038 {
00039 }
00040
00041 ~any()
00042 {
00043 delete content;
00044 }
00045
00046 public:
00047
00048 any & swap(any & rhs)
00049 {
00050 std::swap(content, rhs.content);
00051 return *this;
00052 }
00053
00054 template<typename ValueType>
00055 any & operator=(const ValueType & rhs)
00056 {
00057 any(rhs).swap(*this);
00058 return *this;
00059 }
00060
00061 any & operator=(const any & rhs)
00062 {
00063 any(rhs).swap(*this);
00064 return *this;
00065 }
00066
00067 public:
00068
00069 bool empty() const
00070 {
00071 return !content;
00072 }
00073
00074 const std::type_info & type() const
00075 {
00076 return content ? content->type() : typeid(void);
00077 }
00078
00079 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
00080 private:
00081 #else
00082 public:
00083 #endif
00084
00085 class placeholder
00086 {
00087 public:
00088
00089 virtual ~placeholder()
00090 {
00091 }
00092
00093 public:
00094
00095 virtual const std::type_info & type() const = 0;
00096
00097 virtual placeholder * clone() const = 0;
00098
00099 };
00100
00101 template<typename ValueType>
00102 class holder : public placeholder
00103 {
00104 public:
00105
00106 holder(const ValueType & value)
00107 : held(value)
00108 {
00109 }
00110
00111 public:
00112
00113 virtual const std::type_info & type() const
00114 {
00115 return typeid(ValueType);
00116 }
00117
00118 virtual placeholder * clone() const
00119 {
00120 return new holder(held);
00121 }
00122
00123 public:
00124
00125 ValueType held;
00126
00127 };
00128
00129 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
00130
00131 private:
00132
00133 template<typename ValueType>
00134 friend ValueType * any_cast(any *);
00135
00136 #else
00137
00138 public:
00139
00140 #endif
00141
00142 placeholder * content;
00143
00144 };
00145
00146 class bad_any_cast : public std::bad_cast
00147 {
00148 public:
00149 virtual const char * what() const throw()
00150 {
00151 return "boost::bad_any_cast: "
00152 "failed conversion using boost::any_cast";
00153 }
00154 };
00155
00156 template<typename ValueType>
00157 ValueType * any_cast(any * operand)
00158 {
00159 return operand && operand->type() == typeid(ValueType)
00160 ? &static_cast<any::holder<ValueType> *>(operand->content)->held
00161 : 0;
00162 }
00163
00164 template<typename ValueType>
00165 const ValueType * any_cast(const any * operand)
00166 {
00167 return any_cast<ValueType>(const_cast<any *>(operand));
00168 }
00169
00170 template<typename ValueType>
00171 ValueType any_cast(const any & operand)
00172 {
00173 const ValueType * result = any_cast<ValueType>(&operand);
00174 if(!result)
00175 throw bad_any_cast();
00176
00177 return *result;
00178 }
00179
00180
00181 }
00182
00183
00184
00185
00186
00187
00188
00189 #endif