JayBeams  0.1
Another project to have fun coding.
fixed_string.hpp
Go to the documentation of this file.
1 #ifndef jb_fixed_string_hpp
2 #define jb_fixed_string_hpp
3 
4 #include <boost/functional/hash.hpp>
5 #include <boost/operators.hpp>
6 
7 #include <algorithm>
8 #include <cstring>
9 #include <ostream>
10 #include <type_traits>
11 
12 namespace jb {
13 
14 /**
15  * A helper type to define short (and fixed sized) string fields.
16  *
17  * Many market data protocols uses fields that are short strings, that
18  * is, fixed-length alpha numeric fields. While it is normally easy
19  * to treat these fields as character strings, the fields may not be
20  * NUL terminated. In fact, several protocols define such fields as
21  * left justified, padded with spaces.
22  *
23  * We want an in-memory representation that supports basic comparison
24  * operations, streaming to std::ostream, and conversion to
25  * std::string. We also want the C++ type to be a POD-type, so we can
26  * copy the message buffer directly into memory.
27  *
28  * @tparam wire_size_value the size of the field on the wire, in
29  * bytes.
30  */
31 template <std::size_t wire_size_value>
33  : public boost::totally_ordered<fixed_string<wire_size_value>>,
34  public boost::totally_ordered<
35  fixed_string<wire_size_value>, std::string> {
36 public:
37  /// The size of the field on the wire
38  constexpr static std::size_t wire_size = wire_size_value;
39 
40  /// Default constructor
41  fixed_string() = default;
42 
43  /// Constructor from a std::string
44  explicit fixed_string(std::string const& rhs) {
45  static_assert(
46  std::is_pod<fixed_string<wire_size>>::value,
47  "fixed_string must be a PODType.");
48  std::fill(buffer_, buffer_ + wire_size, ' ');
49  std::strncpy(buffer_, rhs.c_str(), std::min(wire_size, rhs.length()));
50  }
51 
52  /// Assingment from std::string
53  fixed_string& operator=(std::string const& rhs) {
54  std::fill(buffer_, buffer_ + wire_size, ' ');
55  std::strncpy(buffer_, rhs.c_str(), std::min(wire_size, rhs.length()));
56  return *this;
57  }
58 
59  /// Return a representation as a std::string
60  std::string str() const {
61  return std::string(buffer_, wire_size);
62  }
63 
64  //@{
65  /**
66  * @name Base comparison operators
67  */
68  /// compare vs another fixed_string
69  bool operator==(fixed_string const& rhs) const {
70  return std::strncmp(buffer_, rhs.buffer_, wire_size) == 0;
71  }
72 
73  /// compare vs another fixed_string
74  bool operator<(fixed_string const& rhs) const {
75  return std::strncmp(buffer_, rhs.buffer_, wire_size) < 0;
76  }
77 
78  /// compare vs a std::string
79  bool operator==(std::string const& rhs) const {
80  return std::strncmp(buffer_, rhs.c_str(), wire_size) == 0;
81  }
82 
83  /// compare vs a std::string
84  bool operator<(std::string const& rhs) const {
85  return std::strncmp(buffer_, rhs.c_str(), wire_size) < 0;
86  }
87  //@}
88 
89  //@{
90  /// @name default assignment and copy operators
91  fixed_string(fixed_string const& rhs) = default;
92  fixed_string(fixed_string&& rhs) = default;
93  fixed_string& operator=(fixed_string const& rhs) = default;
94  fixed_string& operator=(fixed_string&& rhs) = default;
95  //@}
96 
97 private:
98  /// The in-memory representation
100 };
101 
102 /// Streaming operator for jb::mktdata::fixed_string<>
103 template <std::size_t size>
104 std::ostream& operator<<(std::ostream& os, fixed_string<size> const& x) {
105  return os << x.str();
106 }
107 
108 /// Implement a hash function and integrate with boost::hash
109 template <std::size_t size>
110 std::size_t hash_value(fixed_string<size> const& x) {
111  auto tmp = x.str();
112  return boost::hash_range(tmp.begin(), tmp.end());
113 }
114 
115 /// Define the constexpr
116 template <std::size_t size>
117 constexpr std::size_t fixed_string<size>::wire_size;
118 
119 } // namespace jb
120 
121 #endif // jb_fixed_string_hpp
fixed_string & operator=(std::string const &rhs)
Assingment from std::string.
A helper type to define short (and fixed sized) string fields.
bool operator<(fixed_string const &rhs) const
compare vs another fixed_string
bool operator<(std::string const &rhs) const
compare vs a std::string
std::size_t hash_value(fixed_string< size > const &x)
Implement a hash function and integrate with boost::hash.
fixed_string(std::string const &rhs)
Constructor from a std::string.
char buffer_[wire_size]
The in-memory representation.
fixed_string()=default
Default constructor.
bool operator==(fixed_string const &rhs) const
compare vs another fixed_string
std::string str() const
Return a representation as a std::string.
static constexpr std::size_t wire_size
The size of the field on the wire.
bool operator==(std::string const &rhs) const
compare vs a std::string
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7