include/wipal/tool/si.hxx

00001 /*
00002  * WiPal - A library and a set of tools to manipulate wireless traces.
00003  * Copyright (C) 2007  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
00021  */
00022 #ifndef TOOL_SI_HXX_
00023 # define TOOL_SI_HXX_
00024 
00025 # include <cmath>
00026 # include <iomanip>
00027 
00028 # include "si.hh"
00029 
00030 namespace tool
00031 {
00032 
00033   namespace internals
00034   {
00035 
00036     template <size_t OneUnitInBits>
00037     SI_unit<OneUnitInBits>::SI_unit(size_t count):
00038       bits_ (count * OneUnitInBits)
00039     {
00040     }
00041 
00042     template <size_t OneUnitInBits>
00043     template <size_t OneRHSUnitInBits>
00044     SI_unit<OneUnitInBits>::
00045     SI_unit(const SI_unit<OneRHSUnitInBits>& rhs): bits_ (rhs.bits())
00046     {
00047     }
00048 
00049     template <size_t OneUnitInBits>
00050     size_t
00051     SI_unit<OneUnitInBits>::bits() const
00052     {
00053       return bits_;
00054     }
00055 
00056     template <size_t OneUnitInBits>
00057     size_t
00058     SI_unit<OneUnitInBits>::bytes() const
00059     {
00060       return bits_ * 8;
00061     }
00062 
00063     template <size_t OneUnitInBits>
00064     std::string
00065     SI_unit<OneUnitInBits>::abbrev()
00066     {
00067       return abbrev_;
00068     }
00069 
00070     template <size_t OneUnitInBits>
00071     std::ostream&
00072     operator << (std::ostream& os, const SI_unit<OneUnitInBits>& unit)
00073     {
00074       const std::streamsize     default_p = os.precision();
00075       const std::ios::fmtflags  default_f = os.flags();
00076       const double              v = unit.bits() / OneUnitInBits;
00077       const std::string         u = SI_unit<OneUnitInBits>::abbrev();
00078 
00079       return os << std::fixed << std::setprecision (0)
00080                 << std::floor(v + 0.5) << ' ' << u
00081                 << std::setprecision (default_p)
00082                 << std::resetiosflags (~default_f)
00083                 << std::setiosflags (default_f);
00084     }
00085 
00086   } // End of namespace internals.
00087 
00088 } // End of namespace tool.
00089 
00090 #endif // ! TOOL_SI_HXX_

Generated on Tue Jan 15 19:32:31 2008 for wipal by  doxygen 1.5.4