Orxonox  0.0.5 Codename: Arcturus
Classes | Namespaces | Functions
MultiType.h File Reference

Declaration of the MultiType and some helper constructs. More...

#include "UtilPrereqs.h"
#include <cassert>
#include <string>
#include <utility>
#include <OgreVector2.h>
#include <OgreVector3.h>
#include <OgreVector4.h>
#include <OgreQuaternion.h>
#include <OgreColourValue.h>
#include <loki/TypeTraits.h>
#include "Math.h"
#include "mbool.h"

Go to the source code of this file.

Classes

class  orxonox::MultiType
 The MultiType can hold a value of many possible types and convert them to other types. More...
 
class  orxonox::MultiType::MT_ValueBase
 MT_ValueBase is an almost pure virtual baseclass of MT_Value<T>, which holds the value of the MultiType. More...
 

Namespaces

 orxonox
 Die Wagnis Klasse hat die folgenden Aufgaben:
 

Functions

template<>
bool orxonox::MultiType::MT_ValueBase::isType< orxonox::ColourValue > () const
 
template<>
bool orxonox::MultiType::MT_ValueBase::isType< orxonox::Degree > () const
 
template<>
bool orxonox::MultiType::MT_ValueBase::isType< orxonox::Quaternion > () const
 
template<>
bool orxonox::MultiType::MT_ValueBase::isType< orxonox::Radian > () const
 
template<>
bool orxonox::MultiType::MT_ValueBase::isType< orxonox::Vector2 > () const
 
template<>
bool orxonox::MultiType::MT_ValueBase::isType< orxonox::Vector3 > () const
 
template<>
bool orxonox::MultiType::MT_ValueBase::isType< orxonox::Vector4 > () const
 
template<>
bool orxonox::MultiType::MT_ValueBase::isType< std::string > () const
 
_UtilExport std::ostream & orxonox::operator<< (std::ostream &outstream, const MultiType &mt)
 Puts the MultiType on a stream by using the native << operator of the current type. More...
 

Detailed Description

Declaration of the MultiType and some helper constructs.

The MultiType can hold a value of one of the following types:

The MultiType has an internal "type" determined by the first assigned value, using one of these ways:

If you assign another value of another type, the MultiType keeps "its" type and converts the new value to the old type.

If you want to change the type, there are three possibilities:

Examples:

MultiType a = 10; // a has now the type int and the value 10
a.set("3.14"); // a has still the type int and "3.14" gets converted, therefore the value is now 3
a.force<float>("3.14"); // a has now the type float and "3.14" gets converted to 3.14f
a.convert<bool>(); // converts 3.14f to bool, which is true
a = false; // assigns false, this is equivalent to a.setValue(false)

You can pass a MultiType to a function as an argument, even if the argument is not of type MultiType. This works, because the MultiType is automatically converted to the right type.

Example:

void myfunction(int value)
{
orxout() << "doubled value is " << (2 * value) << endl;
}
MultiType a = "50"; // Note: We assigned a string
myfunction(a); // a is converted to int and passed to the function, which prints "value is 100"

Note however that it is of course quite expensive to convert values, especially std::string <-> value. So if you can, always assign a value with the right type to avoid conversion.

Note
Whenever a value gets converted, there is a boolean return value telling you whether it was successful or not. If it wasn't a zero value is assigned with the help of zeroise<T>().