1 |
|
|
/*************************************** |
2 |
|
|
Auteur : Pierre Aubert |
3 |
|
|
Mail : pierre.aubert@lapp.in2p3.fr |
4 |
|
|
Licence : CeCILL-C |
5 |
|
|
****************************************/ |
6 |
|
|
|
7 |
|
|
#ifndef __CONFIGTEMPLATE_H_IMPL__ |
8 |
|
|
#define __CONFIGTEMPLATE_H_IMPL__ |
9 |
|
|
|
10 |
|
|
#include "convertToString.h" |
11 |
|
|
|
12 |
|
|
#include "ConfigTemplate.h" |
13 |
|
|
|
14 |
|
|
///Default constructor of ConfigTemplate |
15 |
|
|
template<typename T> |
16 |
|
1 |
ConfigTemplate<T>::ConfigTemplate() |
17 |
✓✓✓ |
1 |
:ConfigBase(), p_data(NULL) |
18 |
|
|
{ |
19 |
|
|
|
20 |
|
1 |
} |
21 |
|
|
|
22 |
|
|
///Default constructor of ConfigTemplate |
23 |
|
|
/** @param data : data to be used |
24 |
|
|
* @param name : name of the variable to be set |
25 |
|
|
* @param documentation : documentation of the variable |
26 |
|
|
*/ |
27 |
|
|
template<typename T> |
28 |
|
3 |
ConfigTemplate<T>::ConfigTemplate(T & data, const std::string & name, const std::string & documentation) |
29 |
|
3 |
:ConfigBase(name, documentation), p_data(&data) |
30 |
|
|
{ |
31 |
|
|
|
32 |
|
3 |
} |
33 |
|
|
|
34 |
|
|
///Destructor of ConfigTemplate |
35 |
|
|
template<typename T> |
36 |
|
16 |
ConfigTemplate<T>::~ConfigTemplate(){ |
37 |
|
|
|
38 |
|
16 |
} |
39 |
|
|
|
40 |
|
|
///Parse the value |
41 |
|
|
/** @param[out] parser : file parser |
42 |
|
|
* @return true if the parameters has been found, false otherwise |
43 |
|
|
*/ |
44 |
|
|
template<typename T> |
45 |
|
7 |
bool ConfigTemplate<T>::parseVar(PFileParser & parser){ |
46 |
✓✓ |
7 |
if(!parser.isMatchToken(p_name)){return false;} |
47 |
✓✓ |
6 |
if(p_isParsed){ |
48 |
✓✓ |
1 |
std::cerr << "ConfigTemplate<T>::parseVar : error at " << parser.getLocation() << std::endl; |
49 |
|
1 |
std::cerr << "\tvariable '" << p_name << " already parsed with value '" << (*p_data) << "'" << std::endl; |
50 |
✓ |
1 |
throw std::runtime_error("ConfigTemplate<T>::parseVar : parsing error"); |
51 |
|
|
} |
52 |
|
|
|
53 |
✓✓✓✓
|
5 |
if(!parser.isMatch("=")){ |
54 |
✓✓ |
1 |
std::cerr << "ConfigTemplate<T>::parseVar : error at " << parser.getLocation() << std::endl; |
55 |
|
1 |
std::cerr << "\texpect '=' after variable name '"<<p_name<<"'" << std::endl; |
56 |
✓ |
1 |
throw std::runtime_error("ConfigTemplate<T>::parseVar : parsing error expect '='"); |
57 |
|
|
} |
58 |
|
|
|
59 |
✓ |
4 |
*p_data = stringToValue<T>(parser.getNextToken()); |
60 |
|
|
|
61 |
✓✓✓✓
|
4 |
if(!parser.isMatch(";")){ |
62 |
✓✓ |
1 |
std::cerr << "ConfigTemplate<T>::parseVar : error at " << parser.getLocation() << std::endl; |
63 |
|
1 |
std::cerr << "\texpect ';' after variable definition '" << p_name << " = " << (*p_data) << "'" << std::endl; |
64 |
✓ |
1 |
throw std::runtime_error("ConfigTemplate<T>::parseVar : parsing error expect ';'"); |
65 |
|
|
} |
66 |
|
3 |
p_isParsed = true; |
67 |
|
3 |
return true; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
///Save the variable into a string |
71 |
|
|
/** @return corresponding string |
72 |
|
|
*/ |
73 |
|
|
template<typename T> |
74 |
|
4 |
std::string ConfigTemplate<T>::saveToString() const{ |
75 |
✓ |
4 |
std::string body("\t"); |
76 |
✓✓ |
4 |
body += p_name + " = "; |
77 |
✓✗ |
4 |
if(p_data != NULL){ |
78 |
✓✓ |
4 |
body += convertToString(*p_data); |
79 |
|
|
} |
80 |
✓ |
4 |
body += ";"; |
81 |
✓✗ |
4 |
if(p_documentation != ""){ |
82 |
✓✓ |
4 |
body += "\t#" + p_documentation; |
83 |
|
|
} |
84 |
✓ |
4 |
body += "\n"; |
85 |
|
4 |
return body; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
#endif |
89 |
|
|
|
90 |
|
|
|
91 |
|
|
|