Changes between Version 2 and Version 3 of code/doc/MultiType
- Timestamp:
- Sep 22, 2008, 8:16:53 PM (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
code/doc/MultiType
v2 v3 5 5 == Introduction == 6 6 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 theMultiType return 105.3 as a float.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. 8 8 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 theMultiType and C++ does an implicit conversion to the requested type.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. 10 10 11 == Example ==11 == Introducing example == 12 12 This is what you got: 13 13 {{{ … … 36 36 37 37 === 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);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); 42 42 43 43 Important: 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. 44 44 45 45 === Retrieve a value === 46 There are four three to retrieve a value from a MultiType:47 * Implicitconversion: float value = myMT; (or float value = (float)myMT;)46 There are four three to retrieve a value from a !MultiType: 47 * '''Implicit''' conversion: float value = myMT; (or float value = (float)myMT;) 48 48 * 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(). 50 50 51 If the current value of the MultiType has another type, it will be converted to the implicitly or explicitly requested type.51 If the current value of the !MultiType has another type, it will be converted to the implicitly or explicitly requested type. 52 52 53 53 === 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.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. 55 55 56 56 Example: 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 aMultiType 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. 58 58 59 59 But of course you can change this type: 60 60 61 61 === Change type === 62 There are three possible ways to change the internal type of a MultiType:62 There are three possible ways to change the internal type of a !MultiType: 63 63 * '''setType<'''''type'''''>()''': This changes the type and resets the value. 64 64 * '''convert<'''''type'''''>()''': This changes the type and converts the current value to the new type. … … 69 69 Of course SomeType and OtherType from the example above can't be anything, otherwise the class would be called AnyType and probably being useless. 70 70 71 These are the types supported by MultiType:71 These are the types supported by !MultiType: 72 72 73 73 * All primitives: … … 96 96 * Radian 97 97 * Degree 98 99 == Examples == 100 {{{ 101 MultiType value = 10; // internal type: int, value: 10 102 value = 12.3; // internal type: int, value: 12 103 // Note: The internal type is still int 104 105 value.setValue("100"); // internal type: int, value: 100 106 // Note: The string was converted to an int 107 108 value.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 112 value.convert<int>(); // internal type: int, value: 12 113 // Note: We converted the value back to int, so it's 12 again 114 }}} 115 116 {{{ 117 MultiType value1 = 10; // internal type: int, value: 10 118 MultiType value2 = 12.3; // internal type: float, value: 12.3 119 120 value2.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 }}}