OnixS C++ SGX Titan ITCH Market Data Handler  1.2.2
API documentation
Formatting.Helpers.h
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19 
20 // Formatting helpers
21 #pragma once
22 
23 #include <stdexcept>
24 #include <util/TextBuilder.h>
25 
26 #include "BookImpl.h"
27 #include "NamespaceHelper.h"
28 
29 namespace OnixS {
30 namespace Util {
31 
32 // std::pair formatting
33 template<typename TFirst, typename TSecond>
34 inline TextBuilder & operator<<(TextBuilder & tb, const std::pair<TFirst, TSecond> & obj) {
35  return tb << obj.first << ':' << obj.second;
36 }
37 
38 // Wrapper for join collection items using delimiter during formatting
39 template<class Container>
40 struct JoinWrapper /*: public boost::noncopyable*/
41 {
42  const Container & container;
43  const char * const delimiter;
44 
45  explicit
46  JoinWrapper(const Container & ctnr, const char * delim = "\n")
47  : container(ctnr)
48  , delimiter(delim)
49  {
50  if(delimiter == nullptr)
51  throw std::invalid_argument("JoinWrapper::ctor, delimiter can not be NULL");
52  }
53  JoinWrapper(const JoinWrapper& other)
54  : container(other.container), delimiter(other.delimiter){}
55 
56 private:
57  JoinWrapper& operator=(const JoinWrapper&);
58 };
59 
60 // creates JoinWrapper
61 template<class Container>
62 inline
63 JoinWrapper<Container> join(const Container & container, const char * delimiter)
64 {
66 }
67 
68 // Joins members of wrapped container using delimiters
69 template<class Container>
70 inline
71 TextBuilder & operator<<(TextBuilder & tb, const JoinWrapper<Container> & wrapper)
72 {
73  const char * const delimiter = wrapper.delimiter;
74  typename Container::const_iterator it = wrapper.container.begin(), e = wrapper.container.end();
75 
76  if(it != e) {
77  tb << *it;
78  ++it;
79  for(; it != e; ++it)
80  tb << delimiter << *it ;
81  }
82  return tb;
83 }
84 
85 inline
86 TextBuilder & operator<<(TextBuilder & tb, const HANDLER_NAMESPACE::OrderBookInternal& book)
87 {
88  tb << book.toFormattedString();
89  return tb;
90 }
91 
92 }} // ::OnixS::Util
93 
94 ONIXS_HANDLER_NAMESPACE_BEGIN
95 
96 using Util::TextBuilder;
97 
98 // Effective formatting output to std::ostream using TextBuilder
99 template<typename TObj>
100 std::ostream & TextBuilderToStdStream(std::ostream & stream, const TObj & obj);
101 
102 // Effective formatting output to std::ostream using TextBuilder
103 template<typename TObj>
104 std::ostream & TextBuilderToStdStream(std::ostream & stream, const TObj * obj);
105 
106 /////////////////////////////////////////////////////////////////////////////////////////
107 // Inline implementation
108 
109 template<typename TObj>
110 inline
111 std::ostream & TextBuilderToStdStream(std::ostream & stream, const TObj & obj) {
112  TextBuilder tb;
113  return stream << (tb << obj).toString();
114 }
115 
116 template<typename TObj>
117 inline
118 std::ostream & TextBuilderToStdStream(std::ostream & stream, const TObj * obj) {
119  TextBuilder tb;
120  return stream << (tb << obj).toString();
121 }
122 
123 ONIXS_HANDLER_NAMESPACE_END
124 
JoinWrapper(const Container &ctnr, const char *delim="\n")
const char *const delimiter
JoinWrapper(const JoinWrapper &other)
TextBuilder & operator<<(TextBuilder &tb, const std::pair< TFirst, TSecond > &obj)
JoinWrapper< Container > join(const Container &container, const char *delimiter)
std::ostream & TextBuilderToStdStream(std::ostream &stream, const TObj *obj)
const Container & container