Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Type Support

Classes

class  elm::type_info< T >
 

Detailed Description

ELM provides several facilities to handle type and parametric types.

Basic Types
#include <elm/types.h>
using namespace elm;

This include provides alternative non-ambiguous names for integer types (Integer Operations) and associated basic operations.

Type Information
#include <elm/type_info.h>
using namespace elm;

Class elm::type_info<T> allows to get from the type passed as the parametric argument. This may be useful for templates using static evaluation. The common fields includes:

Notice that ELM provides type information for basic (integer, float, string) and composed types (pointer, references) but need the developer help for more complex types like classes, struct or unions. Either one can add the specialized type_info structure for the defined type:

class MyClass { ... };
template <> struct type_info<T> { static cstring name(void) { return "MyClass"; } }

Or just add a static member named "__type_name" in the class declaration:

class MyClass {
public:
static cstring __type_name(void) { return "MyClass"; }
...
};
Type Selection Helper

A big issue in C++ is the selection of type for parameters and functions results. A common practice is to pass a simple type as is as parameter and to use const references for bigger types. This policy is easy to apply when the type is known but what to do when the parameter method is in a template class. Here, a conservative approach advise to use const reference in any case in the hope that the compiler will optimize the code and use only straight type for scalar types.

ELM proposes an alternative to this approach that works with template. Using the type_info<T> class (that is aware of the actual type of the generic parameter), one can obtain the type declaration that fits the best its use:

Notice type_info<T>::embed_t that allows storing references: in fact, when T is a reference, it is stored automatically as a pointer. type_info<T>::put() and type_info<T>::get() can then be used to store the generic value or to get it.

Enumeration Information

Users may also benefit from whole facilities of input/output, serialization and like for the enumerated type. First, the type_info must be specialized (in the header containing the enumeration declaration):

typedef enum { V1, V2, V3, ... } my_enum;
...
namespace elm {
template <> struct type_info<my_enum>: enum_info<my_enum> { };
}

And then to provided the missing (usually in the source file providing implementation).

namespace elm {
template <> cstring enum_info<my_enum>::name(void) { return "my_enum"; }
template <> enum_info<my_enum>::value_t enum_info<my_enum>::values[] = {
value("V1", V1),
value("V2", V2),
value("V3", V3),
last()
};
}

As the primitives provided by ELM, the values may examined with such a loop:

for(type_info<my_enum>::iterator i = type_info<my_enum>::begin(); i != type_info<my_enum>::end(); i++)
cout << "name = " << i.name << ", value = " << i.value << io::endl;
Array Copying

The include file <otawa/util/array.h> provides functions to handling arrays:

According to the type of the type of the items, these functions may invokes specific system functions to make faster this operation. Usually, only types without pointers can only be processed in this way (scalar data) but you can informs that your own class can be shallowly copied by specializing the type_info class:

class MyClass {
...
};
template <>
class type_info<MyClass>: public class_t<T> { enum { is_deep = 1 }; };