optionstructure.h 9.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*!
 * \file OptionStructure.h
 * \brief Classes for different Options in rtsn
 * \author S. Schotthoefer
 *
 * Disclaimer: This class structure was copied and modifed with open source permission from SU2 v7.0.3 https://su2code.github.io/
 */

#ifndef OPTION_STRUCTURE_H
#define OPTION_STRUCTURE_H

#include <map>
#include <sstream>
14
15
#include <string>
#include <vector>
16
17

// Base Class for all kinds of options
18
19
20
21
class OptionBase
{

  private:
22
    std::vector<std::string> _value; /*! @brief: String name of the option */
23
24

  public:
25
    OptionBase() {}
26
27
    virtual ~OptionBase() = 0;

28
    virtual std::string SetValue( std::vector<std::string> value ); /*! @brief: Set string name of the option */
29

30
    std::vector<std::string> GetValue(); /*! @brief: Get string name of the option */
31

32
33
34
35
36
    virtual void SetDefault() = 0; /*! @brief:  Set default name for the option */
    /*! @brief: Check if an option is defined multiple times in a config file, if yes, return a string stating this. */
    std::string OptionCheckMultipleValues( std::vector<std::string>& option_value, std::string type_id, std::string option_name );
    /*! @brief: If a bad value for an option is detected, this function creates the corresponding output string. */
    std::string BadValue( std::vector<std::string>& option_value, std::string type_id, std::string option_name );
37
38
};

39
40
class OptionDouble : public OptionBase
{
41
42
43
    double& _field;    /*! @brief: Reference to the double field value */
    double _def;       /*! @brief: Default value */
    std::string _name; /*! @brief: String identifier for the option */
44
45

  public:
steffen.schotthoefer's avatar
steffen.schotthoefer committed
46
    OptionDouble( std::string option_field_name, double& option_field, double default_value );
47

48
    ~OptionDouble() override {}
steffen.schotthoefer's avatar
steffen.schotthoefer committed
49
50
51
52

    std::string SetValue( std::vector<std::string> option_value ) override;

    void SetDefault() override;
53
54
};

55
56
class OptionString : public OptionBase
{
Steffen Schotthöfer's avatar
Steffen Schotthöfer committed
57
    std::string& _field; /*! @brief: Reference to the string field value */
58
59
    std::string _def;    /*! @brief: Default value */
    std::string _name;   /*! @brief: string identifier for the option */
60
61

  public:
steffen.schotthoefer's avatar
steffen.schotthoefer committed
62
    OptionString( std::string option_field_name, std::string& option_field, std::string default_value );
63

64
    ~OptionString() override {}
65

steffen.schotthoefer's avatar
steffen.schotthoefer committed
66
67
68
    std::string SetValue( std::vector<std::string> option_value ) override;

    void SetDefault() override;
69
70
};

71
72
class OptionInt : public OptionBase
{
73
74
75
    int& _field;       /*! @brief: Reference to the int field value */
    int _def;          /*! @brief: Default value */
    std::string _name; /*! @brief: string identifier for the option */
76
77

  public:
steffen.schotthoefer's avatar
steffen.schotthoefer committed
78
    OptionInt( std::string option_field_name, int& option_field, int default_value );
79

80
    ~OptionInt() override {}
steffen.schotthoefer's avatar
steffen.schotthoefer committed
81
82
83
84

    std::string SetValue( std::vector<std::string> option_value ) override;

    void SetDefault() override;
85
86
};

87
88
class OptionULong : public OptionBase
{
89
90
91
    unsigned long& _field; /*! @brief: Reference to the unsigned long field value */
    unsigned long _def;    /*! @brief: Default value */
    std::string _name;     /*! @brief: string identifier for the option */
92
93

  public:
steffen.schotthoefer's avatar
steffen.schotthoefer committed
94
    OptionULong( std::string option_field_name, unsigned long& option_field, unsigned long default_value );
95

96
    ~OptionULong() override {}
steffen.schotthoefer's avatar
steffen.schotthoefer committed
97
98
99
100

    std::string SetValue( std::vector<std::string> option_value ) override;

    void SetDefault() override;
101
102
};

steffen.schotthoefer's avatar
steffen.schotthoefer committed
103
class OptionUShort : public OptionBase
104
{
105
106
107
    unsigned short& _field; /*! @brief: Reference to the unsigned short field value */
    unsigned short _def;    /*! @brief: Default value */
    std::string _name;      /*! @brief: string identifier for the option */
108
109

  public:
steffen.schotthoefer's avatar
steffen.schotthoefer committed
110
    OptionUShort( std::string option_field_name, unsigned short& option_field, unsigned short default_value );
111

112
    ~OptionUShort() override {}
steffen.schotthoefer's avatar
steffen.schotthoefer committed
113
114
115
116

    std::string SetValue( std::vector<std::string> option_value ) override;

    void SetDefault() override;
117
118
};

119
120
class OptionLong : public OptionBase
{
121
122
123
    long& _field;      /*! @brief: Reference to the long field value */
    long _def;         /*! @brief: Default value */
    std::string _name; /*! @brief: string identifier for the option */
124
125

  public:
steffen.schotthoefer's avatar
steffen.schotthoefer committed
126
    OptionLong( std::string option_field_name, long& option_field, long default_value );
127

128
    ~OptionLong() override {}
steffen.schotthoefer's avatar
steffen.schotthoefer committed
129
130
131
132

