1 |
|
|
/*************************************** |
2 |
|
|
Auteur : Pierre Aubert |
3 |
|
|
Mail : pierre.aubert@lapp.in2p3.fr |
4 |
|
|
Licence : CeCILL-C |
5 |
|
|
****************************************/ |
6 |
|
|
|
7 |
|
|
#ifndef __POPTIONVALUE_IMPL_H__ |
8 |
|
|
#define __POPTIONVALUE_IMPL_H__ |
9 |
|
|
|
10 |
|
|
#include <stdexcept> |
11 |
|
|
#include "OptionValue.h" |
12 |
|
|
|
13 |
|
|
///Set the value in the OptionValue |
14 |
|
|
/** @param value : value of the OptionValue |
15 |
|
|
*/ |
16 |
|
|
template<typename T> |
17 |
|
62 |
void OptionValue::setDefaultValue(const T & value){ |
18 |
✓ |
124 |
std::string valueBinStr(convertToString(value)); |
19 |
|
124 |
VecValue vecValue; |
20 |
✓ |
62 |
vecValue.push_back(valueBinStr); |
21 |
✓ |
62 |
p_vecDefaultValue = vecValue; |
22 |
|
62 |
} |
23 |
|
|
|
24 |
|
|
///Set the value in the OptionValue |
25 |
|
|
/** @param value : value of the OptionValue |
26 |
|
|
*/ |
27 |
|
|
template<typename T> |
28 |
|
|
void OptionValue::setDefaultValue(const std::vector<T> & value){ |
29 |
|
|
p_vecDefaultValue = value; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
///Set the value in the OptionValue |
33 |
|
|
/** @param value : value of the OptionValue |
34 |
|
|
*/ |
35 |
|
|
template<typename T> |
36 |
|
|
void OptionValue::setDefaultValue(const std::list<T> & value){ |
37 |
|
|
VecValue vecValue; |
38 |
|
|
for(typename std::list<T>::const_iterator it(value.begin()); it != value.end(); ++it){ |
39 |
|
|
vecValue.push_back(convertToString(*it)); |
40 |
|
|
} |
41 |
|
|
p_vecDefaultValue = vecValue; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
///Get the value of the option |
45 |
|
|
/** @param[out] value : value of the option |
46 |
|
|
* @param isParsed : true if the option is parsed, false if not |
47 |
|
|
*/ |
48 |
|
|
template<typename T> |
49 |
|
48 |
void OptionValue::getValue(T & value, bool isParsed) const{ |
50 |
|
48 |
checkTypeFromTemplate<T>(); |
51 |
✓✓ |
48 |
if(isParsed){ |
52 |
✗✓ |
25 |
if(p_vecValue.size() > 1lu){ |
53 |
|
|
std::runtime_error("OptionValue::getValue : several value but only one value in parameter"); |
54 |
|
|
} |
55 |
|
25 |
value = stringToValue<T>(p_vecValue.front()); |
56 |
|
|
}else{ |
57 |
✓✗ |
23 |
if(p_vecDefaultValue.size() == 1lu){ |
58 |
|
23 |
value = stringToValue<T>(p_vecDefaultValue.front()); |
59 |
|
|
}else{ |
60 |
|
|
std::runtime_error("OptionValue::getValue : several value but only one value in parameter"); |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
48 |
} |
64 |
|
|
|
65 |
|
|
///Get the value of the option |
66 |
|
|
/** @param[out] vecValue : value of the option |
67 |
|
|
* @param isParsed : true if the option is parsed, false if not |
68 |
|
|
*/ |
69 |
|
|
template<typename T> |
70 |
|
6 |
void OptionValue::getValue(std::vector<T> & vecValue, bool isParsed) const{ |
71 |
|
6 |
checkTypeFromTemplate<T>(); |
72 |
✓✗ |
6 |
if(isParsed){ |
73 |
✓✓ |
16 |
for(VecValue::const_iterator it(p_vecValue.begin()); it != p_vecValue.end(); ++it){ |
74 |
✓✓ |
10 |
vecValue.push_back(stringToValue<T>(*it)); |
75 |
|
|
} |
76 |
|
|
}else{ |
77 |
|
|
for(VecValue::const_iterator it(p_vecDefaultValue.begin()); it != p_vecDefaultValue.end(); ++it){ |
78 |
|
|
vecValue.push_back(stringToValue<T>(*it)); |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
6 |
} |
82 |
|
|
|
83 |
|
|
///Get the value of the option |
84 |
|
|
/** @param[out] vecValue : value of the option |
85 |
|
|
* @param isParsed : true if the option is parsed, false if not |
86 |
|
|
*/ |
87 |
|
|
template<typename T> |
88 |
|
4 |
void OptionValue::getValue(std::list<T> & vecValue, bool isParsed) const{ |
89 |
|
4 |
checkTypeFromTemplate<T>(); |
90 |
✓✓ |
4 |
if(isParsed){ |
91 |
✓✓ |
4 |
for(VecValue::const_iterator it(p_vecValue.begin()); it != p_vecValue.end(); ++it){ |
92 |
✓✗✓✗
|
3 |
vecValue.push_back(stringToValue<T>(*it)); |
93 |
|
|
} |
94 |
|
|
}else{ |
95 |
✓✓ |
6 |
for(VecValue::const_iterator it(p_vecDefaultValue.begin()); it != p_vecDefaultValue.end(); ++it){ |
96 |
✓✗✓✗
|
3 |
vecValue.push_back(stringToValue<T>(*it)); |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
4 |
} |
100 |
|
|
|
101 |
|
|
///Check the type from the template |
102 |
|
|
/** Throw exception if there is an incompatibility |
103 |
|
|
*/ |
104 |
|
|
template<typename T> |
105 |
|
60 |
void OptionValue::checkTypeFromTemplate() const{ |
106 |
|
60 |
OptionType::OptionType optionTypeFromDefault = getOptionTypeFromType<T>(); |
107 |
✗✓ |
60 |
if(!isOptionTypeCompatible(p_type, optionTypeFromDefault)){ |
108 |
|
|
std::stringstream strError; |
109 |
|
|
strError << "OptionValue::checkTypeFromTemplate : Incompatible types from parameters (" << convertOptionTypeToString(p_type) << ") ad for default type ("<<convertOptionTypeToString(optionTypeFromDefault)<<")"; |
110 |
|
|
throw std::runtime_error(strError.str()); |
111 |
|
|
} |
112 |
|
60 |
} |
113 |
|
|
|
114 |
|
|
#endif |
115 |
|
|
|
116 |
|
|
|