Project

General

Profile

SO3Engine
SO3Any.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25// -- Based on boost::any, original copyright information follows --
26// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
27//
28// Distributed under the Boost Software License, Version 1.0. (See
29// accompAnying file LICENSE_1_0.txt or copy at
30// http://www.boost.org/LICENSE_1_0.txt)
31// -- End original copyright --
32
33#ifndef __SO3_ANY_H__
34#define __SO3_ANY_H__
35
37#include "OgreException.h"
38#include <algorithm>
39#include <typeinfo>
40
41namespace SO3
42{
43
47class SAny
48{
49public:
53 {
54 public:
55 protected:
56 private:
57
58 public:
61 virtual ~placeholder()
62 {
63 }
64
67 virtual const std::type_info& GetType() const = 0;
68
71 virtual placeholder* Clone() const = 0;
72
75 virtual void WriteToStream(std::ostream& o) = 0;
76 protected:
77 private:
78 };
79
82 template<typename ValueType> class holder : public placeholder
83 {
84 public:
85 ValueType held;
86 protected:
87 private:
88
89 public:
92 holder(const ValueType& value) : held(value)
93 {
94 }
95
98 virtual const std::type_info& GetType() const
99 {
100 return typeid(ValueType);
101 }
102
105 virtual placeholder* Clone() const
106 {
107 return new holder(held);
108 }
109
112 virtual void WriteToStream(std::ostream& o)
113 {
114 o << held;
115 }
116 };
117
118protected:
120 template<typename ValueType> friend ValueType* any_cast(SAny *);
121private:
122
123public:
127 {
128 }
129
132 template<typename ValueType> explicit SAny(const ValueType& value) : mContent(new holder<ValueType>(value))
133 {
134 }
135
138 SAny(const SAny& other) : mContent(other.mContent ? other.mContent->Clone() : 0)
139 {
140 }
141
144 virtual ~SAny()
145 {
146 Destroy();
147 }
148
152 {
153 std::swap(mContent, rhs.mContent);
154 return *this;
155 }
156
159 template<typename ValueType> SAny& operator=(const ValueType& rhs)
160 {
161 SAny(rhs).Swap(*this);
162 return *this;
163 }
164
167 SAny& operator=(const SAny& rhs)
168 {
169 SAny(rhs).Swap(*this);
170 return *this;
171 }
172
175 bool IsEmpty() const
176 {
177 return !mContent;
178 }
179
182 const std::type_info& GetType() const
183 {
184 return mContent ? mContent->GetType() : typeid(void);
185 }
186
189 inline friend std::ostream& operator <<(std::ostream& o, const SAny& v)
190 {
191 if (v.mContent)
193 return o;
194 }
195
198 void Destroy()
199 {
200 SO3_SAFE_DELETE(mContent);
201 }
202
205 template<typename ValueType> ValueType operator()() const
206 {
207 if(!mContent)
208 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Bad cast from uninitialised SAny", "SAny::operator()");
209 else if(GetType() == typeid(ValueType))
210 return static_cast<SAny::holder<ValueType>*>(mContent)->held;
211 else
212 {
213 std::ostringstream str;
214 str << "Bad cast from type '" << GetType().name() << "' " << "to '" << typeid(ValueType).name() << "'";
215 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, str.str(), "SAny::operator()");
216 }
217 }
218};
219
224class SAnyNumeric : public SAny
225{
226public:
227protected:
231 {
232 public:
233 protected:
234 private:
235
236 public:
240 {
241 }
242
245 virtual placeholder* Add(placeholder* rhs) = 0;
246
249 virtual placeholder* Subtract(placeholder* rhs) = 0;
250
253 virtual placeholder* Multiply(placeholder* rhs) = 0;
254
257 virtual placeholder* Multiply(float factor) = 0;
258
261 virtual placeholder* Divide(placeholder* rhs) = 0;
262 protected:
263 private:
264 };
265
268 template<typename ValueType> class numholder : public numplaceholder
269 {
270 public:
271 ValueType held;
272 protected:
273 private:
274
275 public:
278 numholder(const ValueType& value) : held(value)
279 {
280 }
281
284 virtual const std::type_info& GetType() const
285 {
286 return typeid(ValueType);
287 }
288
291 virtual placeholder* Clone() const
292 {
293 return new numholder(held);
294 }
295
299 {
300 return new numholder(held + static_cast<numholder*>(rhs)->held);
301 }
302
306 {
307 return new numholder(held - static_cast<numholder*>(rhs)->held);
308 }
309
313 {
314 return new numholder(held * static_cast<numholder*>(rhs)->held);
315 }
316
319 virtual placeholder* Multiply(float factor)
320 {
321 return new numholder(held * factor);
322 }
323
327 {
328 return new numholder(held / static_cast<numholder*>(rhs)->held);
329 }
330
333 virtual void WriteToStream(std::ostream& o)
334 {
335 o << held;
336 }
337 protected:
338 private:
339 };
340private:
341
342public:
346 {
347 }
348
351 template<typename ValueType> SAnyNumeric(const ValueType& value)
352 {
353 mContent = new numholder<ValueType>(value);
354 }
355
358 SAnyNumeric(const SAnyNumeric& other) : SAny()
359 {
360 mContent = other.mContent ? other.mContent->Clone() : 0;
361 }
362
366 {
367 mContent = pholder;
368 }
369
373 {
374 SAnyNumeric(rhs).Swap(*this);
375 return *this;
376 }
377
381 {
382 return SAnyNumeric(static_cast<numplaceholder*>(mContent)->Add(rhs.mContent));
383 }
384
388 {
389 return SAnyNumeric(static_cast<numplaceholder*>(mContent)->Subtract(rhs.mContent));
390 }
391
395 {
396 return SAnyNumeric(static_cast<numplaceholder*>(mContent)->Multiply(rhs.mContent));
397 }
398
401 SAnyNumeric operator*(float factor) const
402 {
403 return SAnyNumeric(static_cast<numplaceholder*>(mContent)->Multiply(factor));
404 }
405
409 {
410 return SAnyNumeric(static_cast<numplaceholder*>(mContent)->Divide(rhs.mContent));
411 }
412
416 {
417 *this = SAnyNumeric(static_cast<numplaceholder*>(mContent)->Add(rhs.mContent));
418 return *this;
419 }
420
424 {
425 *this = SAnyNumeric(static_cast<numplaceholder*>(mContent)->Subtract(rhs.mContent));
426 return *this;
427 }
428
432 {
433 *this = SAnyNumeric(static_cast<numplaceholder*>(mContent)->Multiply(rhs.mContent));
434 return *this;
435 }
436
440 {
441 *this = SAnyNumeric(static_cast<numplaceholder*>(mContent)->Divide(rhs.mContent));
442 return *this;
443 }
444protected:
445private:
446};
447
450template<typename ValueType> ValueType* any_cast(SAny* operand)
451{
452 if(operand && operand->GetType() == typeid(ValueType))
453 return &static_cast<SAny::holder<ValueType>*>(operand->mContent)->held;
454 else
455 return 0;
456}
457
460template<typename ValueType> const ValueType* any_cast(const SAny* operand)
461{
462 return any_cast<ValueType>(const_cast<SAny*>(operand));
463}
464
467template<typename ValueType> ValueType any_cast(const SAny& operand)
468{
469 const ValueType* result = any_cast<ValueType>(&operand);
470 if(!result)
471 {
472 std::ostringstream str;
473 str << "Bad cast from type '" << operand.GetType().name() << "' " << "to '" << typeid(ValueType).name() << "'";
474 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, str.str(), "any_cast");
475 }
476 return *result;
477}
478
479}
480
481#endif
librairies include
virtual const std::type_info & GetType() const
Definition SO3Any.h:98
virtual placeholder * Clone() const
Definition SO3Any.h:105
virtual void WriteToStream(std::ostream &o)
Definition SO3Any.h:112
holder(const ValueType &value)
Definition SO3Any.h:92
ValueType held
Definition SO3Any.h:85
virtual const std::type_info & GetType() const =0
virtual void WriteToStream(std::ostream &o)=0
virtual ~placeholder()
Definition SO3Any.h:61
virtual placeholder * Clone() const =0
friend ValueType * any_cast(SAny *)
Definition SO3Any.h:450
bool IsEmpty() const
Definition SO3Any.h:175
SAny(const SAny &other)
Definition SO3Any.h:138
void Destroy()
Definition SO3Any.h:198
friend std::ostream & operator<<(std::ostream &o, const SAny &v)
Definition SO3Any.h:189
SAny & operator=(const ValueType &rhs)
Definition SO3Any.h:159
SAny(const ValueType &value)
Definition SO3Any.h:132
virtual ~SAny()
Definition SO3Any.h:144
const std::type_info & GetType() const
Definition SO3Any.h:182
placeholder * mContent
Definition SO3Any.h:119
SAny & Swap(SAny &rhs)
Definition SO3Any.h:151
SAny & operator=(const SAny &rhs)
Definition SO3Any.h:167
ValueType operator()() const
Definition SO3Any.h:205
virtual placeholder * Multiply(placeholder *rhs)
Definition SO3Any.h:312
virtual placeholder * Add(placeholder *rhs)
Definition SO3Any.h:298
virtual void WriteToStream(std::ostream &o)
Definition SO3Any.h:333
virtual const std::type_info & GetType() const
Definition SO3Any.h:284
virtual placeholder * Clone() const
Definition SO3Any.h:291
virtual placeholder * Subtract(placeholder *rhs)
Definition SO3Any.h:305
numholder(const ValueType &value)
Definition SO3Any.h:278
virtual placeholder * Multiply(float factor)
Definition SO3Any.h:319
virtual placeholder * Divide(placeholder *rhs)
Definition SO3Any.h:326
virtual placeholder * Add(placeholder *rhs)=0
virtual placeholder * Multiply(placeholder *rhs)=0
virtual placeholder * Divide(placeholder *rhs)=0
virtual placeholder * Subtract(placeholder *rhs)=0
virtual placeholder * Multiply(float factor)=0
SAnyNumeric operator/(const SAnyNumeric &rhs) const
Definition SO3Any.h:408
SAnyNumeric(const ValueType &value)
Definition SO3Any.h:351
SAnyNumeric operator+(const SAnyNumeric &rhs) const
Definition SO3Any.h:380
SAnyNumeric(const SAnyNumeric &other)
Definition SO3Any.h:358
SAnyNumeric & operator-=(const SAnyNumeric &rhs)
Definition SO3Any.h:423
SAnyNumeric & operator*=(const SAnyNumeric &rhs)
Definition SO3Any.h:431
SAnyNumeric & operator/=(const SAnyNumeric &rhs)
Definition SO3Any.h:439
SAnyNumeric operator*(float factor) const
Definition SO3Any.h:401
SAnyNumeric(placeholder *pholder)
Definition SO3Any.h:365
SAnyNumeric & operator+=(const SAnyNumeric &rhs)
Definition SO3Any.h:415
SAnyNumeric operator-(const SAnyNumeric &rhs) const
Definition SO3Any.h:387
SAnyNumeric & operator=(const SAnyNumeric &rhs)
Definition SO3Any.h:372
SAnyNumeric operator*(const SAnyNumeric &rhs) const
Definition SO3Any.h:394
ValueType * any_cast(SAny *operand)
Definition SO3Any.h:450
STBI_EXTERN unsigned long const char * str
Definition stb_image.h:1182