trader  v0.1a
A framework to build trading applications
helpers.h
1 #pragma once
2 
3 #include "config.h"
4 #include "endpoint.h"
5 #include "fileoutputstream.h"
6 
7 namespace trader
8 {
9 
10  using Poco::DirectoryIterator;
11  using Poco::File;
12  using Poco::Path;
13  using Poco::Util::AbstractConfiguration;
14  using Poco::Util::Application;
15  using Poco::Util::HelpFormatter;
16  using Poco::Util::Option;
17  using Poco::Util::OptionCallback;
18  using Poco::Util::OptionSet;
19  using namespace Poco;
20  using namespace std;
21 
22  inline void getAPIName(string name, const string &apiFile, string &apiName)
23  {
24  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
25  if (name.length())
26  std::transform(name.begin(), name.begin() + 1, name.begin(), ::toupper);
27  Path p(apiFile);
28  string fileName = p.getBaseName();
29  std::string::size_type pos = fileName.find('.');
30  if (pos != std::string::npos)
31  apiName = fileName.substr(0, pos) + name;
32  else
33  apiName = fileName + name;
34  std::transform(apiName.begin(), apiName.begin() + 1, apiName.begin(), ::toupper);
35  }
36 
38  {
39  public:
40  ScopedNamespace(ApiFileOutputStream &stream, string &nameSpace)
41  : _nameSpace(nameSpace)
42  , _stream(stream)
43  {
44  if (!_nameSpace.empty())
45  {
46  stream << "namespace " << _nameSpace << " {" << endl;
47  stream << endl;
48  ++stream;
49  }
50  }
51 
53  {
54  if (!_nameSpace.empty())
55  {
56  --_stream;
57  _stream << "} //" << _nameSpace << endl;
58  }
59  }
60 
61  private:
62  string _nameSpace;
63  ApiFileOutputStream &_stream;
64  };
65 
66  template < class StreamType = ApiFileOutputStream > class ScopedStream
67  {
68  public:
69  enum IndentType
70  {
71  INC = 0,
72  DEC
73  };
74 
75  ScopedStream(StreamType &stream, bool brackets = true, IndentType indent = INC)
76  : _indent(indent)
77  , _stream(stream)
78  , _brackets(brackets)
79  {
80  if (brackets)
81  stream << "{" << endl;
82  if (indent == INC)
83  ++stream;
84  else
85  --stream;
86  }
87 
88  ~ScopedStream()
89  {
90  if (_indent == INC)
91  --_stream;
92  else
93  ++_stream;
94  if (_brackets)
95  _stream << "}" << endl;
96  }
97 
98  IndentType _indent;
99  StreamType &_stream;
100  bool _brackets;
101  };
102 
103  template < size_t N, class StreamType = ApiFileOutputStream > class ScopedClass
104  {
105  array< const char *, N > parentClasses;
106 
107  public:
108  template < typename... T, typename enable_if< sizeof...(T) == N, int >::type = 0 >
109  ScopedClass(StreamType &stream, const string &className, T... args)
110  : parentClasses{{args}...}
111  , _stream(stream)
112  , _className(className)
113  {
114  stream << "class " << className;
115  bool first = true;
116  for (auto &parentClass : parentClasses)
117  {
118  if (!first)
119  stream << ", ";
120  else
121  stream << " : ";
122  stream << " public " << parentClass;
123  first = false;
124  }
125  stream << " {";
126  stream << endl;
127  ++stream;
128 
129  // public section
130  {
132  stream << "public:" << endl;
133  }
134 
135  stream << endl;
136  }
137 
138  ~ScopedClass()
139  {
140  if (!_className.empty())
141  {
142  --_stream;
143  _stream << "}; //" << _className << endl;
144  _stream << endl;
145  }
146  }
147 
148  private:
149  StreamType &_stream;
150  string _className;
151  };
152 
153  template < size_t N, class StreamType = ApiFileOutputStream > class ScopedStruct
154  {
155  array< const char *, N > parentClasses;
156 
157  public:
158  template < typename... T, typename enable_if< sizeof...(T) == N, int >::type = 0 >
159  ScopedStruct(StreamType &stream, const string &className, T... args)
160  : parentClasses{args...}
161  , _stream(stream)
162  , _className(className)
163  {
164  stream << "struct " << className;
165  bool first = true;
166  for (auto &parentClass : parentClasses)
167  {
168  if (!first)
169  stream << ", ";
170  else
171  stream << " : ";
172  stream << " public " << parentClass;
173  first = false;
174  }
175  stream << " {";
176  stream << endl;
177  ++stream;
178  stream << endl;
179  }
180 
181  ~ScopedStruct()
182  {
183  if (!_className.empty())
184  {
185  --_stream;
186  _stream << trader::endl;
187  _stream << "}" << trader::cendl;
188  _stream << trader::endl;
189  }
190  }
191 
192  private:
193  StreamType &_stream;
194  string _className;
195  };
196 
197  inline void startHeader(ApiFileOutputStream &stream, Int32 num, ...)
198  {
199  stream << "#pragma once " << endl;
200  va_list arguments;
201  va_start(arguments, num);
202  for (Int32 cnt = 0; cnt < num; cnt++)
203  {
204  const char *fileName = va_arg(arguments, const char *);
205  stream << "#include \"" << fileName << "\"" << endl;
206  }
207  va_end(arguments);
208  stream << endl;
209  stream << endl;
210  }
211 
212  inline void startCpp(ApiFileOutputStream &stream, Int32 num, ...)
213  {
214  stream << "#include \"stdafx.h\" " << endl;
215  va_list arguments;
216  va_start(arguments, num);
217  for (Int32 cnt = 0; cnt < num; cnt++)
218  {
219  const char *fileName = va_arg(arguments, const char *);
220  stream << "#include \"" << fileName << "\"" << endl;
221  }
222  va_end(arguments);
223  stream << endl;
224  }
225 
226  inline void construct(ApiFileOutputStream &cpp, const string &className, Int32 num, ...)
227  {
228  cpp << className << "::" << className << "() ";
229  {
231  va_list arguments;
232  va_start(arguments, num);
233  poco_assert(num % 2 == 0);
234  for (Int32 cnt = 0; cnt < num; cnt += 2)
235  {
236  const char *str = va_arg(arguments, const char *);
237  cpp << str << " = \"" << va_arg(arguments, const char *) << "\"" << cendl;
238  }
239  va_end(arguments);
240  }
241  cpp << endl;
242  cpp << className << "::~" << className << "() ";
243  {
245  }
246  cpp << endl;
247  }
248 
249  inline void construct_header(ApiFileOutputStream &stream, const string &className, Int32 num, ...)
250  {
251  stream << className << "(";
252  va_list arguments;
253  va_start(arguments, num);
254  poco_assert(num % 2 == 0);
255  for (Int32 cnt = 0; cnt < num; cnt += 2)
256  {
257  if (cnt > 0)
258  stream << ", ";
259  const char *str = va_arg(arguments, const char *);
260  stream << str << " " << va_arg(arguments, const char *);
261  }
262  stream << ")" << cendl;
263  va_end(arguments);
264  stream << endl;
265  stream << "~" << className << "()" << cendl;
266  stream << endl;
267  }
268 
269  inline void construct_ex(ApiFileOutputStream &cpp, const string &className, Int32 numParams, Int32 numInitializer,
270  Int32 numStatements, ...)
271  {
272  cpp << className << "::" << className << "(";
273  va_list arguments;
274  va_start(arguments, numStatements);
275  poco_assert(numParams % 2 == 0);
276  for (Int32 cnt = 0; cnt < numParams; cnt += 2)
277  {
278  if (cnt > 0)
279  cpp << ", ";
280  const char *str = va_arg(arguments, const char *);
281  cpp << str << " " << va_arg(arguments, const char *);
282  }
283  cpp << ") ";
284  {
286  poco_assert(numInitializer % 2 == 0);
287  for (Int32 cnt = 0; cnt < numInitializer; cnt += 2)
288  {
289  const char *str = va_arg(arguments, const char *);
290  cpp << str << " = " << va_arg(arguments, const char *) << trader::cendl;
291  }
292  for (Int32 cnt = 0; cnt < numStatements; cnt++)
293  {
294  cpp << va_arg(arguments, const char *) << trader::endl;
295  }
296  }
297  va_end(arguments);
298  cpp << endl;
299  cpp << className << "::~" << className << "() ";
300  {
302  }
303  cpp << endl;
304  }
305 
306  inline std::string &replace(std::string &str, const char *from, const char *to)
307  {
308  size_t start_pos = 0;
309  while ((start_pos = str.find(from, start_pos)) != std::string::npos)
310  {
311  str.replace(start_pos, strlen(from), to);
312  start_pos += strlen(to); // Handles case where 'to' is a substring of 'from'
313  }
314  return str;
315  }
316 } // namespace trader
317 
318 #ifdef _DEBUG
319 #define CODEGEN_DEBUG(x) x
320 #else
321 #define CODEGEN_DEBUG(x)
322 #endif
Definition: fileoutputstream.h:61
Definition: helpers.h:103
Definition: fileoutputstream.h:3
Definition: helpers.h:66
Definition: helpers.h:153
Definition: app.h:7
Definition: helpers.h:37