Any.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #ifndef __OpenKarto_Any_h__
21 #define __OpenKarto_Any_h__
22 
23 #include <algorithm>
24 #include <typeinfo>
25 
26 #include <OpenKarto/Exception.h>
27 
28 namespace karto
29 {
30 
32 
33 
37 
38  // The following code is from the boost library with the following license
39  // and modified to fit into the KartoSDK
40 
41  // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
42  //
43  // Distributed under the Boost Software License, Version 1.0. (See
44  // accompAnying file LICENSE_1_0.txt or copy at
45  // http://www.boost.org/LICENSE_1_0.txt)
46  // -- End original copyright --
47 
51 
55  class Any
56  {
57  public:
61  Any()
62  : m_pContent(NULL)
63  {
64  }
65 
71  template<typename T>
72  Any(const T& rValue)
73  : m_pContent(new Holder<T>(rValue))
74  {
75  }
76 
83  Any(const Any& rOther)
84  : m_pContent(rOther.m_pContent ? rOther.m_pContent->Clone() : NULL)
85  {
86  }
87 
91  ~Any()
92  {
93  delete m_pContent;
94  }
95 
96  public:
102  Any& Swap(Any& rOther)
103  {
104  std::swap(m_pContent, rOther.m_pContent);
105  return *this;
106  }
107 
111  template<typename T>
112  Any & operator=(const T& rOther)
113  {
114  Any(rOther).Swap(*this);
115  return *this;
116  }
117 
121  Any& operator=(const Any& rOther)
122  {
123  Any(rOther).Swap(*this);
124  return *this;
125  }
126 
127  public:
132  kt_bool IsEmpty() const
133  {
134  return !m_pContent;
135  }
136 
140  const std::type_info& GetType() const
141  {
142  return m_pContent ? m_pContent->GetType() : typeid(void);
143  }
144 
145  private:
147  {
148  public:
149 
150  virtual ~PlaceHolder()
151  {
152  }
153 
154  public:
155  virtual const std::type_info & GetType() const = 0;
156 
157  virtual PlaceHolder * Clone() const = 0;
158  };
159 
160  template<typename T>
161  class Holder : public PlaceHolder
162  {
163  public:
164  Holder(const T & value)
165  : held(value)
166  {
167  }
168 
169  public:
170  virtual const std::type_info & GetType() const
171  {
172  return typeid(T);
173  }
174 
175  virtual PlaceHolder * Clone() const
176  {
177  return new Holder(held);
178  }
179 
180  public:
181  T held;
182 
183  private:
184  Holder & operator=(const Holder &);
185  };
186 
187  public:
191  static const Any Empty;
192 
193  private:
194  template<typename T>
195  friend T * any_cast(Any *);
196 
198  }; // class Any
199 
205  template<typename T>
206  T * any_cast(Any* pAny)
207  {
208  return pAny && pAny->GetType() == typeid(T) ? &static_cast<Any::Holder<T> *>(pAny->m_pContent)->held : NULL;
209  }
210 
216  template<typename T>
217  inline const T * any_cast(const Any* pAny)
218  {
219  return any_cast<T>(const_cast<Any*>(pAny));
220  }
221 
228  template<typename T>
229  inline T any_cast(const Any& rAny)
230  {
231  const T* pResult = any_cast<T>(&rAny);
232  if (!pResult)
233  {
234  throw karto::Exception("Unable to perform any_cast");
235  }
236 
237  return *pResult;
238  }
239 
241 
242 }
243 
244 #endif // __OpenKarto_Any_h__
bool kt_bool
Definition: Types.h:145
Definition: Any.h:55
virtual PlaceHolder * Clone() const =0
virtual ~PlaceHolder()
Definition: Any.h:150
kt_bool IsEmpty() const
Definition: Any.h:132
virtual const std::type_info & GetType() const =0
static const Any Empty
Definition: Any.h:191
Any()
Definition: Any.h:61
Any(const T &rValue)
Definition: Any.h:72
const std::type_info & GetType() const
Definition: Any.h:140
virtual const std::type_info & GetType() const
Definition: Any.h:170
Any & operator=(const Any &rOther)
Definition: Any.h:121
Holder(const T &value)
Definition: Any.h:164
~Any()
Definition: Any.h:91
PlaceHolder * m_pContent
Definition: Any.h:197
Any(const Any &rOther)
Definition: Any.h:83
virtual PlaceHolder * Clone() const
Definition: Any.h:175
Definition: Any.cpp:20
Any & operator=(const T &rOther)
Definition: Any.h:112
friend T * any_cast(Any *)
Definition: Any.h:206
Any & Swap(Any &rOther)
Definition: Any.h:102


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Tue Nov 7 2017 06:02:36