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

expandlib-vartype-list.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_LIST_H_INCLUDED
00031 #define EXPANDLIB_VARTYPE_LIST_H_INCLUDED
00032 
00033 #include <boost/lexical_cast.hpp>
00034 
00035 #include <stdarg.h>
00036 #include <tchar.h>
00037 
00038 #include "expandlib-vartype.h"
00039 
00040 namespace expander { // *** Start of namespace
00041 
00043 
00060     template<
00061         // Used for the expandable string (and the expanded string)
00062         class StringType = ::std::string,
00063         class ItemType = expvartype_text< StringType >
00064         >
00065     class expvartype_list : public expandvartype<StringType> {
00066     protected:
00067         ::std::vector<ItemType> list;
00068         const _TCHAR* const ItemSeparator;
00069 
00070     public:
00071 
00073         expvartype_list(const _TCHAR *const is=_T(", ")) : ItemSeparator(is) {}
00074 
00076         expvartype_list(const StringType &s, const _TCHAR *const is=_T(", ")) : ItemSeparator(is) { SetValue(s); }
00077 
00079         expvartype_list(const _TCHAR * s, const _TCHAR *const is=_T(", ")) : ItemSeparator(is) { SetValue(s); }
00080 
00082 
00085         template<class T> expvartype_list(T beg, const T end, const _TCHAR *const is=_T(", ")) : ItemSeparator(is)
00086         {
00087             for(; beg != end; ++beg) {
00088                 list.push_back(ItemType(*beg));
00089             }
00090         }
00091 
00093         template<class T> expvartype_list(const ::std::vector<ItemType>::size_type n, const T val, const _TCHAR *const is=_T(", ")) : list(n, ItemType(val)), ItemSeparator(is) {}
00094 
00095         virtual void AppendValue(StringType &dest) const throw()
00096         {
00097             {
00098                 for(::std::vector<ItemType>::const_iterator it=list.begin(); it!=list.end(); ++it) {
00099                     if ( it != list.begin() ) dest += ItemSeparator;
00100                     (*it).AppendValue(dest);
00101                 }
00102             }
00103         }
00104 
00105         virtual void AppendValue(StringType &dest, const FParamVectorType &params, const FVarMapType &vars, ExpandResultsType &results) const throw()
00106         {
00107             if ( params.size() == 0 ) {
00108                 for(::std::vector<ItemType>::const_iterator it=list.begin(); it!=list.end(); ++it) {
00109                     if ( it != list.begin() ) dest += ItemSeparator;
00110                     (*it).AppendValue(dest, params, vars, results);
00111                 }
00112             } else if ( params[0] == _T("|") ) {
00113                 const FParamVectorType tparams(params.begin()+1, params.end());
00114                 {
00115                     for(::std::vector<ItemType>::const_iterator it=list.begin(); it!=list.end(); ++it) {
00116                         if ( it != list.begin() ) dest += ItemSeparator;
00117                         (*it).AppendValue(dest, tparams, vars, results);
00118                     }
00119                 }
00120             } else if ( params.size() == 1 ) {
00121                 try {
00122                     list.at(boost::lexical_cast<::std::vector<ItemType>::size_type>(params[0])).AppendValue(dest);
00123                 } catch(boost::bad_lexical_cast) {
00124                     if ( params[0] == _T("-1") ) {
00125                         list.back().AppendValue(dest);
00126                     } else if ( params[0] == _T("size") ) {
00127                         try {
00128                             dest += boost::lexical_cast<StringType>(list.size());
00129                         } catch(...) {
00130                             throw general_error("expvartype_list","Error during lexical_cast<StringType>(list.size()).");
00131                         }
00132                     } else {
00133                         for(::std::vector<ItemType>::const_iterator it=list.begin(); it!=list.end(); ++it) {
00134                             if ( it != list.begin() ) dest += params[0];
00135                             (*it).AppendValue(dest);
00136                         }
00137                     }
00138                 } catch(...) {
00139                     if ( params[0] == _T("-1") ) {
00140                         list.back().AppendValue(dest);
00141                     } else {
00142                         throw general_error(_T("expvartype_list"),_T("Error during at(boost::lexical_cast<size_type>(params[0])).AppendValue(dest)."));
00143                     }
00144                 }
00145             } else { // First parameter is either index or separator, from the second the parameters are passed on
00146                 const FParamVectorType tparams(params.begin()+1, params.end());
00147                 try {
00148                     list.at(boost::lexical_cast<::std::vector<ItemType>::size_type>(params[0])).AppendValue(dest, tparams, vars, results);
00149                 } catch(boost::bad_lexical_cast) {
00150                     if ( params[0] == _T("-1") ) {
00151                         list.back().AppendValue(dest, tparams, vars, results);
00152                     } else if ( params[0] == _T("size") ) {
00153                         try {
00154                             dest += boost::lexical_cast<StringType>(list.size());
00155                         } catch(...) {
00156                             throw general_error(_T("expvartype_list"),_T("Error during lexical_cast<StringType>(list.size())."));
00157                         }
00158                     } else {
00159                         for(::std::vector<ItemType>::const_iterator it=list.begin(); it!=list.end(); ++it) {
00160                             if ( it != list.begin() ) dest += params[0];
00161                             (*it).AppendValue(dest, tparams, vars, results);
00162                         }
00163                     }
00164                 } catch(...) {
00165                     if ( params[0] == _T("-1") ) {
00166                         list.back().AppendValue(dest);
00167                     } else {
00168                         throw general_error(_T("expvartype_list"),_T("Error during at(boost::lexical_cast<size_type>(params[0])).AppendValue(dest)."));
00169                     }
00170                 }
00171             }
00172         }
00173 
00175 
00177         virtual void SetValue(const StringType &s) { }
00178 
00179         inline ::std::vector<ItemType>& get_list() { return list; }
00180     };
00181 
00182 } // *** End of namespace
00183 
00184 #endif //EXPANDLIB_VARTYPE_LIST_H_INCLUDED

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