| 1 | /*! |
|---|
| 2 | * @file executor_substring.h |
|---|
| 3 | * Definition of a Generic Executor |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | /* |
|---|
| 7 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 8 | |
|---|
| 9 | Copyright (C) 2004 orx |
|---|
| 10 | |
|---|
| 11 | This program is free software; you can redistribute it and/or modify |
|---|
| 12 | it under the terms of the GNU General Public License as published by |
|---|
| 13 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 14 | any later version. |
|---|
| 15 | |
|---|
| 16 | ### File Specific: |
|---|
| 17 | main-programmer: Benjamin Grauer |
|---|
| 18 | co-programmer: ... |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #ifndef __EXECUTOR_SUBSTRING_H_ |
|---|
| 24 | #define __EXECUTOR_SUBSTRING_H_ |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #include "executor_generic.h" |
|---|
| 28 | #include "substring.h" |
|---|
| 29 | #ifdef FUNCTOR_CALL_TYPE |
|---|
| 30 | #undef FUNCTOR_CALL_TYPE |
|---|
| 31 | #endif |
|---|
| 32 | //! Use SubString as the FUNCTION_CALL_TYPE so that we can easily extend the Engine. |
|---|
| 33 | #define FUNCTOR_CALL_TYPE const SubString |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * @brief converts an input string into a value, if no value was converted, uses defaultValue. |
|---|
| 37 | * @param input the input as a String. |
|---|
| 38 | * @param defaultValue the Default Value set, if the input could not be converted into type. |
|---|
| 39 | * @returns either the converted value or defaultValue. |
|---|
| 40 | */ |
|---|
| 41 | template<typename type> type fromString(const std::string& input, type defaultValue) { return defaultValue; }; |
|---|
| 42 | template<> bool fromString<bool>(const std::string& input, bool defaultValue); |
|---|
| 43 | template<> int fromString<int>(const std::string& input, int defaultValue); |
|---|
| 44 | template<> unsigned int fromString<unsigned int>(const std::string& input, unsigned int defaultValue); |
|---|
| 45 | template<> float fromString<float>(const std::string& input, float defaultValue); |
|---|
| 46 | template<> char fromString<char>(const std::string& input, char defaultValue); |
|---|
| 47 | template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue); |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * @brief converts to any type from a MultiType |
|---|
| 51 | * @param multi the MultiType to convert. |
|---|
| 52 | * @returns the converted Value. |
|---|
| 53 | */ |
|---|
| 54 | template<typename type> type fromMulti(const MultiType& multi) { /* return defaultValue; */ }; |
|---|
| 55 | template<> bool fromMulti<bool>(const MultiType& multi); |
|---|
| 56 | template<> int fromMulti<int>(const MultiType& multi); |
|---|
| 57 | template<> unsigned int fromMulti<unsigned int>(const MultiType& multi); |
|---|
| 58 | template<> float fromMulti<float>(const MultiType& multi); |
|---|
| 59 | template<> char fromMulti<char>(const MultiType& multi); |
|---|
| 60 | template<> const std::string& fromMulti<const std::string&>(const MultiType& multi); |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * @brief retrieves a default value from an array of default values, at possition i. |
|---|
| 64 | * @param defaultValues the Array of default values. |
|---|
| 65 | * @param i the index. |
|---|
| 66 | * @returns the Default Value at Position i |
|---|
| 67 | */ |
|---|
| 68 | template<typename type> type getDefault(const MultiType* const defaultValues, unsigned int i) { return (type)0; }; |
|---|
| 69 | template<> bool getDefault<bool>(const MultiType* const defaultValues, unsigned int i); |
|---|
| 70 | template<> int getDefault<int>(const MultiType* const defaultValues, unsigned int i); |
|---|
| 71 | template<> unsigned int getDefault<unsigned int>(const MultiType* const defaultValues, unsigned int i); |
|---|
| 72 | template<> float getDefault<float>(const MultiType* const defaultValues, unsigned int i); |
|---|
| 73 | template<> char getDefault<char>(const MultiType* const defaultValues, unsigned int i); |
|---|
| 74 | template<> const std::string& getDefault<const std::string&>(const MultiType* const defaultValues, unsigned int i); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * @brief to remove writing errors, this function is Used. |
|---|
| 79 | * @param sub The SubString to use |
|---|
| 80 | * @param default The default Values. |
|---|
| 81 | */ |
|---|
| 82 | template<> class ExecutorEvaluater <const SubString> |
|---|
| 83 | { |
|---|
| 84 | public: |
|---|
| 85 | /** @brief Executes the Evaluater |
|---|
| 86 | * @param CallValue the Value that should be converted |
|---|
| 87 | * @param defaults the default Values. |
|---|
| 88 | */ |
|---|
| 89 | template <typename ToType, int index> |
|---|
| 90 | static ToType getValue(const SubString& CallValue, const MultiType* const defaults) |
|---|
| 91 | { |
|---|
| 92 | return (CallValue.size() > index) ? |
|---|
| 93 | fromString<ToType>(CallValue[index], getDefault<ToType>(defaults, index)) : |
|---|
| 94 | fromMulti<ToType>(defaults[index]); |
|---|
| 95 | } |
|---|
| 96 | /** @returns the default Value of a SubString, namely NullSubString or SubString() */ |
|---|
| 97 | static const SubString& defaultValue() { return SubString::NullSubString; }; |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | #endif /* __EXECUTOR_SUBSTRING_H_ */ |
|---|