mirror of
https://github.com/OpenCMT/uLib.git
synced 2025-12-06 07:21:31 +01:00
[uLib Geometry]
adds ImageSpace ImageMap and ImageData stats ProgrammableAccess to data
This commit is contained in:
@@ -43,28 +43,6 @@
|
||||
|
||||
namespace uLib {
|
||||
|
||||
template< typename T, Size_t size >
|
||||
class Array : public std::array<T,size> {
|
||||
|
||||
};
|
||||
|
||||
|
||||
template< typename T, Size_t size >
|
||||
class SmartArray : public SmartPointer< Array<T,size> > {
|
||||
typedef SmartPointer< Array<T,size> > ptr;
|
||||
public:
|
||||
SmartArray() : ptr(new Array<T,size>()) { }
|
||||
SmartArray( const SmartArray ©) : ptr(copy) { }
|
||||
virtual ~SmartArray() {}
|
||||
|
||||
T& operator[](unsigned int p) {
|
||||
return ptr::get()->at(p);
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
62
src/Core/Named.h
Normal file
62
src/Core/Named.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*//////////////////////////////////////////////////////////////////////////////
|
||||
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
|
||||
All rights reserved
|
||||
|
||||
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
|
||||
|
||||
------------------------------------------------------------------
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
#ifndef U_CORE_NAMED_H
|
||||
#define U_CORE_NAMED_H
|
||||
|
||||
#include <Core/Types.h>
|
||||
#include <Core/String.h>
|
||||
|
||||
namespace uLib {
|
||||
|
||||
class Named {
|
||||
public:
|
||||
Named() {}
|
||||
Named(const String &string) : m_name(string) {}
|
||||
Named(const char *name) : m_name(name) {}
|
||||
|
||||
// default generic behaviour //
|
||||
template < typename T >
|
||||
Named(T t) : m_name(typeid(t).name()) {}
|
||||
|
||||
const String GetName() const { return m_name; }
|
||||
|
||||
void SetName(const String name) { m_name = name; }
|
||||
|
||||
template < typename T >
|
||||
void SetName(T t) { m_name = typeid(t).name(); }
|
||||
|
||||
private:
|
||||
String m_name;
|
||||
};
|
||||
|
||||
|
||||
} // uLib
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // NAMED_H
|
||||
@@ -4,6 +4,11 @@
|
||||
#include <Core/Macros.h>
|
||||
#include <Core/Types.h>
|
||||
#include <Core/SmartPointer.h>
|
||||
#include <Core/Named.h>
|
||||
|
||||
#include <Core/Function.h>
|
||||
|
||||
#include <boost/any.hpp>
|
||||
|
||||
namespace uLib {
|
||||
|
||||
@@ -11,11 +16,36 @@ namespace uLib {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename R>
|
||||
class ProgrammableAccessor_Base {
|
||||
|
||||
|
||||
|
||||
|
||||
class any_c : public boost::any {
|
||||
typedef boost::any any;
|
||||
|
||||
public:
|
||||
virtual R Get(void *) const = 0;
|
||||
virtual void Set(void *,const R) const = 0;
|
||||
|
||||
any_c() : any() {}
|
||||
|
||||
template<typename ValueType>
|
||||
any_c(const ValueType & value) : any(value) {}
|
||||
|
||||
any_c(const any & other) : any(other) {}
|
||||
|
||||
// this is needed to tait static_cast using any_cast function //
|
||||
template <typename _TT>
|
||||
operator _TT () const { return boost::any_cast<_TT>(*this); }
|
||||
|
||||
};
|
||||
|
||||
|
||||
template <typename D>
|
||||
class ProgrammableAccessor_Base {
|
||||
protected:
|
||||
|
||||
public:
|
||||
virtual D Get(void *) const = 0;
|
||||
virtual void Set(void *,const D) const = 0;
|
||||
|
||||
template <typename T>
|
||||
T Get(void *ob) const {
|
||||
@@ -24,56 +54,56 @@ public:
|
||||
|
||||
template <typename T>
|
||||
void Set(void *ob, const T data) const {
|
||||
this->Set(ob,static_cast<R>(data));
|
||||
this->Set(ob,static_cast<D>(data));
|
||||
}
|
||||
|
||||
virtual ~ProgrammableAccessor_Base() {}
|
||||
};
|
||||
|
||||
|
||||
template<typename R, typename T>
|
||||
class functor_by_ref : public ProgrammableAccessor_Base<R>
|
||||
template<typename D, typename R, typename T>
|
||||
class functor_by_ref : public ProgrammableAccessor_Base<D>
|
||||
{
|
||||
public:
|
||||
explicit functor_by_ref(R&(T::*__pf)()) : MFRef(__pf) {}
|
||||
R Get(void * ptr) const
|
||||
{ return static_cast<R>((reinterpret_cast<T *>(ptr)->*MFRef)()); }
|
||||
void Set(void * ptr, const R data) const
|
||||
{ ((reinterpret_cast<T *>(ptr)->*MFRef)()) = data; }
|
||||
D Get(void * ptr) const
|
||||
{ return static_cast<D>((reinterpret_cast<T *>(ptr)->*MFRef)()); }
|
||||
void Set(void * ptr, const D data) const
|
||||
{ ((reinterpret_cast<T *>(ptr)->*MFRef)()) = static_cast<const R>(data); }
|
||||
private:
|
||||
R&(T::*MFRef)();
|
||||
};
|
||||
|
||||
template<typename R, typename T>
|
||||
class functor_by_mfptr_cc : public ProgrammableAccessor_Base<R>
|
||||
template<typename D, typename R, typename T>
|
||||
class functor_by_mfptr_cc : public ProgrammableAccessor_Base<D>
|
||||
{
|
||||
typedef R(T::*GetterType)() const;
|
||||
typedef void(T::*SetterType)(const R);
|
||||
public:
|
||||
explicit functor_by_mfptr_cc( GetterType _get, SetterType _set ) :
|
||||
GetPtr(_get), SetPtr(_set) {}
|
||||
R Get(void * ptr) const
|
||||
{ return static_cast<R>((reinterpret_cast<T *>(ptr)->*GetPtr)()); }
|
||||
void Set(void * ptr, const R data) const
|
||||
{ if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(data); }
|
||||
D Get(void * ptr) const
|
||||
{ return static_cast<D>((reinterpret_cast<T *>(ptr)->*GetPtr)()); }
|
||||
void Set(void * ptr, const D data) const
|
||||
{ if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(static_cast<const R>(data)); }
|
||||
|
||||
private:
|
||||
GetterType GetPtr;
|
||||
SetterType SetPtr;
|
||||
};
|
||||
|
||||
template<typename R, typename T>
|
||||
class functor_by_mfptr_vv : public ProgrammableAccessor_Base<R>
|
||||
template<typename D, typename R, typename T>
|
||||
class functor_by_mfptr_vv : public ProgrammableAccessor_Base<D>
|
||||
{
|
||||
typedef R(T::*GetterType)();
|
||||
typedef void(T::*SetterType)(R);
|
||||
public:
|
||||
explicit functor_by_mfptr_vv( GetterType _get, SetterType _set ) :
|
||||
GetPtr(_get), SetPtr(_set) {}
|
||||
R Get(void * ptr) const
|
||||
{ return static_cast<R>((reinterpret_cast<T *>(ptr)->*GetPtr)()); }
|
||||
void Set(void * ptr, const R data) const
|
||||
{ if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(data); }
|
||||
D Get(void * ptr) const
|
||||
{ return static_cast<D>((reinterpret_cast<T *>(ptr)->*GetPtr)()); }
|
||||
void Set(void * ptr, const D data) const
|
||||
{ if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(static_cast<const R>(data)); }
|
||||
|
||||
private:
|
||||
GetterType GetPtr;
|
||||
@@ -81,15 +111,15 @@ private:
|
||||
};
|
||||
|
||||
|
||||
template<typename R, typename T>
|
||||
class functor_by_member : public ProgrammableAccessor_Base<R>
|
||||
template<typename D, typename R, typename T>
|
||||
class functor_by_member : public ProgrammableAccessor_Base<D>
|
||||
{
|
||||
public:
|
||||
explicit functor_by_member(R T::*__pf) : MDPtr(__pf) {}
|
||||
R Get(void * ptr) const
|
||||
{ return static_cast<R>(reinterpret_cast<T *>(ptr)->*MDPtr); }
|
||||
void Set(void * ptr, const R data) const
|
||||
{ (reinterpret_cast<T *>(ptr)->*MDPtr) = data; }
|
||||
D Get(void * ptr) const
|
||||
{ return static_cast<D>(reinterpret_cast<T *>(ptr)->*MDPtr); }
|
||||
void Set(void * ptr, const D data) const
|
||||
{ (reinterpret_cast<T *>(ptr)->*MDPtr) = static_cast<const D>(data); }
|
||||
private:
|
||||
R T::*MDPtr;
|
||||
};
|
||||
@@ -103,36 +133,86 @@ private:
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
template <typename R>
|
||||
class ProgrammableAccessor {
|
||||
|
||||
|
||||
|
||||
template <typename D >
|
||||
class ProgrammableAccessor : public Named {
|
||||
public:
|
||||
template <typename T>
|
||||
ProgrammableAccessor(R(T::*_pg)()const, void(T::*_ps)(const R) = NULL) :
|
||||
m_base(new detail::functor_by_mfptr_cc<R,T>(_pg,_ps)) {}
|
||||
|
||||
struct Wrapper {
|
||||
Wrapper(const ProgrammableAccessor<D> *ac, void *ob) :
|
||||
m_access(ac), m_object(ob)
|
||||
{ assert(ob != NULL); }
|
||||
|
||||
template <typename T>
|
||||
inline T Get() const { return m_access->Get<T>(m_object); }
|
||||
|
||||
template <typename T>
|
||||
inline void Set(const T data) const { return m_access->Set<T>(m_object,data); }
|
||||
|
||||
void *m_object;
|
||||
const ProgrammableAccessor<D> *m_access;
|
||||
};
|
||||
|
||||
ProgrammableAccessor() : Named("no access defined") {}
|
||||
|
||||
template < typename F >
|
||||
ProgrammableAccessor(F f) : Named(f)
|
||||
{ SetAccessFunctions(f); }
|
||||
|
||||
template < typename F1, typename F2 >
|
||||
ProgrammableAccessor(F1 f1, F2 f2) : Named(f1) // << FIX
|
||||
{ SetAccessFunctions(f1,f2); }
|
||||
|
||||
|
||||
// ----- move to factory //
|
||||
template <typename R, typename T>
|
||||
inline void SetAccessFunctions(R(T::*_pg)()const, void(T::*_ps)(const R) = NULL)
|
||||
{
|
||||
m_base = SmartPointer< detail::ProgrammableAccessor_Base<D> >(new detail::functor_by_mfptr_cc<D,R,T>(_pg,_ps));
|
||||
this->SetName(_pg); // << FIX
|
||||
}
|
||||
|
||||
template <typename R, typename T>
|
||||
inline void SetAccessFunctions(R(T::*_pg)(), void(T::*_ps)(R) = NULL) {
|
||||
m_base = SmartPointer< detail::ProgrammableAccessor_Base<D> >(new detail::functor_by_mfptr_vv<D,R,T>(_pg,_ps));
|
||||
this->SetName(_pg); // << FIX
|
||||
}
|
||||
|
||||
template <typename R, typename T>
|
||||
inline void SetAccessFunctions(R&(T::*_pf)()) {
|
||||
m_base = SmartPointer< detail::ProgrammableAccessor_Base<D> >(new detail::functor_by_ref<D,R,T>(_pf));
|
||||
this->SetName(_pf);
|
||||
}
|
||||
|
||||
template <typename R, typename T>
|
||||
inline void SetAccessFunctions(R T::*_pf) {
|
||||
m_base = SmartPointer< detail::ProgrammableAccessor_Base<D> >(new detail::functor_by_member<D,R,T>(_pf));
|
||||
this->SetName(_pf);
|
||||
}
|
||||
// ------ //
|
||||
|
||||
const Wrapper operator() (void *ob) const { return Wrapper(this,ob); }
|
||||
|
||||
inline D Get(void *ob) const { return m_base->Get(ob); }
|
||||
|
||||
inline void Set(void *ob, const D data) const { m_base->Set(ob,data); }
|
||||
|
||||
template <typename T>
|
||||
ProgrammableAccessor(R(T::*_pg)(), void(T::*_ps)(R) = NULL) :
|
||||
m_base(new detail::functor_by_mfptr_vv<R,T>(_pg,_ps)) {}
|
||||
inline T Get(void *ob) const { return m_base->Get<T>(ob); }
|
||||
|
||||
template <typename T>
|
||||
ProgrammableAccessor(R&(T::*_pf)()) :
|
||||
m_base(new detail::functor_by_ref<R,T>(_pf)) {}
|
||||
|
||||
template <typename T>
|
||||
ProgrammableAccessor(R T::*_pf) :
|
||||
m_base(new detail::functor_by_member<R,T>(_pf)) {}
|
||||
|
||||
template <typename OtherR>
|
||||
inline OtherR Get(void *ob) const { return m_base->Get<OtherR>(ob); }
|
||||
|
||||
template <typename OtherR>
|
||||
inline void Set(void *ob, const OtherR data) const { return m_base->Set<OtherR>(ob,data); }
|
||||
inline void Set(void *ob, const T data) const { return m_base->Set<T>(ob,data); }
|
||||
|
||||
private:
|
||||
SmartPointer< detail::ProgrammableAccessor_Base<R> > m_base;
|
||||
SmartPointer< detail::ProgrammableAccessor_Base<D> > m_base;
|
||||
};
|
||||
|
||||
|
||||
typedef ProgrammableAccessor<detail::any_c> ProgrammableAccessorAny;
|
||||
|
||||
|
||||
} // uLib
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <Core/Vector.h>
|
||||
#include <Core/ProgrammableAccessor.h>
|
||||
|
||||
#include <iterator>
|
||||
|
||||
namespace uLib {
|
||||
|
||||
class TestVoxel {
|
||||
@@ -22,6 +24,64 @@ public:
|
||||
|
||||
|
||||
|
||||
//template<typename _T>
|
||||
//class PointerIterator :
|
||||
// public std::iterator<std::random_access_iterator_tag, _T>
|
||||
//{
|
||||
//protected:
|
||||
// _T* m_pData;
|
||||
//public:
|
||||
// typedef std::random_access_iterator_tag iterator_category;
|
||||
// typedef typename std::iterator<std::random_access_iterator_tag, _T>::value_type value_type;
|
||||
// typedef typename std::iterator<std::random_access_iterator_tag, _T>::difference_type difference_type;
|
||||
// typedef typename std::iterator<std::random_access_iterator_tag, _T>::reference reference;
|
||||
// typedef typename std::iterator<std::random_access_iterator_tag, _T>::pointer pointer;
|
||||
|
||||
// PointerIterator() : m_pData(NULL) {}
|
||||
|
||||
// template<typename T2>
|
||||
// PointerIterator(const PointerIterator<T2>& r) : m_pData(&(*r)) {}
|
||||
|
||||
// PointerIterator(pointer pData) : m_pData(pData) {}
|
||||
|
||||
// template<typename T2>
|
||||
// PointerIterator& operator=(const PointerIterator<T2>& r)
|
||||
// { m_pData = &(*r); return *this; }
|
||||
|
||||
// PointerIterator& operator++() // PREFIX
|
||||
// { ++m_pData; return *this; }
|
||||
// PointerIterator& operator--() // PREFIX
|
||||
// { --m_pData; return *this; }
|
||||
// PointerIterator operator++(int) // POSTFIX
|
||||
// { return PointerIterator(m_pData++); }
|
||||
// PointerIterator operator--(int) // POSTFIX
|
||||
// { return PointerIterator(m_pData--); }
|
||||
// PointerIterator operator+(const difference_type& n) const
|
||||
// { return PointerIterator(m_pData + n); }
|
||||
// PointerIterator& operator+=(const difference_type& n)
|
||||
// { m_pData += n; return *this; }
|
||||
// PointerIterator operator-(const difference_type& n) const
|
||||
// { return PointerIterator(pointer(m_pData - n)); }
|
||||
// PointerIterator& operator-=(const difference_type& n) { m_pData -= n; return *this; }
|
||||
// reference operator*() const { return *m_pData; }
|
||||
// pointer operator->() const { return m_pData; }
|
||||
// reference operator[](const difference_type& n) const { return m_pData[n]; }
|
||||
//};
|
||||
|
||||
|
||||
|
||||
//class DataSetView {
|
||||
|
||||
// template <typename T>
|
||||
// T Get(Id_t id) { return m_view.Get<T>(this->GetPointData(id)); }
|
||||
|
||||
|
||||
|
||||
//private:
|
||||
// ProgrammableAccessorAny m_view;
|
||||
//};
|
||||
|
||||
|
||||
} // uLib
|
||||
|
||||
using namespace uLib;
|
||||
@@ -42,13 +102,18 @@ int main() {
|
||||
|
||||
v[5].m_data = 5552368.0;
|
||||
|
||||
ProgrammableAccessor<float> f1(&TestVoxel::m_data);
|
||||
ProgrammableAccessor<float> f2(&TestVoxel::GetRef);
|
||||
ProgrammableAccessor<int> f1(&TestVoxel::m_data);
|
||||
ProgrammableAccessor<char> f2(&TestVoxel::ConstGet);
|
||||
ProgrammableAccessorAny f3(&TestVoxel::m_data);
|
||||
ProgrammableAccessor<detail::any_c> f4(&TestVoxel::m_data);
|
||||
|
||||
|
||||
|
||||
int i=0;
|
||||
foreach (TestVoxel &el,v) {
|
||||
f1.Set(&el,i++);
|
||||
std::cout << " -> " << f2.Get<double>(&el) << "\n";
|
||||
std::cout << " -> " << f2.Get(&el) << " - " << f3.Get<float>(&el) << f4.Get<int>(&el) << f1(&el).Get<float>() << "\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user