OnixS C++ SGX Titan OUCH Trading Handler  1.2.0
API documentation
Defines.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <string>
8 #include <ostream>
9 
11 
12 
13 /// String helpers
14 struct ONIXS_SGXTITAN_OUCH_API RightPaddedStringTraits
15 {
16  static void fillBufferAndAddSpaces(Byte* buffer, size_t size, Byte filler, const char* value);
17  static void fillBufferAndAddSpaces(Byte* buffer, size_t size, Byte filler, Byte value);
18 };
19 
20 struct ONIXS_SGXTITAN_OUCH_API LeftPaddedStringTraits
21 {
22  static void fillBufferAndAddSpaces(Byte* buffer, size_t size, Byte filler, const char* value);
23  static void fillBufferAndAddSpaces(Byte* buffer, size_t size, Byte filler, Byte value);
24 };
25 
26 /// ASCII encoded, right-padded with filler.
27 template
28 <
29  size_t N,
30  Byte FILLER = 0,
31  typename Traits = RightPaddedStringTraits
32 >
33 struct String
34 {
35  Byte data[N];
36 
37  ///
38  size_t size() const
39  {
40  return N;
41  }
42 
44  {
45  Traits::fillBufferAndAddSpaces(data, N, FILLER, "");
46  }
47 
48  explicit
49  String(const std::string& value)
50  {
51  Traits::fillBufferAndAddSpaces(data, N, FILLER, value.c_str());
52  }
53 
54  explicit
55  String(const char* value)
56  {
57  Traits::fillBufferAndAddSpaces(data, N, FILLER, value);
58  }
59 
60  explicit
61  String(Byte value)
62  {
63  Traits::fillBufferAndAddSpaces(data, N, FILLER, value);
64  }
65 
66  /// Returns string representation.
67  std::string toString () const
68  {
69  return std::string((const char*) data, strnlen((const char*) data, N));
70  }
71 
72  /// Constructs StrRef instance.
73  StrRef toStrRef() const
74  {
75  return StrRef((const char*) data, strnlen((const char*) data, N));
76  }
77 
78  /// Sets value from std::string
79  String<N, FILLER, Traits>& operator=(const std::string& value)
80  {
81  Traits::fillBufferAndAddSpaces(data, N, FILLER, value.c_str());
82 
83  return *this;
84  }
85 
86  /// Compares with another instance.
87  bool operator== (const String<N>& other) const
88  {
89  return (0 == memcmp(data, other.data, N));
90  }
91 
92  /// Compares with another instance.
93  bool operator!= (const String<N>& other) const
94  {
95  return (0 != memcmp(data, other.data, N));
96  }
97 };
98 
99 template<size_t N>
100 bool operator==(const String<N>& ref, const std::string& str)
101 {
102  return ref == String<N>(str);
103 }
104 
105 template<size_t N>
106 bool operator!=(const String<N>& ref, const std::string& str)
107 {
108  return ref != String<N>(str);
109 }
110 
111 template<size_t N>
112 bool operator==(const String<N>& ref, Byte ch)
113 {
114  return ref.size() == 1 && ref == String<N>(ch);
115 }
116 
117 template<size_t N>
118 bool operator!=(const String<N>& ref, Byte ch)
119 {
120  return ref.size() != 1 || ref != String<N>(ch);
121 }
122 
123 template<size_t N>
124 std::ostream& operator<<(std::ostream& stream, const String<N>& value)
125 {
126  stream << '\'' << value.toString() << '\'';
127  return stream;
128 }
129 
130 /// Serializes given string into a string.
131 template<size_t N>
132 void toStr(std::string& str, const String<N>& value)
133 {
134  str.append((const char*) value.data, strnlen((const char*) value.data, N));
135 }
136 
137 /// Serializes given string into a string.
138 template<size_t N>
139 inline std::string toStr(const String<N>& value)
140 {
141  std::string str;
142 
143  toStr(str, value);
144 
145  return str;
146 }
147 
148 }}}}
String(const std::string &value)
Definition: Defines.h:49
UInt8 Byte
Alias for Byte.
Definition: Memory.h:30
std::string toString() const
Returns string representation.
Definition: Defines.h:67
ASCII encoded, right-padded with filler.
Definition: Defines.h:33
StrRef toStrRef() const
Constructs StrRef instance.
Definition: Defines.h:73
Provides efficient way of accessing text-based FIX field values.
Definition: String.h:41
#define ONIXS_SGXTITAN_OUCH_NAMESPACE_BEGIN
Definition: Bootstrap.h:27
bool operator!=(const String< N > &ref, Byte ch)
Definition: Defines.h:118
String< N, FILLER, Traits > & operator=(const std::string &value)
Sets value from std::string.
Definition: Defines.h:79
std::string toStr(const String< N > &value)
Serializes given string into a string.
Definition: Defines.h:139
bool operator==(const String< N > &ref, Byte ch)
Definition: Defines.h:112