GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/OptionParser/src/OptionValue_impl.h Lines: 33 39 84.6 %
Date: 2023-10-11 10:52:07 Branches: 25 57 43.9 %

Line Branch Exec Source
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
43
void OptionValue::setDefaultValue(const T & value){
18
86
	std::string valueBinStr(convertToString(value));
19
86
	VecValue vecValue;
20
43
	vecValue.push_back(valueBinStr);
21
43
	p_vecDefaultValue = vecValue;
22
43
}
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
30
void OptionValue::getValue(T & value, bool isParsed) const{
50
30
	checkTypeFromTemplate<T>();
51
30
	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
5
		if(p_vecDefaultValue.size() == 1lu){
58
5
			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
30
}
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
28
void OptionValue::getValue(std::vector<T> & vecValue, bool isParsed) const{
71
28
	checkTypeFromTemplate<T>();
72
28
	if(isParsed){
73
6
		for(VecValue::const_iterator it(p_vecValue.begin()); it != p_vecValue.end(); ++it){
74
1
			vecValue.push_back(stringToValue<T>(*it));
75
		}
76
	}else{
77
23
		for(VecValue::const_iterator it(p_vecDefaultValue.begin()); it != p_vecDefaultValue.end(); ++it){
78
			vecValue.push_back(stringToValue<T>(*it));
79
		}
80
	}
81
28
}
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
62
void OptionValue::checkTypeFromTemplate() const{
106
62
	OptionType::OptionType optionTypeFromDefault = getOptionTypeFromType<T>();
107
62
	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
62
}
113
114
#endif
115
116