Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1745


Ignore:
Timestamp:
Sep 8, 2008, 11:17:20 PM (16 years ago)
Author:
rgrieder
Message:
  • Extended convert a little bit support template specialisations as well.
  • Converted conversion function with Math and std::string to Math.h and .cc
Location:
code/branches/core3/src/util
Files:
3 edited

Legend:

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

    r1744 r1745  
    5050///////////////////////////////////
    5151
     52// These conversions exhibit ambiguous << or >> operators when using stringstream
    5253inline bool explicitConversion(std::string* output, const char input)
    5354{
     55    *output = std::string(input, 1);
    5456    return true;
    5557}
    5658inline bool explicitConversion(std::string* output, const unsigned char input)
    5759{
     60    *output = std::string(input, 1);
    5861    return true;
    5962}
    6063inline bool explicitConversion(char* output, const std::string input)
    6164{
     65    if (input != "")
     66        *output = input[0];
     67    else
     68        *output = '\0';
    6269    return true;
    6370}
    6471inline bool explicitConversion(unsigned char* output, const std::string input)
    6572{
     73    if (input != "")
     74        *output = input[0];
     75    else
     76        *output = '\0';
    6677    return true;
    6778}
     
    7485   is described in 'Moder C++ design' by Alexandrescu (2001). */
    7586
    76 template <int a, int b>
    77 struct TemplateDebugger
    78 {
    79     static int debug(int c, int d) { return 0; } //BOOST_STATIC_ASSERT(0); }
    80 };
     87//template <int a, int b>
     88//struct TemplateDebugger
     89//{
     90//    static int debug(int c, int d) { return 0; } //BOOST_STATIC_ASSERT(0); }
     91//};
    8192
    8293namespace conversionTests
     
    8899}
    89100
    90 namespace generalFunctionTemplate
    91 {
     101//namespace generalFunctionTemplate
     102//{
    92103    // Keep this function out of conversion namespaces because the compiler gives a general
    93104    // template in the same namesapace a higher priority than any specialisation.
    94     // This function simply accepts anything but has lower priority to specialisations.
     105    // This function simply accepts anything but has lower priority than specialisations.
    95106    // It can be identified by the larger return value.
    96107    template <class AnyToType, class AnyFromType>
     
    103114                return *(new conversionTests::VeryBigStruct());
    104115    }
    105 }
     116    //conversionTests::VeryBigStruct explicitConversion(...);
     117//}
    106118
    107119namespace conversionTests
    108120{
    109     using namespace generalFunctionTemplate; // Why in separate namespace? See above
    110 
     121    //using namespace generalFunctionTemplate; // Why in separate namespace? See above
     122
     123    // This operators simply accept anything but have lower priority than specialisations.
     124    // It can be identified by the larger return value.
    111125    template <class Any>
    112126    conversionTests::VeryBigStruct operator<<(std::ostream& outstream, const Any anything);
     
    114128    conversionTests::VeryBigStruct operator>>(std::istream& instream,  const Any anything);
    115129
     130    // checks for implicit conversion
    116131    template <class FromType, class ToType>
    117132    class ImplicitConversion
     
    119134    private:
    120135        static VerySmallStruct test(ToType); // only accepts ToType, but is preferred over '...'
    121                 template <class AnyType>
    122         static VeryBigStruct   test(const AnyType anything);    // accepts anything
     136                //template <class AnyType>
     137        static VeryBigStruct   test(...);//const AnyType anything);    // accepts anything
    123138        static FromType object; // helper object to handle private c'tor and d'tor
    124139    public:
     
    126141    };
    127142
     143    // checks for explicit conversion with explicitConversion()
    128144    template <class FromType, class ToType, int asdf>
    129145    class ExplicitConversion
     
    148164    };
    149165
     166    // checks for conversion via istringstream
    150167    template <class Type>
    151168    class IStringStreamOperator
     
    157174    };
    158175
     176    // checks for conversion via ostringstream
    159177    template <class Type>
    160178    class OStringStreamOperator
     
    214232        }
    215233    };
    216 
    217 
    218     ///////////////////////
    219     //Explicit Conversion//
    220     ///////////////////////
    221 
    222     // We can cast explicitely, this overwrites any other possible cast
     234}
     235
     236
     237///////////////////////
     238//Explicit Conversion//
     239///////////////////////
     240
     241// template that is used when no explicit template specialisation is available
     242template <class ToType, class FromType>
     243struct ConverterSpecialised
     244{
     245    static bool convert(ToType* output, const FromType& input)
     246    {
     247        // check for explicit conversion via function overloading
     248        return conversion::convert(output, input,
     249            conversion::ExplicitPossible<ExplicitConversion<FromType, ToType>::exists>());
     250    }
     251};
     252
     253namespace conversion
     254{
     255    // We can cast explicitely via function overloading, this overwrites any other possible cast
    223256    template <class ToType, class FromType>
    224257    inline bool convert(ToType* output, const FromType& input, ExplicitPossible<true>)
     
    231264    }
    232265
    233     // No explict cast, try implicit
     266    // No function explict conversion, try implicit cast
    234267    template <class ToType, class FromType>
    235268    inline bool convert(ToType* output, const FromType& input, ExplicitPossible<false>)
    236269    {
    237270        return convert(output, input, ImplicitPossible<ImplicitConversion<FromType, ToType>::exists>());
     271        //return ConverterSpecialised<ToType, FromType>::convert(output, input);
    238272    }
    239273
     
    349383
    350384
    351     /////////////////
    352     //Special Cases//
    353     /////////////////
     385    ///////////////
     386    //const char*//
     387    ///////////////
    354388
    355389    // delegate conversion from const char* via string
     
    361395        { return Converter<ToType, std::string, Dummy>::convert(output, input); }
    362396    };
    363 #if 0
    364     // conversion char to std::string leads to ambiguous operator <<
    365     template <int Dummy>
    366     struct Converter<std::string, char, Dummy>
    367     {
    368         static bool convert(std::string* output, const char input)
    369         { return convertOStringStream(output, input); }
    370     };
    371 
    372     // conversion unsigned char to std::string leads to ambiguous operator <<
    373     template <int Dummy>
    374     struct Converter<std::string, unsigned char, Dummy>
    375     {
    376         static bool convert(std::string* output, const char input)
    377         { return convertOStringStream(output, input); }
    378     };
    379 
    380     // conversion std::string to char leads to ambiguous operator >>
    381     template <int Dummy>
    382     struct Converter<char, std::string, Dummy>
    383     {
    384         static bool convert(char* output, const std::string input)
    385         { return convertIStringStream(output, input); }
    386     };
    387 
    388     // conversion std::string to unsigned char leads to ambiguous operator >>
    389     template <int Dummy>
    390     struct Converter<unsigned char, std::string, Dummy>
    391     {
    392         static bool convert(unsigned char* output, const std::string input)
    393         { return convertIStringStream(output, input); }
    394     };
    395 #endif
    396397}
    397398
     
    409410inline bool convertValue(ToType* output, const FromType& input)
    410411{
    411     // check whether we can convert one type to the other explicitely.
     412    // check whether we can convert one type to the other explicitely via function overloading
    412413    //conversionTests::ExplicitConversion<FromType, ToType, 4>::test();
    413     return conversion::convert(output, input,
    414         conversion::ExplicitPossible<ExplicitConversion<FromType, ToType>::exists>());
     414    return ConverterSpecialised<ToType, FromType>::convert(output, input);
     415    //return conversion::convert(output, input,
     416    //    conversion::ExplicitPossible<ExplicitConversion<FromType, ToType>::exists>());
    415417}
    416418
     
    447449}
    448450
    449 
    450 /////////////////////
    451 // SPECIALIZATIONS //
    452 /////////////////////
    453 
    454 /////////////
    455 // SAMPLES //
    456 /////////////
    457 /*
    458 // convert everything to xyz
    459 template <class FromType>
    460 struct ConverterSpecialized<FromType, xyz, _ToType_>
    461 {
    462     enum { specialized = true };
    463     static bool convert(xyz* output, const FromType& input)
    464     { return ...; }
    465 };
    466 
    467 // convert xyz to everything
    468 template <class ToType>
    469 struct ConverterSpecialized<xyz, ToType, _FromType_>
    470 {
    471     enum { specialized = true };
    472     static bool convert(ToType* output, const xyz& input)
    473     { return ...; }
    474 };
    475 
    476 // convert abc to xyz
    477 template <>
    478 struct ConverterSpecialized<abc, xyz, _Explicit_>
    479 {
    480     enum { specialized = true };
    481     static bool convert(xyz* output, const abc& input)
    482     { return ...; }
    483 };
    484 */
    485 
    486 
    487 /////////////////
    488 // CONST CHAR* //
    489 /////////////////
    490 /*
    491 // convert from const char* --> use conversions with std::string
    492 template <class ToType, class Type>
    493 struct ConverterSpecialized<const char*, ToType, Type>
    494 {
    495     enum { specialized = true };
    496     static bool convert(ToType* output, const char* input)
    497     {
    498         return ConverterSpecialized<std::string, ToType, Type>::convert(output, input);
    499     }
    500 };
    501 
    502 // convert from const char* --> use conversions with std::string
    503 template <>
    504 struct ConverterSpecialized<const char*, std::string, _ToType_>
    505 {
    506     enum { specialized = true };
    507     static bool convert(std::string* output, const char* input)
    508     {
    509         *output = input;
    510         return true;
    511     }
    512 };
    513 
    514 // convert from const char* _Explicit_ --> use conversions with std::string
    515 //template <class ToType>
    516 //struct ConverterSpecialized<const char*, ToType, _Explicit_>
    517 //{
    518 //    enum { specialized = true };
    519 //    static bool convert(ToType* output, const char* input)
    520 //    {
    521 //        return ConverterSpecialized<std::string, ToType, _Explicit_>::convert(output, input);
    522 //    }
    523 //};
    524 
    525 // convert from char* without const is not allowed
    526 //template <class ToType, class Type>
    527 //struct ConverterSpecialized<char*, ToType, Type>
    528 //{
    529 //    enum { specialized = true };
    530 //    static bool convert(ToType* output, const char* input)
    531 //    {
    532 //        BOOST_STATIC_ASSERT(sizeof(ToType) == 0);
    533 //    }
    534 //};
    535 
    536 // No support for char* without const
    537 //template <class ToType, class Type>
    538 //struct ConverterSpecialized<char*, ToType, Type>
    539 //{
    540 //    enum { specialized = true };
    541 //    static bool convert(ToType* output, const char* input)
    542 //    {
    543 //        BOOST_STATIC_ASSERT(sizeof(ToType) == 0);
    544 //    }
    545 //};
    546 
    547 // convert to const char* is not supported (possible memory leak)
    548 template <class FromType, class Type>
    549 struct ConverterSpecialized<FromType, const char*, Type>
    550 {
    551     enum { specialized = true };
    552     static bool convert(const char** output, const FromType& input)
    553     {
    554         BOOST_STATIC_ASSERT(sizeof(FromType) == 0);
    555     }
    556 };
    557 
    558 // convert to char* is not supported (possible memory leak)
    559 // Note: It actually does need both specializations for const char* and char*
    560 //template <class FromType, class Type>
    561 //struct ConverterSpecialized<FromType, char*, Type>
    562 //{
    563 //    enum { specialized = true };
    564 //    static bool convert(char** output, const FromType& input)
    565 //    {
    566 //        BOOST_STATIC_ASSERT(sizeof(FromType) == 0);
    567 //    }
    568 //};
    569 
    570 
    571 ////////////////////
    572 // MATH TO STRING //
    573 ////////////////////
    574 
    575 // Vector2 to std::string
    576 template <>
    577 struct ConverterSpecialized<orxonox::Vector2, std::string, _Explicit_>
    578 {
    579     enum { specialized = true };
    580     static bool convert(std::string* output, const orxonox::Vector2& input)
    581     {
    582         std::ostringstream ostream;
    583         if (ostream << input.x << "," << input.y)
    584         {
    585             (*output) = ostream.str();
    586             return true;
    587         }
    588         return false;
    589     }
    590 };
    591 
    592 // Vector3 to std::string
    593 template <>
    594 struct ConverterSpecialized<orxonox::Vector3, std::string, _Explicit_>
    595 {
    596     enum { specialized = true };
    597     static bool convert(std::string* output, const orxonox::Vector3& input)
    598     {
    599         std::ostringstream ostream;
    600         if (ostream << input.x << "," << input.y << "," << input.z)
    601         {
    602             (*output) = ostream.str();
    603             return true;
    604         }
    605         return false;
    606     }
    607 };
    608 
    609 // Vector4 to std::string
    610 template <>
    611 struct ConverterSpecialized<orxonox::Vector4, std::string, _Explicit_>
    612 {
    613     enum { specialized = true };
    614     static bool convert(std::string* output, const orxonox::Vector4& input)
    615     {
    616         std::ostringstream ostream;
    617         if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w)
    618         {
    619             (*output) = ostream.str();
    620             return true;
    621         }
    622         return false;
    623     }
    624 };
    625 
    626 // Quaternion to std::string
    627 template <>
    628 struct ConverterSpecialized<orxonox::Quaternion, std::string, _Explicit_>
    629 {
    630     enum { specialized = true };
    631     static bool convert(std::string* output, const orxonox::Quaternion& input)
    632     {
    633         std::ostringstream ostream;
    634         if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z)
    635         {
    636             (*output) = ostream.str();
    637             return true;
    638         }
    639         return false;
    640     }
    641 };
    642 
    643 // ColourValue to std::string
    644 template <>
    645 struct ConverterSpecialized<orxonox::ColourValue, std::string, _Explicit_>
    646 {
    647     enum { specialized = true };
    648     static bool convert(std::string* output, const orxonox::ColourValue& input)
    649     {
    650         std::ostringstream ostream;
    651         if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)
    652         {
    653             (*output) = ostream.str();
    654             return true;
    655         }
    656         return false;
    657     }
    658 };
    659 
    660 
    661 ////////////////////
    662 // STRING TO MATH //
    663 ////////////////////
    664 
    665 // std::string to Vector2
    666 template <>
    667 struct ConverterSpecialized<std::string, orxonox::Vector2, _Explicit_>
    668 {
    669     enum { specialized = true };
    670     static bool convert(orxonox::Vector2* output, const std::string& input)
    671     {
    672         unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
    673         if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
    674 
    675         SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
    676         if (tokens.size() >= 2)
    677         {
    678             if (!ConvertValue(&(output->x), tokens[0]))
    679                 return false;
    680             if (!ConvertValue(&(output->y), tokens[1]))
    681                 return false;
    682 
    683             return true;
    684         }
    685         return false;
    686     }
    687 };
    688 
    689 // std::string to Vector3
    690 template <>
    691 struct ConverterSpecialized<std::string, orxonox::Vector3, _Explicit_>
    692 {
    693     enum { specialized = true };
    694     static bool convert(orxonox::Vector3* output, const std::string& input)
    695     {
    696         unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
    697         if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
    698 
    699         SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
    700         if (tokens.size() >= 3)
    701         {
    702             if (!ConvertValue(&(output->x), tokens[0]))
    703                 return false;
    704             if (!ConvertValue(&(output->y), tokens[1]))
    705                 return false;
    706             if (!ConvertValue(&(output->z), tokens[2]))
    707                 return false;
    708 
    709             return true;
    710         }
    711         return false;
    712     }
    713 };
    714 
    715 // std::string to Vector4
    716 template <>
    717 struct ConverterSpecialized<std::string, orxonox::Vector4, _Explicit_>
    718 {
    719     enum { specialized = true };
    720     static bool convert(orxonox::Vector4* output, const std::string& input)
    721     {
    722         unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
    723         if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
    724 
    725         SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
    726         if (tokens.size() >= 4)
    727         {
    728             if (!ConvertValue(&(output->x), tokens[0]))
    729                 return false;
    730             if (!ConvertValue(&(output->y), tokens[1]))
    731                 return false;
    732             if (!ConvertValue(&(output->z), tokens[2]))
    733                 return false;
    734             if (!ConvertValue(&(output->w), tokens[3]))
    735                 return false;
    736 
    737             return true;
    738         }
    739         return false;
    740     }
    741 };
    742 
    743 // std::string to Quaternion
    744 template <>
    745 struct ConverterSpecialized<std::string, orxonox::Quaternion, _Explicit_>
    746 {
    747     enum { specialized = true };
    748     static bool convert(orxonox::Quaternion* output, const std::string& input)
    749     {
    750         unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
    751         if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
    752 
    753         SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
    754         if (tokens.size() >= 4)
    755         {
    756             if (!ConvertValue(&(output->w), tokens[0]))
    757                 return false;
    758             if (!ConvertValue(&(output->x), tokens[1]))
    759                 return false;
    760             if (!ConvertValue(&(output->y), tokens[2]))
    761                 return false;
    762             if (!ConvertValue(&(output->z), tokens[3]))
    763                 return false;
    764 
    765             return true;
    766         }
    767         return false;
    768     }
    769 };
    770 
    771 // std::string to ColourValue
    772 template <>
    773 struct ConverterSpecialized<std::string, orxonox::ColourValue, _Explicit_>
    774 {
    775     enum { specialized = true };
    776     static bool convert(orxonox::ColourValue* output, const std::string& input)
    777     {
    778         unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
    779         if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
    780 
    781         SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
    782         if (tokens.size() >= 4)
    783         {
    784             if (!ConvertValue(&(output->r), tokens[0]))
    785                 return false;
    786             if (!ConvertValue(&(output->g), tokens[1]))
    787                 return false;
    788             if (!ConvertValue(&(output->b), tokens[2]))
    789                 return false;
    790             if (!ConvertValue(&(output->a), tokens[3]))
    791                 return false;
    792 
    793             return true;
    794         }
    795         return false;
    796     }
    797 };
    798 */
    799451#endif /* _Convert_H__ */
  • code/branches/core3/src/util/Math.cc

    r1566 r1745  
    3030
    3131#include "Math.h"
     32#include "Convert.h"
    3233
    3334/**
     
    123124    return (targetposition + targetvelocity * time);
    124125}
     126
     127// std::string to Vector2
     128bool explicitConversion(orxonox::Vector2* output, const std::string& input)
     129{
     130    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     131    if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
     132
     133    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
     134    if (tokens.size() >= 2)
     135    {
     136        if (!ConvertValue(&(output->x), tokens[0]))
     137            return false;
     138        if (!ConvertValue(&(output->y), tokens[1]))
     139            return false;
     140
     141        return true;
     142    }
     143    return false;
     144}
     145
     146// std::string to Vector3
     147bool explicitConversion(orxonox::Vector3* output, const std::string& input)
     148{
     149    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     150    if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
     151
     152    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
     153    if (tokens.size() >= 3)
     154    {
     155        if (!ConvertValue(&(output->x), tokens[0]))
     156            return false;
     157        if (!ConvertValue(&(output->y), tokens[1]))
     158            return false;
     159        if (!ConvertValue(&(output->z), tokens[2]))
     160            return false;
     161
     162        return true;
     163    }
     164    return false;
     165}
     166
     167// std::string to Vector4
     168bool explicitConversion(orxonox::Vector4* output, const std::string& input)
     169{
     170    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     171    if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
     172
     173    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
     174    if (tokens.size() >= 4)
     175    {
     176        if (!ConvertValue(&(output->x), tokens[0]))
     177            return false;
     178        if (!ConvertValue(&(output->y), tokens[1]))
     179            return false;
     180        if (!ConvertValue(&(output->z), tokens[2]))
     181            return false;
     182        if (!ConvertValue(&(output->w), tokens[3]))
     183            return false;
     184
     185        return true;
     186    }
     187    return false;
     188}
     189
     190// std::string to Quaternion
     191bool explicitConversion(orxonox::Quaternion* output, const std::string& input)
     192{
     193    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     194    if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
     195
     196    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
     197    if (tokens.size() >= 4)
     198    {
     199        if (!ConvertValue(&(output->w), tokens[0]))
     200            return false;
     201        if (!ConvertValue(&(output->x), tokens[1]))
     202            return false;
     203        if (!ConvertValue(&(output->y), tokens[2]))
     204            return false;
     205        if (!ConvertValue(&(output->z), tokens[3]))
     206            return false;
     207
     208        return true;
     209    }
     210    return false;
     211}
     212
     213// std::string to ColourValue
     214bool explicitConversion(orxonox::ColourValue* output, const std::string& input)
     215{
     216    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     217    if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
     218
     219    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
     220    if (tokens.size() >= 4)
     221    {
     222        if (!ConvertValue(&(output->r), tokens[0]))
     223            return false;
     224        if (!ConvertValue(&(output->g), tokens[1]))
     225            return false;
     226        if (!ConvertValue(&(output->b), tokens[2]))
     227            return false;
     228        if (!ConvertValue(&(output->a), tokens[3]))
     229            return false;
     230
     231        return true;
     232    }
     233    return false;
     234}
  • code/branches/core3/src/util/Math.h

    r1566 r1745  
    4141#include <OgreQuaternion.h>
    4242#include <OgreColourValue.h>
     43#include "SubString.h"
    4344
    4445namespace orxonox
     
    6465_UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity);
    6566
     67/////////////////////////////////////
     68// Conversion Functions of our own //
     69/////////////////////////////////////
     70
     71// Vector2 to std::string
     72inline bool explicitConversion(std::string* output, const orxonox::Vector2& input)
     73{
     74    std::ostringstream ostream;
     75    if (ostream << input.x << "," << input.y)
     76    {
     77        (*output) = ostream.str();
     78        return true;
     79    }
     80    return false;
     81}
     82
     83// Vector3 to std::string
     84inline bool explicitConversion(std::string* output, const orxonox::Vector3& input)
     85{
     86    std::ostringstream ostream;
     87    if (ostream << input.x << "," << input.y << "," << input.z)
     88    {
     89        (*output) = ostream.str();
     90        return true;
     91    }
     92    return false;
     93}
     94
     95// Vector4 to std::string
     96inline bool explicitConversion(std::string* output, const orxonox::Vector4& input)
     97{
     98    std::ostringstream ostream;
     99    if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w)
     100    {
     101        (*output) = ostream.str();
     102        return true;
     103    }
     104    return false;
     105}
     106
     107// Quaternion to std::string
     108inline bool explicitConversion(std::string* output, const orxonox::Quaternion& input)
     109{
     110    std::ostringstream ostream;
     111    if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z)
     112    {
     113        (*output) = ostream.str();
     114        return true;
     115    }
     116    return false;
     117}
     118
     119// ColourValue to std::string
     120inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input)
     121{
     122    std::ostringstream ostream;
     123    if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)
     124    {
     125        (*output) = ostream.str();
     126        return true;
     127    }
     128    return false;
     129}
     130
     131// std::string to Vector2
     132_UtilExport bool explicitConversion(orxonox::Vector2* output, const std::string& input);
     133// std::string to Vector3
     134_UtilExport bool explicitConversion(orxonox::Vector3* output, const std::string& input);
     135// std::string to Vector4
     136_UtilExport bool explicitConversion(orxonox::Vector4* output, const std::string& input);
     137// std::string to Quaternion
     138_UtilExport bool explicitConversion(orxonox::Quaternion* output, const std::string& input);
     139// std::string to ColourValue
     140_UtilExport bool explicitConversion(orxonox::ColourValue* output, const std::string& input);
     141
    66142template <typename T>
    67143inline T sgn(T x)
Note: See TracChangeset for help on using the changeset viewer.