7 #include "fwMemory/ByteSize.hpp" 9 #include "fwMemory/exception/BadCast.hpp" 11 #include <boost/spirit/include/phoenix_operator.hpp> 12 #include <boost/spirit/include/qi.hpp> 19 const std::uint64_t ByteSize::Bytes = 1;
22 const std::uint64_t ByteSize::KB = 1000LL;
23 const std::uint64_t ByteSize::MB = 1000000LL;
24 const std::uint64_t ByteSize::GB = 1000000000LL;
25 const std::uint64_t ByteSize::TB = 1000000000000LL;
26 const std::uint64_t ByteSize::PB = 1000000000000000LL;
29 const std::uint64_t ByteSize::KiB = 1LL << 10;
30 const std::uint64_t ByteSize::MiB = 1LL << 20;
31 const std::uint64_t ByteSize::GiB = 1LL << 30;
32 const std::uint64_t ByteSize::TiB = 1LL << 40;
33 const std::uint64_t ByteSize::PiB = 1LL << 50;
46 (unit == Bytes) || (unit == KB) || (unit == MB) || (unit == GB) || (unit == TB) || (unit == PB)
47 || (unit == KiB) || (unit == MiB) || (unit == GiB) || (unit == TiB) || (unit == PiB));
57 (unit == Bytes) || (unit == KB) || (unit == MB) || (unit == GB) || (unit == TB) || (unit == PB)
58 || (unit == KiB) || (unit == MiB) || (unit == GiB) || (unit == TiB) || (unit == PiB));
75 ByteSize& ByteSize::operator= ( SizeType size )
83 ByteSize& ByteSize::operator= (
double size )
96 ByteSize& ByteSize::operator= (
const std::string& size )
107 (unit == Bytes) || (unit == KB) || (unit == MB) || (unit == GB) || (unit == TB) || (unit == PB)
108 || (unit == KiB) || (unit == MiB) || (unit == GiB) || (unit == TiB) || (unit == PiB));
122 (unit == Bytes) || (unit == KB) || (unit == MB) || (unit == GB) || (unit == TB) || (unit == PB)
123 || (unit == KiB) || (unit == MiB) || (unit == GiB) || (unit == TiB) || (unit == PiB));
124 m_size =
static_cast<SizeType
>(size*unit);
131 SizeType newSize = 0;
132 bool r = parseSize(size, newSize);
145 std::string ByteSize::unitToString(ByteSize::UnitType unit)
169 (unit == Bytes) || (unit == KB) || (unit == MB) || (unit == GB) || (unit == TB) || (unit == PB)
170 || (unit == KiB) || (unit == MiB) || (unit == GiB) || (unit == TiB) || (unit == PiB));
176 bool ByteSize::parseSize(
const std::string& s, SizeType& size)
178 using ::boost::phoenix::ref;
179 using ::boost::spirit::ascii::no_case;
180 using ::boost::spirit::ascii::space;
181 using ::boost::spirit::qi::as;
182 using ::boost::spirit::qi::_1;
183 using ::boost::spirit::qi::double_;
184 using ::boost::spirit::qi::eoi;
185 using ::boost::spirit::qi::phrase_parse;
186 using ::boost::spirit::qi::symbols;
187 using ::boost::spirit::qi::ulong_long;
189 std::string::const_iterator first = s.begin();
190 std::string::const_iterator last = s.end();
192 ByteSize::SizeType intSize = 0;
193 double floatSize = 0;
194 ByteSize::SizeType multiplier = ByteSize::Bytes;
196 symbols<char, ByteSize::SizeType> unit;
199 (
"b", ByteSize::Bytes ) (
"byte", ByteSize::Bytes ) (
"bytes", ByteSize::Bytes )
200 (
"kb", ByteSize::KB )
201 (
"mb", ByteSize::MB )
202 (
"gb", ByteSize::GB )
203 (
"tb", ByteSize::TB )
204 (
"pb", ByteSize::PB )
205 (
"k", ByteSize::KiB ) (
"kib", ByteSize::KiB )
206 (
"m", ByteSize::MiB ) (
"mib", ByteSize::MiB )
207 (
"g", ByteSize::GiB ) (
"gib", ByteSize::GiB )
208 (
"t", ByteSize::TiB ) (
"tib", ByteSize::TiB )
209 (
"p", ByteSize::PiB ) (
"pib", ByteSize::PiB )
213 r = phrase_parse(first, last,
217 ( &((double_) >> no_case[-unit] >> *space >> eoi)
218 >> double_[ ref(floatSize) = _1 ] >> no_case[-unit[ ref(multiplier) = _1 ]] )
219 | ( &((ulong_long) >> no_case[-unit] >> *space >> eoi)
220 >> ulong_long[ ref(intSize) = _1 ] >> no_case[-unit[ ref(multiplier) = _1 ]] )
228 if (!r || first != last)
235 size = intSize * multiplier;
237 else if (floatSize != 0)
244 size =
static_cast< ByteSize::SizeType
>(floatSize * multiplier);
258 (unit == Bytes) || (unit == KB) || (unit == MB) || (unit == GB) || (unit == TB) || (unit == PB)
259 || (unit == KiB) || (unit == MiB) || (unit == GiB) || (unit == TiB) || (unit == PiB));
260 std::stringstream sstr;
261 sstr << std::noshowpoint;
269 sstr << (static_cast<double>(m_size) / unit);
271 sstr <<
" " << unitToString(unit);
280 static UnitType si [] = {Bytes, KB, MB, GB, TB, PB};
281 static UnitType iec [] = {Bytes, KiB, MiB, GiB, TiB, PiB};
282 const size_t sizeOfStandardSet = 5;
284 UnitType* unitSet = iec;
291 for ( i = 1; i < sizeOfStandardSet; ++i )
293 if (m_size < unitSet[i])
FWMEMORY_API ByteSize()
Default constructor.
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)
The namespace fwMemory contains tools to manage memory. Use for dump.
FWMEMORY_API void setSize(SizeType size, UnitType unit=Bytes)
Build a ByteSize object from given size and unit.
FWMEMORY_API std::string getHumanReadableSize(StandardType standard=IEC)
Convert this size to a human readable string in the required Convert this size to a human readable st...
Implements an exception class for bad cast.
FWMEMORY_API std::string getSizeAsString(UnitType unit=Bytes)
Convert this size to a string with specified unit.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...