ByteCount.H

Go to the documentation of this file.
00001 /*!@file Util/ByteCount.H A class to handle byte counts, parsing them
00002   from strings, and converting between bytes, kibibytes, mebibytes,
00003   etc. */
00004 
00005 // //////////////////////////////////////////////////////////////////// //
00006 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00007 // by the University of Southern California (USC) and the iLab at USC.  //
00008 // See http://iLab.usc.edu for information about this project.          //
00009 // //////////////////////////////////////////////////////////////////// //
00010 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00011 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00012 // in Visual Environments, and Applications'' by Christof Koch and      //
00013 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00014 // pending; application number 09/912,225 filed July 23, 2001; see      //
00015 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00016 // //////////////////////////////////////////////////////////////////// //
00017 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00018 //                                                                      //
00019 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00020 // redistribute it and/or modify it under the terms of the GNU General  //
00021 // Public License as published by the Free Software Foundation; either  //
00022 // version 2 of the License, or (at your option) any later version.     //
00023 //                                                                      //
00024 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00025 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00026 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00027 // PURPOSE.  See the GNU General Public License for more details.       //
00028 //                                                                      //
00029 // You should have received a copy of the GNU General Public License    //
00030 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00031 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00032 // Boston, MA 02111-1307 USA.                                           //
00033 // //////////////////////////////////////////////////////////////////// //
00034 //
00035 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00036 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/ByteCount.H $
00037 // $Id: ByteCount.H 6164 2006-01-27 22:23:29Z rjpeters $
00038 //
00039 
00040 #ifndef UTIL_BYTECOUNT_H_DEFINED
00041 #define UTIL_BYTECOUNT_H_DEFINED
00042 
00043 #include "Util/sformat.H"
00044 
00045 #include <string>
00046 
00047 //! A class to handle parsing + converting byte counts
00048 class ByteCount
00049 {
00050 public:
00051   //! Construct with a number of bytes
00052   ByteCount(size_t nbytes = 0) : itsCount(nbytes) {}
00053 
00054   //! Construct by parsing a string: format is "<float>[<B|KiB|MiB|GiB>]"
00055   /*! If there is no suffix, we assume that the value is a literal
00056       byte count. Otherwise, the suffix can be B, KiB, MiB, GiB to
00057       specify a multiplier of 1, 2^10, 2^20, or 2^30, respectively. */
00058   ByteCount(const std::string& str);
00059 
00060   //! Return a pretty-printed string using the highest possible suffix
00061   /*! NOTE that if you convert to string using prettyPrint() and then
00062       rescan that string back into a ByteCount, there will almost
00063       certainly be inaccuracy due to floating point roundoff. */
00064   std::string prettyPrint() const;
00065 
00066   // NOTE: there is no special function to change the number of bytes,
00067   // however you can use the assignment operator along with a
00068   // constructed temporary, e.g. ByteCount x; ... ;  x=ByteCount(10);
00069 
00070   //! number of bytes represented
00071   size_t bytes() const { return itsCount; }
00072 
00073   //! Returns bytes()/1024.0
00074   /*! To avoid confusion we use kibibytes (KiB) instead of kilobytes,
00075       since 'kilo' strictly means 10^3 but is often also used to refer
00076       to 2^10. On the other hand, 'kibi' unequivocally refers to
00077       2^10. */
00078   double kibibytes() const { return itsCount/1024.0; }
00079 
00080   //! Returns bytes()/(1024.0*1024.0)
00081   /*! To avoid confusion we use mebibytes (MiB) instead of megabytes,
00082       since 'mega' strictly means 10^6 but is often also used to refer
00083       to 2^20. On the other hand, 'mebi' unequivocally refers to
00084       2^20. */
00085   double mebibytes() const { return itsCount/(1024.0*1024.0); }
00086 
00087   //! Returns bytes()/(1024.0*1024.0*1024.0)
00088   /*! To avoid confusion we use gibibytes (GiB) instead of gigabytes,
00089       since 'giga' strictly means 10^9 but is often also used to refer
00090       to 2^30. On the other hand, 'gibi' unequivocally refers to
00091       2^30. */
00092   double gibibytes() const { return itsCount/(1024.0*1024.0*1024.0); }
00093 
00094   //! synonym for bytes()
00095   size_t B() const { return bytes(); }
00096 
00097   //! synonym for kibibytes()
00098   double KiB() const { return kibibytes(); }
00099 
00100   //! synonym for mebibytes()
00101   double MiB() const { return mebibytes(); }
00102 
00103   //! synonym for gibibytes()
00104   double GiB() const { return gibibytes(); }
00105 
00106   //! Equality operator
00107   bool operator==(const ByteCount& that) const
00108   { return (this->itsCount == that.itsCount); }
00109 
00110   //! Inequality operator
00111   bool operator!=(const ByteCount& that) const
00112   { return (this->itsCount != that.itsCount); }
00113 
00114 private:
00115   size_t itsCount;
00116 };
00117 
00118 //! ByteCount overload: format is "<int>"
00119 /*! Note that we don't do any abbreviations to KiB, Mib, GiB, etc. on
00120     output, since that would involve a conversion to floating point
00121     that might result in data loss after a conversion to string and
00122     back. If you want such an abbreviated string, you can use
00123     ByteCount::prettyPrint(). */
00124 inline std::string convertToString(const ByteCount& val)
00125 {
00126   return sformat("%"ZU, val.bytes());
00127 }
00128 
00129 //! ByteCount overload: format is "<float>[<B|KiB|MiB|GiB>]"
00130 inline void convertFromString(const std::string& str, ByteCount& val)
00131 {
00132   val = ByteCount(str);
00133 }
00134 
00135 // ######################################################################
00136 /* So things look consistent in everyone's emacs... */
00137 /* Local Variables: */
00138 /* indent-tabs-mode: nil */
00139 /* End: */
00140 
00141 #endif // UTIL_BYTECOUNT_H_DEFINED
Generated on Sun May 8 08:06:58 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3