    std::string SetValue( std::vector<std::string> option_value ) override;

    void SetDefault() override;
133
134
};

135
136
class OptionBool : public OptionBase
{
137
138
139
    bool& _field;      /*! @brief: Reference to the bool field value */
    bool _def;         /*! @brief: Default value */
    std::string _name; /*! @brief: string identifier for the option */
140
141

  public:
steffen.schotthoefer's avatar
steffen.schotthoefer committed
142
    OptionBool( std::string option_field_name, bool& option_field, bool default_value );
143

144
    ~OptionBool() override {}
steffen.schotthoefer's avatar
steffen.schotthoefer committed
145
146
147
148

    std::string SetValue( std::vector<std::string> option_value ) override;

    void SetDefault() override;
149
150
};

151
152
class OptionStringList : public OptionBase
{
153
154
155
    std::vector<std::string>& _field; /*! @brief: Reference to the string list field value. no default value */
    std::string _name;                /*! @brief: string identifier for the option */
    unsigned short& _size;            /*! @brief: Size of string list */
156
157

  public:
steffen.schotthoefer's avatar
steffen.schotthoefer committed
158
    OptionStringList( std::string option_field_name, unsigned short& list_size, std::vector<std::string>& option_field );
159

160
    ~OptionStringList() override {}
161

steffen.schotthoefer's avatar
steffen.schotthoefer committed
162
    std::string SetValue( std::vector<std::string> option_value ) override;
163

steffen.schotthoefer's avatar
steffen.schotthoefer committed
164
    void SetDefault() override;
165
166
};

167
168
169
170
171
// Template classes (TODO: Find a way to split code to .cpp)

template <class Tenum> class OptionEnum : public OptionBase
{

Steffen Schotthöfer's avatar
Steffen Schotthöfer committed
172
173
174
175
    std::map<std::string, Tenum> _map; /*! @brief: Map <String name of the option in cfg file, enum field name of cpp framework> */
    Tenum& _field;                     /*! @brief: Reference to the enum fieldname */
    Tenum _def;                        /*! @brief: Default value */
    std::string _name;                 /*! @brief: string identifier for the option */
176
177
178
179
180
181
182
183
184
185
186
187
188
189

  public:
    OptionEnum( std::string option_field_name, const std::map<std::string, Tenum> m, Tenum& option_field, Tenum default_value )
        : _field( option_field ) {
        this->_map  = m;
        this->_def  = default_value;
        this->_name = option_field_name;
    }

    ~OptionEnum() override {}

    std::string SetValue( std::vector<std::string> option_value ) override {
        OptionBase::SetValue( option_value );
        // Check if there is more than one string
Steffen Schotthöfer's avatar
Steffen Schotthöfer committed
190
        std::string out = OptionCheckMultipleValues( option_value, "enum", this->_name );
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
        if( out.compare( "" ) != 0 ) {
            return out;
        }
        // Check to see if the enum value is in the map
        if( this->_map.find( option_value[0] ) == _map.end() ) {
            std::string str;
            str.append( this->_name );
            str.append( ": invalid option value " );
            str.append( option_value[0] );
            str.append( ". Check current RTSN options in config_template.cfg." );
            return str;
        }
        // If it is there, set the option value
        Tenum val    = this->_map[option_value[0]];
        this->_field = val;
        return "";
    }

    void SetDefault() override { this->_field = this->_def; }
};

template <class Tenum> class OptionEnumList : public OptionBase
{

Steffen Schotthöfer's avatar
Steffen Schotthöfer committed
215
216
217
218
    std::map<std::string, Tenum> _map; /*! @brief: Map <String name of the option in cfg file, enum field name of cpp framework> */
    std::vector<Tenum>& _field;        /*! @brief: Reference to the enum list fieldname. No default value */
    std::string _name;                 /*! @brief: string identifier for the option */
    unsigned short& _size;             /*! @brief: Size of enum list */
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261

  public:
    OptionEnumList( std::string option_field_name, const std::map<std::string, Tenum> m, std::vector<Tenum>& option_field, unsigned short& list_size )
        : _field( option_field ), _size( list_size ) {
        this->_map  = m;
        this->_name = option_field_name;
    }

    ~OptionEnumList() override {}

    std::string SetValue( std::vector<std::string> option_value ) override {
        OptionBase::SetValue( option_value );
        if( option_value.size() == 1 && option_value[0].compare( "NONE" ) == 0 ) {
            this->_size = 0;
            return "";
        }
        // size is the length of the option list
        this->_size = option_value.size();
        std::vector<Tenum> enums( _size );
        // unsigned short* enums = new unsigned short[_size];
        for( int i = 0; i < this->_size; i++ ) {
            // Check to see if the enum value is in the map
            if( this->_map.find( option_value[i] ) == _map.end() ) {
                std::string str;
                str.append( this->_name );
                str.append( ": invalid option value " );
                str.append( option_value[i] );
                str.append( ". Check current RTSN options in config_template.cfg." );
                return str;
            }
            // If it is there, set the option value
            enums[i] = this->_map[option_value[i]];
        }
        this->_field = enums;
        return "";
    }

    void SetDefault() override {
        // No default to set
        _size = 0;
    }
};

262
#endif    // OPTION_STRUCTURE_H