| 1 | = HowTo: ConfigValue = |
| 2 | [[TracNav(TracNav/TOC_Development)]] |
| 3 | |
| 4 | == Description == |
| 5 | Config-values are member variables of a class that can be configured in the [wiki:howto/ConfigFile config-file]. The configured value will be assigned to every instance of this class. |
| 6 | |
| 7 | To use config-values in your class, you have to follow those steps: |
| 8 | 1. Inherit from [wiki:OrxonoxClass] or a derivative |
| 9 | 1. Then you have to add {{{void setConfigValues()}}} to the class |
| 10 | 1. Include [wiki:ConfigValueIncludes core/ConfigValueIncludes.h] in the source file |
| 11 | 1. Add '''SetConfigValue('''''variable, defaultvalue''''')''' macro-calls into {{{setConfigValues()}}} |
| 12 | 1. Call {{{setConfigValues()}}} from the constructor of your class |
| 13 | |
| 14 | == Example == |
| 15 | |
| 16 | *.h file: |
| 17 | {{{ |
| 18 | class MyClass : public SomeObject (1) |
| 19 | { |
| 20 | public: |
| 21 | MyClass(); |
| 22 | void setConfigValues(); (2) |
| 23 | |
| 24 | ... |
| 25 | |
| 26 | private: |
| 27 | float value_; |
| 28 | }; |
| 29 | }}} |
| 30 | |
| 31 | *.cc file: |
| 32 | {{{ |
| 33 | #include "core/CoreIncludes.h" |
| 34 | #include "core/ConfigValueIncludes.h" (3) |
| 35 | |
| 36 | // Constructor: |
| 37 | MyClass::MyClass() |
| 38 | { |
| 39 | RegisterObject(MyClass); |
| 40 | |
| 41 | this->setConfigValues(); (5) |
| 42 | } |
| 43 | |
| 44 | void MyClass::setConfigValues() |
| 45 | { |
| 46 | SetConfigValue(value_, 123.4); (4) |
| 47 | } |
| 48 | }}} |
| 49 | |
| 50 | == Inheritance == |
| 51 | There are three possible ways to deal with inheritance and config-values: |
| 52 | === Use a config-value in derived classes === |
| 53 | If a config-value is defined for a base-class, all derived classes will be configured by this value. |
| 54 | |
| 55 | '''Example''': |
| 56 | |
| 57 | {{{BaseClass}}} implements a config value: |
| 58 | {{{ |
| 59 | class BaseClass : public OrxonoxClass |
| 60 | { |
| 61 | public: |
| 62 | ... |
| 63 | void setConfigValues(); |
| 64 | |
| 65 | protected: |
| 66 | float value_; |
| 67 | }; |
| 68 | |
| 69 | void BaseClass::setConfigValues() |
| 70 | { |
| 71 | SetConfigValue(value_, 123.4); |
| 72 | } |
| 73 | }}} |
| 74 | |
| 75 | {{{DerivedClass}}} inherits from {{{BaseClass}}} without implementing {{{setConfigValues}}} again: |
| 76 | {{{ |
| 77 | class DerivedClass : public BaseClass |
| 78 | { |
| 79 | }; |
| 80 | }}} |
| 81 | |
| 82 | In the config-file this will look like this: |
| 83 | {{{ |
| 84 | [BaseClass] |
| 85 | value_=123.4 |
| 86 | }}} |
| 87 | Then {{{BaseClass::value_}}} and {{{DerivedClass::value_}}} are both set by the config-value of {{{BaseClass}}} (and therefore 123.4). |
| 88 | |
| 89 | === Implementing a config-value only in a derived class === |
| 90 | It's possible to define a config-value for a variable which was inherited from a base class. |
| 91 | |
| 92 | '''Example''': |
| 93 | |
| 94 | {{{BaseClass}}} declares a variable but no config-value: |
| 95 | {{{ |
| 96 | class BaseClass : public OrxonoxClass |
| 97 | { |
| 98 | ... |
| 99 | |
| 100 | protected: |
| 101 | float baseValue_; |
| 102 | }; |
| 103 | }}} |
| 104 | |
| 105 | {{{DerivedClass}}} derives from {{{BaseClass}}} and implements the config-value: |
| 106 | {{{ |
| 107 | class DerivedClass : public BaseClass |
| 108 | { |
| 109 | public: |
| 110 | ... |
| 111 | void setConfigValues(); |
| 112 | }; |
| 113 | |
| 114 | void DerivedClass::setConfigValues() |
| 115 | { |
| 116 | SetConfigValue(baseValue_, 10.1); |
| 117 | } |
| 118 | }}} |
| 119 | |
| 120 | In the config-file this will look like this: |
| 121 | {{{ |
| 122 | [DerivedClass] |
| 123 | value_=10.1 |
| 124 | }}} |
| 125 | |
| 126 | Then {{{DerivedClass::baseValue_}}} is set by the config-value (and therefore 10.1) but {{{BaseClass::baseValue_}}} isn't configurable. |
| 127 | |
| 128 | === Overwriting config-values in a derived class === |
| 129 | If both, a base class and a derived class, implement the same value, instances of the base class use the configured value from the config-value-section of the base class while instances of the derived class (and further derived classes) use the overwritten value. |
| 130 | |
| 131 | '''Example''': |
| 132 | |
| 133 | {{{BaseClass}}} implements a config value: |
| 134 | {{{ |
| 135 | class BaseClass : public OrxonoxClass |
| 136 | { |
| 137 | public: |
| 138 | ... |
| 139 | void setConfigValues(); |
| 140 | |
| 141 | protected: |
| 142 | float value_; |
| 143 | }; |
| 144 | |
| 145 | void BaseClass::setConfigValues() |
| 146 | { |
| 147 | SetConfigValue(value_, 123.4); |
| 148 | } |
| 149 | }}} |
| 150 | |
| 151 | {{{DerivedClass}}} derives from {{{BaseClass}}} and implements the config-value again: |
| 152 | {{{ |
| 153 | class DerivedClass : public BaseClass |
| 154 | { |
| 155 | public: |
| 156 | ... |
| 157 | void setConfigValues(); |
| 158 | }; |
| 159 | |
| 160 | void DerivedClass::setConfigValues() |
| 161 | { |
| 162 | SetConfigValue(value_, 10.1); |
| 163 | } |
| 164 | }}} |
| 165 | |
| 166 | In the config-file this will look like this: |
| 167 | {{{ |
| 168 | [BaseClass] |
| 169 | value_=123.4 |
| 170 | |
| 171 | [DerivedClass] |
| 172 | value_=10.1 |
| 173 | }}} |
| 174 | |
| 175 | Then {{{BaseClass::value_}}} is set by the config-value of {{{BaseClass}}} (and therefore 123.4) but {{{DerivedClass::value_}}} uses the config-value of {{{DerivedClass}}} (and therefore 10.1). |