castemplates.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 //  Copyright (c) 2006 by Rafael Ostertag
00004 //
00005 //  This program is free software; you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 //
00019 //
00020 // $Id: castemplates.h,v 1.14 2006/12/28 19:01:24 rafi Exp $
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         // Holds all fetched objects
00102         std::vector<CASObject*> fetched_objects;
00103 
00104         // Removal list holds all objects, from where we fetch
00105         // the operands.
00106         std::vector<CASObject*> removal_list;
00107 
00108         // This loop searches for nested objects
00109         std::vector<CASObject*>::iterator op_it = object->operands.begin();
00110 
00111         // Loop thru all operands
00112         while ( op_it != object->operands.end() ) {
00113             CASObject *casobj = ( *op_it );
00114             if ( object->GetType() == casobj->GetType() ) {
00115                 // Fetch all operands from the nested object into the operands
00116                 // vector of this object
00117                 T *tp = dynamic_cast<T*> ( casobj );
00118 #ifdef DEBUG
00119                 assert ( casobj != 0 );
00120 #endif
00121                 removal_list.push_back ( tp );
00122 
00123                 // Set the fetched operand to zero, so we can identify
00124                 // it later in order to remove it.
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         // Destroy the fetched objects
00139         CASObject::Garbage->Put ( removal_list );
00140 
00141         // Remove the 0's from this operand list
00142         std::vector<CASObject*>::iterator re_it =
00143             std::remove (
00144                     object->operands.begin(),
00145                     object->operands.end(),
00146                     ( CASObject* ) 0 );
00147 
00148         // shrink the vector
00149         object->operands.erase ( re_it, object->operands.end() );
00150 
00151         // copy the fetched objects to the operands vector
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     // Holds the addresses to the evaluated objects
00179     std::vector<CASObject*> eval;
00180 
00181     // Evaluate the operands
00182     std::transform ( o->operands.begin(),
00183                      o->operands.end(),
00184                      std::back_inserter ( eval ),
00185                      CASEvaluateObject() );
00186 
00187     // Put the previous operands to the garbage
00188     CASObject::Garbage->Put ( o->operands );
00189 
00190     // Now, clear the existing list
00191     o->operands.clear();
00192 
00193     // and replace it with the evaluated ones
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     // Holds the addresses to the evaluated objects
00221     std::vector<CASObject*> eval;
00222 
00223     // Evaluate the operands
00224     std::transform ( o->operands.begin(),
00225                      o->operands.end(),
00226                      std::back_inserter ( eval ),
00227                      CASExpandObject() );
00228 
00229     // Put the previous operands to the garbage
00230     CASObject::Garbage->Put ( o->operands );
00231 
00232     // Now, clear the existing list
00233     o->operands.clear();
00234 
00235     // and replace it with the evaluated ones
00236     o->operands = eval;
00237 }
00238 
00243 #endif /* _CASTEMPLATES_H */

Generated on Sun Dec 31 01:57:27 2006 for ECAS by  doxygen 1.4.7