GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/OptionParser/src/OptionValue_impl.h Lines: 26 31 83.9 %
Date: 2025-04-24 04:07:07 Branches: 18 45 40.0 %

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
80
void OptionValue::setDefaultValue(const T & value){
18
160
	std::string valueBinStr(convertToString(value));
19
160
	VecValue vecValue;
20
80
	vecValue.push_back(valueBinStr);
21
80
	p_vecDefaultValue = vecValue;
22
80
}
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
66
void OptionValue::getValue(T & value, bool isParsed) const{
50
66
	checkTypeFromTemplate<T>();
51
66
	if(isParsed){
52
33
		if(p_vecValue.size() > 1lu){
53
			std::runtime_error("OptionValue::getValue : several value but only one value in parameter");
54
		}
55
33
		value = stringToValue<T>(p_vecValue.front());
56
	}else{
57
33
		if(p_vecDefaultValue.size() == 1lu){
58
33
			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
66
}
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
void OptionValue::getValue(std::vector<T> & vecValue, bool isParsed) const{
71
	checkTypeFromTemplate<T>();
72
	if(isParsed){
73
		for(VecValue::const_iterator it(p_vecValue.begin()); it != p_vecValue.end(); ++it){
74
			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
}
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
13
void OptionValue::getValue(std::list<T> & vecValue, bool isParsed) const{
89
13
	checkTypeFromTemplate<T>();
90
13
	if(isParsed){
91
27
		for(VecValue::const_iterator it(p_vecValue.begin()); it != p_vecValue.end(); ++it){
92

18
			vecValue.push_back(stringToValue<T>(*it));
93
		}
94
	}else{
95
7
		for(VecValue::const_iterator it(p_vecDefaultValue.begin()); it != p_vecDefaultValue.end(); ++it){
96

3
			vecValue.push_back(stringToValue<T>(*it));
97
		}
98
	}
99
13
}
100
101
///Check the type from the template
102
/**	Throw exception if there is an incompatibility
103
*/
104
template<typename T>
105
79
void OptionValue::checkTypeFromTemplate() const{
106
79
	OptionType::OptionType optionTypeFromDefault = getOptionTypeFromType<T>();
107
79
	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
79
}
113
114
#endif
115
116