casconvert.cc

Go to the documentation of this file.
00001 //
00002 //  Copyright (c) 2006 by Rafael Ostertag
00003 //
00004 //  This program is free software; you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation; either version 2 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program; if not, write to the Free Software
00016 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 //
00018 //
00019 // $Id: casconvert.cc,v 1.8 2006/12/31 00:51:31 rafi Exp $
00020 //
00021 
00036 #ifdef HAVE_CONFIG_H
00037 #include "config.h"
00038 #endif
00039 
00040 #include <cassert>
00041 
00042 #include "casconvert.h"
00043 #include "casobject.h"
00044 #include "casfuncobj.h"
00045 #include "casexception.h"
00046 
00047 
00048 // Private methods
00049 // ----------------------------------------------------------------------------
00050 // ----------------------------------------------------------------------------
00051 
00052 // Protected methods
00053 // ----------------------------------------------------------------------------
00069 template<class T> T*
00070 CASConvert::vector2type() const throw ( EDoesNotApply ) {
00071     if ( vector.size() < 1 ) {
00072         throw EDoesNotApply();
00073     }
00074 
00075     T* typed = new T;
00076     typed->operands.clear();
00077 
00078     std::transform ( vector.begin(),
00079                      vector.end(),
00080                      std::back_inserter ( typed->operands ),
00081                      CASCloneObject() );
00082     typed->Sort();
00083 
00084     return typed;
00085 }
00086 
00109 template<class T> T*
00110 CASConvert::casobject2type() const throw ( EDoesNotApply ) {
00111     if ( casobject == 0 ) {
00112         throw EDoesNotApply();
00113     }
00114 
00115     T *typed = new T;
00116     if ( casobject->meta.IsSingleValue() ) {
00117         typed->Append ( casobject );
00118         return typed;
00119     }
00120 
00121     switch ( casobject->GetType() ) {
00122     case CT_SUM: {
00123             Sum *tmp = dynamic_cast<Sum*> ( casobject );
00124 #ifdef DEBUG
00125             assert ( tmp != 0 );
00126 #endif
00127             std::transform ( tmp->operands.begin(),
00128                              tmp->operands.end(),
00129                              std::back_inserter ( typed->operands ),
00130                              CASCloneObject() );
00131             return typed;
00132         }
00133     case CT_PRODUCT: {
00134             Product *tmp = dynamic_cast<Product*> ( casobject->Clone() );
00135 #ifdef DEBUG
00136             assert ( tmp != 0 );
00137 #endif
00138             std::transform ( tmp->operands.begin(),
00139                              tmp->operands.end(),
00140                              std::back_inserter ( typed->operands ),
00141                              CASCloneObject() );
00142             return typed;
00143         }
00144     case CT_FRACTION: {
00145             Fraction *tmp = dynamic_cast<Fraction*> ( casobject );
00146 #ifdef DEBUG
00147             assert ( tmp != 0 );
00148 #endif
00149             typed->Append ( tmp->numerator );
00150             typed->Append ( tmp->denominator );
00151             return typed;
00152         }
00153 
00154     default:
00155         std::string m ( "Cannot convert to " );
00156         m += typeid ( T ).name();
00157         throw ( EDoesNotApply ( m ) );
00158     }
00159 }
00160 
00182 Variable*
00183 CASConvert::vector2variable() const throw ( EDoesNotApply ) {
00184     if ( vector.size() != 1 ) {
00185         throw EDoesNotApply();
00186     }
00187 
00188     if ( !vector[0]->meta.IsSingleValue() ) {
00189         throw EDoesNotApply();
00190     }
00191 
00192     if ( vector[0]->meta.IsPureNumerical() ) {
00193         throw EDoesNotApply();
00194     }
00195 
00196     Variable *tmp = dynamic_cast<Variable*> ( vector[0] );
00197     if ( tmp == 0 ) {
00198         throw EDoesNotApply();
00199     }
00200 
00201     Variable *var = new Variable();
00202     *var = *tmp;
00203 
00204     return var;
00205 }
00206 
00216 Variable*
00217 CASConvert::casobject2variable() const throw ( EDoesNotApply ) {
00218     if ( casobject == 0 ) {
00219         throw EDoesNotApply();
00220     }
00221 
00222     if ( !casobject->meta.IsSingleValue() ) {
00223         throw EDoesNotApply();
00224     }
00225 
00226     if ( casobject->meta.IsPureNumerical() ) {
00227         throw EDoesNotApply();
00228     }
00229 
00230     Variable *tmp = dynamic_cast<Variable*> ( casobject );
00231     if ( tmp == 0 ) {
00232         throw EDoesNotApply();
00233     }
00234 
00235     Variable *var = new Variable();
00236     *var = *tmp;
00237 
00238     return var;
00239 }
00240 
00249 LongInt*
00250 CASConvert::vector2longint() const throw ( EDoesNotApply ) {
00251     if ( vector.size() != 1 ) {
00252         throw EDoesNotApply();
00253     }
00254 
00255     if ( !vector[0]->meta.IsSingleValue() ) {
00256         throw EDoesNotApply();
00257     }
00258 
00259     if ( !vector[0]->meta.IsPureNumerical() ) {
00260         throw EDoesNotApply();
00261     }
00262 
00263     LongInt *tmp = dynamic_cast<LongInt*> ( vector[0] );
00264     if ( tmp == 0 ) {
00265         throw EDoesNotApply();
00266     }
00267 
00268     LongInt *lint = new LongInt();
00269     *lint = *tmp;
00270 
00271     return lint;
00272 }
00273 
00280 LongInt*
00281 CASConvert::casobject2longint() const throw ( EDoesNotApply ) {
00282     if ( casobject == 0 ) {
00283         throw EDoesNotApply();
00284     }
00285 
00286     if ( !casobject->meta.IsSingleValue() ) {
00287         throw EDoesNotApply();
00288     }
00289 
00290     if ( !casobject->meta.IsPureNumerical() ) {
00291         throw EDoesNotApply();
00292     }
00293 
00294     LongInt  *tmp = dynamic_cast<LongInt*> ( casobject );
00295     if ( tmp == 0 ) {
00296         throw EDoesNotApply();
00297     }
00298 
00299     LongInt *lint = new LongInt();
00300     *lint = *tmp;
00301 
00302     return lint;
00303 }
00304 
00312 Fraction*
00313 CASConvert::vector2fraction() const throw ( EDoesNotApply ) {
00314     if ( vector.size() < 2 ) {
00315         throw EDoesNotApply();
00316     }
00317 
00318     Fraction *frac = new Fraction();
00319 #ifdef DEBUG
00320     assert ( frac->numerator != 0 );
00321     assert ( frac->denominator != 0 );
00322 #endif
00323     CASObject::Garbage->Put ( frac->numerator );
00324     CASObject::Garbage->Put ( frac->denominator );
00325     frac->numerator = vector[0]->Clone();
00326     frac->denominator = vector[1]->Clone();
00327 
00328     return frac;
00329 }
00330 
00338 Fraction*
00339 CASConvert::casobject2fraction() const throw ( EDoesNotApply ) {
00340     if ( casobject == 0 ) {
00341         throw EDoesNotApply();
00342     }
00343 
00344     Fraction *frac = new Fraction();
00345 #ifdef DEBUG
00346     assert ( frac->numerator != 0 );
00347 #endif
00348     CASObject::Garbage->Put ( frac->numerator );
00349     frac->numerator = casobject->Clone();
00350 
00351     return frac;
00352 }
00353 // ----------------------------------------------------------------------------
00354 
00355 // Constructors & Destructor methods
00356 // ----------------------------------------------------------------------------
00367 CASConvert::CASConvert ( const CASObject* o ) {
00368     checkptr ( o );
00369     casobject = o->Clone();
00370 }
00371 
00379 CASConvert::CASConvert ( const std::vector<const CASObject*>& v ) : casobject ( 0 ) {
00380     std::transform ( v.begin(),
00381                      v.end(),
00382                      std::back_inserter ( vector ),
00383                      CASCloneObject() );
00384 }
00385 
00390 CASConvert::~CASConvert() {
00391     if ( casobject != 0 ) {
00392         CASObject::Garbage->Put ( casobject );
00393     }
00394 
00395     CASObject::Garbage->Put ( vector );
00396 }
00397 
00403 CASConvert::CASConvert ( const CASConvert &o ) {
00404     if ( o.casobject != 0 ) {
00405         casobject = o.casobject->Clone();
00406     }
00407 
00408     std::transform ( o.vector.begin(),
00409                      o.vector.end(),
00410                      std::back_inserter ( vector ),
00411                      CASCloneObject() );
00412 }
00413 
00414 // ----------------------------------------------------------------------------
00415 
00416 // Public methods
00417 // ----------------------------------------------------------------------------
00425 Sum*
00426 CASConvert::ToSum() const throw ( EDoesNotApply ) {
00427     if ( casobject != 0 ) {
00428         return casobject2type<Sum>();
00429     } else {
00430         return vector2type<Sum>();
00431     }
00432 }
00433 
00441 Product*
00442 CASConvert::ToProduct() const throw ( EDoesNotApply ) {
00443     if ( casobject != 0 ) {
00444         return casobject2type<Product>();
00445     } else {
00446         return vector2type<Product>();
00447     }
00448 }
00449 
00457 Variable*
00458 CASConvert::ToVariable() const throw ( EDoesNotApply ) {
00459     if ( casobject != 0 ) {
00460         return casobject2variable();
00461     } else {
00462         return vector2variable();
00463     }
00464 }
00465 
00473 LongInt*
00474 CASConvert::ToLongInt() const throw ( EDoesNotApply ) {
00475     if ( casobject != 0 ) {
00476         return casobject2longint();
00477     } else {
00478         return vector2longint();
00479     }
00480 }
00481 
00489 Fraction*
00490 CASConvert::ToFraction() const throw ( EDoesNotApply ) {
00491     if ( casobject != 0 ) {
00492         return casobject2fraction();
00493     } else {
00494         return vector2fraction();
00495     }
00496 }
00497 // ----------------------------------------------------------------------------
00498 
00499 // Operator methods
00500 // ----------------------------------------------------------------------------
00506 CASConvert&
00507 CASConvert::operator= ( const CASConvert &o ) {
00508     if ( this == &o ) {
00509         return *this;
00510     }
00511 
00512     if ( casobject != 0 ) {
00513         CASObject::Garbage->Put ( casobject );
00514     }
00515 
00516     CASObject::Garbage->Put ( vector );
00517     vector.clear();
00518 
00519     if ( o.casobject != 0 ) {
00520         casobject = o.casobject->Clone();
00521     }
00522 
00523     std::transform ( o.vector.begin(),
00524                      o.vector.end(),
00525                      std::back_inserter ( vector ),
00526                      CASCloneObject() );
00527 
00528     return *this;
00529 }
00530 
00531 // ----------------------------------------------------------------------------
00532 

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