Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

expandlib-vartype-numeric.h

Go to the documentation of this file.
00001 
00002 /* This file is part of Expandlib.
00003 
00004   Copyright (C) 2002 Jasper van de Gronde
00005 
00006   This software is provided 'as-is', without any express or implied
00007   warranty.  In no event will the authors be held liable for any damages
00008   arising from the use of this software.
00009 
00010   Permission is granted to anyone to use this software for any purpose,
00011   including commercial applications, and to alter it and redistribute it
00012   freely, subject to the following restrictions:
00013 
00014   1. The origin of this software must not be misrepresented; you must not
00015      claim that you wrote the original software. If you use this software
00016      in a product, an acknowledgment in the product documentation would be
00017      appreciated but is not required.
00018   2. Altered source versions must be plainly marked as such, and must not be
00019      misrepresented as being the original software.
00020   3. This notice may not be removed or altered from any source distribution.
00021 
00022   Jasper van de Gronde th.v.d.gronde@hccnet.nl
00023 
00024 */
00025 
00030 #ifndef EXPANDLIB_VARTYPE_NUMERIC_H_INCLUDED
00031 #define EXPANDLIB_VARTYPE_NUMERIC_H_INCLUDED
00032 
00033 #include <boost/lexical_cast.hpp>
00034 
00035 #include <tchar.h>
00036 
00037 #include "expandlib-vartype.h"
00038 #include "expandlib-vartype-numeric-traits.h"
00039 
00040 namespace expander { // *** Start of namespace
00041 
00043 
00046     template<
00047         // Used for the expandable string (and the expanded string)
00048         class StringType = ::std::string,
00049         class NumType = int,
00050         class NumTraits = expvartype_numeric_traits_normal<NumType>
00051         >
00052     class expvartype_numeric : public expandvartype<StringType> {
00053     protected:
00054         NumType val;
00055         StringType val_str;
00056 
00057     public:
00058 
00060         expvartype_numeric()
00061         {
00062             val=NumTraits::defaultvalue();
00063             try {
00064                 val_str=boost::lexical_cast<StringType>(val);
00065             } catch(...) {
00066                 throw general_error(_T("expvartype_numeric"),_T("Error during lexical_cast<StringType>(val)."));
00067             }
00068         }
00069 
00071         expvartype_numeric(const StringType &s)
00072         {
00073             try {
00074                 set_value(boost::lexical_cast<NumType>(s));
00075             } catch(...) {
00076                 val=NumTraits::defaultvalue();
00077             }
00078         }
00079 
00081         expvartype_numeric(const _TCHAR * s)
00082         {
00083             try {
00084                 set_value(boost::lexical_cast<NumType>(s));
00085             } catch(...) {
00086                 val=NumTraits::defaultvalue();
00087             }
00088         }
00089 
00091         expvartype_numeric(const NumType v) { set_value(v); };
00092 
00093         virtual void AppendValue(StringType &dest) const throw()
00094         {
00095             dest+=val_str;
00096         }
00097 
00098         virtual void AppendValue(StringType &dest, const FParamVectorType &params, const FVarMapType &vars, ExpandResultsType &results) const throw()
00099         {
00100             dest+=val_str;
00101         }
00102 
00103         virtual void SetValue(const StringType &s) throw()
00104         {
00105             try {
00106                 set_value(boost::lexical_cast<NumType>(s));
00107             } catch(...) {
00108                 val=NumTraits::defaultvalue();
00109             }
00110         }
00111 
00113         inline NumType get_value() { return val; }
00114 
00116         inline void set_value(const NumType v)
00117         {
00118             val=NumTraits::validate(v);
00119             try {
00120                 val_str=boost::lexical_cast<StringType>(val);
00121             } catch(...) {
00122                 throw general_error(_T("expvartype_numeric"),_T("Error during lexical_cast<StringType>(val)."));
00123             }
00124         }
00125     };
00126 
00127 } // *** End of namespace
00128 
00129 #endif //EXPANDLIB_VARTYPE_NUMERIC_H_INCLUDED

Generated on Tue Feb 4 17:24:13 2003 for ExpandLib by doxygen1.3-rc2