image_array.hh

00001 // Copyright (C) 2001, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_CORE_IMPL_IMAGE_ARRAY_HH
00029 # define OLENA_CORE_IMPL_IMAGE_ARRAY_HH
00030 
00031 # include <oln/core/internal/fake.hh>
00032 # include <mlc/contract.hh>
00033 # include <oln/core/coord.hh>
00034 # include <oln/core/impl/image_impl.hh>
00035 # include <iostream>
00036 # include <ntg/all.hh>
00037 
00038 namespace oln {
00039 
00046   template<class T>
00047   void
00048   allocate_data_(T*& buffer, size_t s)
00049   {
00050     precondition(s > 0);
00051     buffer = new T[s];
00052   }
00053 
00059   template<class T>
00060   void
00061   desallocate_data_(T*& buffer)
00062   {
00063     precondition(buffer != 0);
00064     delete[] buffer;
00065     buffer = 0; // security
00066   }
00067 
00068   namespace impl {
00069     template<class T, class Exact>
00070     class image_array;
00071   } // end of impl
00072 
00073 
00079   template<class T, class Exact>
00080   struct impl_traits<impl::image_array<T, Exact> >: public impl_traits<impl::image_impl<Exact> >
00081   {
00082 
00083   };
00084 
00087   namespace impl {
00088 
00094     template<class T, class Exact>
00095     class image_array: public image_impl<Exact>
00096     {
00097 
00098     public:
00099 
00100       typedef typename impl_traits<Exact>::point_type point_type;
00101       typedef typename impl_traits<Exact>::value_type value_type;
00102       typedef typename impl_traits<Exact>::size_type size_type;
00103       typedef Exact exact_type;
00104 
00105 
00106       typedef image_impl<Exact> super_type;
00107       typedef image_array<T, Exact> self_type;
00108 
00109       enum { dim = impl_traits<Exact>::dim };
00110 
00111       friend class image_impl<Exact>;
00112 
00119       image_array(const size_type& s): super_type(s), buffer_(0)
00120       {
00121         allocate_data_(buffer_, len(s));
00122       }
00123 
00124       image_array() : buffer_(0)
00125       {}
00126 
00127       image_array(const self_type&);     // cpy ctor  w/o impl
00128 
00129       void
00130       operator=(const self_type&); // assign op w/o impl
00131 
00137       const T*
00138       buffer() const
00139       {
00140         invariant(buffer_ != 0);
00141         return buffer_;
00142       }
00143 
00149       T*
00150       buffer()
00151       {
00152         invariant(buffer_ != 0);
00153         return buffer_;
00154       }
00155 
00157       size_t
00158       len() const
00159       {
00160         return len(this->size());
00161       }
00162 
00164       size_t
00165       len(const size_type& s) const
00166       {
00167         return this->exact().len_(s);
00168       }
00169 
00170     protected:
00171 
00172 
00173       ~image_array()
00174       {
00175         if (buffer_)
00176           desallocate_data_(buffer_);
00177       }
00178 
00187       void
00188       clone_to_(exact_type* output_data) const
00189       {
00190         precondition(output_data != 0);
00191         precondition(output_data->len() == len());
00192         memcpy(output_data->buffer(),
00193                buffer_,
00194                len() * sizeof(T));
00195       }
00196 
00197       T* buffer_;
00198 
00199     };
00200 
00201   } // end of namespace impl
00202 
00203 } // end of namespace oln
00204 
00205 #endif // ! OLENA_CORE_IMPL_IMAGE_ARRAY_HH

Generated on Thu Apr 15 20:13:11 2004 for Olena by doxygen 1.3.6-20040222