fix leak issue

This commit is contained in:
Christian Zimmermann 2017-08-04 16:19:50 +02:00
parent d2a1286d17
commit 7a16f22f78
5 changed files with 159 additions and 124 deletions

View file

@ -10,77 +10,7 @@ namespace MultiArrayTools
using namespace MultiArrayHelper; using namespace MultiArrayHelper;
} }
/*****************************
* ContainerRangeFactory *
*****************************/
template <class... Ranges>
ContainerRangeFactory<Ranges...>::ContainerRangeFactory(const std::shared_ptr<Ranges>&... rs)
{
mProd = std::make_shared<ContainerRange<Ranges...> >( rs... );
}
template <class... Ranges>
ContainerRangeFactory<Ranges...>::
ContainerRangeFactory(const typename ContainerRange<Ranges...>::SpaceType& space)
{
mProd = std::make_shared<ContainerRange<Ranges...> >( space );
}
template <class... Ranges>
std::shared_ptr<RangeBase> ContainerRangeFactory<Ranges...>::create()
{
setSelf();
return mProd;
}
/**********************
* ContainerRange *
**********************/
template <class... Ranges>
ContainerRange<Ranges...>::ContainerRange(const std::shared_ptr<Ranges>&... rs) :
mSpace( std::make_tuple( rs... ) ) {}
template <class... Ranges>
ContainerRange<Ranges...>::ContainerRange(const SpaceType& space) : mSpace( space ) {}
template <class... Ranges>
size_t ContainerRange<Ranges...>::dim() const
{
return sizeof...(Ranges);
}
template <class... Ranges>
size_t ContainerRange<Ranges...>::size() const
{
return PackNum<sizeof...(Ranges)-1>::getSize(mSpace);
}
template <class... Ranges>
typename ContainerRange<Ranges...>::IndexType ContainerRange<Ranges...>::begin() const
{
ContainerIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<ContainerRange<Ranges...> >( RB::mThis ) );
return i = 0;
}
template <class... Ranges>
typename ContainerRange<Ranges...>::IndexType ContainerRange<Ranges...>::end() const
{
ContainerIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<ContainerRange<Ranges...> >( RB::mThis ) );
return i = size();
}
template <class... Ranges>
std::shared_ptr<IndexBase> ContainerRange<Ranges...>::index() const
{
return std::make_shared<ContainerIndex<typename Ranges::IndexType...> >
( std::dynamic_pointer_cast<ContainerRange<Ranges...> >( RB::mThis ) );
}
/********************** /**********************
* ContainerIndex * * ContainerIndex *
**********************/ **********************/
@ -113,6 +43,9 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator++() ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator++()
{ {
if(mExternControl){
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
}
PackNum<sizeof...(Indices)-1>::pp( mIPack ); PackNum<sizeof...(Indices)-1>::pp( mIPack );
++IB::mPos; ++IB::mPos;
return *this; return *this;
@ -121,6 +54,9 @@ namespace MultiArrayTools
template <class... Indices> template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator--() ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator--()
{ {
if(mExternControl){
IB::mPos = PackNum<sizeof...(Indices)-1>::makePos(mIPack);
}
PackNum<sizeof...(Indices)-1>::mm( mIPack ); PackNum<sizeof...(Indices)-1>::mm( mIPack );
--IB::mPos; --IB::mPos;
return *this; return *this;
@ -165,6 +101,19 @@ namespace MultiArrayTools
return IB::mPos; return IB::mPos;
} }
template <class... Indices>
bool ContainerIndex<Indices...>::first() const
{
return pos() == 0;
}
template <class... Indices>
bool ContainerIndex<Indices...>::last() const
{
return pos() == IB::mRangePtr->size() - 1;
}
template <class... Indices> template <class... Indices>
ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()(const std::shared_ptr<Indices>&... inds) ContainerIndex<Indices...>& ContainerIndex<Indices...>::operator()(const std::shared_ptr<Indices>&... inds)
{ {
@ -173,4 +122,77 @@ namespace MultiArrayTools
return *this; return *this;
} }
/*****************************
* ContainerRangeFactory *
*****************************/
template <class... Ranges>
ContainerRangeFactory<Ranges...>::ContainerRangeFactory(const std::shared_ptr<Ranges>&... rs)
{
mProd = std::shared_ptr<ContainerRange<Ranges...> >( new ContainerRange<Ranges...>( rs... ) );
}
template <class... Ranges>
ContainerRangeFactory<Ranges...>::
ContainerRangeFactory(const typename ContainerRange<Ranges...>::SpaceType& space)
{
mProd = std::shared_ptr<ContainerRange<Ranges...> >( new ContainerRange<Ranges...>( space ) );
}
template <class... Ranges>
std::shared_ptr<RangeBase> ContainerRangeFactory<Ranges...>::create()
{
setSelf();
return mProd;
}
/**********************
* ContainerRange *
**********************/
template <class... Ranges>
ContainerRange<Ranges...>::ContainerRange(const std::shared_ptr<Ranges>&... rs) :
mSpace( std::make_tuple( rs... ) ) {}
template <class... Ranges>
ContainerRange<Ranges...>::ContainerRange(const SpaceType& space) : mSpace( space ) {}
template <class... Ranges>
size_t ContainerRange<Ranges...>::dim() const
{
return sizeof...(Ranges);
}
template <class... Ranges>
size_t ContainerRange<Ranges...>::size() const
{
return PackNum<sizeof...(Ranges)-1>::getSize(mSpace);
}
template <class... Ranges>
typename ContainerRange<Ranges...>::IndexType ContainerRange<Ranges...>::begin() const
{
ContainerIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<ContainerRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
return i = 0;
}
template <class... Ranges>
typename ContainerRange<Ranges...>::IndexType ContainerRange<Ranges...>::end() const
{
ContainerIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<ContainerRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
return i = size();
}
template <class... Ranges>
std::shared_ptr<IndexBase> ContainerRange<Ranges...>::index() const
{
return std::make_shared<ContainerIndex<typename Ranges::IndexType...> >
( std::dynamic_pointer_cast<ContainerRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
}
} // end namespace MultiArrayTools } // end namespace MultiArrayTools

View file

@ -13,6 +13,47 @@
namespace MultiArrayTools namespace MultiArrayTools
{ {
template <class... Indices>
class ContainerIndex : public IndexInterface<std::tuple<decltype(Indices().meta())...> >
{
public:
typedef IndexBase IB;
typedef std::tuple<decltype(Indices().meta())...> MetaType;
typedef std::tuple<std::shared_ptr<Indices>...> IndexPack;
typedef IndexInterface<std::tuple<decltype(Indices().meta())...> > IndexI;
protected:
bool mExternControl = false;
IndexPack mIPack;
public:
ContainerIndex() = default;
ContainerIndex(const ContainerIndex& in);
ContainerIndex& operator=(const ContainerIndex& in);
template <class MRange>
ContainerIndex(const std::shared_ptr<MRange>& range);
virtual ContainerIndex& operator++() override;
virtual ContainerIndex& operator--() override;
virtual ContainerIndex& operator=(size_t pos) override;
virtual MetaType meta() const override;
virtual ContainerIndex& at(const MetaType& metaPos) override;
virtual bool first() const override;
virtual bool last() const override;
virtual size_t dim() const override;
virtual size_t pos() const override; // recalculate when externalControl == true
ContainerIndex& operator()(const std::shared_ptr<Indices>&... inds); // control via external indices
};
template <class... Ranges> template <class... Ranges>
class ContainerRangeFactory : public RangeFactoryBase class ContainerRangeFactory : public RangeFactoryBase
@ -39,7 +80,18 @@ namespace MultiArrayTools
typedef RangeBase RB; typedef RangeBase RB;
typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType; typedef std::tuple<std::shared_ptr<Ranges>...> SpaceType;
typedef typename RangeInterface<ContainerIndex<typename Ranges::IndexType...> >::IndexType IndexType; typedef typename RangeInterface<ContainerIndex<typename Ranges::IndexType...> >::IndexType IndexType;
protected:
ContainerRange() = default;
ContainerRange(const ContainerRange& in) = delete;
ContainerRange& operator=(const ContainerRange& in) = delete;
ContainerRange(const std::shared_ptr<Ranges>&... rs);
ContainerRange(const SpaceType& space);
SpaceType mSpace;
public:
static const size_t sdim = sizeof...(Ranges); static const size_t sdim = sizeof...(Ranges);
virtual size_t dim() const override; virtual size_t dim() const override;
@ -52,53 +104,8 @@ namespace MultiArrayTools
friend ContainerRangeFactory<Ranges...>; friend ContainerRangeFactory<Ranges...>;
protected:
ContainerRange() = default;
ContainerRange(const ContainerRange& in) = delete;
ContainerRange(const std::shared_ptr<Ranges>&... rs);
ContainerRange(const SpaceType& space);
SpaceType mSpace;
}; };
template <class... Indices>
class ContainerIndex : public IndexInterface<std::tuple<decltype(Indices().meta())...> >
{
public:
typedef IndexBase IB;
typedef std::tuple<decltype(Indices().meta())...> MetaType;
typedef std::tuple<std::shared_ptr<Indices>...> IndexPack;
typedef IndexInterface<std::tuple<decltype(Indices().meta())...> > IndexI;
ContainerIndex() = default;
ContainerIndex(const ContainerIndex& in);
ContainerIndex& operator=(const ContainerIndex& in);
template <class MRange>
ContainerIndex(const std::shared_ptr<MRange>& range);
virtual ContainerIndex& operator++() override;
virtual ContainerIndex& operator--() override;
virtual ContainerIndex& operator=(size_t pos) override;
virtual MetaType meta() const override;
virtual ContainerIndex& at(const MetaType& metaPos) override;
virtual size_t dim() const override;
virtual size_t pos() const override; // recalculate when externalControl == true
ContainerIndex& operator()(const std::shared_ptr<Indices>&... inds); // control via external indices
protected:
bool mExternControl = false;
IndexPack mIPack;
};
} // end namespace MultiArrayTools } // end namespace MultiArrayTools
#include "container_range.cc" #include "container_range.cc"

View file

@ -204,7 +204,8 @@ namespace MultiArrayTools
typename MultiRange<Ranges...>::IndexType MultiRange<Ranges...>::begin() const typename MultiRange<Ranges...>::IndexType MultiRange<Ranges...>::begin() const
{ {
MultiIndex<typename Ranges::IndexType...> MultiIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<MultiRange<Ranges...> >( RB::mThis ) ); i( std::dynamic_pointer_cast<MultiRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
return i = 0; return i = 0;
} }
@ -212,7 +213,8 @@ namespace MultiArrayTools
typename MultiRange<Ranges...>::IndexType MultiRange<Ranges...>::end() const typename MultiRange<Ranges...>::IndexType MultiRange<Ranges...>::end() const
{ {
MultiIndex<typename Ranges::IndexType...> MultiIndex<typename Ranges::IndexType...>
i( std::dynamic_pointer_cast<MultiRange<Ranges...> >( RB::mThis ) ); i( std::dynamic_pointer_cast<MultiRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis )) );
return i = size(); return i = size();
} }
@ -220,6 +222,7 @@ namespace MultiArrayTools
std::shared_ptr<IndexBase> MultiRange<Ranges...>::index() const std::shared_ptr<IndexBase> MultiRange<Ranges...>::index() const
{ {
return std::make_shared<MultiIndex<typename Ranges::IndexType...> > return std::make_shared<MultiIndex<typename Ranges::IndexType...> >
( std::dynamic_pointer_cast<MultiRange<Ranges...> >( RB::mThis ) ); ( std::dynamic_pointer_cast<MultiRange<Ranges...> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
} }
} }

