ivl 679
ivl/details/array/impl/ptr_interface.hpp
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00024 #ifndef IVL_ARRAY_DETAILS_PTR_INTERFACE_HPP
00025 #define IVL_ARRAY_DETAILS_PTR_INTERFACE_HPP
00026 
00027 namespace ivl {
00028 
00029 namespace array_details {
00030 
00031 template<
00032         class A,
00033         class WRITEABLE = typename A::is_writeable,
00034         class HAS_C_PTR = typename A::has_c_ptr
00035 >
00036 struct ptr_interface
00037 {
00038 };
00039 
00040 // non-const&, with c_ptr
00041 template<class A>
00042 struct ptr_interface<A, types::t_true, types::t_true>
00043 {
00044 public:
00045         typedef typename A::elem_type elem_type;
00046         elem_type* ptr;
00047         ptr_interface(A& a)
00048         {
00049                 ptr = a.c_ptr();
00050         }
00051 };
00052 
00053 // const&, with c_ptr
00054 template<class A>
00055 struct ptr_interface<A, types::t_false, types::t_true>
00056 {
00057 public:
00058         typedef typename A::elem_type elem_type;
00059         const elem_type* ptr;
00060         ptr_interface(const A& a)
00061         {
00062                 ptr = a.c_ptr();
00063         }
00064 };
00065 
00066 // non-const&, without c_ptr
00067 template<class A>
00068 struct ptr_interface<A, types::t_true, types::t_false>
00069 {
00070         A& original; // this is a reference type
00071 
00072         //array<typename A::elem_type> temporary;
00073         // the below line could yield better performance than a single dim array in
00074         // copying from-to array_nd's.
00075         // in case seq_nd is faster loop in some types e.g. subarrays.
00076         // though this is not proven.
00077         typename types::normal_type<A>::type temporary;
00078 
00079 public:
00080         typedef typename A::elem_type elem_type;
00081         elem_type* ptr;
00082 
00083         ptr_interface(A& a) : original(a), temporary(a)
00084         {
00085                 ptr = temporary.c_ptr();
00086         }
00087         ~ptr_interface()
00088         {
00089                 original = temporary;
00090         }
00091 };
00092 
00093 // const&, without c_ptr
00094 template<class A>
00095 struct ptr_interface<A, types::t_false, types::t_false>
00096 {
00097         const A& original; // this is a reference type
00098 
00099         //array<typename A::elem_type> temporary;
00100         // the below line could yield better performance than a single dim array in
00101         // copying from array_nd's.
00102         // in case seq_nd is faster loop in some types e.g. subarrays.
00103         // though this is not proven.
00104         typename types::normal_type<A>::type temporary;
00105 
00106 public:
00107         typedef typename A::elem_type elem_type;
00108         const elem_type* ptr;
00109 
00110         ptr_interface(A& a) : original(a), temporary(a)
00111         {
00112                 ptr = temporary.c_ptr();
00113         }
00114 };
00115 
00116 } /* namespace array_details */
00117 
00118 
00119 /* This class provides a pointer interface to an existing array
00120 of any type. If the array type has has_c_ptr = types::t_true,
00121 the array is used as is, without copy. If not, a temporary array
00122 is created, the data is copied from the original array and,
00123 if the array type is not const, the
00124 data is copied back to the original array at destruction. */
00125 
00126 template <class A>
00127 struct ptr_interface
00128 : public array_details::ptr_interface<
00129         typename types::bare_type<A>::type,
00130         typename types::t_and<types::t_not<types::is_const<A> >,
00131                 typename A::is_writeable>::type,
00132         typename A::has_c_ptr>
00133 {
00134         typedef array_details::ptr_interface<
00135         typename types::bare_type<A>::type,
00136         typename types::t_and<types::t_not<types::is_const<A> >,
00137                 typename A::is_writeable>::type,
00138         typename A::has_c_ptr> base_class;
00139 
00140         template <class X>
00141         ptr_interface(X& a) : base_class(a) { }
00142 };
00143 
00144 } /* namespace ivl */
00145 
00146 #endif // IVL_ARRAY_DETAILS_PTR_INTERFACE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations