OpenVDB: Formats.h Source File

OpenVDB 3.1.0

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

Formats.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2015 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 //
36
37
38 #ifndef OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
39 #define OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
40
41 #include <iosfwd>
42 #include <sstream>
43 #include <string>
44 #include <openvdb/version.h>
45 #include <openvdb/Platform.h>
46
47
48 namespace openvdb {
49 OPENVDB_USE_VERSION_NAMESPACE
50 namespace OPENVDB_VERSION_NAME {
51 namespace util {
52
62 OPENVDB_API int
63 printBytes(std::ostream& os, uint64_t bytes,
64 const std::string& head = "",
65 const std::string& tail = "\n",
66 bool exact = false, int width = 8, int precision = 3);
67
77 OPENVDB_API int
78 printNumber(std::ostream& os, uint64_t number,
79 const std::string& head = "",
80 const std::string& tail = "\n",
81 bool exact = true, int width = 8, int precision = 3);
82
83
85
86
88 template<typename IntT>
89 class FormattedInt
90 {
91 public:
92 static char sep() { return ','; }
93
94 FormattedInt(IntT n): mInt(n) {}
95
96 std::ostream& put(std::ostream& os) const
97 {
98 // Convert the integer to a string.
99 std::ostringstream ostr;
100 ostr << mInt;
101 std::string s = ostr.str();
102 // Prefix the string with spaces if its length is not a multiple of three.
103 size_t padding = (s.size() % 3) ? 3 - (s.size() % 3) : 0;
104 s = std::string(padding, ' ') + s;
105 // Construct a new string in which groups of three digits are followed
106 // by a separator character.
107 ostr.str("");
108 for (size_t i = 0, N = s.size(); i < N; ) {
109 ostr << s[i];
110 ++i;
111 if (i >= padding && i % 3 == 0 && i < s.size()) {
112 ostr << sep();
113 }
114 }
115 // Remove any padding that was added and output the string.
116 s = ostr.str();
117 os << s.substr(padding, s.size());
118 return os;
119 }
120
121 private:
122 IntT mInt;
123 };
124
125 template<typename IntT>
126 std::ostream& operator<<(std::ostream& os, const FormattedInt<IntT>& n) { return n.put(os); }
127
129 template<typename IntT>
130 FormattedInt<IntT> formattedInt(IntT n) { return FormattedInt<IntT>(n); }
131
132 } // namespace util
133 } // namespace OPENVDB_VERSION_NAME
134 } // namespace openvdb
135
136 #endif // OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
137
138 // Copyright (c) 2012-2015 DreamWorks Animation LLC
139 // All rights reserved. This software is distributed under the
140 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:195
openvdb::v3_1_0::util::FormattedInt::FormattedInt
FormattedInt(IntT n)
Definition: Formats.h:94
openvdb::v3_1_0::util::formattedInt
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:130
openvdb::v3_1_0::util::printNumber
OPENVDB_API int printNumber(std::ostream &os, uint64_t number, const std::string &head="", const std::string &tail="\n", bool exact=true, int width=8, int precision=3)
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb
Definition: Exceptions.h:39
openvdb::v3_1_0::util::FormattedInt::put
std::ostream & put(std::ostream &os) const
Definition: Formats.h:96
openvdb::v3_1_0::util::printBytes
OPENVDB_API int printBytes(std::ostream &os, uint64_t bytes, const std::string &head="", const std::string &tail="\n", bool exact=false, int width=8, int precision=3)
version.h
openvdb::v3_1_0::util::FormattedInt::sep
static char sep()
Definition: Formats.h:92
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_1_0::util::FormattedInt
I/O manipulator that formats integer values with thousands separators.
Definition: Formats.h:89
Platform.h