OpenVDB  6.1.0
Formats.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2019 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 {
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 
92 OPENVDB_API int
93 printTime(std::ostream& os, double milliseconds,
94  const std::string& head = "",
95  const std::string& tail = "\n",
96  int width = 4, int precision = 1, int verbose = 0);
97 
98 
100 
101 
103 template<typename IntT>
105 {
106 public:
107  static char sep() { return ','; }
108 
109  FormattedInt(IntT n): mInt(n) {}
110 
111  std::ostream& put(std::ostream& os) const
112  {
113  // Convert the integer to a string.
114  std::ostringstream ostr;
115  ostr << mInt;
116  std::string s = ostr.str();
117  // Prefix the string with spaces if its length is not a multiple of three.
118  size_t padding = (s.size() % 3) ? 3 - (s.size() % 3) : 0;
119  s = std::string(padding, ' ') + s;
120  // Construct a new string in which groups of three digits are followed
121  // by a separator character.
122  ostr.str("");
123  for (size_t i = 0, N = s.size(); i < N; ) {
124  ostr << s[i];
125  ++i;
126  if (i >= padding && i % 3 == 0 && i < s.size()) {
127  ostr << sep();
128  }
129  }
130  // Remove any padding that was added and output the string.
131  s = ostr.str();
132  os << s.substr(padding, s.size());
133  return os;
134  }
135 
136 private:
137  IntT mInt;
138 };
139 
140 template<typename IntT>
141 std::ostream& operator<<(std::ostream& os, const FormattedInt<IntT>& n) { return n.put(os); }
142 
144 template<typename IntT>
146 
147 } // namespace util
148 } // namespace OPENVDB_VERSION_NAME
149 } // namespace openvdb
150 
151 #endif // OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
152 
153 // Copyright (c) 2012-2019 DreamWorks Animation LLC
154 // All rights reserved. This software is distributed under the
155 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:288
I/O manipulator that formats integer values with thousands separators.
Definition: Formats.h:104
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:125
std::ostream & put(std::ostream &os) const
Definition: Formats.h:111
Definition: Exceptions.h:40
Library and file format version numbers.
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:145
OPENVDB_API int printTime(std::ostream &os, double milliseconds, const std::string &head="", const std::string &tail="\n", int width=4, int precision=1, int verbose=0)
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_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)
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:177
static char sep()
Definition: Formats.h:107
FormattedInt(IntT n)
Definition: Formats.h:109