FormatableString.cpp
Go to the documentation of this file.
00001 /*
00002         Aseba - an event-based framework for distributed robot control
00003         Copyright (C) 2007--2012:
00004                 Stephane Magnenat <stephane at magnenat dot net>
00005                 (http://stephane.magnenat.net)
00006                 and other contributors, see authors.txt for details
00007         
00008         This program is free software: you can redistribute it and/or modify
00009         it under the terms of the GNU Lesser General Public License as published
00010         by the Free Software Foundation, version 3 of the License.
00011         
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU Lesser General Public License for more details.
00016         
00017         You should have received a copy of the GNU Lesser General Public License
00018         along with this program. If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include <cassert>
00022 #include <string>
00023 #include <sstream>
00024 #include <ios>
00025 #include <iostream>
00026 #include <iomanip>
00027 
00028 #include "FormatableString.h"
00029 
00030 namespace Aseba
00031 {
00034         
00035         template<typename charT>
00036         void BasicFormatableString<charT>::proceedReplace(const S &replacement)
00037         {
00038                 typename std::basic_ostringstream<charT> search;
00039                 search << "%" << this->argLevel;
00040                 typename S::size_type pos = this->find(search.str(), 0);
00041                 assert(pos != S::npos);
00042                 this->replace(pos, search.str().length(), replacement);
00043                 ++argLevel;
00044         }
00045         
00046         template<typename charT>
00047         BasicFormatableString<charT> &BasicFormatableString<charT>::arg(int value, int fieldWidth, int base, charT fillChar)
00048         {
00049                 typename std::basic_ostringstream<charT> oss;
00050                 oss << std::setbase(base);
00051                 oss.width(fieldWidth);
00052                 oss.fill(fillChar);
00053                 
00054                 // transform value into S
00055                 oss << value;
00056         
00057                 proceedReplace(oss.str());
00058                 
00059                 // return reference to this so that .arg can proceed further
00060                 return *this;
00061         }
00062         
00063         template<typename charT>
00064         BasicFormatableString<charT> &BasicFormatableString<charT>::arg(unsigned value, int fieldWidth, int base, charT fillChar)
00065         {
00066                 typename std::basic_ostringstream<charT> oss;
00067                 oss << std::setbase(base);
00068                 oss.width(fieldWidth);
00069                 oss.fill(fillChar);
00070                 
00071                 // transform value into S
00072                 oss << value;
00073         
00074                 proceedReplace(oss.str());
00075                 
00076                 // return reference to this so that .arg can proceed further
00077                 return *this;
00078         }
00079         
00080         template<typename charT>
00081         BasicFormatableString<charT> &BasicFormatableString<charT>::arg(float value, int fieldWidth, int precision, charT fillChar)
00082         {
00083                 typename std::basic_ostringstream<charT> oss;
00084                 oss.precision(precision);
00085                 oss.width(fieldWidth);
00086                 oss.fill(fillChar);
00087         
00088                 oss.setf(oss.fixed, oss.floatfield);
00089                 // transform value into S
00090                 oss << value;
00091         
00092                 proceedReplace(oss.str());
00093                 
00094                 // return reference to this so that .arg can proceed further
00095                 return *this;
00096         }
00097         
00098         template<typename charT>
00099         BasicFormatableString<charT> &BasicFormatableString<charT>::operator=(const S& str)
00100         {
00101                 this->assign(str);
00102                 this->argLevel = 0;
00103                 return (*this);
00104         }
00105         
00106         template class BasicFormatableString<char>;
00107         template class BasicFormatableString<wchar_t>;
00108         
00110 }


aseba
Author(s): Stéphane Magnenat
autogenerated on Thu Jan 2 2014 11:17:16