OnixS C++ SGX Titan OUCH Trading Handler  1.2.0
API documentation
SerializationUtils.h
Go to the documentation of this file.
1 // Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2 //
3 // This software owned by Onix Solutions Limited [OnixS] and is
4 // protected by copyright law and international copyright treaties.
5 //
6 // Access to and use of the software is governed by the terms of the applicable
7 // OnixS Software Services Agreement (the Agreement) and Customer end user license
8 // agreements granting a non-assignable, non-transferable and non-exclusive license
9 // to use the software for it's own data processing purposes under the terms defined
10 // in the Agreement.
11 //
12 // Except as otherwise granted within the terms of the Agreement, copying or
13 // reproduction of any part of this source code or associated reference material
14 // to any other location for further reproduction or redistribution, and any
15 // amendments to this copyright notice, are expressly prohibited.
16 //
17 // Any reproduction or redistribution for sale or hiring of the Software not in
18 // accordance with the terms of the Agreement is a violation of copyright law.
19 //
20 
21 #pragma once
22 
23 #include <stdexcept>
24 #include <sstream>
25 
27 
29 
30 // Boolean values serialization.
31 
32 inline
33 void
35  std::string& str,
36  bool value)
37 {
38  str += (value ? "true" : "false");
39 }
40 
41 // Container serialization.
42 
43 template
44 <
45  class Item
46 >
47 inline
48 void
50  std::string& str,
51  const Item& item)
52 {
53  toStr(str, item);
54 }
55 
56 inline
57 void
59  std::string& str,
60  const std::string& item)
61 {
62  str += item;
63 }
64 
65 template
66 <
67  class Iterator
68 >
69 inline
70 void
72  std::string& str,
73  Iterator containerBegin,
74  Iterator containerEnd,
75  const std::string& delimiter = ",")
76 {
77  bool addDelimiter = false;
78 
79  while (
80  containerBegin != containerEnd)
81  {
82  if (addDelimiter)
83  str += delimiter;
84 
85  addDelimiter = true;
86 
87  itemToStr(
88  str,
89  *containerBegin);
90 
91  ++containerBegin;
92  }
93 }
94 
95 // Enumeration type serialization.
96 
97 template
98 <
99  class Constant
100 >
101 void
103  const Char* enumeration,
104  Constant /*constant*/)
105 {
106  std::string reason;
107 
108  reason +=
109  "Cannot convert constant to/from text "
110  "because it's out of a valid range "
111  "[enumType=";
112 
113  reason += enumeration;
114  reason += ",constant=";
115 
116  //toStr(reason, constant);
117 
118  reason += "]. ";
119 
120  throw std::out_of_range(reason);
121 }
122 
123 }}}}
124 
125 #define \
126  ONIXS_SGXTITAN_OUCH_ENUM_TO_STR_BEGIN(Enumeration) \
127  void \
128  toStr( \
129  std::string& str, \
130  Enumeration::Enum value) \
131  { \
132  typedef \
133  Enumeration \
134  Values; \
135  \
136  switch (value) \
137  {
138 
139 #define \
140  ONIXS_SGXTITAN_OUCH_ENUM_TO_STR_ENTRY(Value) \
141  case Values::Value: \
142  str += #Value ; \
143  break;
144 
145 #define \
146  ONIXS_SGXTITAN_OUCH_ENUM_TO_STR_CUSTOM_ENTRY(Value, Str) \
147  case Values::Value: \
148  str += (Str); \
149  break;
150 
151 #define \
152  ONIXS_SGXTITAN_OUCH_ENUM_TO_STR_END(Enumeration) \
153  default: \
154  std::stringstream ss;\
155  ss << value;\
156  str += "Undefined("; \
157  str += ss.str(); \
158  str += ")" ; \
159  } \
160  }
161 
162 #define \
163  ONIXS_SGXTITAN_OUCH_STR_TO_ENUM_BEGIN(Enumeration) \
164  Enumeration::Enum \
165  strTo##Enumeration( \
166  const std::string& str) \
167  { \
168  typedef \
169  Enumeration \
170  Values;
171 
172 #define \
173  ONIXS_SGXTITAN_OUCH_STR_TO_ENUM_ENTRY(Value) \
174  if (str == #Value) \
175  return Values::Value;
176 
177 #define \
178  ONIXS_SGXTITAN_OUCH_STR_TO_ENUM_CUSTOM_ENTRY(Value, Str) \
179  if (str == (Str)) \
180  return Values::Value;
181 
182 #define \
183  ONIXS_SGXTITAN_OUCH_STR_TO_ENUM_END(Enumeration) \
184  throwOutOfEnumRange( \
185  #Enumeration, \
186  str); \
187  \
188  return \
189  static_cast \
190  <Enumeration::Enum> \
191  (0); \
192  }
193 
194 #define \
195  ONIXS_SGXTITAN_OUCH_FROM_STR_BEGIN(Enumeration) \
196  bool \
197  fromStr( \
198  Enumeration::Enum& value,\
199  const Char* str, \
200  size_t size) \
201  { \
202  typedef \
203  Enumeration \
204  Values; \
205  \
206  const StrRef strRef(str, size);
207 
208 #define \
209  ONIXS_SGXTITAN_OUCH_FROM_STR_ENTRY(Value) \
210  if (strRef == #Value) \
211  { \
212  value = Values::Value; \
213  \
214  return true; \
215  }
216 
217 #define \
218  ONIXS_SGXTITAN_OUCH_FROM_STR_CUSTOM_ENTRY(Value, Str) \
219  if (strRef == (Str)) \
220  { \
221  value = Values::Value; \
222  \
223  return true; \
224  }
225 
226 #define \
227  ONIXS_SGXTITAN_OUCH_FROM_STR_END(Enumeration) \
228  return false; \
229  }
230 
231 #define \
232  ONIXS_SGXTITAN_OUCH_DECLARE_T0_STR_FUNCTIONS(Type) \
233  void toStr(std::string&, Type);\
234  inline std::string toStr(Type value)\
235  {\
236  std::string str;\
237  toStr(str, value);\
238  return str;\
239  }
void itemToStr(std::string &str, const std::string &item)
char Char
Character type alias.
Definition: String.h:38
void containerToStr(std::string &str, Iterator containerBegin, Iterator containerEnd, const std::string &delimiter=",")
void throwOutOfEnumRange(const Char *enumeration, Constant)
void boolToStr(std::string &str, bool value)
#define ONIXS_SGXTITAN_OUCH_NAMESPACE_BEGIN
Definition: Bootstrap.h:27
ONIXS_SGXTITAN_OUCH_API void toStr(std::string &, OutboundMessageTypes::Enum)
Appends string presentation of object.