Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1778 for code


Ignore:
Timestamp:
Sep 14, 2008, 3:40:48 PM (16 years ago)
Author:
rgrieder
Message:

Convert.h done. Has yet to be tested with gcc. And the comments have to be adapted.

Location:
code/branches/core3/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core3/src/orxonox/Orxonox.cc

    r1777 r1778  
    8686    assert(res == var##nr##2)
    8787
    88 template <>
    89 struct ConverterExplicit<orxonox::Radian, const char*>
    90 {
    91     static bool convert(orxonox::Radian* output, const char* input)
    92     {
    93         //std::string asdf("asfd");
    94         //std::cout << asdf;
    95         float temp;
    96         convertValue(&temp, input);
    97         *output = temp;
    98     }
    99 };
     88//template <>
     89//struct ConverterExplicit<orxonox::Radian, const char*>
     90//{
     91//    static bool convert(orxonox::Radian* output, const char* input)
     92//    {
     93//        //std::string asdf("asfd");
     94//        //std::cout << asdf;
     95//        float temp;
     96//        convertValue(&temp, input);
     97//        *output = temp;
     98//    }
     99//};
    100100
    101101class FooBar { };
  • code/branches/core3/src/util/ArgReader.cc

    r1777 r1778  
    6565#include "Convert.h"
    6666
    67 template<>
    68 struct ConverterExplicit<const char*, FooBar>
    69 {
    70     static bool convert(const char** output, const FooBar input)
    71     {
    72         return true;
    73     }
    74 };
     67//template<>
     68//struct ConverterExplicit<const char*, FooBar>
     69//{
     70//    static bool convert(const char** output, const FooBar input)
     71//    {
     72//        return true;
     73//    }
     74//};
    7575
    7676#include "MultiType.h"
     
    9090  //val2 = val1;
    9191  //convertValue(&val2, val1);
    92   //explicitConversion(&FooBar(), val1);
     92  //convertValue(&FooBar(), val1);
    9393
    9494  //using namespace1::fooBar1;
     
    9696  //int val1;
    9797  //char val2;
    98   //explicitConversion(&val1, val2);
     98  //convertValue(&val1, val2);
    9999
    100100  //std::istringstream asdf;
  • code/branches/core3/src/util/Convert.h

    r1777 r1778  
    5858   is described in 'Moder C++ design' by Alexandrescu (2001). */
    5959
    60 namespace conversionTests
    61 {
    62     // A struct that is guaranteed to be larger than any return type of our conversion functions.
    63     // So we simply add all the sizes of the return types plus a little bit more.
    64     struct VeryBigStruct
    65     {
    66         char intSize[sizeof(int)];
    67         char issSize[sizeof(std::istringstream)];
    68         char ossSize[sizeof(std::ostringstream)];
    69         char boolSize[sizeof(bool)];
    70         char addingMore[4096]; // just to be sure ;)
    71     };
    72 }
    73 
    74 namespace conversion_another_namespace
    75 {
    76     // We want to keep the templates for the convert functions out of global namespace so that there
    77     // are no ambiguities. These templates are never used anyway, they only serve to detect whether
    78     // there is a global funciton for a specific conversion or not.
    79     // Why the seperate namespace? --> see 'using' statement at the end of conversionTests::
    80     //template <class AnyToType, class AnyFromType>
    81     //conversionTests::VeryBigStruct explicitConversion(AnyToType* output, const AnyFromType input);
    82     //template <class Any, int Dummy>
    83     //conversionTests::VeryBigStruct operator<<(std::ostream& outstream, const Any& anything);
    84     //template <class Any, int Dummy>
    85     //conversionTests::VeryBigStruct operator>>(std::istream& instream,  const Any& anything);
    86 }
    87 
    88 namespace conversionTests
    89 {
    9060// disable warnings about possible loss of data
    9161#if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC
     
    9363#  pragma warning(disable:4244)
    9464#endif
     65
     66namespace conversionTests
     67{
     68    // A struct that is guaranteed to be larger than any return type of our conversion functions.
     69    // So we simply add all the sizes of the return types plus a little bit more.
     70    struct VeryBigStruct
     71    {
     72        char intSize[sizeof(int)];
     73        char addingMore[4096]; // just to be sure ;)
     74    };
    9575
    9676    template <class FromType, class ToType>
     
    11090        enum { exists = !(sizeof(test(object)) == sizeof(VeryBigStruct)) };
    11191    };
     92}
    11293
    11394#if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC
    11495#  pragma warning(pop)
    11596#endif
    116 
    117     //template <class FromType, class ToType>
    118     //class ExplicitConversion
    119     //{
    120     //private:
    121     //    ExplicitConversion(); ExplicitConversion(const ExplicitConversion&); ~ExplicitConversion();
    122     //    static FromType objectFromType; // helper object to handle private c'tor and d'tor
    123     //    static ToType   objectToType;   // helper object to handle private c'tor and d'tor
    124     //public:
    125     //    enum { exists = !(sizeof(explicitConversion(&objectToType, objectFromType)) == sizeof(VeryBigStruct)) };
    126     //};
    127 
    128     //template <class Type>
    129     //class IStreamOperator
    130     //{
    131     //    IStreamOperator(); IStreamOperator(const IStreamOperator&); ~IStreamOperator();
    132     //    static std::istream istream_; // helper object to perform the '>>' operation
    133     //    static Type object;           // helper object to handle private c'tor and d'tor
    134     //public:
    135     //    enum { exists = !(sizeof(istream_ >> object) == sizeof(VeryBigStruct)) };
    136     //};
    137 
    138     //template <class Type>
    139     //class OStreamOperator
    140     //{
    141     //    OStreamOperator(); OStreamOperator(const OStreamOperator&); ~OStreamOperator();
    142     //    static std::ostream ostream_; // helper object to perform the '<<' operation
    143     //    static Type object;           // helper object to handle private c'tor and d'tor
    144     //public:
    145     //    enum { exists = !(sizeof(ostream_ << object) == sizeof(VeryBigStruct)) };
    146     //};
    147 
    148     // Somehow msvc and gcc don't like it when using function arguments that have a type
    149     // in a third namespace. The global namespace functions then get overridden by the
    150     // templates above. So we simply put the generic ones in a another namespace.
    151     // Note: DON'T place this statement before a class template. That would trigger
    152     //       a heavy bug (namespace leakage) in msvc 2005.
    153     //using namespace conversion_another_namespace;
    154 }
    15597
    15698
     
    162104    Overwrites:
    163105    1. (Partial) template specialisation of ConverterExplicit::convert
    164     2. Global functions explicitConversion(ToType* output, const FromType input)
     106    2. Global functions convertValue(ToType* output, const FromType input)
    165107    Fallbacks:
    166108    3. Any possible implicit conversion. This includes FooBar --> int if FooBar defines operator float().
     
    171113
    172114    There are obviously a lot of ways to specifiy a user defined conversion. What should I use?
    173     When using any non-template function based conversion (implicit conversion, explicitConversion, << or >>)
     115    When using any non-template function based conversion (implicit conversion, convertValue, << or >>)
    174116    then you should consider that this function has to be defined prior to including this file.
    175117    If you do not whish do do that, you will have to spcecialsize the ConverterExplicit template.
     
    193135///////////////////
    194136
    195 namespace conversion
    196 {
    197     // Default template for stringtream, no Conversion possible
    198     template <class ToType, class FromType>
    199     struct ConverterSS
    200     {
    201         static bool convert(ToType* output, FromType input)
    202         {
    203             COUT(2) << "Could not convert value of type " << typeid(FromType).name()
    204                     << " to type " << typeid(ToType).name() << std::endl;
    205             return false;
    206         }
    207     };
    208 }
     137// Default template for fallbackConvert, no conversion possible
     138template <class ToType, class FromType>
     139inline 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.
     153template <class ToType, class FromType>
     154struct ConverterFallbackTemplate
     155{
     156    static bool convert(ToType* output, const FromType& input)
     157    {
     158        return fallbackConversion(output, input);
     159    }
     160};
     161
     162// Default template for stringstream
     163template <class ToType, class FromType>
     164struct ConverterStringStream
     165{
     166    static bool convert(ToType* output, const FromType& input)
     167    {
     168        return fallbackConversion(output, input);
     169    }
     170};
    209171
    210172
     
    215177namespace fallbackTemplates
    216178{
    217     template <class Any>
    218     bool operator <<(std::ostream& outstream,  const Any& anything)
    219     {
    220         COUT(2) << "Could not convert value of type " << typeid(Any).name()
    221                 << " to std::string" << std::endl;
    222         return false;
    223     }
    224 }
    225 
    226 namespace conversion
    227 {
    228     //using namespace fallbackTemplates;
    229     // template that evaluates whether OStringStream is possible for conversions to std::string
    230179    template <class FromType>
    231     struct ConverterSS<std::string, FromType>
    232     {
    233         // probe for '<<' ostream operator
    234         static bool convert(std::string* output, const FromType& input)
     180    inline bool operator <<(std::ostream& outstream,  const FromType& input)
     181    {
     182        std::string temp;
     183        if (ConverterFallbackTemplate<std::string, FromType>::convert(&temp, input))
    235184        {
    236             std::ostringstream oss;
    237             if (oss << input)
    238             {
    239                 (*output) = oss.str();
    240                 return true;
    241             }
    242             else
    243                 return false;
     185            std::operator <<(outstream, temp);
     186            return true;
    244187        }
    245     };
    246 
    247 }
     188        else
     189            return false;
     190    }
     191}
     192
     193// template that evaluates whether OStringStream is possible for conversions to std::string
     194template <class FromType>
     195struct ConverterStringStream<std::string, FromType>
     196{
     197    static bool convert(std::string* output, const FromType& input)
     198    {
     199        using namespace fallbackTemplates;
     200        std::ostringstream oss;
     201        if (oss << input)
     202        {
     203            (*output) = oss.str();
     204            return true;
     205        }
     206        else
     207            return false;
     208    }
     209};
    248210
    249211
     
    254216namespace fallbackTemplates
    255217{
    256     template <class Any>
    257     bool operator >>(std::istream& instream,  const Any& anything)
    258     {
    259         COUT(2) << "Could not convert std::string value to type " << typeid(ToType).name() << std::endl;
    260         return false;
    261     }
    262 }
    263 
    264 namespace conversion
    265 {
    266     // template that evaluates whether IStringStream is possible for conversions from std::string
    267218    template <class ToType>
    268     struct ConverterSS<ToType, std::string>
    269     {
    270         static bool convert(ToType* output, const std::string& input)
     219    inline bool operator >>(std::istream& instream, ToType& output)
     220    {
     221        return ConverterFallbackTemplate<ToType, std::string>
     222            ::convert(&output, static_cast<std::istringstream&>(instream).str());
     223    }
     224}
     225
     226// template that evaluates whether IStringStream is possible for conversions from std::string
     227template <class ToType>
     228struct ConverterStringStream<ToType, std::string>
     229{
     230    static bool convert(ToType* output, const std::string& input)
     231    {
     232        using namespace fallbackTemplates;
     233        std::istringstream iss(input);
     234        if (iss >> (*output))
    271235        {
    272             std::istringstream iss(input);
    273             if (iss >> (*output))
    274             {
    275                 return true;
    276             }
    277             else
    278                 return false;
     236            return true;
    279237        }
    280     };
    281 
    282     using namespace fallbackTemplates;
    283 }
     238        else
     239            return false;
     240    }
     241};
    284242
    285243
     
    295253    return true;
    296254}
     255
    297256// static cast no possible, try stringstream conversion next
    298257template <class ToType, class FromType>
    299258inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>)
    300259{
    301     return conversion::ConverterSS<ToType, FromType>::convert(output, input);
    302 }
    303 
    304 
    305 /////////////////////////
    306 // Explicit conversion //
    307 /////////////////////////
    308 
    309 namespace fallbackTemplates
    310 {
    311     // We want to keep the templates for the convert functions out of global namespace so that there
    312     // are no ambiguities. These templates are never used anyway, they only serve to detect whether
    313     // there is a global funciton for a specific conversion or not.
    314     // Note: Don't put these functions in a separate namespace and add a 'using' directive here.
    315     // For MS sake, just don't!
    316     template <class ToType, class FromType>
    317     bool explicitConversion(ToType* output, const FromType input)
    318     {
    319         // try implict conversion by probing first because we use '...' instead of a template
    320         const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists;
    321         //using namespace conversion;
    322         return convertImplicitely(output, input, ::Int2Type<probe>());
    323     }
    324 }
    325 
    326 
    327 /////////////////////
    328 // Local Overwrite //
    329 /////////////////////
    330 
    331 // Template that is used when no explicit template specialisation is available.
    332 // Try explicitConversion() function next.
    333 template <class ToType, class FromType>
    334 struct ConverterExplicit
     260    return ConverterStringStream<ToType, FromType>::convert(output, input);
     261}
     262
     263
     264///////////////////////
     265// Explicit Fallback //
     266///////////////////////
     267
     268template <class ToType, class FromType>
     269inline 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.
     278template <class ToType, class FromType>
     279struct ConverterExplicitTemplate
    335280{
    336281    static bool convert(ToType* output, const FromType& input)
    337282    {
    338         using namespace fallbackTemplates;
    339283        return explicitConversion(output, input);
    340284    }
     
    348292/**
    349293@brief
    350     Converts any value to any other as long as there exits a conversion.
     294    Converts any value to any other as long as there exists a conversion.
    351295    Otherwise, the conversion will generate a runtime warning.
    352296    For information about the different conversion methods (user defined too), see the section
    353297    'Actual conversion sequence' in this file above.
     298@note
     299    This function is only a fallback if there is no appropriate 'convertValue' function.
    354300*/
    355301template <class ToType, class FromType>
    356302inline bool convertValue(ToType* output, const FromType& input)
    357303{
    358     // check whether we can convert one type to the other explicitely via explicit template specialisations
    359     return ConverterExplicit<ToType, FromType>::convert(output, input);
     304    return ConverterExplicitTemplate<ToType, FromType>::convert(output, input);
    360305}
    361306
     
    425370inline bool explicitConversion(ToType* output, const char* input)
    426371{
    427     return ConverterExplicit<ToType, std::string>::convert(output, input);
     372    return convertValue<ToType, std::string>(output, input);
    428373}
    429374
  • code/branches/core3/src/util/Math.cc

    r1777 r1778  
    125125}
    126126
     127//////////////////////////
     128// Conversion functions //
     129//////////////////////////
     130
    127131// std::string to Vector2
    128 bool explicitConversion(orxonox::Vector2* output, const std::string& input)
     132bool fallbackConversion(orxonox::Vector2* output, const std::string& input)
    129133{
    130134    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     
    145149
    146150// std::string to Vector3
    147 bool explicitConversion(orxonox::Vector3* output, const std::string& input)
     151bool fallbackConversion(orxonox::Vector3* output, const std::string& input)
    148152{
    149153    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     
    166170
    167171// std::string to Vector4
    168 bool explicitConversion(orxonox::Vector4* output, const std::string& input)
     172bool fallbackConversion(orxonox::Vector4* output, const std::string& input)
    169173{
    170174    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     
    189193
    190194// std::string to Quaternion
    191 bool explicitConversion(orxonox::Quaternion* output, const std::string& input)
     195bool fallbackConversion(orxonox::Quaternion* output, const std::string& input)
    192196{
    193197    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
     
    212216
    213217// std::string to ColourValue
    214 bool explicitConversion(orxonox::ColourValue* output, const std::string& input)
     218bool fallbackConversion(orxonox::ColourValue* output, const std::string& input)
    215219{
    216220    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
  • code/branches/core3/src/util/MathConvert.h

    r1777 r1778  
    2727 */
    2828
    29 /*!
     29/**
    3030    @file
    31     @brief Actual conversion functions.
     31    @brief
     32        Math conversion functions. Definitions are in Math.cc
    3233*/
    3334
     
    3839#include "Convert.h"
    3940#include "Math.h"
    40 
    41 // Note: explicitConversion function is used whenever possible prior to
    42 //       template specialisations to allow for manual calls and overwrites.
    4341
    4442
     
    113111
    114112// std::string to Vector2
    115 _UtilExport bool explicitConversion(orxonox::Vector2* output, const std::string& input);
     113_UtilExport bool fallbackConversion(orxonox::Vector2* output, const std::string& input);
    116114// std::string to Vector3
    117 _UtilExport bool explicitConversion(orxonox::Vector3* output, const std::string& input);
     115_UtilExport bool fallbackConversion(orxonox::Vector3* output, const std::string& input);
    118116// std::string to Vector4
    119 _UtilExport bool explicitConversion(orxonox::Vector4* output, const std::string& input);
     117_UtilExport bool fallbackConversion(orxonox::Vector4* output, const std::string& input);
    120118// std::string to Quaternion
    121 _UtilExport bool explicitConversion(orxonox::Quaternion* output, const std::string& input);
     119_UtilExport bool fallbackConversion(orxonox::Quaternion* output, const std::string& input);
    122120// std::string to ColourValue
    123 _UtilExport bool explicitConversion(orxonox::ColourValue* output, const std::string& input);
     121_UtilExport bool fallbackConversion(orxonox::ColourValue* output, const std::string& input);
    124122
    125123
     
    130128// From Radian
    131129template <class ToType>
    132 inline bool explicitConversion(ToType* output, const orxonox::Radian input)
     130inline bool fallbackConversion(ToType* output, const orxonox::Radian input)
    133131{
    134132    return convertValue<ToType, Ogre::Real>(output, input.valueRadians());
     
    137135// From Degree
    138136template <class ToType>
    139 inline bool explicitConversion(ToType* output, const orxonox::Degree input)
     137inline bool fallbackConversion(ToType* output, const orxonox::Degree input)
    140138{
    141139    return convertValue<ToType, Ogre::Real>(output, input.valueDegrees());
     
    144142// To Radian
    145143template <class FromType>
    146 inline bool explicitConversion(orxonox::Radian* output, const FromType input)
     144inline bool fallbackConversion(orxonox::Radian* output, const FromType input)
    147145{
    148146    float temp;
     
    158156// To Degree
    159157template <class FromType>
    160 inline bool explicitConversion(orxonox::Degree* output, const FromType input)
     158inline bool fallbackConversion(orxonox::Degree* output, const FromType input)
    161159{
    162160    float temp;
     
    169167        return false;
    170168}
    171 // Radian to Radian
    172 inline bool explicitConversion(orxonox::Radian* output, const orxonox::Radian input)
    173 { *output = input; return true; }
    174 // Degree to Degree
    175 inline bool explicitConversion(orxonox::Degree* output, const orxonox::Degree input)
    176 { *output = input; return true; }
    177 // Radian to Degree
    178 inline bool explicitConversion(orxonox::Degree* output, const orxonox::Radian input)
    179 { *output = input; return true; }
    180 // Degree to Radian
    181 inline bool explicitConversion(orxonox::Radian* output, const orxonox::Degree input)
    182 { *output = input; return true; }
    183 
    184 //template <class ToType>
    185 //struct ConverterExplicit<ToType, Ogre::Radian>
    186 //{
    187 //    static bool convert(ToType* output, const Ogre::Radian input)
    188 //    {
    189 //        return ConverterExplicit<ToType, Ogre::Real>::convert(output, input.valueRadians());
    190 //    }
    191 //};
    192 
    193 //// Conversion from Ogre::Radian
    194 //template <class ToType>
    195 //struct ConverterExplicit<ToType, const Ogre::Radian>
    196 //{
    197 //    static bool convert(ToType* output, const Ogre::Radian input)
    198 //    {
    199 //        return ConverterExplicit<ToType, Ogre::Real>::convert(output, input.getValueRadian());
    200 //    }
    201 //};
    202 
    203 //template <class ToType>
    204 //inline bool explicitConversion(ToType* output, const Ogre::Radian& input)
    205 //{
    206 //    return ConvertValue(output, ;
    207 //}
    208 
    209 //// Conversion from Ogre::Degrees
    210 //inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input)
    211 //{
    212 //    std::ostringstream ostream;
    213 //    if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)
    214 //    {
    215 //        (*output) = ostream.str();
    216 //        return true;
    217 //    }
    218 //    return false;
    219 //}
    220 //
    221 //// Conversion to Ogre::Radian
    222 //inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input)
    223 //{
    224 //    std::ostringstream ostream;
    225 //    if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)
    226 //    {
    227 //        (*output) = ostream.str();
    228 //        return true;
    229 //    }
    230 //    return false;
    231 //}
    232 //
    233 //// Conversion to Ogre::Degrees
    234 //inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input)
    235 //{
    236 //    std::ostringstream ostream;
    237 //    if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a)
    238 //    {
    239 //        (*output) = ostream.str();
    240 //        return true;
    241 //    }
    242 //    return false;
    243 //}
    244169
    245170#endif /* _MathConvert_H__ */
Note: See TracChangeset for help on using the changeset viewer.