-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.h
63 lines (47 loc) · 1.15 KB
/
option.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef OPTION_H
#define OPTION_H
#include <string>
#include <sstream>
#include <iostream>
template <typename T>
void from_string(T& value, const std::string& str);
class Option
{
public:
Option(const std::string& name, bool mandatory);
virtual ~Option(){}
bool mandatory() const;
const std::string& name() const;
void operator<<(const std::string& str);
private:
virtual void setValue(const std::string& str) = 0;
std::string m_name;
bool m_mandatory;
};
template <typename T>
class OptionImpl : public Option
{
public:
OptionImpl(const std::string& name, T& value) : Option(name, true), m_value(value)
{
}
OptionImpl(const std::string& name, T& value, const T& default_value) : Option(name, false), m_value(value)
{
m_value = default_value;
}
private:
void setValue(const std::string& str)
{
from_string(m_value, str);
}
T& m_value;
};
template <typename T>
void from_string(T& value, const std::string& str)
{
std::istringstream iss(str);
iss >> value;
}
template <>
void from_string<std::string>(std::string& value, const std::string& str);
#endif // OPTION_H