• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List

/export/development/ViennaMath/viennamath/runtime/functor_wrapper.hpp

Go to the documentation of this file.
00001 #ifndef VIENNAMATH_RUNTIME_FUNCTOR_WRAPPER_HPP
00002 #define VIENNAMATH_RUNTIME_FUNCTOR_WRAPPER_HPP
00003 
00004 /* =======================================================================
00005    Copyright (c) 2012, Institute for Microelectronics,
00006                        Institute for Analysis and Scientific Computing,
00007                        TU Wien.
00008                              -----------------
00009                ViennaMath - Symbolic and Numerical Math in C++
00010                              -----------------
00011 
00012    Author:     Karl Rupp                          rupp@iue.tuwien.ac.at
00013 
00014    License:    MIT (X11), see file LICENSE in the ViennaMath base directory
00015 ======================================================================= */
00016 
00017 
00018 
00019 
00020 #include <ostream>
00021 #include <sstream>
00022 #include "viennamath/forwards.h"
00023 
00028 namespace viennamath
00029 {
00030   
00031   //
00032   // Modification of expressions
00033   //
00034   
00036   template <typename InterfaceType /* see forwards.h for default argument */>
00037   class rt_manipulation_interface
00038   {
00039     public:
00041       virtual InterfaceType * operator()(InterfaceType const * e) const { return e->clone(); };
00042       
00044       virtual bool modifies(InterfaceType const * e) const { return false; }
00045   };
00046 
00048   template <typename InterfaceType /* default argument in forwards.h */>
00049   class rt_manipulation_wrapper
00050   {
00051     public:
00052       template <typename T>
00053       rt_manipulation_wrapper(T const * t) : functor_(t) {}
00054       
00055       InterfaceType * operator()(InterfaceType const * e) const
00056       {
00057         return functor_.get()->operator()(e); 
00058       }
00059 
00060       bool modifies(InterfaceType const * e) const { return functor_->modifies(e); }
00061 
00062     private:
00063       std::auto_ptr< const rt_manipulation_interface<InterfaceType> > functor_;
00064   };
00065 
00066   
00067   //
00068   // Traversal of expressions (no modification)
00069   //
00070   
00071   
00076   template <typename InterfaceType /* see forwards.h for default argument */>
00077   class rt_traversal_interface
00078   {
00079     public:
00081       virtual void operator()(InterfaceType const * e) const = 0;
00082       
00084       virtual bool step_into(InterfaceType const * e) const { return true; }
00085   };
00086   
00088   template <typename InterfaceType /* default argument in forwards.h */>
00089   class rt_traversal_wrapper
00090   {
00091     public:
00092       template <typename T>
00093       rt_traversal_wrapper(T const * t) : functor_(t) {}
00094       
00095       void operator()(InterfaceType const * e) const
00096       {
00097         functor_.get()->operator()(e); 
00098       }
00099 
00100       bool step_into(InterfaceType const * e) const { return functor_.get()->step_into(e); }
00101 
00102     private:
00103       std::auto_ptr< const rt_traversal_interface<InterfaceType> > functor_;
00104   };
00105 
00106 
00111   template <typename CastToType>
00112   struct callback_if_castable
00113   {
00115     template <typename InterfaceType, typename FunctorType>
00116     static bool apply(InterfaceType const * e, FunctorType const & functor)
00117     {
00118       CastToType const * ptr = dynamic_cast< CastToType const *>(e);
00119       if (ptr != NULL)
00120       {
00121         functor(*ptr);
00122         return true;
00123       }
00124       return false;
00125     }
00126     
00127   };
00128   
00129     
00130 }
00131 
00132 #endif

Generated on Wed Feb 29 2012 21:50:43 for ViennaMath by  doxygen 1.7.1