localdata.hh

Go to the documentation of this file.
00001 /*************************************************************************
00002 
00003 Copyright Rice University, 2004.
00004 All rights reserved.
00005 
00006 Permission is hereby granted, free of charge, to any person obtaining a
00007 copy of this software and associated documentation files (the "Software"),
00008 to deal in the Software without restriction, including without limitation
00009 the rights to use, copy, modify, merge, publish, distribute, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, provided that the above copyright notice(s) and this
00012 permission notice appear in all copies of the Software and that both the
00013 above copyright notice(s) and this permission notice appear in supporting
00014 documentation.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
00019 RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
00020 NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
00021 DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00022 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00023 ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
00024 THIS SOFTWARE.
00025 
00026 Except as contained in this notice, the name of a copyright holder shall
00027 not be used in advertising or otherwise to promote the sale, use or other
00028 dealings in this Software without prior written authorization of the
00029 copyright holder.
00030 
00031 **************************************************************************/
00032 
00033 #ifndef __RVL_LDC
00034 #define __RVL_LDC
00035 
00036 #include "data.hh"
00037 
00038 namespace RVL {
00039 
00072   template<class DataType>
00073   class LocalEvaluation;
00074 
00075   template<class DataType>
00076   class LocalConstEval;
00077 
00078   template<class DataType>
00079   class LocalDataContainer: public DataContainer {
00080 
00081   public:
00082 
00083     LocalDataContainer() {}
00084     LocalDataContainer(const LocalDataContainer<DataType> & D) {}
00085     virtual ~LocalDataContainer() {}
00086   
00088     virtual size_t getSize() const = 0;
00089 
00091     virtual DataType * getData() = 0;
00092 
00094     virtual DataType const * getData() const = 0;
00095 
00100     void eval(FunctionObject & f,
00101           vector<DataContainer const *> & x) {
00102       try {
00103     vector<LocalDataContainer<DataType> const *> lx(x.size());
00104     for (size_t i=0;i<x.size();i++) {
00105       if (!(lx[i] = dynamic_cast<LocalDataContainer<DataType> const *>(x[i]))) {
00106         RVLException e;
00107         e<<"Error: LocalDataContainer::eval(FO,...)\n";
00108         e<<"at least one of the input DCs is not an LDC\n";
00109         throw e;
00110       }
00111     }
00112     LocalEvaluation<DataType> & lf 
00113       = dynamic_cast<LocalEvaluation<DataType> &>(f);
00114     lf(*this,lx);
00115       }
00116       catch (bad_cast) {
00117     RVLException e;
00118     e<<"Error: LocalDataContainer::eval(FO,...)\n";
00119     e<<"FO is not an LFO\n";
00120     throw e;
00121       } 
00122       catch (RVLException & e) {
00123     e<<"\ncalled from LocalDataContainer::eval\n";
00124     throw e;
00125       }
00126     }
00127 
00129     void eval(FunctionObjectConstEval & f,
00130           vector<DataContainer const *> & x) const {
00131       try {
00132     vector<LocalDataContainer<DataType> const *> ex(x.size()+1);
00133     ex[0]=this;
00134     for (size_t i=0;i<x.size();i++) {
00135       if (!(ex[i+1] = dynamic_cast<LocalDataContainer<DataType> const *>(x[i]))) {
00136         RVLException e;
00137         e<<"Error: LocalDataContainer::eval(FO,...)\n";
00138         e<<"at least one of the input DCs is not an LDC\n";
00139         throw e;
00140       }
00141     }
00142     LocalConstEval<DataType> & lf 
00143       = dynamic_cast<LocalConstEval<DataType> &>(f);
00144     lf(ex);
00145       }
00146       catch (RVLException & e) {
00147     e<<"\ncalled from LocalDataContainer::eval(...) const\n";
00148     throw e;
00149       }
00150     }
00151   };
00152 
00156   template<class DataType>
00157   class LocalDataContainerFactory: public DataContainerFactory {
00158 
00159   public:
00160 
00161     LocalDataContainerFactory() {}
00162     LocalDataContainerFactory( LocalDataContainerFactory<DataType> &) {}
00163     virtual ~LocalDataContainerFactory() {}
00164 
00166     virtual LocalDataContainer<DataType> * buildLocal() const = 0;
00167 
00168     DataContainer * build() const { return buildLocal(); }
00169 
00171     virtual size_t getSize() const = 0;
00172 
00173   };
00174 
00175 
00177   template<class DataType>
00178   class LocalDataContainerSection: public LocalDataContainer<DataType> {
00179 
00180   private:
00181     LocalDataContainer<DataType> & src;
00182     size_t begin;
00183     size_t length;
00184 
00185     LocalDataContainerSection() {}
00186 
00187   public:
00189     LocalDataContainerSection(const LocalDataContainerSection<DataType> & D)
00190       :src(D.src), begin(D.begin), length(D.length) {}
00193     LocalDataContainerSection(LocalDataContainer<DataType> & _src,
00194                   size_t _begin,
00195                   size_t _length)
00196       : src(_src), begin(_begin), length(_length) {
00197       if (begin+length>src.getSize()) {
00198     RVLException e;
00199     e<<"Error: LocalDataContainerSection constructor\n";
00200     e<<"section overruns end of source LDC\n";
00201     e<<"length of source LDC = "<<src.getSize()<<"\n";
00202     e<<"begin index = "<<begin<<" length of section = "<<length<<"\n";
00203     throw e;
00204       } 
00205     }
00206     virtual ~LocalDataContainerSection() {}
00207   
00208     size_t getSize() const { return length; }
00209     DataType * getData() { return &((src.getData())[begin]); }
00210     DataType const * getData() const { return &((src.getData())[begin]); }
00211 
00212     ostream & write(ostream & str) const {
00213       str<<"LocalDataContainerSection object\n";
00214       str<<"LDC view of section beginning at "<<begin<<" of length "<<length<<" of\n";
00215       str<<"elements:\n";
00216       for (size_t i=0;i<length;i++) {
00217     str<<"index = "<<i<<" value = "<<getData()[i]<<"\n";
00218       }
00219       return str;
00220     }
00221   };
00222   
00223 }
00224 
00225 #endif

Generated on 5 Jan 2017 for LocalRVL by  doxygen 1.4.7