Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Version 2 (modified by landauf, 16 years ago) (diff)

MultiType

TracNav(TracNav/TOC_Development)?

Introduction

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.

But what is this good for? Well, just think of an XML level. You write all values as strings, the 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.

Example

This is what you got:

SomeType value = XXXX; // a value

void function(OtherType value); // a function

This is what you want:

function(value); // call the function with the value

This is what you know about SomeType and OtherType:

// nothing

This is how you're doing it:

function(MultiType(value));

Isn't it cool? Sure it is!

Usage

Assign a value

There are three ways to assign a value to a MultiType:

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 section below for detailed information.

Retrieve a value

There are four three to retrieve a value from a MultiType:

  • Implicit conversion: float value = myMT; (or float value = (float)myMT;)
  • Get by pointer: getValue(pointer): float value = 0; myMT.getValue(&value);
  • Explicit function call: float value = myMT.getFloat().

If the current value of the MultiType has another type, it will be converted to the implicitly or explicitly requested type.

Internal type

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.

Example:

  • 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.

But of course you can change this type:

Change type

There are three possible ways to change the internal type of a MultiType:

  • setType<type>(): This changes the type and resets the value.
  • convert<type>(): This changes the type and converts the current value to the new type.
  • setValue<type>(value): This changes the type and assigns a new value. The new value may be of another value than type, it will be converted.

Types

Of course SomeType and OtherType from the example above can't be anything, otherwise the class would be called AnyType and probably being useless.

These are the types supported by MultiType:

  • All primitives:
    • char
    • unsigned char
    • short
    • unsigned short
    • int
    • unsigned int
    • long
    • unsigned long
    • long long
    • unsigned long long
    • float
    • double
    • long double
    • bool
  • All pointers (but be careful with inheritance, in fact it's just handled as a void pointer)
  • std::string
  • Math objects:
    • Vector2
    • Vector3
    • Vector4
    • Quaternion
    • ColourValue
    • Radian
    • Degree