9 #ifndef __NODAMUSHI_SVD_SVD_READER_HPP__ 10 #define __NODAMUSHI_SVD_SVD_READER_HPP__ 15 # include <type_traits> 60 string m =
"File read error : "s +
file_name;
69 invalid_argument(message),
79 static constexpr
size_t UNKNOWN_FILE_LINE= ~(size_t)0;
81 const std::string& file,
size_t file_line = UNKNOWN_FILE_LINE)
84 string m =
"XML parse error occured: "s + file;
85 return {file,m,file_line};
88 const std::string& message,
90 runtime_error(message),
92 line_number(file_line){}
103 static std::pair<size_t,size_t>
_trim(T& str)
106 const size_t len = str.length();
110 if(c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n')
117 if(c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n')
125 static void trim(std::string& str)
128 size_t b = v.first,e=v.second;
129 if(e != str.length())
135 # if __cplusplus >= 201703 136 static void trim(std::string_view& str)
140 size_t b = v.first,e=v.second,len = e-b;
141 std::string_view n(str.data()+b,len);
146 static std::string_view trimc(
const std::string_view& str)
149 size_t b = v.first,e=v.second,len = e-b;
150 return {str.data()+b,len};
153 static std::string_view trimc(
const std::string& str)
156 size_t b = v.first,e=v.second,len = e-b;
157 return {str.data()+b,len};
160 static std::string trimc(
const std::string& str)
164 size_t b = v.first,e=v.second,len = e-b;
165 return std::string(str,b,len);
177 template<
typename CallBack>
181 name(n),attribute(false),
value(),
182 children{},closed(
false),call_back(f),remove(
false){}
185 name(
std::move(n)),attribute(false),
value(),
186 children{},closed(
false),call_back(f),remove(
false){}
192 if(!closed && call_back!=
nullptr){
204 if(children.size()!=0)
205 children.erase(children.begin());
208 if(call_back!=
nullptr){
209 while(!closed && children.empty())
212 return children.size()!=0;
222 for(
auto& c:children)
233 for(
auto& c:children)
254 value.append(s,size);
261 children.emplace_back(name,
value,
true,call_back);
268 children.emplace_back(std::move(name),std::move(
value),
true,call_back);
276 size_t s = children.size();
277 children.emplace_back(std::move(name),call_back);
285 size_t s = children.size();
286 children.emplace_back(name,call_back);
295 if(closed)
return nullptr;
296 if(children.empty())
return this;
298 if(e.closed)
return this;
300 return p ==
nullptr? &e:p;
309 const std::string& val,
312 name(n),attribute(attr),
value(val),
313 children{},closed(attr),call_back(f),remove(
false)
323 name(
std::move(n)),attribute(attr),
value(
std::move(val)),
324 children{},closed(attr),call_back(f),remove(
false)
330 stream_reader_element(
const stream_reader_element&)=
delete;
331 stream_reader_element& operator=(
const stream_reader_element&)=
delete;
332 stream_reader_element(stream_reader_element&&d)=
delete;
333 stream_reader_element& operator=(stream_reader_element&&)=
delete;
355 template<
typename SubClass>
363 "stream_reader_base is base of SubClass");
365 virtual ~stream_reader_base()=
default;
369 children(
std::move(d.children)),
370 remove(d.remove),_active_cache(nullptr)
372 for(
auto & c:children) c.set_callback(
this);
376 children = std::move(d.children);
378 _active_cache=
nullptr;
379 for(
auto & c:children) c.set_callback(
this);
389 # if __cplusplus >= 201703 390 std::string_view get_name()
const{
return std::string_view{};}
391 std::string_view get_value()
const{
return std::string_view{};}
402 if(!children.empty())
403 children.erase(children.begin());
406 SubClass* p = reinterpret_cast<SubClass*>(
this);
407 while(children.empty() && !p->is_end()){
410 return children.size() != 0;
416 SubClass* p = reinterpret_cast<SubClass*>(
this);
424 size_t s = children.size();
425 children.emplace_back(name,
this);
431 size_t s = children.size();
432 children.emplace_back(name,
this);
438 if(_active_cache !=
nullptr && !_active_cache->is_closed()){
440 return _active_cache;
442 _active_cache =
nullptr;
443 if(children.empty())
return nullptr;
444 el& e = children[children.size()-1];
447 return _active_cache;
452 for(
auto& c:children)c.close();
465 #endif // __NODAMUSHI_SVD_SVD_READER_HPP__ boxvec. vector<pointer<X>>
stream_reader_element(std::string &&n, CallBack *f)
bool is_attribute() const
stream_reader_element & add_child(const std::string &name)
void add_attribute(std::string &&name, std::string &&value)
file_read_exception(const std::string &file, const std::string &message)
stream_reader_element(const std::string &n, const std::string &val, bool attr, CallBack *f)
static bool check(const std::string &file_name, bool throw_exception)
void add_value(const char *s, size_t size)
stream_reader_element(std::string &&n, std::string &&val, bool attr, CallBack *f)
const std::string & string_ref
el & add_child(const std::string &name)
std::string get_name() const
XML element class for stream type parser.
bool is_attribute() const
constexpr bool is_closed() const
svd_reader base class for stream type parser subclasses must be implements read method and is_end met...
stream_reader_base & operator=(stream_reader_base &&d)
std::string get_value() const
void add_attribute(const std::string &name, const std::string &value)
void set_callback(CallBack *cb)
stream_reader_element * get_active_open_element()
stream_reader_element & next_child()
stream_reader_base(stream_reader_base &&d)
void set_value(const std::string &s)
el * get_active_open_element()
void add_value(const std::string &s)
This class reperesents SVD(xml) element / attribute.
el & add_child(std::string &&name)
stream_reader_element & add_child(std::string &&name)
string_ref get_name() const
static xml_parse_exception make(const std::string &file, size_t file_line=UNKNOWN_FILE_LINE)
xml_parse_exception(const std::string &file, const std::string &message, size_t file_line)
static std::pair< size_t, size_t > _trim(T &str)
static void trim(std::string &str)
remove white spaces
stream_reader_element(const std::string &n, CallBack *f)