eRPC Generator (erpcgen)  Rev. 1.7.2
NXP Semiconductors
cpptempl.h
1 // Copyright (c) 2010-2014 Ryan Ginstrom
2 // Copyright (c) 2014 Martinho Fernandes
3 // Copyright (c) 2014-2016 Freescale Semiconductor, Inc.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 
23 #pragma once
24 
25 #ifdef _WIN32
26 #pragma warning(disable : 4996) // 'std::copy': Function call with parameters that may be unsafe - this call relies on
27  // the caller to check that the passed values are correct. To disable this warning, use
28  // -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked
29  // Iterators'
30 #pragma warning(disable : 4512) // 'std::copy': Function call with parameters that may be unsafe - this call relies on
31  // the caller to check that the passed values are correct. To disable this warning, use
32  // -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked
33  // Iterators'
34 #define NOEXCEPT // hide unsupported noexcept keyword under VC++
35 #else
36 #define NOEXCEPT noexcept
37 #endif // NOEXCEPT
38 
39 #if __MINGW32__
40 #define NOTHROW throw() // add throw() keyword under MinGW
41 #else
42 #define NOTHROW
43 #endif // NOTHROW
44 
45 #include <boost/lexical_cast.hpp>
46 #include <map>
47 #include <memory>
48 #include <string>
49 #include <unordered_map>
50 #include <vector>
51 
52 #include <iostream>
53 
54 namespace cpptempl {
55 
56 // various typedefs
57 class data_ptr;
58 class data_map;
59 class DataMap;
60 class DataTemplate;
61 
62 typedef std::vector<data_ptr> data_list;
63 
64 // data classes
65 class Data
66 {
67 public:
68  virtual ~Data() = default;
69  virtual bool empty() = 0;
70  virtual std::string getvalue();
71  virtual data_list &getlist();
72  virtual data_map &getmap();
73  virtual int getint() const;
74  virtual void dump(int indent = 0) = 0;
75 };
76 
77 class DataBool : public Data
78 {
79  bool m_value;
80 
81 public:
82  DataBool(bool value)
83  : m_value(value)
84  {
85  }
86  std::string getvalue();
87  virtual int getint() const;
88  bool empty();
89  virtual void dump(int indent = 0);
90 };
91 
92 class DataInt : public Data
93 {
94  int m_value;
95 
96 public:
97  DataInt(int value)
98  : m_value(value)
99  {
100  }
101  DataInt(unsigned int value)
102  : m_value(value)
103  {
104  }
105  std::string getvalue();
106  virtual int getint() const;
107  bool empty();
108  virtual void dump(int indent = 0);
109 };
110 
111 class DataValue : public Data
112 {
113  std::string m_value;
114 
115 public:
116  DataValue(const std::string &value)
117  : m_value(value)
118  {
119  }
120  DataValue(std::string &&value)
121  : m_value(std::move(value))
122  {
123  }
124  std::string getvalue();
125  virtual int getint() const;
126  bool empty();
127  virtual void dump(int indent = 0);
128 };
129 
130 class DataList : public Data
131 {
132  data_list m_items;
133 
134 public:
135  DataList(const data_list &items)
136  : m_items(items)
137  {
138  }
139  DataList(data_list &&items)
140  : m_items(std::move(items))
141  {
142  }
143  data_list &getlist();
144  bool empty();
145  void dump(int indent = 0);
146 };
147 
148 class data_ptr
149 {
150 public:
151  data_ptr() {}
152  template <typename T>
153  data_ptr(const T &data)
154  {
155  this->operator=(data);
156  }
157  data_ptr(DataBool *data);
158  data_ptr(DataInt *data);
159  data_ptr(DataValue *data);
160  data_ptr(DataList *data);
161  data_ptr(DataMap *data);
162  data_ptr(DataTemplate *data);
163  data_ptr(const data_ptr &data) { ptr = data.ptr; }
164  data_ptr(data_ptr &&data) { ptr = std::move(data.ptr); }
165  data_ptr &operator=(const data_ptr &data)
166  {
167  ptr = data.ptr;
168  return *this;
169  }
170  data_ptr &operator=(data_ptr &&data)
171  {
172  ptr = std::move(data.ptr);
173  return *this;
174  }
175  data_ptr &operator=(std::string &&data);
176  data_ptr &operator=(data_map &&data);
177  data_ptr &operator=(data_list &&data);
178  template <typename T>
179  void operator=(const T &data);
180  void push_back(const data_ptr &data);
181  virtual ~data_ptr() {}
182  Data *operator->() { return ptr.get(); }
183  std::shared_ptr<Data> get() { return ptr; }
184  bool is_template() const;
185 
186 private:
187  std::shared_ptr<Data> ptr;
188 };
189 
190 class data_map
191 {
192 public:
193  class key_error : public std::runtime_error
194  {
195  public:
196  key_error(const std::string &msg)
197  : std::runtime_error(msg)
198  {
199  }
200  };
201 
202  data_map()
203  : data()
204  , parent(nullptr)
205  {
206  }
207  data_ptr &operator[](const std::string &key);
208  bool empty();
209  bool has(const std::string &key);
210  data_ptr &parse_path(const std::string &key, bool create = false);
211  void set_parent(data_map *p) { parent = p; }
212 
213 private:
214  std::unordered_map<std::string, data_ptr> data;
215  data_map *parent;
216 
217  friend class DataMap;
218 };
219 
220 class DataMap : public Data
221 {
222  data_map m_items;
223 
224 public:
225  DataMap(const data_map &items)
226  : m_items(items)
227  {
228  }
229  DataMap(data_map &&items)
230  : m_items(std::move(items))
231  {
232  }
233  data_map &getmap();
234  bool empty();
235  void dump(int indent = 0);
236 };
237 
238 template <>
239 void data_ptr::operator=(const bool &data);
240 template <>
241 void data_ptr::operator=(const int &data);
242 template <>
243 void data_ptr::operator=(const unsigned int &data);
244 template <>
245 void data_ptr::operator=(const std::string &data);
246 template <>
247 void data_ptr::operator=(const data_map &data);
248 template <>
249 void data_ptr::operator=(const data_list &data);
250 template <typename T>
251 void data_ptr::operator=(const T &data)
252 {
253  this->operator=(boost::lexical_cast<std::string>(data));
254 }
255 
256 // Custom exception class for library errors
257 class TemplateException : public std::exception
258 {
259  uint32_t m_line;
260  std::string m_reason;
261 
262 public:
263  TemplateException(std::string reason)
264  : std::exception()
265  , m_line(0)
266  , m_reason(reason)
267  {
268  }
269  TemplateException(size_t line, std::string reason);
270  TemplateException(const TemplateException &other) = default;
271  TemplateException &operator=(const TemplateException &other) = default;
272  virtual ~TemplateException() = default;
273 
274  void set_reason(std::string reason) { m_reason = reason; }
275  void set_line_if_missing(size_t line);
276 
277  virtual const char *what() const NOEXCEPT NOTHROW { return m_reason.c_str(); }
278 };
279 
280 // convenience functions for making data objects
281 inline data_ptr make_data(bool val)
282 {
283  return data_ptr(new DataBool(val));
284 }
285 inline data_ptr make_data(int val)
286 {
287  return data_ptr(new DataInt(val));
288 }
289 inline data_ptr make_data(unsigned int val)
290 {
291  return data_ptr(new DataInt(val));
292 }
293 inline data_ptr make_data(std::string &val)
294 {
295  return data_ptr(new DataValue(val));
296 }
297 inline data_ptr make_data(std::string &&val)
298 {
299  return data_ptr(new DataValue(val));
300 }
301 inline data_ptr make_data(data_list &val)
302 {
303  return data_ptr(new DataList(val));
304 }
305 inline data_ptr make_data(data_list &&val)
306 {
307  return data_ptr(new DataList(val));
308 }
309 inline data_ptr make_data(data_map &val)
310 {
311  return data_ptr(new DataMap(val));
312 }
313 inline data_ptr make_data(data_map &&val)
314 {
315  return data_ptr(new DataMap(val));
316 }
317 template <typename T>
318 data_ptr make_data(const T &val)
319 {
320  return data_ptr(boost::lexical_cast<std::string>(val));
321 }
322 
323 void dump_data(data_ptr data);
324 
325 namespace impl {
326 
327 // node classes
328 class Node;
329 typedef std::shared_ptr<Node> node_ptr;
330 typedef std::vector<node_ptr> node_vector;
331 
332 } // namespace impl
333 
334 // List of param names.
335 typedef std::vector<std::string> string_vector;
336 
337 class DataTemplate : public Data
338 {
339  impl::node_vector m_tree;
340  string_vector m_params;
341 
342 public:
343  DataTemplate(const std::string &templateText);
344  DataTemplate(const impl::node_vector &tree)
345  : m_tree(tree)
346  {
347  }
348  DataTemplate(impl::node_vector &&tree)
349  : m_tree(std::move(tree))
350  {
351  }
352  virtual std::string getvalue();
353  virtual bool empty();
354  std::string eval(data_map &data, data_list *param_values = nullptr);
355  void eval(std::ostream &stream, data_map &data, data_list *param_values = nullptr);
356  string_vector &params() { return m_params; }
357  void dump(int indent = 0);
358 };
359 
360 inline data_ptr make_template(const std::string &templateText, const string_vector *param_names = nullptr)
361 {
362  DataTemplate *t = new DataTemplate(templateText);
363  if (param_names)
364  {
365  t->params() = *param_names;
366  }
367  return data_ptr(t);
368 }
369 
370 // The big daddy. Pass in the template and data,
371 // and get out a completed doc.
372 void parse(std::ostream &stream, const std::string &templ_text, data_map &data);
373 std::string parse(const std::string &templ_text, data_map &data);
374 }
Definition: cpptempl.h:111
Definition: cpptempl.h:337
Definition: cpptempl.h:148
Definition: cpptempl.h:257
Definition: cpptempl.h:193
Definition: cpptempl.h:65
Definition: cpptempl.h:92
Definition: cpptempl.h:130
Definition: cpptempl.h:220
Definition: cpptempl.h:190
Definition: cpptempl.h:77
Definition: cpptempl.h:54