fw4spl
ByteSize.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWMEMORY_BYTESIZE_HPP__
8 #define __FWMEMORY_BYTESIZE_HPP__
9 
10 #include "fwMemory/config.hpp"
11 
12 #include <fwCore/base.hpp>
13 
14 #include <cstdint>
15 #include <string>
16 
17 namespace fwMemory
18 {
19 
25 class FWMEMORY_CLASS_API ByteSize
26 {
27 public:
28 
29  typedef enum
30  {
31  SI, IEC
32  } StandardType;
33 
34  FWMEMORY_API static const std::uint64_t Bytes;
35 
36  // SI units
37  FWMEMORY_API static const std::uint64_t KB;
38  FWMEMORY_API static const std::uint64_t MB;
39  FWMEMORY_API static const std::uint64_t GB;
40  FWMEMORY_API static const std::uint64_t TB;
41  FWMEMORY_API static const std::uint64_t PB;
42 
43  // IEC units
44  FWMEMORY_API static const std::uint64_t KiB;
45  FWMEMORY_API static const std::uint64_t MiB;
46  FWMEMORY_API static const std::uint64_t GiB;
47  FWMEMORY_API static const std::uint64_t TiB;
48  FWMEMORY_API static const std::uint64_t PiB;
49 
50  typedef std::uint64_t SizeType;
51  typedef std::uint64_t UnitType;
52 
56  FWMEMORY_API ByteSize ();
57 
62  FWMEMORY_API ByteSize ( SizeType size, UnitType unit = Bytes );
63  template <typename T>
64  ByteSize ( T size, UnitType unit = Bytes ) :
65  m_size(0)
66  {
67  this->setSize( SizeType(size), unit);
68  }
70 
74  FWMEMORY_API ByteSize ( double size, UnitType unit = Bytes );
75 
79  FWMEMORY_API ByteSize ( const std::string& size );
80 
88  FWMEMORY_API std::string getSizeAsString( UnitType unit = Bytes );
99  FWMEMORY_API std::string getHumanReadableSize( StandardType standard = IEC );
100 
104  SizeType getSize()
105  {
106  return m_size;
107  }
108 
113  FWMEMORY_API void setSize( SizeType size, UnitType unit = Bytes );
114  //------------------------------------------------------------------------------
115 
116  template <typename T>
117  void setSize( T size, UnitType unit = Bytes )
118  {
119  this->setSize(SizeType(size), unit);
120  }
122 
126  FWMEMORY_API void setSize( double size, UnitType unit = Bytes );
130  FWMEMORY_API void setSize( const std::string& size );
131 
132  FWMEMORY_API ByteSize& operator= ( SizeType size );
133  FWMEMORY_API ByteSize& operator= ( double size );
134  FWMEMORY_API ByteSize& operator= ( const std::string& size );
135  //------------------------------------------------------------------------------
136 
137  template <typename T>
138  ByteSize& operator= ( T size )
139  {
140  return this->operator=( SizeType(size) );
141  }
142 
143  operator SizeType() {
144  return m_size;
145  }
146  operator std::string() {
147  return getHumanReadableSize();
148  }
149 
150  FWMEMORY_API static bool parseSize(const std::string& s, std::uint64_t& size);
151  FWMEMORY_API static std::string unitToString( UnitType unit );
152 
153 protected:
154  SizeType m_size;
155 
156 };
157 
158 } //namespace fwMemory
159 
160 #endif /* __FWMEMORY_BYTESIZE_HPP__ */
Conversion helper for size in bytes Converts string to number of bytes and vice-versa. This class is also able to manage conversions between units standards (IEC, SI)
Definition: ByteSize.hpp:25
The namespace fwMemory contains tools to manage memory. Use for dump.
Definition: SReader.hpp:20
void setSize(T size, UnitType unit=Bytes)
Build a ByteSize object from given size and unit.
Definition: ByteSize.hpp:117
ByteSize(T size, UnitType unit=Bytes)
Build a ByteSize object from given size and unit.
Definition: ByteSize.hpp:64
SizeType getSize()
Returns size in bytes.
Definition: ByteSize.hpp:104