GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/OptionParser/src/OptionParser_impl.h Lines: 8 8 100.0 %
Date: 2024-12-09 15:30:52 Branches: 6 6 100.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 __POPTIONPARSER_IMPL_H__
8
#define __POPTIONPARSER_IMPL_H__
9
10
#include <sstream>
11
#include "OptionParser.h"
12
13
///Add an option in the OptionParser
14
/**	@param longOption : long option (start with --) as --version
15
 * 	@param shortOption : short option (start with -) as -v
16
 * 	@param defaultValue : default value of the option
17
 * 	@param docString : documentation string of the option
18
 * 	The type of the option will be automatically determined with the type of the default value
19
*/
20
template<typename T>
21
62
void OptionParser::addOption(const std::string & longOption, const std::string & shortOption, const T defaultValue, const std::string & docString){
22
62
	OptionType::OptionType optionType = getOptionTypeFromType<T>();
23
124
	OptionValue value;
24
62
	value.setType(optionType);
25
62
	value.setDefaultValue(defaultValue);
26
124
	Option option(longOption, shortOption, value, false, docString);
27
62
	p_vecMode.back().addOption(option);
28
62
}
29
30
///Add an option in the OptionParser
31
/**	@param longOption : long option (start with --) as --version
32
 * 	@param shortOption : short option (start with -) as -v
33
 * 	@param defaultValue : default value of the option
34
 * 	@param optionType : type of the data of the option (int, float, double, string, etc)
35
 * 	@param docString : documentation string of the option
36
 * 	The function will check if the given optionType is relevant with the type of the default value
37
*/
38
template<typename T>
39
void OptionParser::addOption(const std::string & longOption, const std::string & shortOption, const T defaultValue,
40
				OptionType::OptionType optionType, const std::string & docString)
41
{
42
	checkOptionType<T>(optionType);
43
	OptionValue value;
44
	value.setType(optionType);
45
	value.setDefaultValue(defaultValue);
46
	Option option(longOption, shortOption, value, false, docString);
47
	p_vecMode.back().addOption(option);
48
}
49
50
///Add an option in the OptionParser
51
/**	@param longOption : long option (start with --) as --version
52
 * 	@param shortOption : short option (start with -) as -v
53
 * 	@param defaultValue : default value of the option
54
 * 	@param docString : documentation string of the option
55
 * 	The type of the option will be automatically determined with the type of the default value
56
*/
57
template<typename T>
58
void OptionParser::addOption(const std::string & longOption, const std::string & shortOption, const std::vector<T> & defaultValue, const std::string & docString){
59
	OptionType::OptionType optionType = getOptionTypeFromType<T>();
60
	OptionValue valueVec;
61
	valueVec.setType(optionType);
62
	valueVec.setDefaultValue(defaultValue);
63
	Option option(longOption, shortOption, valueVec, false, docString);
64
	p_vecMode.back().addOption(option);
65
}
66
67
///Add an option in the OptionParser
68
/**	@param longOption : long option (start with --) as --version
69
 * 	@param shortOption : short option (start with -) as -v
70
 * 	@param defaultValue : default value of the option
71
 * 	@param docString : documentation string of the option
72
 * 	The type of the option will be automatically determined with the type of the default value
73
*/
74
template<typename T>
75
void OptionParser::addOption(const std::string & longOption, const std::string & shortOption, const std::list<T> & defaultValue, const std::string & docString){
76
	OptionType::OptionType optionType = getOptionTypeFromType<T>();
77
	OptionValue valueList;
78
	valueList.setType(optionType);
79
	valueList.setDefaultValue(defaultValue);
80
	Option option(longOption, shortOption, valueList, false, docString);
81
	p_vecMode.back().addOption(option);
82
}
83
84
///Add an option in the OptionParser
85
/**	@param longOption : long option (start with --) as --version
86
 * 	@param shortOption : short option (start with -) as -v
87
 * 	@param defaultValue : default value of the option
88
 * 	@param optionType : type of the data of the option (int, float, double, string, etc)
89
 * 	@param docString : documentation string of the option
90
 * 	The function will check if the given optionType is relevant with the type of the default value
91
*/
92
template<typename T>
93
void OptionParser::addOption(const std::string & longOption, const std::string & shortOption, const std::vector<T> & defaultValue,
94
				OptionType::OptionType optionType, const std::string & docString)
95
{
96
	checkOptionType<T>(optionType);
97
	OptionValue valueVec;
98
	valueVec.setDefaultValue(defaultValue);
99
	valueVec.setType(optionType);
100
	Option option(longOption, shortOption, valueVec, false, docString);
101
	p_vecMode.back().addOption(option);
102
}
103
104
///Add an option in the OptionParser
105
/**	@param longOption : long option (start with --) as --version
106
 * 	@param shortOption : short option (start with -) as -v
107
 * 	@param defaultValue : default value of the option
108
 * 	@param optionType : type of the data of the option (int, float, double, string, etc)
109
 * 	@param docString : documentation string of the option
110
 * 	The function will check if the given optionType is relevant with the type of the default value
111
*/
112
template<typename T>
113
void OptionParser::addOption(const std::string & longOption, const std::string & shortOption, const std::list<T> & defaultValue,
114
				OptionType::OptionType optionType, const std::string & docString)
115
{
116
	checkOptionType<T>(optionType);
117
	OptionValue valueList;
118
	valueList.setDefaultValue(defaultValue);
119
	valueList.setType(optionType);
120
	Option option(longOption, shortOption, valueList, false, docString);
121
	p_vecMode.back().addOption(option);
122
}
123
124
///Check the given option type
125
/**	@param optionType : type of the option to be checked
126
 * 	Throw an exception if there is a problem
127
*/
128
template<typename T>
129
void OptionParser::checkOptionType(OptionType::OptionType optionType){
130
	OptionType::OptionType optionTypeFromDefault = getOptionTypeFromType<T>();
131
	if(!isOptionTypeCompatible(optionType, optionTypeFromDefault)){
132
		std::stringstream strError;
133
		strError << "OptionParser::checkOptionType : Incompatible types from parameters (" << convertOptionTypeToString(optionType) << ") ad for default type ("<<convertOptionTypeToString(optionTypeFromDefault)<<")";
134
		throw std::runtime_error(strError.str());
135
	}
136
}
137
138
#endif
139