OnixS C++ SGX Titan ITCH Market Data Handler  1.2.2
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 
28 #include "NamespaceHelper.h"
29 
30 ONIXS_HANDLER_NAMESPACE_BEGIN
31 
32 // Boolean values serialization.
33 
34 inline
35 void
37  std::string& str,
38  bool value)
39 {
40  str += (value ? "true" : "false");
41 }
42 
43 // Container serialization.
44 
45 template
46 <
47  class Item
48 >
49 inline
50 void
52  std::string& str,
53  const Item& item)
54 {
55  toStr(str, item);
56 }
57 
58 inline
59 void
61  std::string& str,
62  const std::string& item)
63 {
64  str += item;
65 }
66 
67 template
68 <
69  class Iterator
70 >
71 inline
72 void
74  std::string& str,
75  Iterator containerBegin,
76  Iterator containerEnd,
77  const std::string& delimiter = ",")
78 {
79  bool addDelimiter = false;
80 
81  while (
82  containerBegin != containerEnd)
83  {
84  if (addDelimiter)
85  str += delimiter;
86 
87  addDelimiter = true;
88 
89  itemToStr(
90  str,
91  *containerBegin);
92 
93  ++containerBegin;
94  }
95 }
96 
97 // Enumeration type serialization.
98 
99 template
100 <
101  class Constant
102 >
103 void
105  const Char* enumeration,
106  Constant constant)
107 {
108  std::string reason;
109 
110  reason +=
111  "Cannot convert constant to/from text "
112  "because it's out of a valid range "
113  "[enumType=";
114 
115  reason += enumeration;
116  reason += ",constant=";
117 
118  toStr(reason, constant);
119 
120  reason += "]. ";
121 
122  throw std::out_of_range(reason);
123 }
124 
125 ONIXS_HANDLER_NAMESPACE_END
126 
127 #define \
128  ONIXS_ENUM_TO_STR_BEGIN(Enumeration) \
129  void \
130  toStr( \
131  std::string& str, \
132  Enumeration::Enum value) \
133  { \
134  typedef \
135  Enumeration \
136  Values; \
137  \
138  switch (value) \
139  {
140 
141 #define \
142  ONIXS_ENUM_TO_STR_ENTRY(Value) \
143  case Values::Value: \
144  str += #Value ; \
145  break;
146 
147 #define \
148  ONIXS_ENUM_TO_STR_CUSTOM_ENTRY(Value, Str) \
149  case Values::Value: \
150  str += Str; \
151  break;
152 
153 #define \
154  ONIXS_ENUM_TO_STR_END(Enumeration) \
155  default: \
156  std::stringstream ss;\
157  ss << value;\
158  str += "Undefined("; \
159  str += ss.str(); \
160  str += ")" ; \
161  } \
162  }
163 
164 #define \
165  ONIXS_STR_TO_ENUM_BEGIN(Enumeration) \
166  Enumeration::Enum \
167  strTo##Enumeration( \
168  const std::string& str) \
169  { \
170  typedef \
171  Enumeration \
172  Values;
173 
174 #define \
175  ONIXS_STR_TO_ENUM_ENTRY(Value) \
176  if (str == #Value) \
177  return Values::Value;
178 
179 #define \
180  ONIXS_STR_TO_ENUM_CUSTOM_ENTRY(Value, Str) \
181  if (str == Str) \
182  return Values::Value;
183 
184 #define \
185  ONIXS_STR_TO_ENUM_END(Enumeration) \
186  throwOutOfEnumRange( \
187  #Enumeration, \
188  str); \
189  \
190  return \
191  static_cast \
192  <Enumeration::Enum> \
193  (0); \
194  }
195 
196 #define \
197  ONIXS_FROM_STR_BEGIN(Enumeration) \
198  bool \
199  fromStr( \
200  Enumeration::Enum& value,\
201  const Char* str, \
202  size_t size) \
203  { \
204  typedef \
205  Enumeration \
206  Values; \
207  \
208  const StrRef strRef(str, size);
209 
210 #define \
211  ONIXS_FROM_STR_ENTRY(Value) \
212  if (strRef == #Value) \
213  { \
214  value = Values::Value; \
215  \
216  return true; \
217  }
218 
219 #define \
220  ONIXS_FROM_STR_CUSTOM_ENTRY(Value, Str) \
221  if (strRef == Str) \
222  { \
223  value = Values::Value; \
224  \
225  return true; \
226  }
227 
228 #define \
229  ONIXS_FROM_STR_END(Enumeration) \
230  return false; \
231  }
232 
233 #define \
234  ONIXS_DECLARE_T0_STR_FUNCTIONS(Type) \
235  void toStr(std::string&, Type);\
236  inline std::string toStr(Type value)\
237  {\
238  std::string str;\
239  toStr(str, value);\
240  return str;\
241  }
char Char
Character type alias.
Definition: String.h:37
void containerToStr(std::string &str, Iterator containerBegin, Iterator containerEnd, const std::string &delimiter=",")
void boolToStr(std::string &str, bool value)
void itemToStr(std::string &str, const std::string &item)
void throwOutOfEnumRange(const Char *enumeration, Constant constant)
ONIXS_SGXTITAN_ITCH_API void toStr(std::string &, MessageType::Enum)
Appends string presentation of object.