27 #ifndef _cmnStreamRawParser_h
28 #define _cmnStreamRawParser_h
103 EntryBase(
const std::string &name,
char delim =
' ',
bool req =
true) :
104 key(name), delimiter(delim), required(req), valid(
false) {}
105 virtual ~EntryBase() {}
107 std::string GetKey()
const {
return key; }
108 bool isRequired()
const {
return required; }
109 bool isValid()
const {
return valid; }
110 void SetValid(
bool val) { valid = val; }
113 virtual bool Parse(std::istream &
CMN_UNUSED(inputStream)) {
return false; }
114 virtual void ToStream(std::ostream & outputStream)
const { outputStream << key <<
" (default)"; }
117 template <
class _elementType>
118 class Entry :
public EntryBase {
119 _elementType *valuePtr;
121 Entry(
const std::string &name, _elementType &data,
char delim =
' ',
bool req =
true) :
122 EntryBase(name, delim, req), valuePtr(&data) {}
125 bool Parse(std::istream &inputStream) {
126 if (valid)
CMN_LOG_INIT_WARNING <<
"cmnStreamRawParser: duplicate entry for " << key << std::endl;
132 catch (
const std::runtime_error &) {
139 void ToStream(std::ostream & outputStream)
const {
140 outputStream << key <<
" ";
141 if (valuePtr && valid) {
145 catch (
const std::runtime_error &e) {
146 outputStream <<
"(invalid: " << e.what() <<
")";
150 outputStream <<
"(invalid)";
154 template <
class _elementType>
155 class EntryStreamable :
public EntryBase {
156 _elementType *valuePtr;
158 EntryStreamable(
const std::string &name, _elementType &data,
bool req =
true) :
159 EntryBase(name,
' ', req), valuePtr(&data) {}
160 ~EntryStreamable() {}
162 bool Parse(std::istream &inputStream) {
163 if (valid)
CMN_LOG_INIT_WARNING <<
"cmnStreamRawParser: duplicate entry for " << key << std::endl;
165 inputStream >> *valuePtr;
166 valid = inputStream.good();
170 void ToStream(std::ostream & outputStream)
const {
171 outputStream << key <<
" ";
172 if (valuePtr && valid)
173 outputStream << *valuePtr;
175 outputStream <<
"(invalid)";
180 struct KeyListLess:
public std::binary_function<const cmnStreamRawParser::EntryBase*, const cmnStreamRawParser::EntryBase*, bool>
182 result_type operator()(first_argument_type p1, second_argument_type p2)
const
184 return p1->GetKey() < p2->GetKey();
188 typedef std::set<cmnStreamRawParser::EntryBase *, KeyListLess> KeyListType;
201 template <
class _elementType>
202 bool AddEntry(
const std::string &name, _elementType &data,
char delim =
' ',
bool req =
true)
204 cmnStreamRawParser::EntryBase *newEntry =
new Entry<_elementType>(name, data, delim, req);
205 bool result = KeyList.insert(newEntry).second;
206 if (!result)
delete newEntry;
217 template <
class _elementType>
220 cmnStreamRawParser::EntryBase *newEntry =
new EntryStreamable<_elementType>(name, data, req);
221 bool result = KeyList.insert(newEntry).second;
222 if (!result)
delete newEntry;
227 void SetAllValid(
bool val =
true);
231 bool Parse(std::istream & inputStream);
234 bool IsValid(
const std::string &name)
const;
238 void ToStream(std::ostream & outputStream)
const;
241 #endif // _cmnStreamRawParser_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
Declaration of cmnLogger amd macros for human readable logging.
static void SerializeText(const DataType &data, std::ostream &outputStream, const char delimiter= ',')
bool AddEntryStreamable(const std::string &name, _elementType &data, bool req=true)
Definition: cmnStreamRawParser.h:218
cmnStreamRawParser()
Definition: cmnStreamRawParser.h:192
Macros to export the symbols of cisstCommon (in a Dll).
Definition: cmnStreamRawParser.h:94
static void DeSerializeText(DataType &data, std::istream &inputStream, const char delimiter= ',')
#define CMN_LOG_INIT_WARNING
Definition: cmnLogger.h:163
bool AddEntry(const std::string &name, _elementType &data, char delim= ' ', bool req=true)
Definition: cmnStreamRawParser.h:202