Remove building with NOCRYPTO option
[minix3.git] / external / bsd / kyua-cli / dist / utils / format / formatter.hpp
blob7b9483d12d0166a859a4d3dd2ede6f29243e7f29
1 // Copyright 2010 Google Inc.
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.
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.
29 /// \file utils/format/formatter.hpp
30 /// Provides the definition of the utils::format::formatter class.
31 ///
32 /// The utils::format::formatter class is a poor man's replacement for the
33 /// Boost.Format library, as it is much simpler and has less dependencies.
34 ///
35 /// Be aware that the formatting supported by this module is NOT compatible
36 /// with printf(3) nor with Boost.Format. The general syntax for a
37 /// placeholder in a formatting string is:
38 ///
39 /// %[0][width][.precision]s
40 ///
41 /// In particular, note that the only valid formatting specifier is %s: the
42 /// library deduces what to print based on the type of the variable passed
43 /// in, not based on what the format string says. Also, note that the only
44 /// valid padding character is 0.
46 #if !defined(UTILS_FORMAT_FORMATTER_HPP)
47 #define UTILS_FORMAT_FORMATTER_HPP
49 #include <sstream>
50 #include <string>
52 namespace utils {
53 namespace format {
56 /// Mechanism to format strings similar to printf.
57 ///
58 /// A formatter always maintains the original format string but also holds a
59 /// partial expansion. The partial expansion is immutable in the context of a
60 /// formatter instance, but calls to operator% return new formatter objects with
61 /// one less formatting placeholder.
62 ///
63 /// In general, one can format a string in the following manner:
64 ///
65 /// \code
66 /// const std::string s = (formatter("%s %s") % "foo" % 5).str();
67 /// \endcode
68 ///
69 /// which, following the explanation above, would correspond to:
70 ///
71 /// \code
72 /// const formatter f1("%s %s");
73 /// const formatter f2 = f1 % "foo";
74 /// const formatter f3 = f2 % 5;
75 /// const std::string s = f3.str();
76 /// \endcode
77 class formatter {
78 /// The original format string provided by the user.
79 std::string _format;
81 /// The current "expansion" of the format string.
82 ///
83 /// This field gets updated on every call to operator%() to have one less
84 /// formatting placeholder.
85 std::string _expansion;
87 /// The position of _expansion from which to scan for placeholders.
88 std::string::size_type _last_pos;
90 /// The position of the first placeholder in the current expansion.
91 std::string::size_type _placeholder_pos;
93 /// The first placeholder in the current expansion.
94 std::string _placeholder;
96 /// Stream used to format any possible argument supplied by operator%().
97 std::ostringstream* _oss;
99 formatter replace(const std::string&) const;
101 void init(void);
102 formatter(const std::string&, const std::string&,
103 const std::string::size_type);
105 public:
106 explicit formatter(const std::string&);
107 ~formatter(void);
109 const std::string& str(void) const;
110 operator const std::string&(void) const;
112 template< typename Type > formatter operator%(const Type&) const;
113 formatter operator%(const bool&) const;
117 } // namespace format
118 } // namespace utils
121 #endif // !defined(UTILS_FORMAT_FORMATTER_HPP)