View file

@ -48,7 +48,7 @@ namespace MultiArrayTools
public: public:
virtual ~RangeBase() = default; virtual ~RangeBase() = default;
virtual size_t size() const = 0; virtual size_t size() const = 0;
virtual size_t dim() const = 0; virtual size_t dim() const = 0;
@ -62,7 +62,7 @@ namespace MultiArrayTools
protected: protected:
RangeBase() = default; RangeBase() = default;
std::shared_ptr<RangeBase> mThis; std::weak_ptr<RangeBase> mThis;
}; };
template <class Index> template <class Index>

View file

@ -122,14 +122,16 @@ namespace MultiArrayTools
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::begin() const typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::begin() const
{ {
SingleIndex<U,TYPE> i( std::dynamic_pointer_cast<SingleRange<U,TYPE> >( RB::mThis ) ); SingleIndex<U,TYPE> i( std::dynamic_pointer_cast<SingleRange<U,TYPE> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
return i = 0; return i = 0;
} }
template <typename U, RangeType TYPE> template <typename U, RangeType TYPE>
typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::end() const typename SingleRange<U,TYPE>::IndexType SingleRange<U,TYPE>::end() const
{ {
SingleIndex<U,TYPE> i( std::dynamic_pointer_cast<SingleRange<U,TYPE> >( RB::mThis ) ); SingleIndex<U,TYPE> i( std::dynamic_pointer_cast<SingleRange<U,TYPE> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
return i = size(); return i = size();
} }
@ -138,7 +140,8 @@ namespace MultiArrayTools
std::shared_ptr<IndexBase> SingleRange<U,TYPE>::index() const std::shared_ptr<IndexBase> SingleRange<U,TYPE>::index() const
{ {
return std::make_shared<SingleIndex<U,TYPE> > return std::make_shared<SingleIndex<U,TYPE> >
( std::dynamic_pointer_cast<SingleRange<U,TYPE> >( RB::mThis ) ); ( std::dynamic_pointer_cast<SingleRange<U,TYPE> >
( std::shared_ptr<RangeBase>( RB::mThis ) ) );
} }
/* /*