Back to index

salome-med  6.5.0
MED_SliceArray.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  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 //  File   : 
00024 //  Author : 
00025 //  Module : 
00026 //  $Header: /home/server/cvs/MED/MED_SRC/src/MEDWrapper/Base/MED_SliceArray.hxx,v 1.6.2.1.6.2.10.1 2012-04-13 08:48:43 vsr Exp $
00027 //
00028 #ifndef MED_SliceArray_HeaderFile
00029 #define MED_SliceArray_HeaderFile
00030 
00031 #ifdef WNT // for correctly compiling "valarray" in modules, which are includes this file
00032   #undef max
00033   #undef min
00034 #endif
00035 
00036 #include <valarray>
00037 #include <stdexcept>
00038 
00039 //#if defined(_DEBUG_)
00040 #  define MED_TCSLICE_CHECK_RANGE
00041 //#endif
00042 
00043 namespace MED
00044 {
00045   //---------------------------------------------------------------
00047 
00052   template<class TValueType> 
00053   class TCSlice
00054   {
00055     const TValueType* myCValuePtr; 
00056     size_t mySourceSize; 
00057     std::slice mySlice; 
00058 
00059   protected:
00060     void
00061     check_id(size_t theId) const
00062     {
00063       long int anId = -1;
00064       if(theId < mySlice.size()){
00065         anId = mySlice.start() + theId*mySlice.stride();
00066         if(anId < (long int)mySourceSize)
00067           return;
00068       }
00069       throw std::out_of_range("TCSlice::check_id");
00070     }
00071 
00073     size_t
00074     calculate_id(size_t theId) const
00075     {
00076       return mySlice.start() + theId*mySlice.stride();
00077     }
00078     
00079     size_t
00080     get_id(size_t theId) const
00081     {
00082 #ifdef MED_TCSLICE_CHECK_RANGE
00083       check_id(theId);
00084 #endif
00085       return calculate_id(theId);
00086     }
00087     
00088     size_t
00089     get_id_at(size_t theId) const
00090     {
00091       check_id(theId);
00092       return calculate_id(theId);
00093     }
00094 
00095   public:
00096     typedef TValueType value_type;
00097 
00099     TCSlice(const value_type* theValuePtr,
00100             size_t theSourceSize,
00101             const std::slice& theSlice): 
00102       myCValuePtr(theValuePtr),
00103       mySourceSize(theSourceSize),
00104       mySlice(theSlice)
00105     {}
00106     
00108     TCSlice(const TVector<value_type>& theContainer,
00109             const std::slice& theSlice): 
00110       myCValuePtr(&theContainer[0]),
00111       mySourceSize(theContainer.size()),
00112       mySlice(theSlice)
00113     {}
00114     
00116     TCSlice():
00117       myCValuePtr(NULL)
00118     {}
00119 
00121     const value_type& 
00122     operator[](size_t theId) const
00123     {
00124       return *(myCValuePtr + get_id(theId));
00125     }
00126     
00127     const value_type& 
00128     at(size_t theId) const
00129     {
00130       return *(myCValuePtr + get_id_at(theId));
00131     }
00132     
00134     size_t
00135     size() const
00136     {
00137       return mySlice.size();
00138     }
00139   };
00140   
00141 
00142   //---------------------------------------------------------------
00144   template<class TValueType> 
00145   class TSlice: public TCSlice<TValueType>
00146   {
00147     TValueType* myValuePtr;
00148     
00149   public:
00150     typedef TValueType value_type;
00151     typedef TCSlice<TValueType> TSupperClass;
00152 
00154     TSlice(value_type* theValuePtr,
00155            size_t theSourceSize,
00156            const std::slice& theSlice): 
00157       TSupperClass(theValuePtr, theSourceSize, theSlice),
00158       myValuePtr(theValuePtr)
00159     {}
00160     
00162     TSlice(TVector<value_type>& theContainer,
00163            const std::slice& theSlice): 
00164       TSupperClass(theContainer, theSlice),
00165       myValuePtr(&theContainer[0])
00166     {}
00167     
00169     TSlice():
00170       myValuePtr(NULL)
00171     {}
00172 
00174     value_type& 
00175     operator[](size_t theId)
00176     {
00177       return *(myValuePtr + this->get_id(theId));
00178     }
00179 
00180     value_type& 
00181     at(size_t theId)
00182     {
00183       return *(myValuePtr + this->get_id_at(theId));
00184     }
00185   };
00186 
00187 }
00188 
00189 #undef MED_TCSLICE_CHECK_RANGE
00190 
00191 #endif