src/MEDWrapper/Base/MED_SliceArray.hxx

Go to the documentation of this file.
00001 //  
00002 //
00003 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
00005 // 
00006 //  This library is free software; you can redistribute it and/or 
00007 //  modify it under the terms of the GNU Lesser General Public 
00008 //  License as published by the Free Software Foundation; either 
00009 //  version 2.1 of the License. 
00010 // 
00011 //  This library is distributed in the hope that it will be useful, 
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00014 //  Lesser General Public License for more details. 
00015 // 
00016 //  You should have received a copy of the GNU Lesser General Public 
00017 //  License along with this library; if not, write to the Free Software 
00018 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
00019 // 
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //
00023 //
00024 //  File   : 
00025 //  Author : 
00026 //  Module : 
00027 //  $Header: /home/server/cvs/MED/MED_SRC/src/MEDWrapper/Base/MED_SliceArray.hxx,v 1.4 2006/06/01 11:21:18 jfa Exp $
00028 
00029 #ifndef MED_SliceArray_HeaderFile
00030 #define MED_SliceArray_HeaderFile
00031 
00032 #include <valarray>
00033 #include <stdexcept>
00034 
00035 //#if defined(_DEBUG_)
00036 #  define MED_TCSLICE_CHECK_RANGE
00037 //#endif
00038 
00039 namespace MED
00040 {
00041   //---------------------------------------------------------------
00043 
00048   template<class TContainer> 
00049   class TCSlice
00050   {
00051     typedef const TContainer* PCContainer;
00052     PCContainer myCContainer; 
00053     std::slice mySlice; 
00054   protected:
00055     void
00056     check_id(size_t theId) const
00057     {
00058       long int anId = -1;
00059       if(theId < mySlice.size()){
00060      anId = mySlice.start() + theId*mySlice.stride();
00061      if(anId < myCContainer->size())
00062        return;
00063       }
00064       throw std::out_of_range("TCSlice::check_id");
00065     }
00066 
00068     size_t
00069     calculate_id(size_t theId) const
00070     {
00071       return mySlice.start() + theId*mySlice.stride();
00072     }
00073     
00074     size_t
00075     get_id(size_t theId) const
00076     {
00077 #ifdef MED_TCSLICE_CHECK_RANGE
00078       check_id(theId);
00079 #endif
00080       return calculate_id(theId);
00081     }
00082     
00083     size_t
00084     get_id_at(size_t theId) const
00085     {
00086       check_id(theId);
00087       return calculate_id(theId);
00088     }
00089 
00090   public:
00091     typedef typename TContainer::value_type value_type;
00092 
00094     TCSlice(const TContainer& theContainer,
00095          const std::slice& theSlice): 
00096       myCContainer(&theContainer),
00097       mySlice(theSlice)
00098     {}
00099     
00101     TCSlice():
00102       myCContainer(NULL)
00103     {}
00104 
00106     const value_type& 
00107     operator[](size_t theId) const
00108     {
00109       return (*myCContainer)[get_id(theId)];
00110     }
00111     
00112     const value_type& 
00113     at(size_t theId) const
00114     {
00115       return (*myCContainer)[get_id_at(theId)];
00116     }
00117     
00119     size_t
00120     size() const
00121     {
00122       return mySlice.size();
00123     }
00124   };
00125   
00126 
00127   //---------------------------------------------------------------
00129   template<class TContainer> 
00130   class TSlice: public TCSlice<TContainer>
00131   {
00132     typedef TContainer* PContainer;
00133     PContainer myContainer;
00134     
00135   public:
00136     typedef typename TContainer::value_type value_type;
00137     typedef TCSlice<TContainer> TSupperClass;
00138 
00140     TSlice(TContainer& theContainer,
00141         const std::slice& theSlice): 
00142       TSupperClass(theContainer,theSlice),
00143       myContainer(&theContainer)
00144     {
00145     }
00146     
00148     TSlice():
00149       myContainer(NULL)
00150     {}
00151 
00153     value_type& 
00154     operator[](size_t theId)
00155     {
00156       return (*myContainer)[this->get_id(theId)];
00157     }
00158 
00159     value_type& 
00160     at(size_t theId)
00161     {
00162       return (*myContainer)[this->get_id_at(theId)];
00163     }
00164   };
00165 
00166 }
00167 
00168 #undef MED_TCSLICE_CHECK_RANGE
00169 
00170 #endif