Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 8, 2008, 1:51:25 AM (16 years ago)
Author:
landauf
Message:

ok, be aware, here comes a big one. people with weak nerves should probably better look away or take some serious drugs.
this update includes partial and explicit class template specialization, partial and explicit specialized template function overloading, template meta programming and a simple typecast.
yeah right, a typecast. but let me explain the whole story from the beginning.

it all started with a simple problem: i had a double in a MultiType and wanted a float, but i always got 0. what was the problem? the conversion 'MultiType to anyting' was handled by the Converter class in util/Convert.h and the Converter was specialized for strings, multitypes, vectors and so on, but not for int, float, bool, …
so i've first wanted to implement a typecast as default, but this was a bad idea because it doesn't work for almost every generic type.
implementing an explicit specialization for every possible pair of primitives (did you ever happened to use an unsigned short? or a long double? no? ignorants :D) would have been a simple but ugly solution.
but there were other problems: if there's a rule to convert a string into anything and another rule to convert anything into an int - what happens if you want to convert a string into an int? compiler error! …ambiguous partial template specialization.
so i've spent days and nights to find a solution. this is my 5th try or so and i'm still really unsure if it works, but it's the first version i want to commit to have at least a backup.
if you're interested in looking at the code you better wait until i've cleaned up the whole thing, it's a real mess. and i want to do further tests, but now i'm tired. good night ;)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core2/src/util/MultiTypeMath.cc

    r960 r1001  
    8989
    9090MultiTypeMath::operator void*() const
    91 { return (this->type_ == MT_void) ? this->value_.void_ : ConvertValueAndReturn<MultiTypeMath, void*>(*this, 0); }
     91{ return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypeMath, void*>(*this, 0); }
    9292MultiTypeMath::operator int() const
    93 { return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypeMath, int>(*this, 0); }
     93{ return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypeMath, int>(*this, 0); }
    9494MultiTypeMath::operator unsigned int() const
    95 { return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypeMath, unsigned int>(*this, 0); }
     95{ return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypeMath, unsigned int>(*this, 0); }
    9696MultiTypeMath::operator char() const
    97 { return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypeMath, char>(*this, 0); }
     97{ return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypeMath, char>(*this, 0); }
    9898MultiTypeMath::operator unsigned char() const
    99 { return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypeMath, unsigned char>(*this, 0); }
     99{ return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypeMath, unsigned char>(*this, 0); }
    100100MultiTypeMath::operator short() const
    101 { return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypeMath, short>(*this, 0); }
     101{ return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypeMath, short>(*this, 0); }
    102102MultiTypeMath::operator unsigned short() const
    103 { return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypeMath, unsigned short>(*this, 0); }
     103{ return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypeMath, unsigned short>(*this, 0); }
    104104MultiTypeMath::operator long() const
    105 { return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypeMath, long>(*this, 0); }
     105{ return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypeMath, long>(*this, 0); }
    106106MultiTypeMath::operator unsigned long() const
    107 { return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypeMath, unsigned long>(*this, 0); }
     107{ return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypeMath, unsigned long>(*this, 0); }
    108108MultiTypeMath::operator float() const
    109 { return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypeMath, float>(*this, 0); }
     109{ return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypeMath, float>(*this, 0); }
    110110MultiTypeMath::operator double() const
    111 { return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypeMath, double>(*this, 0); }
     111{ return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypeMath, double>(*this, 0); }
    112112MultiTypeMath::operator long double() const
    113 { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypeMath, long double>(*this, 0); }
     113{ return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypeMath, long double>(*this, 0); }
    114114MultiTypeMath::operator bool() const
    115 { return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypeMath, bool>(*this, 0); }
     115{ return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypeMath, bool>(*this, 0); }
    116116MultiTypeMath::operator std::string() const
    117 { return (this->type_ == MT_string) ? this->string_ : ConvertValueAndReturn<MultiTypeMath, std::string>(*this); }
     117{ return (this->type_ == MT_string) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this); }
    118118MultiTypeMath::operator const char*() const
    119 { return ((this->type_ == MT_constchar) ? this->string_ : ConvertValueAndReturn<MultiTypeMath, std::string>(*this)).c_str(); }
     119{ return ((this->type_ == MT_constchar) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this)).c_str(); }
    120120MultiTypeMath::operator orxonox::Vector2() const
    121 { return (this->type_ == MT_vector2) ? this->vector2_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Vector2>(*this); }
    122 MultiTypeMath::operator orxonox::Element() const
    123 { return (this->type_ == MT_xmlelement) ? this->xmlelement_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Element>(*this); }
     121{ return (this->type_ == MT_vector2) ? this->vector2_ : getConvertedValue<MultiTypeMath, orxonox::Vector2>(*this); }
    124122MultiTypeMath::operator orxonox::Vector3() const
    125 { return (this->type_ == MT_vector3) ? this->vector3_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Vector3>(*this); }
     123{ return (this->type_ == MT_vector3) ? this->vector3_ : getConvertedValue<MultiTypeMath, orxonox::Vector3>(*this); }
    126124MultiTypeMath::operator orxonox::Quaternion() const
    127 { return (this->type_ == MT_quaternion) ? this->quaternion_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Quaternion>(*this); }
     125{ return (this->type_ == MT_quaternion) ? this->quaternion_ : getConvertedValue<MultiTypeMath, orxonox::Quaternion>(*this); }
    128126MultiTypeMath::operator orxonox::ColourValue() const
    129 { return (this->type_ == MT_colourvalue) ? this->colourvalue_ : ConvertValueAndReturn<MultiTypeMath, orxonox::ColourValue>(*this); }
     127{ return (this->type_ == MT_colourvalue) ? this->colourvalue_ : getConvertedValue<MultiTypeMath, orxonox::ColourValue>(*this); }
    130128MultiTypeMath::operator orxonox::Radian() const
    131 { return (this->type_ == MT_radian) ? this->radian_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Radian>(*this); }
     129{ return (this->type_ == MT_radian) ? this->radian_ : getConvertedValue<MultiTypeMath, orxonox::Radian>(*this); }
    132130MultiTypeMath::operator orxonox::Degree() const
    133 { return (this->type_ == MT_degree) ? this->degree_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Degree>(*this); }
     131{ return (this->type_ == MT_degree) ? this->degree_ : getConvertedValue<MultiTypeMath, orxonox::Degree>(*this); }
    134132
    135133void MultiTypeMath::setValue(const MultiTypeMath& mtm)
     
    167165
    168166    if (this->type_ == MT_vector2)
    169         ConvertValue(&output, this->vector2_);
     167        ConvertValue(&output, this->vector2_, CP_FromType);
    170168    else if (this->type_ == MT_vector3)
    171         ConvertValue(&output, this->vector3_);
     169        ConvertValue(&output, this->vector3_, CP_FromType);
    172170    else if (this->type_ == MT_colourvalue)
    173         ConvertValue(&output, this->colourvalue_);
     171    { std::cout << "3_1\n";
     172        ConvertValue(&output, this->colourvalue_, CP_FromType);}
    174173    else if (this->type_ == MT_quaternion)
    175         ConvertValue(&output, this->quaternion_);
     174        ConvertValue(&output, this->quaternion_, CP_FromType);
    176175    else if (this->type_ == MT_radian)
    177176        ConvertValue(&output, this->radian_);
     
    187186{
    188187    if (this->type_ == MT_vector2)
    189         return ConvertValue(&this->vector2_, value, orxonox::Vector2(0, 0));
     188        return ConvertValue(&this->vector2_, value, orxonox::Vector2(0, 0), CP_FromType);
    190189    else if (this->type_ == MT_vector3)
    191         return ConvertValue(&this->vector3_, value, orxonox::Vector3(0, 0, 0));
     190        return ConvertValue(&this->vector3_, value, orxonox::Vector3(0, 0, 0), CP_FromType);
    192191    else if (this->type_ == MT_colourvalue)
    193         return ConvertValue(&this->colourvalue_, value, orxonox::ColourValue(0, 0, 0, 0));
     192    { std::cout << "4_1\n";
     193        return ConvertValue(&this->colourvalue_, value, orxonox::ColourValue(0, 0, 0, 0), CP_FromType); }
    194194    else if (this->type_ == MT_quaternion)
    195         return ConvertValue(&this->quaternion_, value, orxonox::Quaternion(1, 0, 0, 0));
     195        return ConvertValue(&this->quaternion_, value, orxonox::Quaternion(1, 0, 0, 0), CP_FromType);
    196196    else if (this->type_ == MT_radian)
    197197        return ConvertValue(&this->radian_, value, orxonox::Radian(0));
Note: See TracChangeset for help on using the changeset viewer.