Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2014


Ignore:
Timestamp:
Oct 26, 2008, 12:35:58 AM (16 years ago)
Author:
rgrieder
Message:

Added convert test for gcc.

Location:
code/branches/core3
Files:
1 added
7 edited

Legend:

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

    r1778 r2014  
    231231    Factory::createClassHierarchy();
    232232
    233     std::string nmbr;
    234     char res('a');
    235     //const char* nmbr;
    236     //const char* str;
    237     convertValue(&nmbr, res);
    238     //const unsigned int blah = 4;
    239     //convertValue(nmbr, blah);
    240     //convertValue(&str, 4.0f);
    241 
    242     //using ::operator<<;
    243     using std::string;
    244     int a = 3;
    245     FooBar asdf;
    246     std::string asdf2;
    247     std::ostringstream oss;
    248     char blah(' ');
    249     oss << blah;
    250     std::cout << asdf2;
    251     ConvertValue(&asdf2, asdf);
    252     //if (conversionTests::OStreamOperator<FooBar>::exists)
    253     //    int asdfasdfasdf = 7;
    254     //COUT(3) << asdf;
    255 
    256     TestConv(1, int, (3), float, 3.0);
    257     TestConv(2, int, (3), string, "3");
    258     TestConv(3, string, ("3.0"), float, 3.0f);
    259     TestConv(4, char, ('a'), string, "a");
    260     TestConv(5, string, ("df"), char, 'd');
    261     TestConv(6, Vector2, (3,4), string, "3,4");
    262     TestConv(7, const char*, ("4.3"), float, 4.3f);
    263     TestConv(8, const char*, ("4,3"), Vector2, Vector2(4,3));
    264     //TestConv(9, const char*, ("4.4"), Radian, Radian(4.4));
    265     TestConv(100, int, (3), const char*, "3");
    266     TestConv(101, Vector3, (1, 2, 3), float, 3.0);
    267 
    268     std::ostringstream out;
     233#include "convert_test.cc"
     234
     235    // some special string conversions
     236    const char* myCString = "84.0";
     237    COUT(0) << conversion_cast<std::string>(myCString) << std::endl;
     238    COUT(0) << conversion_cast<float>(myCString) << std::endl;
     239    COUT(0) << conversion_cast<Vector2>(myCString) << std::endl;
     240    COUT(0) << conversion_cast<std::string>(conversion_cast<Radian>(myCString)) << std::endl;
     241    COUT(0) << std::endl;
     242
     243    std::string myString = "3,4,1";
     244    COUT(0) << conversion_cast<Vector3>(myString) << std::endl;
     245   
     246
     247    //std::string nmbr;
     248    //char res('a');
     249    ////const char* nmbr;
     250    ////const char* str;
     251    //convertValue(&nmbr, res);
     252    ////const unsigned int blah = 4;
     253    ////convertValue(nmbr, blah);
     254    ////convertValue(&str, 4.0f);
     255
     256    ////using ::operator<<;
     257    //using std::string;
     258    //int a = 3;
     259    //FooBar asdf;
     260    //std::string asdf2;
     261    //std::ostringstream oss;
     262    //char blah(' ');
     263    //oss << blah;
     264    //std::cout << asdf2;
     265    //ConvertValue(&asdf2, asdf);
     266    ////if (conversionTests::OStreamOperator<FooBar>::exists)
     267    ////    int asdfasdfasdf = 7;
     268    ////COUT(3) << asdf;
     269
     270    //TestConv(1, int, (3), float, 3.0);
     271    //TestConv(2, int, (3), string, "3");
     272    //TestConv(3, string, ("3.0"), float, 3.0f);
     273    //TestConv(4, char, ('a'), string, "a");
     274    //TestConv(5, string, ("df"), char, 'd');
     275    //TestConv(6, Vector2, (3,4), string, "3,4");
     276    //TestConv(7, const char*, ("4.3"), float, 4.3f);
     277    //TestConv(8, const char*, ("4,3"), Vector2, Vector2(4,3));
     278    ////TestConv(9, const char*, ("4.4"), Radian, Radian(4.4));
     279    //TestConv(100, int, (3), const char*, "3");
     280    //TestConv(101, Vector3, (1, 2, 3), float, 3.0);
     281
     282    //std::ostringstream out;
    269283
    270284    std::string mode;
  • code/branches/core3/src/util/Convert.h

    r1779 r2014  
    2323 *      Benjamin Grauer
    2424 *      Fabian 'x3n' Landau
    25  *      Reto Grieder (direct conversion tests)
     25 *      Reto Grieder
    2626 *   Co-authors:
    2727 *      ...
     
    4545#include "Debug.h"
    4646
    47 // Gcc generates warnings when implicitely casting from float to int for instance.
    48 // This is however exactly what convertValue does, so we need to suppress the warnings.
    49 // They only occur in when using the ImplicitConversion template.
     47// GCC generates warnings when implicitely casting from float to int for instance.
     48// This is however exactly what convertValue does, so we need to suppress these warnings.
     49// They only occur when using the ImplicitConversion template.
    5050#if ORXONOX_COMPILER == ORXONOX_COMPILER_GNUC
    5151#  pragma GCC system_header
    5252#endif
    5353
     54
    5455///////////////////////////////////////////////
    5556// Static detection for conversion functions //
    5657///////////////////////////////////////////////
     58
    5759/* The idea to use the sizeof() operator on return functions to determine function existance
    58    is described in 'Moder C++ design' by Alexandrescu (2001). */
     60   is described in 'Modern C++ design' by Alexandrescu (2001). */
    5961
    6062// disable warnings about possible loss of data
     
    6466#endif
    6567
    66 namespace 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     };
    75 
    76     template <class FromType, class ToType>
    77     class ImplicitConversion
    78     {
    79     private:
    80         ImplicitConversion(); ImplicitConversion(const ImplicitConversion&); ~ImplicitConversion();
    81         // Gets chosen only if there is an implicit conversion from FromType to ToType.
    82         static int test(ToType);
    83         // Accepts any argument. Why do we not use a template? The reason is that with templates,
    84         // the function above is only taken iff it is an exact type match. But since we want to
    85         // check for implicit conversion, we have to use the ellipsis.
    86         static VeryBigStruct   test(...);
    87         static FromType object; // helper object to handle private c'tor and d'tor
    88     public:
    89         // test(object) only has 'VerySmallStruct' return type iff the compiler doesn't choose test(...)
    90         enum { exists = !(sizeof(test(object)) == sizeof(VeryBigStruct)) };
    91     };
    92 }
     68template <class FromType, class ToType>
     69class ImplicitConversion
     70{
     71private:
     72    ImplicitConversion(); ImplicitConversion(const ImplicitConversion&); ~ImplicitConversion();
     73    // Gets chosen only if there is an implicit conversion from FromType to ToType.
     74    static char test(ToType);
     75    // Accepts any argument. Why do we not use a template? The reason is that with templates,
     76    // the function above is only taken iff it is an exact type match. But since we want to
     77    // check for implicit conversion, we have to use the ellipsis.
     78    static long long test(...);
     79    static FromType object; // helper object to handle private c'tor and d'tor
     80public:
     81    // test(object) only has 'long long' return type iff the compiler doesn't choose test(...)
     82    enum { exists = (sizeof(test(object)) == sizeof(char)) };
     83};
    9384
    9485#if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC
     
    10293/*
    10394    There is a distinct priority when choosing the right conversion function:
    104     Overwrites:
    105     1. (Partial) template specialisation of ConverterExplicit::convert
    106     2. Global functions convertValue(ToType* output, const FromType input)
     95    Overwrite:
     96    1. (Partial) template specialisation of ConverterExplicit::convert()
    10797    Fallbacks:
    108     3. Any possible implicit conversion. This includes FooBar --> int if FooBar defines operator float().
    109     4. Global or member operators for stringstream when converting from or to std::string (or FROM const char*)
    110     5. Function that simply displays "Could not convert value" with information obtained from typeid().
    111 
    112     A note: There has to be an exact type match (or according to the rules of template spec.) except for 3.
    113 
    114     There are obviously a lot of ways to specifiy a user defined conversion. What should I use?
    115     When using any non-template function based conversion (implicit conversion, convertValue, << or >>)
    116     then you should consider that this function has to be defined prior to including this file.
    117     If you do not whish do do that, you will have to spcecialsize the ConverterExplicit template.
    118     There is a not so obvious advantage of the other way (non-template): You could declare each conversion function
    119     in the Prereqs file of the corresponding library. Then you can use the conversion everywhere.
    120     This is not possible with template specialisations even when defining them in this file (You would create
    121     a circular dependency when using a class from Core for instance, because complete template specialisations
    122     get compiled anyway (there is no template parameter)).
     98    2. Any possible implicit conversion. This includes 'FooBar' --> 'int' if FooBar defines operator float().
     99    3. Global or member operators for stringstream when converting from or to std::string (or FROM const char*)
     100    4. (Partial) template specialisation of ConverterFallback::convert()
     101    5. Function that simply displays "Could not convert value" with type information obtained from typeid().
     102
     103    Notes:
     104    There has to be an exact type match when using template specialisations.
     105    Template specialisations can be defined after including this file. Any implicit cast function or iostream
     106    operator has to be declared BEFORE this file gets parsed.
     107
     108    Defining your own functions:
     109    There are obviously 4 ways to specifiy a user defined conversion. What should I use?
     110
     111    Usually, ConverterFallback fits quite well. You won't have to deal with the conversion from
     112    'MyClass' to 'MyClass' by using another explicit template specialisation to avoid ambiguities.
     113
     114    However if you want to overwrite an implicit conversion or an iostream operator, you really need to
     115    make use of ConverterExplicit.
    123116*/
    124117
    125118namespace
    126119{
    127     // little template that maps to ints to entire types (Alexandrescu 2001)
     120    //! Little template that maps integers to entire types (Alexandrescu 2001)
    128121    template <int I>
    129122    struct Int2Type { };
     
    141134    static bool convert(ToType* output, const FromType& input)
    142135    {
    143         COUT(2) << "Could not convert value of type " << typeid(FromType).name()
    144                 << " to type " << typeid(ToType).name() << std::endl;
     136        //COUT(2) << "Could not convert value of type " << typeid(FromType).name()
     137        //        << " to type " << typeid(ToType).name() << std::endl;
    145138        return false;
    146139    }
     
    183176}
    184177
    185 // template that evaluates whether OStringStream is possible for conversions to std::string
     178// template that evaluates whether we can convert to std::string via ostringstream
    186179template <class FromType>
    187180struct ConverterStringStream<std::string, FromType>
     
    190183    {
    191184        using namespace fallbackTemplates;
     185        // this operator call only chooses fallbackTemplates::operator<< if there's no other fitting function
    192186        std::ostringstream oss;
    193187        if (oss << input)
     
    216210}
    217211
    218 // template that evaluates whether IStringStream is possible for conversions from std::string
     212// template that evaluates whether we can convert from std::string via ostringstream
    219213template <class ToType>
    220214struct ConverterStringStream<ToType, std::string>
     
    224218        using namespace fallbackTemplates;
    225219        std::istringstream iss(input);
     220        // this operator call only chooses fallbackTemplates::operator>> if there's no other fitting function
    226221        if (iss >> (*output))
    227222        {
     
    238233///////////////////
    239234
    240 // static cast no possible, try stringstream conversion next
     235// implicit cast not possible, try stringstream conversion next
    241236template <class ToType, class FromType>
    242237inline bool convertImplicitely(ToType* output, const FromType& input, ::Int2Type<false>)
     
    254249
    255250
    256 ///////////////////////
    257 // Explicit Fallback //
    258 ///////////////////////
     251////////////////////////////////
     252// ConverterExplicit Fallback //
     253////////////////////////////////
    259254
    260255// Default template if no specialisation is available
     
    264259    static bool convert(ToType* output, const FromType& input)
    265260    {
    266         // try implict conversion by probing first because we use '...' instead of a template
    267         const bool probe = conversionTests::ImplicitConversion<FromType, ToType>::exists;
     261        // Try implict cast and probe first. If a simple cast is not possible, it will not compile
     262        // We therefore have to out source it into another template function
     263        const bool probe = ImplicitConversion<FromType, ToType>::exists;
    268264        return convertImplicitely(output, input, ::Int2Type<probe>());
    269265    }
     
    278274@brief
    279275    Converts any value to any other as long as there exists a conversion.
    280     Otherwise, the conversion will generate a runtime warning.
     276    Otherwise, the conversion will generate a runtime warning and return false.
    281277    For information about the different conversion methods (user defined too), see the section
    282278    'Actual conversion sequence' in this file above.
    283 @note
    284     This function is only a fallback if there is no appropriate 'convertValue' function.
    285279*/
    286280template <class ToType, class FromType>
     
    299293// Calls convertValue and returns true if the conversion was successful.
    300294// Otherwise the fallback is used.
     295/**
     296@brief
     297    Converts any value to any other as long as there exists a conversion.
     298    Otherwise, the conversion will generate a runtime warning and return false.
     299    For information about the different conversion methods (user defined too), see the section
     300    'Actual conversion sequence' in this file above.
     301    If the conversion doesn't succeed, 'fallback' is written to '*output'.
     302@param fallback
     303    A default value that gets written to '*output' if there is no conversion.
     304*/
     305template<class FromType, class ToType>
     306inline bool convertValue(ToType* output, const FromType& input, const ToType& fallback)
     307{
     308    if (convertValue(output, input))
     309        return true;
     310    else
     311    {
     312        (*output) = fallback;
     313        return false;
     314    }
     315}
     316
     317// for compatibility reason. (capital 'c' in ConvertValue)
    301318template<class FromType, class ToType>
    302319inline bool ConvertValue(ToType* output, const FromType& input, const ToType& fallback)
    303320{
    304     if (convertValue(output, input))
    305         return true;
    306 
    307     (*output) = fallback;
    308     return false;
     321    return convertValue(output, input, fallback);
    309322}
    310323
     
    314327{
    315328    ToType output;
    316     ConvertValue(&output, input);
     329    convertValue(&output, input);
    317330    return output;
    318331}
     
    323336{
    324337    ToType output;
    325     ConvertValue(&output, input, fallback);
     338    convertValue(&output, input, fallback);
    326339    return output;
    327340}
     
    333346{
    334347    ToType output;
    335     ConvertValue(&output, input);
    336     return output;
    337 }
    338 
    339 // Like conversion_cast above, but uses a fallback on failure.
    340 template<class ToType, class FromType>
    341 inline ToType conversion_cast(const FromType& input, const ToType& fallback)
    342 {
    343     ToType output;
    344     ConvertValue(&output, input, fallback);
     348    convertValue(&output, input);
    345349    return output;
    346350}
     
    351355////////////////////////////////
    352356
    353 // delegate conversion from const char* to std::string
     357// Delegate conversion from const char* to std::string
    354358template <class ToType>
    355359struct ConverterExplicit<ToType, const char*>
  • code/branches/core3/src/util/MathConvert.h

    r1779 r2014  
    3737
    3838#include "UtilPrereqs.h"
     39#include "Math.h"
    3940#include "Convert.h"
    40 #include "Math.h"
    4141
    4242
     
    153153// From Radian
    154154template <class ToType>
    155 inline bool fallbackConversion(ToType* output, const orxonox::Radian input)
    156 {
    157     return convertValue<ToType, Ogre::Real>(output, input.valueRadians());
    158 }
     155struct ConverterFallback<ToType, orxonox::Radian>
     156{
     157    static bool convert(ToType* output, const orxonox::Radian& input)
     158    {
     159        return convertValue<ToType, Ogre::Real>(output, input.valueRadians());
     160    }
     161};
    159162
    160163// From Degree
    161164template <class ToType>
    162 inline bool fallbackConversion(ToType* output, const orxonox::Degree input)
    163 {
    164     return convertValue<ToType, Ogre::Real>(output, input.valueDegrees());
    165 }
     165struct ConverterFallback<ToType, orxonox::Degree>
     166{
     167    static bool convert(ToType* output, const orxonox::Degree& input)
     168    {
     169        return convertValue<ToType, Ogre::Real>(output, input.valueDegrees());
     170    }
     171};
    166172
    167173// To Radian
    168174template <class FromType>
    169 inline bool fallbackConversion(orxonox::Radian* output, const FromType input)
    170 {
    171     float temp;
    172     if (convertValue(&temp, input))
    173     {
    174         *output = temp;
    175         return true;
    176     }
    177     else
    178         return false;
    179 }
     175struct ConverterFallback<orxonox::Radian, FromType>
     176{
     177    static bool convert(orxonox::Radian* output, const FromType& input)
     178    {
     179        float temp;
     180        if (convertValue(&temp, input))
     181        {
     182            *output = temp;
     183            return true;
     184        }
     185        else
     186            return false;
     187    }
     188};
    180189
    181190// To Degree
    182191template <class FromType>
    183 inline bool fallbackConversion(orxonox::Degree* output, const FromType input)
    184 {
    185     float temp;
    186     if (convertValue(&temp, input))
    187     {
    188         *output = temp;
    189         return true;
    190     }
    191     else
    192         return false;
    193 }
     192struct ConverterFallback<orxonox::Degree, FromType>
     193{
     194    static bool convert(orxonox::Degree* output, const FromType& input)
     195    {
     196        float temp;
     197        if (convertValue(&temp, input))
     198        {
     199            *output = temp;
     200            return true;
     201        }
     202        else
     203            return false;
     204    }
     205};
    194206
    195207#endif /* _MathConvert_H__ */
  • code/branches/core3/visual_studio/audio_properties.vsprops

    r1572 r2014  
    1313        <Tool
    1414                Name="VCLinkerTool"
    15                 AdditionalDependencies="libvorbisfile-1.2.0$(CSS).lib alut$(CS).lib OpenAL32$(CS).lib"
     15                AdditionalDependencies="vorbisfile$(CSS).lib alut$(CS).lib OpenAL32$(CS).lib"
    1616        />
    1717</VisualStudioPropertySheet>
  • code/branches/core3/visual_studio/base_properties.vsprops

    r1658 r2014  
    88        <Tool
    99                Name="VCCLCompilerTool"
    10                 AdditionalIncludeDirectories="&quot;$(RootDir)&quot;;&quot;$(RootDir)src&quot;;&quot;$(RootDir)src\orxonox&quot;;&quot;$(RootDir)src\tolua&quot;;&quot;$(RootDir)src\ois&quot;;&quot;$(LibDir)ogre-1.4.9\OgreMain\include&quot;;&quot;$(LibDir)boost-1.35.0&quot;;&quot;$(LibDir)enet-1.2\include&quot;;&quot;$(LibDir)libogg-1.1.3\include&quot;;&quot;$(LibDir)libvorbis-1.2.0\include&quot;;&quot;$(LibDir)lua-5.1.3\src&quot;;&quot;$(LibDir)openal-1.1\include&quot;;&quot;$(LibDir)openal-1.1\alut\include&quot;;&quot;$(LibDir)tcl-8.5.\generic&quot;;&quot;$(LibDir)zlib-1.2.3&quot;"
     10                AdditionalIncludeDirectories="&quot;$(RootDir)&quot;;&quot;$(RootDir)src&quot;;&quot;$(RootDir)src\orxonox&quot;;&quot;$(RootDir)src\ceguilua-0.6.1&quot;;&quot;$(LibDir)ogre-1.4.9\include&quot;;&quot;$(LibDir)boost-1.35.0\include&quot;;&quot;$(LibDir)cegui-0.6.1\include&quot;;&quot;$(LibDir)enet-1.2\include&quot;;&quot;$(LibDir)libogg-1.1.3\include&quot;;&quot;$(LibDir)libvorbis-1.2.0\include&quot;;&quot;$(LibDir)openal-1.1\include&quot;;&quot;$(LibDir)freealut-1.1.0\include&quot;;&quot;$(LibDir)tcl-8.5.2\include&quot;;&quot;$(LibDir)zlib-1.2.3\include&quot;;&quot;$(LibDir)lua-5.1.3\include&quot;"
    1111                PreprocessorDefinitions="WIN32;__WIN32__;_WIN32;BOOST_ALL_DYN_LINK;OIS_DYNAMIC_LIB; ZLIB_WINAPI"
    1212                WarningLevel="3"
     
    2222                Name="VCLinkerTool"
    2323                OutputFile="$(OutDir)$(ProjectName)$(CS).dll"
    24                 AdditionalLibraryDirectories="$(DependencyDir)lib"
     24                AdditionalLibraryDirectories="&quot;$(LibDir)boost-1.35.0\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)cegui-0.6.1\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)enet-1.2\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)freealut-1.1.0\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)libogg-1.1.3\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)libvorbis-1.2.0\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)ogre-1.4.9\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)openal-1.1\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)tcl-8.5.2\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)zlib-1.2.3\lib\$(ConfigurationName)&quot;;&quot;$(LibDir)lua-5.1.3\lib\$(ConfigurationName)&quot;"
    2525                SubSystem="1"
    2626                ImportLibrary="$(LibOutDir)$(TargetName).lib"
     
    4545        <UserMacro
    4646                Name="LibDir"
    47                 Value="$(RootDir)..\lib_src\"
     47                Value="$(RootDir)..\lib_dist\vc8\dependencies\$(SolutionName)\"
    4848        />
    4949        <UserMacro
  • code/branches/core3/visual_studio/core_properties.vsprops

    r1729 r2014  
    1313        <Tool
    1414                Name="VCLinkerTool"
    15                 AdditionalDependencies="OgreMain$(CSS).lib lua-5.1.3$(CS).lib tcl85t.lib"
     15                AdditionalDependencies="OgreMain$(CSS).lib lua$(CS).lib tcl85t.lib"
    1616        />
    1717</VisualStudioPropertySheet>
  • code/branches/core3/visual_studio/orxonox_properties.vsprops

    r1729 r2014  
    1515        <Tool
    1616                Name="VCLinkerTool"
    17                 AdditionalDependencies="OgreMain$(CSS).lib tcl85t.lib enet-1.2$(CS).lib zlib-1.2.3$(CS).lib"
     17                AdditionalDependencies="OgreMain$(CSS).lib tcl85t.lib enet$(CS).lib zlib$(CS).lib"
    1818                OutputFile="$(OutDir)$(ProjectName)$(CS).exe"
    1919                IgnoreDefaultLibraryNames="LIBCMT;LIBCMTD"
Note: See TracChangeset for help on using the changeset viewer.