OnixS C++ Eurex T7 Market and Reference Data Interface (EMDI, RDI, EOBI) Handlers  8.1.0
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 namespace OnixS {
27 namespace Util {
28 
29 // std::pair formatting
30 template<typename TFirst, typename TSecond>
31 inline TextBuilder & operator<<(TextBuilder & tb, const std::pair<TFirst, TSecond> & obj) {
32  return tb << obj.first << ':' << obj.second;
33 }
34 
35 // Wrapper for join collection items using delimiter during formatting
36 template<class Container>
37 struct JoinWrapper
38 {
39  const Container & container;
40  const char * const delimiter;
41 
42  explicit
43  JoinWrapper(const Container & ctnr, const char * delim = "\n")
44  : container(ctnr)
45  , delimiter(delim)
46  {
47  if(delimiter == nullptr)
48  throw std::invalid_argument("JoinWrapper::ctor, delimiter can not be nullptr");
49  }
50 };
51 
52 // creates JoinWrapper
53 template<class Container>
54 inline
55 JoinWrapper<Container> join(const Container & container, const char * delimiter)
56 {
58 }
59 
60 // Joins members of wrapped container using delimiters
61 template<class Container>
62 inline
63 TextBuilder & operator<<(TextBuilder & tb, const JoinWrapper<Container> & wrapper)
64 {
65  const char * const delimiter = wrapper.delimiter;
66  typename Container::const_iterator it = wrapper.container.begin(), e = wrapper.container.end();
67 
68  if(it != e) {
69  tb << *it;
70  ++it;
71  for(; it != e; ++it)
72  tb << delimiter << *it ;
73  }
74  return tb;
75 }
76 
77 }} // ::OnixS::Util
78 
79 namespace OnixS {
80 namespace Eurex {
81 namespace MarketData {
82 
83 using Util::TextBuilder;
84 
85 // Effective formatting output to std::ostream using TextBuilder
86 template<typename TObj>
87 std::ostream & TextBuilderToStdStream(std::ostream & stream, const TObj & obj);
88 
89 // Effective formatting output to std::ostream using TextBuilder
90 template<typename TObj>
91 std::ostream & TextBuilderToStdStream(std::ostream & stream, const TObj * obj);
92 
93 /////////////////////////////////////////////////////////////////////////////////////////
94 // Inline implementation
95 
96 template<typename TObj>
97 inline
98 std::ostream & TextBuilderToStdStream(std::ostream & stream, const TObj & obj) {
99  TextBuilder tb;
100  return stream << (tb << obj).toString();
101 }
102 
103 template<typename TObj>
104 inline
105 std::ostream & TextBuilderToStdStream(std::ostream & stream, const TObj * obj) {
106  TextBuilder tb;
107  return stream << (tb << obj).toString();
108 }
109 
110 }}}
111 
std::ostream & TextBuilderToStdStream(std::ostream &stream, const TObj *obj)
JoinWrapper(const Container &ctnr, const char *delim="\n")
Util::TextBuilder TextBuilder
Definition: Formatting.h:43
const char *const delimiter
Definition: Defines.h:30
JoinWrapper< Container > join(const Container &container, const char *delimiter)
const Container & container