ivl 679
|
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_CORE_DETAILS_KEYWORD_OPERATORS_LINK_HPP 00025 #define IVL_CORE_DETAILS_KEYWORD_OPERATORS_LINK_HPP 00026 00027 namespace ivl { 00028 00029 namespace core_details { 00030 00031 template<class A, class IS_ITER_C_PTR> 00032 struct storable_link_ops_4 { }; 00033 00034 template<class A> 00035 struct storable_link_ops_4<A, types::t_true> 00036 { 00037 typedef typename types::change_data_class_set< 00038 data::const_link, A>::type type; 00039 static inline type from(const A& a) { return type(a); } 00040 }; 00041 00042 template<class A> 00043 struct storable_link_ops_4<A, types::t_false> 00044 { 00045 typedef typename types::change_data_class_set< 00046 data::const_link, A>::type type; 00047 static inline type from(const A& a) 00048 { 00049 ivl::array<typename A::elem_type, data::ref_iterator<> > 00050 data(a.c_ptr(), a.length()); 00051 return type(a.size(), data); 00052 } 00053 }; 00054 00055 template<class A, class HAS_C_PTR> 00056 struct storable_link_ops_3 { }; 00057 00058 template<class A> 00059 struct storable_link_ops_3<A, types::t_true> : 00060 public storable_link_ops_4<A, 00061 typename types::is_ptr<typename A::const_iterator>::type> 00062 { }; 00063 00064 template<class A> 00065 struct storable_link_ops_3<A, types::t_false> 00066 { 00067 typedef typename types::change_data_class_set< 00068 data::storable_link, A>::type type; 00069 static inline type from(const A& a) 00070 { 00071 void* c_data = malloc(sizeof(typename A::elem_type) * a.length()); 00072 array<typename A::elem_type, data::const_link> 00073 data(c_data, a.length()); 00074 // copy the data to out new array, which is a reference to a pointer 00075 // that needs to be deallocated. 00076 data = a; 00077 type r(a.size(), data); 00078 r.set_free(); 00079 return r; 00080 } 00081 }; 00082 00083 // --- 00084 00085 template<class A, class IS_IVL_ARRAY> 00086 struct storable_link_ops_2 { }; 00087 00088 template<class A> 00089 struct storable_link_ops_2<A, types::t_false> 00090 { 00091 typedef const A& type; 00092 static inline type from(type a) { return a; } 00093 }; 00094 00095 template<class A> 00096 struct storable_link_ops_2<A, types::t_true> : 00097 public storable_link_ops_3<A, typename A::has_c_ptr> 00098 { }; 00099 00100 // --- 00101 00102 template<class A> 00103 struct storable_link_ops : public storable_link_ops_2<A, 00104 typename types::is_ivl_array<A>::type> 00105 { }; 00106 00107 } /* namespace core_details */ 00108 00109 template<class A> 00110 typename core_details::storable_link_ops<A>::type storable_link(const A& a) 00111 { 00112 return core_details::storable_link_ops<A>::from(a); 00113 } 00114 00115 } /* namespace ivl */ 00116 00117 #endif // IVL_CORE_DETAILS_KEYWORD_OPERATORS_LINK_HPP