cisst-saw
Loading...
Searching...
No Matches
cmnStreamRawParser.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3
4/*
5 Author(s): Peter Kazanzides
6
7 (C) Copyright 2009-2016 Johns Hopkins University (JHU), All Rights Reserved.
8
9--- begin cisst license - do not edit ---
10
11This software is provided "as is" under an open source license, with
12no warranty. The complete license can be found in license.txt and
13http://www.cisst.org/cisst/license.txt.
14
15--- end cisst license ---
16
17*/
18
19
24#pragma once
25
26#ifndef _cmnStreamRawParser_h
27#define _cmnStreamRawParser_h
28
29#include <iostream>
30#include <string>
31#include <set>
32#include <functional>
33
36
38
92
94{
95 class EntryBase {
96 protected:
97 std::string key;
98 char delimiter;
99 bool required;
100 bool valid;
101 public:
102 EntryBase(const std::string &name, char delim = ' ', bool req = true) :
103 key(name), delimiter(delim), required(req), valid(false) {}
104 virtual ~EntryBase() {}
105
106 std::string GetKey() const { return key; }
107 bool isRequired() const { return required; }
108 bool isValid() const { return valid; }
109 void SetValid(bool val) { valid = val; }
110
111 // Following should be implemented in the derived classes
112 virtual bool Parse(std::istream &CMN_UNUSED(inputStream)) { return false; }
113 virtual void ToStream(std::ostream & outputStream) const { outputStream << key << " (default)"; }
114 };
115
116 template <class _elementType>
117 class Entry : public EntryBase {
118 _elementType *valuePtr;
119 public:
120 Entry(const std::string &name, _elementType &data, char delim = ' ', bool req = true) :
121 EntryBase(name, delim, req), valuePtr(&data) {}
122 ~Entry() {}
123
124 bool Parse(std::istream &inputStream) {
125 if (valid) {
126 CMN_LOG_INIT_WARNING << "cmnStreamRawParser: duplicate entry for " << key << std::endl;
127 }
128 if (valuePtr) {
129 try {
130 cmnData<_elementType>::DeSerializeText(*valuePtr, inputStream, delimiter);
131 valid = true;
132 }
133 catch (const std::runtime_error &) {
134 valid = false;
135 }
136 }
137 return valid;
138 }
139
140 void ToStream(std::ostream & outputStream) const {
141 outputStream << key << " ";
142 if (valuePtr && valid) {
143 try {
144 cmnData<_elementType>::SerializeText(*valuePtr, outputStream, delimiter);
145 }
146 catch (const std::runtime_error &e) {
147 outputStream << "(invalid: " << e.what() << ")";
148 }
149 }
150 else
151 outputStream << "(invalid)";
152 }
153 };
154
155 template <class _elementType>
156 class EntryStreamable : public EntryBase {
157 _elementType *valuePtr;
158 public:
159 EntryStreamable(const std::string &name, _elementType &data, bool req = true) :
160 EntryBase(name, ' ', req), valuePtr(&data) {}
161 ~EntryStreamable() {}
162
163 bool Parse(std::istream &inputStream) {
164 if (valid) {
165 CMN_LOG_INIT_WARNING << "cmnStreamRawParser: duplicate entry for " << key << std::endl;
166 }
167 if (valuePtr)
168 inputStream >> *valuePtr;
169 valid = inputStream.good();
170 return valid;
171 }
172
173 void ToStream(std::ostream & outputStream) const {
174 outputStream << key << " ";
175 if (valuePtr && valid)
176 outputStream << *valuePtr;
177 else
178 outputStream << "(invalid)";
179 }
180 };
181
182 // Comparison operator for std::set
183 struct KeyListLess: public std::binary_function<const cmnStreamRawParser::EntryBase*, const cmnStreamRawParser::EntryBase*, bool>
184 {
185 result_type operator()(first_argument_type p1, second_argument_type p2) const
186 {
187 return p1->GetKey() < p2->GetKey();
188 }
189 };
190
191 typedef std::set<cmnStreamRawParser::EntryBase *, KeyListLess> KeyListType;
192 KeyListType KeyList;
193
194public:
197
204 template <class _elementType>
205 bool AddEntry(const std::string &name, _elementType &data, char delim = ' ', bool req = true)
206 {
207 cmnStreamRawParser::EntryBase *newEntry = new Entry<_elementType>(name, data, delim, req);
208 bool result = KeyList.insert(newEntry).second;
209 if (!result) delete newEntry; // if not inserted, delete entry
210 return result;
211 }
212
220 template <class _elementType>
221 bool AddEntryStreamable(const std::string &name, _elementType &data, bool req = true)
222 {
223 cmnStreamRawParser::EntryBase *newEntry = new EntryStreamable<_elementType>(name, data, req);
224 bool result = KeyList.insert(newEntry).second;
225 if (!result) delete newEntry; // if not inserted, delete entry
226 return result;
227 }
228
230 void SetAllValid(bool val = true);
231
234 bool Parse(std::istream & inputStream);
235
237 bool IsValid(const std::string &name) const;
238
241 void ToStream(std::ostream & outputStream) const;
242};
243
244#endif // _cmnStreamRawParser_h
Definition cmnDataFunctions.h:51
cmnStreamRawParser()
Definition cmnStreamRawParser.h:195
bool AddEntryStreamable(const std::string &name, _elementType &data, bool req=true)
Definition cmnStreamRawParser.h:221
void ToStream(std::ostream &outputStream) const
bool AddEntry(const std::string &name, _elementType &data, char delim=' ', bool req=true)
Definition cmnStreamRawParser.h:205
void SetAllValid(bool val=true)
bool IsValid(const std::string &name) const
bool Parse(std::istream &inputStream)
Macros to export the symbols of cisstCommon (in a Dll).
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Declaration of cmnLogger amd macros for human readable logging.
#define CMN_LOG_INIT_WARNING
Definition cmnLogger.h:163
#define CMN_UNUSED(argument)
Definition cmnPortability.h:497
const_reference operator()(size_type rowIndex, size_type colIndex) const CISST_THROW(std
Definition vctDynamicConstMatrixBase.h:352