1// Copyright 2014 The Kyua Authors.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#if !defined(UTILS_FORMAT_CONTAINERS_IPP)
30#define UTILS_FORMAT_CONTAINERS_IPP
31
32#include "utils/format/containers.hpp"
33
34#include <ostream>
35
36
37/// Injects the object into a stream.
38///
39/// \param output The stream into which to inject the object.
40/// \param object The object to format.
41///
42/// \return The output stream.
43template< typename K, typename V >
44std::ostream&
45std::operator<<(std::ostream& output, const std::map< K, V >& object)
46{
47    output << "map(";
48    typename std::map< K, V >::size_type counter = 0;
49    for (typename std::map< K, V >::const_iterator iter = object.begin();
50         iter != object.end(); ++iter, ++counter) {
51        if (counter != 0)
52            output << ", ";
53        output << (*iter).first << "=" << (*iter).second;
54    }
55    output << ")";
56    return output;
57}
58
59
60/// Injects the object into a stream.
61///
62/// \param output The stream into which to inject the object.
63/// \param object The object to format.
64///
65/// \return The output stream.
66template< typename T1, typename T2 >
67std::ostream&
68std::operator<<(std::ostream& output, const std::pair< T1, T2 >& object)
69{
70    output << "pair(" << object.first << ", " << object.second << ")";
71    return output;
72}
73
74
75/// Injects the object into a stream.
76///
77/// \param output The stream into which to inject the object.
78/// \param object The object to format.
79///
80/// \return The output stream.
81template< typename T >
82std::ostream&
83std::operator<<(std::ostream& output, const std::shared_ptr< T > object)
84{
85    if (object.get() == NULL) {
86        output << "<NULL>";
87    } else {
88        output << *object;
89    }
90    return output;
91}
92
93
94/// Injects the object into a stream.
95///
96/// \param output The stream into which to inject the object.
97/// \param object The object to format.
98///
99/// \return The output stream.
100template< typename T >
101std::ostream&
102std::operator<<(std::ostream& output, const std::set< T >& object)
103{
104    output << "set(";
105    typename std::set< T >::size_type counter = 0;
106    for (typename std::set< T >::const_iterator iter = object.begin();
107         iter != object.end(); ++iter, ++counter) {
108        if (counter != 0)
109            output << ", ";
110        output << (*iter);
111    }
112    output << ")";
113    return output;
114}
115
116
117/// Injects the object into a stream.
118///
119/// \param output The stream into which to inject the object.
120/// \param object The object to format.
121///
122/// \return The output stream.
123template< typename T >
124std::ostream&
125std::operator<<(std::ostream& output, const std::vector< T >& object)
126{
127    output << "[";
128    for (typename std::vector< T >::size_type i = 0; i < object.size(); ++i) {
129        if (i != 0)
130            output << ", ";
131        output << object[i];
132    }
133    output << "]";
134    return output;
135}
136
137
138#endif  // !defined(UTILS_FORMAT_CONTAINERS_IPP)
139