Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1779 for code/branches/core3


Ignore:
Timestamp:
Sep 14, 2008, 11:31:28 PM (16 years ago)
Author:
rgrieder
Message:

gcc didn't like me..

Location:
code/branches/core3/src/util
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core3/src/util/Convert.h

    r1778 r1779  
    135135///////////////////
    136136
    137 // Default template for fallbackConvert, no conversion possible
    138 template <class ToType, class FromType>
    139 inline bool fallbackConversion(ToType* output, FromType input)
    140 {
    141     COUT(2) << "Could not convert value of type " << typeid(FromType).name()
    142             << " to type " << typeid(ToType).name() << std::endl;
    143     return false;
    144 }
    145 
    146 
    147 /////////////////////
    148 // fallbackConvert //
    149 /////////////////////
    150 
    151 // Class template used when << or >> was not available with string conversions
    152 // It is required to call not yet declared fallbackConvert functions.
    153 template <class ToType, class FromType>
    154 struct ConverterFallbackTemplate
     137// Default template. No conversion available at all.
     138template <class ToType, class FromType>
     139struct ConverterFallback
    155140{
    156141    static bool convert(ToType* output, const FromType& input)
    157142    {
    158         return fallbackConversion(output, input);
    159     }
    160 };
     143        COUT(2) << "Could not convert value of type " << typeid(FromType).name()
     144                << " to type " << typeid(ToType).name() << std::endl;
     145        return false;
     146    }
     147};
     148
     149
     150///////////////////////
     151// ConverterFallback //
     152///////////////////////
    161153
    162154// Default template for stringstream
     
    166158    static bool convert(ToType* output, const FromType& input)
    167159    {
    168         return fallbackConversion(output, input);
     160        return ConverterFallback<ToType, FromType>::convert(output, input);
    169161    }
    170162};
     
    181173    {
    182174        std::string temp;
    183         if (ConverterFallbackTemplate<std::string, FromType>::convert(&temp, input))
     175        if (ConverterFallback<std::string, FromType>::convert(&temp, input))
    184176        {
    185177            std::operator <<(outstream, temp);
     
    219211    inline bool operator >>(std::istream& instream, ToType& output)
    220212    {
    221         return ConverterFallbackTemplate<ToType, std::string>
     213        return ConverterFallback<ToType, std::string>
    222214            ::convert(&output, static_cast<std::istringstream&>(instream).str());
    223215    }
     
    246238///////////////////
    247239
     240// static cast no possible, try stringstream conversion next
     241template <class ToType, class FromType>
     242inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>)
     243{
     244    return ConverterStringStream<ToType, FromType>::convert(output, input);
     245}
     246
    248247// We can cast implicitely
    249248template <class ToType, class FromType>
     
    252251    (*output) = static_cast<ToType>(input);
    253252    return true;
    254 }
    255 
    256 // static cast no possible, try stringstream conversion next
    257 template <class ToType, class FromType>
    258 inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>)
    259 {
    260     return ConverterStringStream<ToType, FromType>::convert(output, input);
    261253}
    262254
     
    266258///////////////////////
    267259
    268 template <class ToType, class FromType>
    269 inline bool explicitConversion(ToType* output, const FromType& input)
    270 {
    271     // try implict conversion by probing first because we use '...' instead of a template
    272     const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists;
    273     return convertImplicitely(output, input, ::Int2Type<probe>());
    274 }
    275 
    276 
    277 // Indirect calls over a class template so we can call functions not yet declared.
    278 template <class ToType, class FromType>
    279 struct ConverterExplicitTemplate
     260// Default template if no specialisation is available
     261template <class ToType, class FromType>
     262struct ConverterExplicit
    280263{
    281264    static bool convert(ToType* output, const FromType& input)
    282265    {
    283         return explicitConversion(output, input);
     266        // try implict conversion by probing first because we use '...' instead of a template
     267        const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists;
     268        return convertImplicitely(output, input, ::Int2Type<probe>());
    284269    }
    285270};
     
    302287inline bool convertValue(ToType* output, const FromType& input)
    303288{
    304     return ConverterExplicitTemplate<ToType, FromType>::convert(output, input);
     289    return ConverterExplicit<ToType, FromType>::convert(output, input);
    305290}
    306291
     
    368353// delegate conversion from const char* to std::string
    369354template <class ToType>
    370 inline bool explicitConversion(ToType* output, const char* input)
    371 {
    372     return convertValue<ToType, std::string>(output, input);
    373 }
     355struct ConverterExplicit<ToType, const char*>
     356{
     357    static bool convert(ToType* output, const char* input)
     358    {
     359        return convertValue<ToType, std::string>(output, input);
     360    }
     361};
    374362
    375363// These conversions would exhibit ambiguous << or >> operators when using stringstream
    376 inline bool explicitConversion(std::string* output, const char input)
    377 {
    378     *output = std::string(1, input);
    379     return true;
    380 }
    381 inline bool explicitConversion(std::string* output, const unsigned char input)
    382 {
    383     *output = std::string(1, input);
    384     return true;
    385 }
    386 inline bool explicitConversion(char* output, const std::string input)
    387 {
    388     if (input != "")
    389         *output = input[0];
    390     else
    391         *output = '\0';
    392     return true;
    393 }
    394 inline bool explicitConversion(unsigned char* output, const std::string input)
    395 {
    396     if (input != "")
    397         *output = input[0];
    398     else
    399         *output = '\0';
    400     return true;
    401 }
     364template <>
     365struct ConverterExplicit<std::string, char>
     366{
     367    static bool convert(std::string* output, const char input)
     368    {
     369        *output = std::string(1, input);
     370        return true;
     371    }
     372};
     373template <>
     374struct ConverterExplicit<std::string, unsigned char>
     375{
     376    static bool convert(std::string* output, const unsigned char input)
     377    {
     378        *output = std::string(1, input);
     379        return true;
     380    }
     381};
     382template <>
     383struct ConverterExplicit<char, std::string>
     384{
     385    static bool convert(char* output, const std::string input)
     386    {
     387        if (input != "")
     388            *output = input[0];
     389        else
     390            *output = '\0';
     391        return true;
     392    }
     393};
     394template <>
     395struct ConverterExplicit<unsigned char, std::string>
     396{
     397    static bool convert(unsigned char* output, const std::string input)
     398    {
     399        if (input != "")
     400            *output = input[0];
     401        else
     402            *output = '\0';
     403        return true;
     404    }
     405};
    402406
    403407#endif /* _Convert_H__ */
  • code/branches/core3/src/util/Math.cc

    r1778 r1779  
    130130
    131131// std::string to Vector2
    132 bool fallbackConversion(orxonox::Vector2* output, const std::string& input)
    133 {
    134     unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
    135     if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
    136 
    137     SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
     132bool ConverterFallback<orxonox::Vector2, std::string>::convert(orxonox::Vector2* output, const std::string& input)
     133{
     134    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     135    if ((opening_parenthesis = input.find('(')) == std::string::npos)
     136        opening_parenthesis = 0;
     137    else
     138        opening_parenthesis++;
     139
     140    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
     141                     ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
    138142    if (tokens.size() >= 2)
    139143    {
     
    149153
    150154// std::string to Vector3
    151 bool fallbackConversion(orxonox::Vector3* output, const std::string& input)
    152 {
    153     unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
    154     if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
    155 
    156     SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
     155bool ConverterFallback<orxonox::Vector3, std::string>::convert(orxonox::Vector3* output, const std::string& input)
     156{
     157    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     158    if ((opening_parenthesis = input.find('(')) == std::string::npos)
     159        opening_parenthesis = 0;
     160    else
     161        opening_parenthesis++;
     162
     163    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
     164                     ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
    157165    if (tokens.size() >= 3)
    158166    {
     
    170178
    171179// std::string to Vector4
    172 bool fallbackConversion(orxonox::Vector4* output, const std::string& input)
     180bool ConverterFallback<orxonox::Vector4, std::string>::convert(orxonox::Vector4* output, const std::string& input)
     181{
     182    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     183    if ((opening_parenthesis = input.find('(')) == std::string::npos)
     184        opening_parenthesis = 0;
     185    else
     186        opening_parenthesis++;
     187
     188    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
     189                     ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
     190    if (tokens.size() >= 4)
     191    {
     192        if (!ConvertValue(&(output->x), tokens[0]))
     193            return false;
     194        if (!ConvertValue(&(output->y), tokens[1]))
     195            return false;
     196        if (!ConvertValue(&(output->z), tokens[2]))
     197            return false;
     198        if (!ConvertValue(&(output->w), tokens[3]))
     199            return false;
     200
     201        return true;
     202    }
     203    return false;
     204}
     205
     206// std::string to Quaternion
     207bool ConverterFallback<orxonox::Quaternion, std::string>::convert(orxonox::Quaternion* output, const std::string& input)
    173208{
    174209    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     
    178213    if (tokens.size() >= 4)
    179214    {
    180         if (!ConvertValue(&(output->x), tokens[0]))
    181             return false;
    182         if (!ConvertValue(&(output->y), tokens[1]))
    183             return false;
    184         if (!ConvertValue(&(output->z), tokens[2]))
    185             return false;
    186         if (!ConvertValue(&(output->w), tokens[3]))
    187             return false;
    188 
    189         return true;
    190     }
    191     return false;
    192 }
    193 
    194 // std::string to Quaternion
    195 bool fallbackConversion(orxonox::Quaternion* output, const std::string& input)
     215        if (!ConvertValue(&(output->w), tokens[0]))
     216            return false;
     217        if (!ConvertValue(&(output->x), tokens[1]))
     218            return false;
     219        if (!ConvertValue(&(output->y), tokens[2]))
     220            return false;
     221        if (!ConvertValue(&(output->z), tokens[3]))
     222            return false;
     223
     224        return true;
     225    }
     226    return false;
     227}
     228
     229// std::string to ColourValue
     230bool ConverterFallback<orxonox::ColourValue, std::string>::convert(orxonox::ColourValue* output, const std::string& input)
    196231{
    197232    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     
    201236    if (tokens.size() >= 4)
    202237    {
    203         if (!ConvertValue(&(output->w), tokens[0]))
    204             return false;
    205         if (!ConvertValue(&(output->x), tokens[1]))
    206             return false;
    207         if (!ConvertValue(&(output->y), tokens[2]))
    208             return false;
    209         if (!ConvertValue(&(output->z), tokens[3]))
    210             return false;
    211 
    212         return true;
    213     }
    214     return false;
    215 }
    216 
    217 // std::string to ColourValue
    218 bool fallbackConversion(orxonox::ColourValue* output, const std::string& input)
    219 {
    220     unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
    221     if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
    222 
    223     SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
    224     if (tokens.size() >= 4)
    225     {
    226238        if (!ConvertValue(&(output->r), tokens[0]))
    227239            return false;
  • code/branches/core3/src/util/MathConvert.h

    r1778 r1779  
    4646
    4747// Vector2 to std::string
    48 inline bool explicitConversion(std::string* output, const orxonox::Vector2& input)
     48template <>
     49struct ConverterExplicit<std::string, orxonox::Vector2>
    4950{
    50     std::ostringstream ostream;
    51     if (ostream << input.x << "," << input.y)
     51    static bool convert(std::string* output, const orxonox::Vector2& input)
    5252    {
    53         (*output) = ostream.str();
    54         return true;
     53        std::ostringstream ostream;
     54        if (ostream << input.x << "," << input.y)
     55        {
     56            (*output) = ostream.str();
     57            return true;
     58        }
     59        return false;
    5560    }
    56     return false;
    57 }
     61};
    5862
    5963// Vector3 to std::string
    60 inline bool explicitConversion(std::string* output, const orxonox::Vector3& input)
     64template <>
     65struct ConverterExplicit<std::string, orxonox::Vector3>
    6166{
    62     std::ostringstream ostream;
    63     if (ostream << input.x << "," << input.y << "," << input.z)
     67    static bool convert(std::string* output, const orxonox::Vector3& input)
    6468    {
    65         (*output) = ostream.str();
    66         return true;
     69        std::ostringstream ostream;
     70        if (ostream << input.x << "," << input.y << "," << input.z)
     71        {
     72            (*output) = ostream.str();
     73            return true;
     74        }
     75        return false;
    6776    }
    68     return false;
    69 }
     77};
    7078
    7179// Vector4 to std::string
    72 inline bool explicitConversion(std::string* output, const orxonox::Vector4& input)
     80template <>
     81struct ConverterExplicit<std::string, orxonox::Vector4>
    7382{
    74     std::ostringstream ostream;
    75     if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w)
     83    static bool convert(std::string* output, const orxonox::Vector4& input)
    7684    {
    77         (*output) = ostream.str();
    78         return true;
     85        std::ostringstream ostream;
     86        if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w)
     87        {
     88            (*output) = ostream.str();
     89            return true;
     90        }
     91        return false;
    7992    }
    80     return false;
    81 }
     93};
    8294
    8395// Quaternion to std::string
    84 inline bool explicitConversion(std::string* output, const orxonox::Quaternion& input)
     96template <>
     97struct ConverterExplicit<std::string, orxonox::Quaternion>
    8598{
    86     std::ostringstream ostream;
    87     if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z)
     99    static bool convert(std::string* output, const orxonox::Quaternion& input)
    88100    {
    89         (*output) = ostream.str();
    90         return true;
     101        std::ostringstream ostream;
     102        if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z)
     103        {
     104            (*output) = ostream.str();
     105            return true;
     106        }
     107        return false;
    91108    }
    92     return false;
    93 }
     109};
    94110
    95111// ColourValue to std::string
    96 inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input)
     112template <>
     113struct ConverterExplicit<std::string, orxonox::ColourValue>
    97114{
    98     std::ostringstream ostream;
    99     if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)
     115    static bool convert(std::string* output, const orxonox::ColourValue& input)
    100116    {
    101         (*output) = ostream.str();
    102         return true;
     117        std::ostringstream ostream;
     118        if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)
     119        {
     120            (*output) = ostream.str();
     121            return true;
     122        }
     123        return false;
    103124    }
    104     return false;
    105 }
     125};
    106126
    107127
     
    111131
    112132// std::string to Vector2
    113 _UtilExport bool fallbackConversion(orxonox::Vector2* output, const std::string& input);
     133template <> struct _UtilExport ConverterFallback<orxonox::Vector2,     std::string>
     134{ static bool convert(orxonox::Vector2*     output, const std::string& input); };
    114135// std::string to Vector3
    115 _UtilExport bool fallbackConversion(orxonox::Vector3* output, const std::string& input);
     136template <> struct _UtilExport ConverterFallback<orxonox::Vector3,     std::string>
     137{ static bool convert(orxonox::Vector3*     output, const std::string& input); };
    116138// std::string to Vector4
    117 _UtilExport bool fallbackConversion(orxonox::Vector4* output, const std::string& input);
     139template <> struct _UtilExport ConverterFallback<orxonox::Vector4,     std::string>
     140{ static bool convert(orxonox::Vector4*     output, const std::string& input); };
    118141// std::string to Quaternion
    119 _UtilExport bool fallbackConversion(orxonox::Quaternion* output, const std::string& input);
     142template <> struct _UtilExport ConverterFallback<orxonox::Quaternion,  std::string>
     143{ static bool convert(orxonox::Quaternion*  output, const std::string& input); };
    120144// std::string to ColourValue
    121 _UtilExport bool fallbackConversion(orxonox::ColourValue* output, const std::string& input);
     145template <> struct _UtilExport ConverterFallback<orxonox::ColourValue, std::string>
     146{ static bool convert(orxonox::ColourValue* output, const std::string& input); };
    122147
    123148
Note: See TracChangeset for help on using the changeset viewer.