fix non-static functor issues

This commit is contained in:
Christian Zimmermann 2018-09-12 20:56:55 +02:00
parent 6896cc3ca7
commit 405df0c426
3 changed files with 48 additions and 21 deletions

View file

@ -108,17 +108,17 @@ namespace MultiArrayTools
private: private:
mutable T mVal; mutable T mVal;
Function mFunc; std::shared_ptr<Function> mFunc;
mutable std::shared_ptr<MAType> mMaPtr; mutable std::shared_ptr<MAType> mMaPtr;
public: public:
DEFAULT_MEMBERS(FunctionalMultiArray); DEFAULT_MEMBERS(FunctionalMultiArray);
FunctionalMultiArray(const std::shared_ptr<SRanges>&... ranges, const Function& func); FunctionalMultiArray(const std::shared_ptr<SRanges>&... ranges, const std::shared_ptr<Function>& func);
FunctionalMultiArray(const std::shared_ptr<SRanges>&... ranges); FunctionalMultiArray(const std::shared_ptr<SRanges>&... ranges);
FunctionalMultiArray(const typename CRange::Space& space); FunctionalMultiArray(const typename CRange::Space& space);
FunctionalMultiArray(const typename CRange::Space& space, const Function& func); FunctionalMultiArray(const typename CRange::Space& space, const std::shared_ptr<Function>& func);
virtual const T& operator[](const IndexType& i) const override; virtual const T& operator[](const IndexType& i) const override;
virtual const T& at(const typename CRange::IndexType::MetaType& meta) const override; virtual const T& at(const typename CRange::IndexType::MetaType& meta) const override;
@ -164,9 +164,9 @@ namespace MultiArrayTools
struct Application struct Application
{ {
template <typename T, class Function, typename Meta> template <typename T, class Function, typename Meta>
static inline T apply(const Function& f, const Meta& m) static inline T apply(const std::shared_ptr<Function>& f, const Meta& m)
{ {
return f(m); return (*f)(m);
} }
}; };
@ -174,7 +174,7 @@ namespace MultiArrayTools
struct Application<true> struct Application<true>
{ {
template <typename T, class Function, typename Meta> template <typename T, class Function, typename Meta>
static inline T apply(const Function& f, const Meta& m) static inline T apply(const std::shared_ptr<Function>& f, const Meta& m)
{ {
return Function::apply(m); return Function::apply(m);
} }
@ -182,7 +182,7 @@ namespace MultiArrayTools
template <typename T, class Function, class... SRanges> template <typename T, class Function, class... SRanges>
FunctionalMultiArray<T,Function,SRanges...>::FunctionalMultiArray(const std::shared_ptr<SRanges>&... ranges, FunctionalMultiArray<T,Function,SRanges...>::FunctionalMultiArray(const std::shared_ptr<SRanges>&... ranges,
const Function& func) : const std::shared_ptr<Function>& func) :
MultiArrayBase<T,SRanges...>(ranges...), mFunc(func) {} MultiArrayBase<T,SRanges...>(ranges...), mFunc(func) {}
template <typename T, class Function, class... SRanges> template <typename T, class Function, class... SRanges>
@ -195,7 +195,7 @@ namespace MultiArrayTools
template <typename T, class Function, class... SRanges> template <typename T, class Function, class... SRanges>
FunctionalMultiArray<T,Function,SRanges...>::FunctionalMultiArray(const typename CRange::Space& space, FunctionalMultiArray<T,Function,SRanges...>::FunctionalMultiArray(const typename CRange::Space& space,
const Function& func) : const std::shared_ptr<Function>& func) :
MultiArrayBase<T,SRanges...>(space), mFunc(func) {} MultiArrayBase<T,SRanges...>(space), mFunc(func) {}

View file

@ -373,11 +373,37 @@ namespace MultiArrayTools
}; };
namespace
{
template <bool FISSTATIC>
struct OpMaker
{
template <class OpFunction, class... Ops>
static inline auto mkOperation(const std::shared_ptr<OpFunction>& f, const Ops&... ops)
-> Operation<typename OpFunction::value_type,OpFunction,Ops...>
{
return Operation<typename OpFunction::value_type,OpFunction,Ops...>(f,ops...);
}
};
template <>
struct OpMaker<true>
{
template <class OpFunction, class... Ops>
static inline auto mkOperation(const std::shared_ptr<OpFunction>& f, const Ops&... ops)
-> Operation<typename OpFunction::value_type,OpFunction,Ops...>
{
return Operation<typename OpFunction::value_type,OpFunction,Ops...>(ops...);
}
};
}
template <class OpFunction, class... Ops> template <class OpFunction, class... Ops>
auto mkOperation(const OpFunction& f, const Ops&... ops) auto mkOperation(const std::shared_ptr<OpFunction>& f, const Ops&... ops)
-> Operation<typename OpFunction::value_type,OpFunction,Ops...> -> Operation<typename OpFunction::value_type,OpFunction,Ops...>
{ {
return Operation<typename OpFunction::value_type,OpFunction,Ops...>(ops...); return OpMaker<OpFunction::FISSTATIC>::mkOperation(f, ops...);
} }

View file

@ -35,8 +35,7 @@ namespace MultiArrayTools
typedef ValueRange<U> RangeType; typedef ValueRange<U> RangeType;
typedef ValueIndex IType; typedef ValueIndex IType;
ValueIndex(const U& val); // default range ValueIndex(const std::shared_ptr<RangeType>& rptr);
ValueIndex(U&& val); // default range
static constexpr IndexType sType() { return IndexType::SINGLE; } static constexpr IndexType sType() { return IndexType::SINGLE; }
static constexpr size_t totalDim() { return 1; } static constexpr size_t totalDim() { return 1; }
@ -124,6 +123,12 @@ namespace MultiArrayTools
static constexpr size_t SIZE = 1; static constexpr size_t SIZE = 1;
static constexpr bool HASMETACONT = false; static constexpr bool HASMETACONT = false;
static std::shared_ptr<ValueRange> Default()
{
ValueRangeFactory<U> vrf;
return std::dynamic_pointer_cast<ValueRange>( vrf.create() );
}
protected: protected:
ValueRange() = default; ValueRange() = default;
@ -147,19 +152,15 @@ namespace MultiArrayTools
std::shared_ptr<RangeBase> mkDefaultValueRange() std::shared_ptr<RangeBase> mkDefaultValueRange()
{ {
ValueRangeFactory<U> vrf; ValueRangeFactory<U> vrf;
return vrf->create(); return vrf.create();
} }
} }
template <typename U> template <typename U>
ValueIndex<U>::ValueIndex(const U& val) : IndexInterface<ValueIndex<U>,U>(mkDefaultValueRange<U>(), 0), ValueIndex<U>::ValueIndex(const std::shared_ptr<RangeType>& rptr) :
mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr)), IndexInterface<ValueIndex<U>,U>(rptr, 0),
mMeta(val) {} mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr))
{}
template <typename U>
ValueIndex<U>::ValueIndex(U&& val) : IndexInterface<ValueIndex<U>,U>(mkDefaultValueRange<U>(), 0),
mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr)),
mMeta(val) {}
template <typename U> template <typename U>
IndexType ValueIndex<U>::type() const IndexType ValueIndex<U>::type() const