00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00041 #ifndef _CASTEMPLATES_H
00042 #define _CASTEMPLATES_H
00043
00044 #ifdef HAVE_CONFIG_H
00045 #include "config.h"
00046 #endif
00047
00048 #if defined(DEBUG) || defined(DEBUG2)
00049 #include <iostream>
00050 #include <typeinfo>
00051 #include <cassert>
00052 #endif
00053
00054 #include <algorithm>
00055 #include <iterator>
00056 #include <functional>
00057
00058
00059
00060 #include "casobject.h"
00061 #include "casexception.h"
00062 #include "casfuncobj.h"
00063
00086 template<class T> void resolve_parentheses ( CASObject *o ) {
00087 checkptr ( o );
00088
00089 T *object = dynamic_cast<T*> ( o );
00090 checkptr ( object );
00091 #ifdef DEBUG2
00092 std::cerr << "Resolving parentheses of type: " << typeid ( *object ).name() << std::endl;
00093 o->Print();
00094 std::cout << std::endl;
00095 #endif
00096
00097 bool has_found;
00098 do {
00099 has_found = false;
00100
00101
00102 std::vector<CASObject*> fetched_objects;
00103
00104
00105
00106 std::vector<CASObject*> removal_list;
00107
00108
00109 std::vector<CASObject*>::iterator op_it = object->operands.begin();
00110
00111
00112 while ( op_it != object->operands.end() ) {
00113 CASObject *casobj = ( *op_it );
00114 if ( object->GetType() == casobj->GetType() ) {
00115
00116
00117 T *tp = dynamic_cast<T*> ( casobj );
00118 #ifdef DEBUG
00119 assert ( casobj != 0 );
00120 #endif
00121 removal_list.push_back ( tp );
00122
00123
00124
00125 *op_it = 0;
00126
00127 std::transform ( tp->operands.begin(),
00128 tp->operands.end(),
00129 std::back_inserter ( fetched_objects ),
00130 CASCloneObject() );
00131
00132 has_found = true;
00133 }
00134 op_it++;
00135 }
00136
00137
00138
00139 CASObject::Garbage->Put ( removal_list );
00140
00141
00142 std::vector<CASObject*>::iterator re_it =
00143 std::remove (
00144 object->operands.begin(),
00145 object->operands.end(),
00146 ( CASObject* ) 0 );
00147
00148
00149 object->operands.erase ( re_it, object->operands.end() );
00150
00151
00152 std::copy ( fetched_objects.begin(),
00153 fetched_objects.end(),
00154 std::back_inserter ( object->operands ) );
00155
00156 } while ( has_found );
00157 o->meta.UnsetSorted();
00158 o->meta.UnsetEvaluated();
00159 o->Sort();
00160 }
00161
00177 template<class T> void evaluate_operands ( T *o ) {
00178
00179 std::vector<CASObject*> eval;
00180
00181
00182 std::transform ( o->operands.begin(),
00183 o->operands.end(),
00184 std::back_inserter ( eval ),
00185 CASEvaluateObject() );
00186
00187
00188 CASObject::Garbage->Put ( o->operands );
00189
00190
00191 o->operands.clear();
00192
00193
00194 std::copy ( eval.begin(),
00195 eval.end(),
00196 std::back_inserter ( o->operands )
00197 );
00198 }
00199
00215 template<class T> void expand_operands ( T *o ) {
00216 #ifdef DEBUG2
00217 std::cerr << "Evaluate operands of: " << typeid ( *o ).name() << std::endl;
00218 #endif
00219
00220
00221 std::vector<CASObject*> eval;
00222
00223
00224 std::transform ( o->operands.begin(),
00225 o->operands.end(),
00226 std::back_inserter ( eval ),
00227 CASExpandObject() );
00228
00229
00230 CASObject::Garbage->Put ( o->operands );
00231
00232
00233 o->operands.clear();
00234
00235
00236 o->operands = eval;
00237 }
00238
00243 #endif