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/MultiTypeString.cc

    r960 r1001  
    4343        else if (this->type_ == MT_string)
    4444            return (this->string_ == mts.string_);
    45         else if (this->type_ == MT_xmlelement)
    46             return (&this->xmlelement_ == &mts.xmlelement_);
    4745    }
    4846
     
    5856        else if (this->type_ == MT_string)
    5957            return (this->string_ != mts.string_);
    60         else if (this->type_ == MT_xmlelement)
    61             return (&this->xmlelement_ != &mts.xmlelement_);
    6258    }
    6359
     
    6662
    6763MultiTypeString::operator void*() const
    68 { return (this->type_ == MT_void) ? this->value_.void_ : ConvertValueAndReturn<MultiTypeString, void*>(*this, 0); }
     64{ return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypeString, void*>(*this, 0); }
    6965MultiTypeString::operator int() const
    70 { return (this->type_ == MT_int) ? this->value_.int_ : ConvertValueAndReturn<MultiTypeString, int>(*this, 0); }
     66{ return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypeString, int>(*this, 0); }
    7167MultiTypeString::operator unsigned int() const
    72 { return (this->type_ == MT_uint) ? this->value_.uint_ : ConvertValueAndReturn<MultiTypeString, unsigned int>(*this, 0); }
     68{ return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypeString, unsigned int>(*this, 0); }
    7369MultiTypeString::operator char() const
    74 { return (this->type_ == MT_char) ? this->value_.char_ : ConvertValueAndReturn<MultiTypeString, char>(*this, 0); }
     70{ return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypeString, char>(*this, 0); }
    7571MultiTypeString::operator unsigned char() const
    76 { return (this->type_ == MT_uchar) ? this->value_.uchar_ : ConvertValueAndReturn<MultiTypeString, unsigned char>(*this, 0); }
     72{ return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypeString, unsigned char>(*this, 0); }
    7773MultiTypeString::operator short() const
    78 { return (this->type_ == MT_short) ? this->value_.short_ : ConvertValueAndReturn<MultiTypeString, short>(*this, 0); }
     74{ return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypeString, short>(*this, 0); }
    7975MultiTypeString::operator unsigned short() const
    80 { return (this->type_ == MT_ushort) ? this->value_.ushort_ : ConvertValueAndReturn<MultiTypeString, unsigned short>(*this, 0); }
     76{ return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypeString, unsigned short>(*this, 0); }
    8177MultiTypeString::operator long() const
    82 { return (this->type_ == MT_long) ? this->value_.long_ : ConvertValueAndReturn<MultiTypeString, long>(*this, 0); }
     78{ return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypeString, long>(*this, 0); }
    8379MultiTypeString::operator unsigned long() const
    84 { return (this->type_ == MT_ulong) ? this->value_.ulong_ : ConvertValueAndReturn<MultiTypeString, unsigned long>(*this, 0); }
     80{ return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypeString, unsigned long>(*this, 0); }
    8581MultiTypeString::operator float() const
    86 { return (this->type_ == MT_float) ? this->value_.float_ : ConvertValueAndReturn<MultiTypeString, float>(*this, 0); }
     82{ return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypeString, float>(*this, 0); }
    8783MultiTypeString::operator double() const
    88 { return (this->type_ == MT_double) ? this->value_.double_ : ConvertValueAndReturn<MultiTypeString, double>(*this, 0); }
     84{ return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypeString, double>(*this, 0); }
    8985MultiTypeString::operator long double() const
    90 { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : ConvertValueAndReturn<MultiTypeString, long double>(*this, 0); }
     86{ return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypeString, long double>(*this, 0); }
    9187MultiTypeString::operator bool() const
    92 { return (this->type_ == MT_bool) ? this->value_.bool_ : ConvertValueAndReturn<MultiTypeString, bool>(*this, 0); }
     88{ return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypeString, bool>(*this, 0); }
    9389MultiTypeString::operator std::string() const
    94 { return (this->type_ == MT_string) ? this->string_ : ConvertValueAndReturn<MultiTypeString, std::string>(*this); }
     90{ return (this->type_ == MT_string) ? this->string_ : getConvertedValue<MultiTypeString, std::string>(*this); }
    9591MultiTypeString::operator const char*() const
    96 { return ((this->type_ == MT_constchar) ? this->string_ : ConvertValueAndReturn<MultiTypeString, std::string>(*this)).c_str(); }
    97 MultiTypeString::operator orxonox::Element() const
    98 { return (this->type_ == MT_xmlelement) ? this->xmlelement_ : ConvertValueAndReturn<MultiTypeString, orxonox::Element>(*this); }
     92{ return ((this->type_ == MT_constchar) ? this->string_ : getConvertedValue<MultiTypeString, std::string>(*this)).c_str(); }
    9993
    10094void MultiTypeString::setValue(const MultiTypeString& mts)
     
    110104    else if (this->type_ == MT_string)
    111105        return "string";
    112     else if (this->type_ == MT_xmlelement)
    113         return "XML-element";
    114106    else
    115107        return MultiTypePrimitive::getTypename();
     
    124116    else if (this->type_ == MT_string)
    125117        return this->string_;
    126     else if (this->type_ == MT_xmlelement)
    127         ConvertValue(&output, this->xmlelement_);
    128118    else
    129119        return MultiTypePrimitive::toString();
     
    138128    else if (this->type_ == MT_string)
    139129        this->string_ = value;
    140     else if (this->type_ == MT_xmlelement)
    141         return ConvertValue(&this->xmlelement_, value, orxonox::Element());
    142130    else
    143131        return MultiTypePrimitive::fromString(value);
Note: See TracChangeset for help on using the changeset viewer.