19 #ifndef __otbOGRFieldWrapper_txx
20 #define __otbOGRFieldWrapper_txx
28 #include <boost/mpl/map.hpp>
29 #include <boost/mpl/vector.hpp>
30 #include <boost/mpl/pair.hpp>
31 #include <boost/mpl/int.hpp>
32 #include <boost/mpl/at.hpp>
33 #include <boost/mpl/assert.hpp>
36 #include <boost/static_assert.hpp>
37 #include <boost/range/size.hpp>
38 #include <boost/type_traits/is_same.hpp>
42 #include "ogr_feature.h"
43 #include "cpl_string.h"
51 namespace otb {
namespace ogr {
62 using namespace boost::mpl;
71 typedef boost::mpl::map
72 < pair<int , int_<OFTInteger> >
73 , pair<std::vector<int> , int_<OFTIntegerList> >
74 , pair<double , int_<OFTReal> >
75 , pair<std::vector<double> , int_<OFTRealList> >
76 , pair<std::string , int_<OFTString> >
77 , pair<char* , int_<OFTString> >
78 , pair<char const* , int_<OFTString> >
79 , pair<std::vector<std::string>, int_<OFTStringList> >
101 , T ( OGRFeature::*ptr_to_function )(int)
102 ,
typename FinalReturnType = T
106 static FinalReturnType call(OGRFeature &f,
int index)
108 return (f.*ptr_to_function)(index);
125 <
typename FinalReturnType = std::vector<std::string>
129 static FinalReturnType call(OGRFeature &f,
int index)
131 char ** sl = f.GetFieldAsStringList(index);
132 FinalReturnType res(sl, sl+CSLCount(sl));
151 , void ( OGRFeature::*ptr_to_function )(int, T value)
152 ,
typename ActualParamType = T
156 static void call(OGRFeature &f,
int index, T
const& value)
158 (f.*ptr_to_function)(index, value);
177 , T
const* ( OGRFeature::*ptr_to_function )(
int,
int*)
178 ,
typename FinalReturnType = std::vector<T>
182 static FinalReturnType call(OGRFeature &f,
int index)
185 T
const* raw_container = (f.*ptr_to_function)(index, &nb);
186 const FinalReturnType res(raw_container+0, raw_container+nb);
204 , void ( OGRFeature::*ptr_to_function )(int, int, T*)
205 ,
typename ActualParamType = std::vector<T>
213 , void ( OGRFeature::*ptr_to_function )(int, int, T*)
214 ,
typename ActualParamType
218 static void call(OGRFeature &f,
int index, ActualParamType
const& container)
220 const int nb = boost::size(container);
221 (f.*ptr_to_function)(index, nb, const_cast <T*>(&container[0]));
229 , void ( OGRFeature::*ptr_to_function )(int, int, T*)
230 ,
typename ActualParamType
234 static void call(OGRFeature &f,
int index, ActualParamType
const& container)
236 const int nb = boost::size(container);
237 std::vector<T> v(boost::begin(container), boost::end(container));
238 (f.*ptr_to_function)(index, nb, &v[0]);
254 <
typename ActualParamType
258 static void call(OGRFeature &f,
int index, ActualParamType
const& container)
279 , void ( OGRFeature::*ptr_to_function )(int, int, T*)
280 ,
typename ActualParamType = std::vector<T>
284 static void call(OGRFeature &f,
int index, ActualParamType
const& container)
291 >::call(f, index, container);
302 < pair<int_<OFTInteger>, MemberGetterPtr<int, &OGRFeature::GetFieldAsInteger> >
303 , pair<int_<OFTIntegerList>, MemberContainerGetterPtr<int, &OGRFeature::GetFieldAsIntegerList> >
304 , pair<int_<OFTReal>, MemberGetterPtr<double, &OGRFeature::GetFieldAsDouble> >
305 , pair<int_<OFTRealList>, MemberContainerGetterPtr<double, &OGRFeature::GetFieldAsDoubleList> >
306 , pair<int_<OFTString>, MemberGetterPtr<char const*, &OGRFeature::GetFieldAsString, std::string> >
307 , pair<int_<OFTStringList>, StringListMemberGetterPtr<std::vector<std::string> > >
321 , pair<int_<OFTString>,
MemberSetterPtr<
char const*, &OGRFeature::SetField> >
337 static T
const&
convert(T
const& value) {
return value; }
346 static T
const*
convert(
const T value[N]) {
return &value[0]; }
355 static char const*
convert(std::string
const& value) {
return value.c_str(); }
368 assert(m_Feature &&
"OGR Fields must be associated to a valid feature");
369 assert(
int(m_index) < m_Feature->GetFieldCount() &&
"Out-of-range index for a OGR field");
370 assert(m_Feature->GetFieldDefnRef(m_index) &&
"No definition available for the OGR field");
373 template <
typename T>
379 typedef typename Converter::type InterfaceType;
382 BOOST_MPL_ASSERT_MSG(!boost::is_array<InterfaceType>::value, InterFaceType_Cant_Be_An_array, (T, InterfaceType));
383 typedef typename boost::mpl::at<internal::FieldType_Map, InterfaceType>::type Kind;
384 BOOST_MPL_ASSERT_MSG(!(boost::is_same<Kind, boost::mpl::void_>::value), UNEXPECTED_KIND_TYPE, (T, InterfaceType, Kind));
385 const int VALUE = Kind::value;
386 assert(m_Definition.GetType() == VALUE &&
"OGR field type mismatches the type of new field value");
387 typedef typename boost::mpl::at<internal::FieldSetters_Map, Kind>::type SetterType;
391 SetterType::call(*m_Feature, m_index, Converter::convert(value));
394 template <
typename T>
399 assert(HasBeenSet() &&
"Cannot access the value of a field that hasn't been set");
400 typedef typename boost::mpl::at<internal::FieldType_Map, T>::type Kind;
401 const int VALUE = Kind::value;
403 assert(m_Definition.GetType() == VALUE &&
"OGR field type mismatches the type of requested field value");
404 typedef typename boost::mpl::at<internal::FieldGetters_Map, Kind>::type GetterType;
408 return GetterType::call(*m_Feature, m_index);
416 return UncheckedPrintSelf(os, indent);
423 return UncheckedHasBeenSet();
438 assert(f.
GetType() == this->GetType() &&
"Cannot assign from a field that doesn't have the same definition");
450 return const_cast <
Field*>(
this)->ogr();
457 OGRField * f = m_Feature->GetRawFieldRef(m_index);
458 assert(f &&
"The field obtained shall not be null");
462 #endif // __otbOGRFieldWrapper_txx