00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 {
00041
00043
00060 template<
00061
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 ¶ms, 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 {
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 }
00183
00184 #endif //EXPANDLIB_VARTYPE_LIST_H_INCLUDED