| | 1 | = HowTo: Identifiers = |
| | 2 | [[TracNav(TracNav/TOC_Development)]] |
| | 3 | [[TOC]] |
| | 4 | |
| | 5 | == Description == |
| | 6 | '''Important''': Identifiers are only available to classes in the class hierarchy. Read [wiki:howto/ClassHierarchy this] for more information. |
| | 7 | |
| | 8 | The Identifier is a construct used to identify a class at runtime. It is also a container for several class-related stuff, but this is not topic of this page. Read [wiki:Identifier this] for more detailed information. |
| | 9 | |
| | 10 | This page explains how to work with Identifiers. |
| | 11 | |
| | 12 | == Get the Identifier of a class == |
| | 13 | To get the Identifier belonging to a class, just use '''Class('''''ClassName''''')''': |
| | 14 | {{{ |
| | 15 | Identifier* identifier = Class(MyClass); |
| | 16 | }}} |
| | 17 | |
| | 18 | It's also possible to get the Identifier of a class by using a string: |
| | 19 | {{{ |
| | 20 | Identifier* identifier = ClassByString("MyClass"); |
| | 21 | }}} |
| | 22 | |
| | 23 | == Get the Identifier of an object == |
| | 24 | If you want to know to which class an object belongs, just use '''getIdentifer()''': |
| | 25 | |
| | 26 | {{{ |
| | 27 | BaseObject* object = getSomeObject(); |
| | 28 | |
| | 29 | Identifer* identifier = object->getIdentifier(); |
| | 30 | }}} |
| | 31 | |
| | 32 | Now how do you get information about this class? For example the name? Just call '''getName()''' on the Identifier: |
| | 33 | {{{ |
| | 34 | std::cout << "The object is a " << identifier->getName() << std::endl; |
| | 35 | }}} |
| | 36 | |
| | 37 | The output of this code might be: "The object is a WorldEntity". |
| | 38 | |
| | 39 | == Compare classes == |
| | 40 | If you want to know if an object is an instance of a class inheriting from a given class (or it's the class itself), use '''isA('''''identifier''''')''': |
| | 41 | {{{ |
| | 42 | BaseObject* object = getSomeObject(); |
| | 43 | |
| | 44 | bool isAMyClass = object->getIdentifier()->isA(Class(MyClass)); |
| | 45 | |
| | 46 | if (isAMyClass) |
| | 47 | std::cout << "Object is a MyClass!" << std::endl; |
| | 48 | }}} |
| | 49 | |
| | 50 | You can do this even shorter: |
| | 51 | {{{ |
| | 52 | object->isA(Class(MyClass)); |
| | 53 | }}} |
| | 54 | |
| | 55 | Apart from isA(...), there are other comparision functions: |
| | 56 | * isExactlyA |
| | 57 | * isChildOf |
| | 58 | * isDirectChildOf |
| | 59 | * isParentOf |
| | 60 | * isDirectParentOf |
| | 61 | |
| | 62 | Read [wiki:Identifier#Comparison this] for more information. |
| | 63 | |