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.h

    r947 r1001  
    3333#include <iostream>
    3434#include "UtilPrereqs.h"
    35 #include "XMLIncludes.h"
    36 #include "tinyxml/ticpp.h"
    3735
    3836#include "MultiTypePrimitive.h"
     
    5755        inline MultiTypeString(const char*           value)   { this->setValue(value); }
    5856        inline MultiTypeString(const std::string&    value)   { this->setValue(value); }
    59         inline MultiTypeString(const orxonox::Element& value) { this->setValue(value); }
    6057        inline MultiTypeString(const MultiTypeString& mts)    { this->setValue(mts);   }
    6158        virtual inline ~MultiTypeString() {}
     
    6461        inline MultiTypeString& operator=(const char*             value)   { this->setValue(value); return *this; }
    6562        inline MultiTypeString& operator=(const std::string&      value)   { this->setValue(value); return *this; }
    66         inline MultiTypeString& operator=(const orxonox::Element& value)   { this->setValue(value); return *this; }
    6763        inline MultiTypeString& operator=(const MultiTypeString& mts)      { this->setValue(mts);   return *this; }
    6864
     
    7066        inline bool operator==(const char*             value) const { return (this->string_      == std::string(value)); }
    7167        inline bool operator==(const std::string&      value) const { return (this->string_      == value);              }
    72         inline bool operator==(const orxonox::Element& value) const { return (&this->xmlelement_ == &value);             }
    7368        bool operator==(const MultiTypeString& mts) const;
    7469
     
    7671        inline bool operator!=(const char*             value) const { return (this->string_      != std::string(value)); }
    7772        inline bool operator!=(const std::string&      value) const { return (this->string_      != value);              }
    78         inline bool operator!=(const orxonox::Element& value) const { return (&this->xmlelement_ != &value);             }
    7973        bool operator!=(const MultiTypeString& mts) const;
    8074
     
    9488        virtual operator std::string()          const;
    9589        virtual operator const char*()          const;
    96         virtual operator orxonox::Element()     const;
    9790
    9891        using MultiTypePrimitive::setValue;
    9992        inline void setValue(const char*             value) { this->type_ = MT_string;     this->string_     = std::string(value); }
    10093        inline void setValue(const std::string&      value) { this->type_ = MT_string;     this->string_     = value;              }
    101         inline void setValue(const orxonox::Element& value) { this->type_ = MT_xmlelement; this->xmlelement_ = value;              }
    10294        void setValue(const MultiTypeString& mts);
    10395
    10496        inline std::string getString()          const { return this->string_;         }
    10597        inline const char*  getConstChar()      const { return this->string_.c_str(); }
    106         inline orxonox::Element getXMLElement() const { return this->xmlelement_;     }
    10798
    10899        inline std::string& getString()          { return this->string_;         }
    109100        inline const char*  getConstChar()       { return this->string_.c_str(); }
    110         inline orxonox::Element& getXMLElement() { return this->xmlelement_;     }
    111101
    112102        using MultiTypePrimitive::getValue;
    113103        inline void getValue(std::string*      variable) const { (*variable) = this->string_;         }
    114104        inline void getValue(const char**      variable) const { (*variable) = this->string_.c_str(); }
    115         inline void getValue(orxonox::Element* variable) const { (*variable) = this->xmlelement_;     }
    116105
    117106        virtual std::string getTypename() const;
     
    122111    protected:
    123112        std::string      string_;
    124         orxonox::Element xmlelement_;
    125113};
    126114
Note: See TracChangeset for help on using the changeset viewer.