Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Version 2 and Version 3 of code/doc/MultiType


Ignore:
Timestamp:
Sep 22, 2008, 8:16:53 PM (16 years ago)
Author:
landauf
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/doc/MultiType

    v2 v3  
    55== Introduction ==
    66
    7 The MultiType is a fancy class, designed to accept values of almost every type and to return exactly this value in almost every other type - depending on your needs. This allows you to assign a string, for example "105.3", to a MultiType and to let the MultiType return 105.3 as a float.
     7The !MultiType is a fancy class, designed to accept values of almost every type and to return exactly this value in almost every other type - depending on your needs. This allows you to assign a string, for example "105.3", to a !MultiType and to let the !MultiType return 105.3 as a float.
    88
    9 But what is this good for? Well, just think of an XML level. You write all values as strings, the [wiki:Loader] loads the level, creates new objects and passes the values to the objects. But how should the Loader know, which type a value has? It just loads a string! Thats why the Loader puts the string into a MultiType and passes it to the new object. The object receives the MultiType and C++ does an implicit conversion to the requested type.
     9But what is this good for? Well, just think of an XML level. You write all values as strings, the [wiki:Loader] loads the level, creates new objects and passes the values to the objects. But how should the Loader know, which type a value has? It just loads a string! Thats why the Loader puts the string into a !MultiType and passes it to the new object. The object receives the !MultiType and C++ does an implicit conversion to the requested type.
    1010
    11 == Example ==
     11== Introducing example ==
    1212This is what you got:
    1313{{{
     
    3636
    3737=== Assign a value ===
    38 There are three ways to assign a value to a MultiType:
    39  * Constructor: MultiType myMT = 10;
    40  * Assignment '''operator=''': MultiType myMT myMT = 10;
    41  * '''setValue('''''value''''')''': MultiType myMT myMT.setValue(10);
     38There are three ways to assign a value to a !MultiType:
     39 * '''Constructor''': !MultiType myMT = 10;
     40 * Assignment '''operator=''': !MultiType myMT myMT = 10;
     41 * '''setValue('''''value''''')''': !MultiType myMT myMT.setValue(10);
    4242
    4343Important: If you assign a value of a specific type and then assign a new value of another type, the new value will be converted to the first type. Read the [wiki:MultiType#internaltype section below] for detailed information.
    4444
    4545=== Retrieve a value ===
    46 There are four three to retrieve a value from a MultiType:
    47  * Implicit conversion: float value = myMT; (or float value = (float)myMT;)
     46There are four three to retrieve a value from a !MultiType:
     47 * '''Implicit''' conversion: float value = myMT; (or float value = (float)myMT;)
    4848 * Get by pointer: '''getValue('''''pointer''''')''': float value = 0; myMT.getValue(&value);
    49  * Explicit function call: float value = myMT.getFloat().
     49 * Explicit function call: '''getXXXX()''' float value = myMT.getFloat().
    5050
    51 If the current value of the MultiType has another type, it will be converted to the implicitly or explicitly requested type.
     51If the current value of the !MultiType has another type, it will be converted to the implicitly or explicitly requested type.
    5252
    5353=== Internal type ===
    54 It's very important to know the MultiType has some sort of an internal type. It's the type of the first assigned value. If you assign another value of another type, it will be converted to the first type. If you don't know about this, you may get many unnecessary conversions. But if you know about it, you can save much CPU time.
     54It's very important to know the !MultiType has some sort of an internal type. It's the type of the first assigned value. If you assign another value of another type, it will be converted to the first type. If you don't know about this, you may get many unnecessary conversions. But if you know about it, you can save much CPU time.
    5555
    5656Example:
    57  * Think about a function, receiving a float, and a MultiType, containing a string. If you want to call the function 100'000 times, you will convert the string every f***ing time into a float. So you better create a MultiType with internal type ''float'' and assign the string afterward. This way the string gets already converted when assigned and you save 99'999 conversions.
     57 * Think about a function, receiving a float, and a !MultiType, containing a string. If you want to call the function 100'000 times, you will convert the string every f***ing time into a float. So you better create a !MultiType with internal type ''float'' and assign the string afterward. This way the string gets already converted when assigned and you save 99'999 conversions.
    5858
    5959But of course you can change this type:
    6060
    6161=== Change type ===
    62 There are three possible ways to change the internal type of a MultiType:
     62There are three possible ways to change the internal type of a !MultiType:
    6363 * '''setType<'''''type'''''>()''': This changes the type and resets the value.
    6464 * '''convert<'''''type'''''>()''': This changes the type and converts the current value to the new type.
     
    6969Of course SomeType and OtherType from the example above can't be anything, otherwise the class would be called AnyType and probably being useless.
    7070
    71 These are the types supported by MultiType:
     71These are the types supported by !MultiType:
    7272
    7373 * All primitives:
     
    9696  * Radian
    9797  * Degree
     98
     99== Examples ==
     100{{{
     101MultiType value = 10;        // internal type: int,   value: 10
     102value = 12.3;                // internal type: int,   value: 12
     103                             // Note: The internal type is still int
     104
     105value.setValue("100");       // internal type: int,   value: 100
     106                             // Note: The string was converted to an int
     107
     108value.setValue<float>(12.3); // internal type: float, value: 12.3
     109                             // Note: Now we changed the type to float and 12.3 can
     110                             //       be assigned without loss of precision
     111
     112value.convert<int>();        // internal type: int,   value: 12
     113                             // Note: We converted the value back to int, so it's 12 again
     114}}}
     115
     116{{{
     117MultiType value1 = 10;   // internal type: int,   value: 10
     118MultiType value2 = 12.3; // internal type: float, value: 12.3
     119
     120value2.convert(value1);  // internal type: int,   value: 12
     121
     122// Note: The internal type of value2 was converted to the internal type of value1 (int)
     123//       Therefore the value of value2 is now 12 instead of 12.3
     124
     125}}}