NSVD Reader  0.0.1
imple.hpp
Go to the documentation of this file.
1 
5 /*
6  * These codes are licensed under CC0.
7  * http://creativecommons.org/publicdomain/zero/1.0/
8  */
9 #ifndef __NODAMUSHI_SVD_SVD_PRINTER_IMPLE_HPP__
10 #define __NODAMUSHI_SVD_SVD_PRINTER_IMPLE_HPP__
11 
12 # include "nodamushi/box.hpp"
13 # include "nodamushi/boxvec.hpp"
15 # include <sstream>
16 # include <ios>
17 # include <iomanip>
18 # include <type_traits>
19 namespace nodamushi{
20 namespace svd{
21 
22 namespace details{ template<typename T>struct print_with_svd_escape; }
23 
24 template<typename T>
25 std::ostream& svd_printer<T>::print(
26  std::ostream& o, const_string name, T const& v,int indent)
27 {
28  o <<get_indent(indent)<<"<" <<name<<">";
30  o <<"</" << name << ">";
31  return o;
32 }
33 
34 // for void
35 template<>struct svd_printer<void>
36 {
37  template<typename T>
38  static std::ostream& print(std::ostream& o, const_string name, const T& v,int indent)
39  {
40  return o;
41  }
42 };
43 
44 
45 
46 // ------------------------------------------------------------------------------
47 // ------------------------------------------------------------------------------
48 // svd_value_printer
49 // ------------------------------------------------------------------------------
50 // ------------------------------------------------------------------------------
51 
52 namespace details{
53 
54 template<typename vtype> struct _hex_printer_elem
55 {
56  static std::ostream& print(
57  std::ostream& o, const_string name, vtype v,int indent)
58  {
59  o <<get_indent(indent)<<"<" <<name<<">";
60  if(v){
61  std::ios_base::fmtflags f( o.flags() );
62  o <<"0x"<< std::hex << std::uppercase << v;
63  o.flags( f );
64  }else{
65  o << v;
66  }
67  o <<"</" << name << ">";
68  return o;
69  }
70 };
71 
72 template<typename vtype> struct _hex_printer_attr
73 {
74  static std::ostream& print(std::ostream& o, vtype v)
75  {
76  std::ios_base::fmtflags f( o.flags() );
77  o <<"0x"<< std::hex <<std::uppercase << v;
78  o.flags( f );
79  return o;
80  }
81 };
82 
83 
84 template<typename T> struct _hex_printer_selector
85 {
87  using is_hex_t = typename u::template contains<::nodamushi::svd::hex>;
88  static constexpr bool is_hex = is_hex_t::value;
89  using vtype = typename u::type;
90  using type_a = typename std::conditional<
91  is_hex,
94  using type_e = typename std::conditional<
95  is_hex,
98 };
99 
100 template<typename T>
102 
103 template<typename T>
105 
106 }
107 
108 // attribute
109 template<>struct svd_value_printer<true>
110 {
111  template<typename T,bool required,char... name>
112  static std::ostream& print(
113  std::ostream& o,
115  int indent)
116  {
117  if(v){
118  o << std::string(indent,' ');
119  print_const_string<name...>(o) << "=\"";
121  o<<"\"";
122  }
123  return o;
124  }
125 };
126 
127 
128 // ------------------------------------------------------------------------------
129 
130 template<>struct svd_value_printer<false> // element
131 {
132  template<typename T,bool required,char... name>
133  static std::ostream& print(
134  std::ostream& o,
136  int indent)
137  {
138  NODAMUSHI_LOCAL_CONST_STRING(name,text);
139  return details::select_printer_elem<T>::print(o,text,*v,indent);
140  }
141 
142  template<typename T,bool required,char... name>
143  static std::ostream& print(
144  std::ostream& o,
145  const value<::nodamushi::box<T>,false,required,name...>& v,
146  int indent)
147  {
148  NODAMUSHI_LOCAL_CONST_STRING(name,text);
149  return details::select_printer_elem<T>::print(o,text,**v,indent);
150  }
151 
152  template<typename T,bool required,char... name>
153  static std::ostream& print(
154  std::ostream& o,
155  const value<std::vector<T>,false,required,name...>& v,
156  int indent)
157  {
158  NODAMUSHI_LOCAL_CONST_STRING(name,text);
159  bool first = true;
160  for(const T& t:*v){
161  if(first) first = false;
162  else o << std::endl;
164  }
165  return o;
166  }
167 
168  template<typename T,bool required,char... name>
169  static std::ostream& print(
170  std::ostream& o,
171  const value<std::vector<::nodamushi::box<T>>,false,required,name...>& v,
172  int indent)
173  {
174  NODAMUSHI_LOCAL_CONST_STRING(name,text);
175  bool first = true;
176  for(const ::nodamushi::box<T>& t:*v){
177  if(first) first = false;
178  else o << std::endl;
179  details::select_printer_elem<T>::print(o,text,*t,indent);
180  }
181  return o;
182  }
183 
184 
185  template<typename T,typename PTR,bool required,char... name>
186  static std::ostream& print(
187  std::ostream& o,
188  const value<::nodamushi::boxvec<T,PTR>,false,required,name...>& v,
189  int indent)
190  {
191  NODAMUSHI_LOCAL_CONST_STRING(name,text);
192  bool first = true;
193  for(const T& t:*v){
194  if(first) first = false;
195  else o << std::endl;
197  }
198  return o;
199  }
200 
201 };
202 
203 
204 
205 
206 // ------------------------------------------------------------------------------
207 // ------------------------------------------------------------------------------
208 // print_with_svd_escape
209 // ------------------------------------------------------------------------------
210 // ------------------------------------------------------------------------------
211 
212 namespace details{
213 template<bool b,typename T> struct print_with_svd_escape_helper;
214 
215 template<typename T>struct print_with_svd_escape
216 {
217  static constexpr bool simple_type =
218  (std::is_integral<T>::value
219  || std::is_floating_point<T>::value
220  || std::is_enum<T>::value);
221 
222  static void print(std::ostream& o,T const& v)
223  {
225  }
226 };
227 
228 template<>struct print_with_svd_escape<const char*>
229 {
230  static constexpr bool svd_escape_charactor(char c)
231  {
232  return c == '"' || c == '\'' || c == '<' || c == '>' || c == '&';
233  }
234 
235  static void print(std::ostream& o,const char* itr,const char* const end)
236  {
237  const char* p = itr;
238  while(true){
239  char c;
240  bool e = itr == end || (c=*itr) == '\0';
241  if(e || svd_escape_charactor(c)){
242  if(p != itr) o.write(p,itr-p);
243  if(e) return;
244  switch(c){
245  case '"': o << "&quot;";
246  case '\'':o << "&apos;";
247  case '<': o << "&lt;";
248  case '>': o << "&gt;";
249  case '&': o << "&amp;";
250  }
251  p = ++itr;
252  }else ++itr;
253  }
254  }
255 
256  static void print(std::ostream& o,const char * v)
257  {
258  print(o,v,nullptr);
259  }
260 };
261 
262 template<>struct print_with_svd_escape<bool>
263 {
264  static void print(std::ostream& o,const bool& v)
265  {
266  o << (v? "true": "false");
267  }
268 };
269 
270 
271 template<>struct print_with_svd_escape<std::string>
272 {
273  static void print(std::ostream& o,std::string const& v)
274  {
275  print_with_svd_escape<const char*>::print(o,v.data(),v.data()+v.length());
276  }
277 };
278 
279 # if __cplusplus >= 201703
280 template<>struct print_with_svd_escape<std::string_view>
281 {
282  static void print(std::ostream& o,std::string_view const& v)
283  {
284  print_with_svd_escape<const char*>::print(o,v.data(),v.data()+v.length());
285  }
286 };
287 # endif
288 
289 
290 template<typename T> struct print_with_svd_escape_helper<true,T>
291 {
292  static void print(std::ostream& o,T const& v)
293  {
294  o << v;
295  }
296 };
297 
298 template<typename T> struct print_with_svd_escape_helper<false,T>
299 {
300  static void print(std::ostream& o,T const& v)
301  {
302  std::stringstream s;
303  s << v;
304  std::string str = s.str();
305  print_with_svd_escape<const char*>::print(o,str.data(),str.data()+str.length());
306  }
307 };
308 
309 
310 }// end namespace details
311 }// end namespace svd
312 }// end namespace nodamushi
313 
314 #endif // __NODAMUSHI_SVD_SVD_PRINTER_IMPLE_HPP__
boxvec. vector<pointer<X>>
static std::ostream & print(std::ostream &o, const_string name, vtype v, int indent)
Definition: imple.hpp:56
std::ostream & print_const_string(std::ostream &o)
static std::ostream & print(std::ostream &o, vtype v)
Definition: imple.hpp:74
typename _hex_printer_selector< T >::type_a select_printer_attr
Definition: imple.hpp:101
static void print(std::ostream &o, T const &v)
Definition: imple.hpp:222
static void print(std::ostream &o, T const &v)
Definition: imple.hpp:292
std::string get_indent(int s)
Definition: svd_printer.hpp:48
svd printer
Definition: Access.hpp:143
typename u::template contains<::nodamushi::svd::hex > is_hex_t
Definition: imple.hpp:87
typename std::conditional< is_hex, _hex_printer_elem< vtype >, svd_printer< T > >::type type_e
Definition: imple.hpp:97
box.std::unique_ptr wrapper
const char * const_string
Definition: string_type.hpp:43
static std::ostream & print(std::ostream &o, const value< std::vector<::nodamushi::box< T >>, false, required, name... > &v, int indent)
Definition: imple.hpp:169
static std::ostream & print(std::ostream &o, const value< T, true, required, name... > &v, int indent)
Definition: imple.hpp:112
static std::ostream & print(std::ostream &o, const value<::nodamushi::boxvec< T, PTR >, false, required, name... > &v, int indent)
Definition: imple.hpp:186
static void print(std::ostream &o, const bool &v)
Definition: imple.hpp:264
static void print(std::ostream &o, const char *itr, const char *const end)
Definition: imple.hpp:235
This class reperesents SVD(xml) element / attribute.
Definition: value.hpp:53
static void print(std::ostream &o, const char *v)
Definition: imple.hpp:256
static std::ostream & print(std::ostream &o, const_string name, const T &v, int indent)
Definition: imple.hpp:38
static std::ostream & print(std::ostream &o, const value<::nodamushi::box< T >, false, required, name... > &v, int indent)
Definition: imple.hpp:143
#define NODAMUSHI_LOCAL_CONST_STRING(CHARS, varname)
typename std::conditional< is_hex, _hex_printer_attr< vtype >, details::print_with_svd_escape< T > >::type type_a
Definition: imple.hpp:93
static std::ostream & print(std::ostream &o, const_string name, const T &v, int indent)
Definition: imple.hpp:25
vector<pointer<T>>.
Definition: boxvec.hpp:75
static void print(std::ostream &o, T const &v)
Definition: imple.hpp:300
static void print(std::ostream &o, std::string const &v)
Definition: imple.hpp:273
static std::ostream & print(std::ostream &o, const value< T, false, required, name... > &v, int indent)
Definition: imple.hpp:133
static std::ostream & print(std::ostream &o, const value< std::vector< T >, false, required, name... > &v, int indent)
Definition: imple.hpp:153
typename _hex_printer_selector< T >::type_e select_printer_elem
Definition: imple.hpp:104