From 7a16f22f78c15c33cfb21119f9bc59ff100897b7 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Fri, 4 Aug 2017 16:19:50 +0200 Subject: [PATCH] fix leak issue --- src/container_range.cc | 162 +++++++++++++++++++++++------------------ src/container_range.h | 99 +++++++++++++------------ src/multi_range.cc | 9 ++- src/range_base.h | 4 +- src/single_range.cc | 9 ++- 5 files changed, 159 insertions(+), 124 deletions(-) diff --git a/src/container_range.cc b/src/container_range.cc index 1477a9b..5ac4a1b 100644 --- a/src/container_range.cc +++ b/src/container_range.cc @@ -10,77 +10,7 @@ namespace MultiArrayTools using namespace MultiArrayHelper; } - /***************************** - * ContainerRangeFactory * - *****************************/ - - template - ContainerRangeFactory::ContainerRangeFactory(const std::shared_ptr&... rs) - { - mProd = std::make_shared >( rs... ); - } - template - ContainerRangeFactory:: - ContainerRangeFactory(const typename ContainerRange::SpaceType& space) - { - mProd = std::make_shared >( space ); - } - - template - std::shared_ptr ContainerRangeFactory::create() - { - setSelf(); - return mProd; - } - - /********************** - * ContainerRange * - **********************/ - - template - ContainerRange::ContainerRange(const std::shared_ptr&... rs) : - mSpace( std::make_tuple( rs... ) ) {} - - template - ContainerRange::ContainerRange(const SpaceType& space) : mSpace( space ) {} - - template - size_t ContainerRange::dim() const - { - return sizeof...(Ranges); - } - - template - size_t ContainerRange::size() const - { - return PackNum::getSize(mSpace); - } - - template - typename ContainerRange::IndexType ContainerRange::begin() const - { - ContainerIndex - i( std::dynamic_pointer_cast >( RB::mThis ) ); - return i = 0; - } - - template - typename ContainerRange::IndexType ContainerRange::end() const - { - ContainerIndex - i( std::dynamic_pointer_cast >( RB::mThis ) ); - return i = size(); - } - - template - std::shared_ptr ContainerRange::index() const - { - return std::make_shared > - ( std::dynamic_pointer_cast >( RB::mThis ) ); - } - - /********************** * ContainerIndex * **********************/ @@ -113,6 +43,9 @@ namespace MultiArrayTools template ContainerIndex& ContainerIndex::operator++() { + if(mExternControl){ + IB::mPos = PackNum::makePos(mIPack); + } PackNum::pp( mIPack ); ++IB::mPos; return *this; @@ -121,6 +54,9 @@ namespace MultiArrayTools template ContainerIndex& ContainerIndex::operator--() { + if(mExternControl){ + IB::mPos = PackNum::makePos(mIPack); + } PackNum::mm( mIPack ); --IB::mPos; return *this; @@ -165,6 +101,19 @@ namespace MultiArrayTools return IB::mPos; } + template + bool ContainerIndex::first() const + { + return pos() == 0; + } + + template + bool ContainerIndex::last() const + { + return pos() == IB::mRangePtr->size() - 1; + } + + template ContainerIndex& ContainerIndex::operator()(const std::shared_ptr&... inds) { @@ -173,4 +122,77 @@ namespace MultiArrayTools return *this; } + /***************************** + * ContainerRangeFactory * + *****************************/ + + template + ContainerRangeFactory::ContainerRangeFactory(const std::shared_ptr&... rs) + { + mProd = std::shared_ptr >( new ContainerRange( rs... ) ); + } + + template + ContainerRangeFactory:: + ContainerRangeFactory(const typename ContainerRange::SpaceType& space) + { + mProd = std::shared_ptr >( new ContainerRange( space ) ); + } + + template + std::shared_ptr ContainerRangeFactory::create() + { + setSelf(); + return mProd; + } + + /********************** + * ContainerRange * + **********************/ + + template + ContainerRange::ContainerRange(const std::shared_ptr&... rs) : + mSpace( std::make_tuple( rs... ) ) {} + + template + ContainerRange::ContainerRange(const SpaceType& space) : mSpace( space ) {} + + template + size_t ContainerRange::dim() const + { + return sizeof...(Ranges); + } + + template + size_t ContainerRange::size() const + { + return PackNum::getSize(mSpace); + } + + template + typename ContainerRange::IndexType ContainerRange::begin() const + { + ContainerIndex + i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + return i = 0; + } + + template + typename ContainerRange::IndexType ContainerRange::end() const + { + ContainerIndex + i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + return i = size(); + } + + template + std::shared_ptr ContainerRange::index() const + { + return std::make_shared > + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); + } + } // end namespace MultiArrayTools diff --git a/src/container_range.h b/src/container_range.h index 1245953..820d26b 100644 --- a/src/container_range.h +++ b/src/container_range.h @@ -13,6 +13,47 @@ namespace MultiArrayTools { + + template + class ContainerIndex : public IndexInterface > + { + public: + + typedef IndexBase IB; + typedef std::tuple MetaType; + typedef std::tuple...> IndexPack; + typedef IndexInterface > IndexI; + + protected: + bool mExternControl = false; + IndexPack mIPack; + + public: + ContainerIndex() = default; + + ContainerIndex(const ContainerIndex& in); + ContainerIndex& operator=(const ContainerIndex& in); + + template + ContainerIndex(const std::shared_ptr& 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&... inds); // control via external indices + + }; + template class ContainerRangeFactory : public RangeFactoryBase @@ -39,7 +80,18 @@ namespace MultiArrayTools typedef RangeBase RB; typedef std::tuple...> SpaceType; typedef typename RangeInterface >::IndexType IndexType; + + protected: + ContainerRange() = default; + ContainerRange(const ContainerRange& in) = delete; + ContainerRange& operator=(const ContainerRange& in) = delete; + + ContainerRange(const std::shared_ptr&... rs); + ContainerRange(const SpaceType& space); + SpaceType mSpace; + + public: static const size_t sdim = sizeof...(Ranges); virtual size_t dim() const override; @@ -52,53 +104,8 @@ namespace MultiArrayTools friend ContainerRangeFactory; - protected: - - ContainerRange() = default; - ContainerRange(const ContainerRange& in) = delete; - - ContainerRange(const std::shared_ptr&... rs); - ContainerRange(const SpaceType& space); - - SpaceType mSpace; }; - - template - class ContainerIndex : public IndexInterface > - { - public: - - typedef IndexBase IB; - typedef std::tuple MetaType; - typedef std::tuple...> IndexPack; - typedef IndexInterface > IndexI; - - ContainerIndex() = default; - - ContainerIndex(const ContainerIndex& in); - ContainerIndex& operator=(const ContainerIndex& in); - - template - ContainerIndex(const std::shared_ptr& 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&... inds); // control via external indices - - protected: - - bool mExternControl = false; - IndexPack mIPack; - }; - + } // end namespace MultiArrayTools #include "container_range.cc" diff --git a/src/multi_range.cc b/src/multi_range.cc index 266dd52..7235ff4 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -204,7 +204,8 @@ namespace MultiArrayTools typename MultiRange::IndexType MultiRange::begin() const { MultiIndex - i( std::dynamic_pointer_cast >( RB::mThis ) ); + i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); return i = 0; } @@ -212,7 +213,8 @@ namespace MultiArrayTools typename MultiRange::IndexType MultiRange::end() const { MultiIndex - i( std::dynamic_pointer_cast >( RB::mThis ) ); + i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis )) ); return i = size(); } @@ -220,6 +222,7 @@ namespace MultiArrayTools std::shared_ptr MultiRange::index() const { return std::make_shared > - ( std::dynamic_pointer_cast >( RB::mThis ) ); + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); } } diff --git a/src/range_base.h b/src/range_base.h index e58a7b2..e06f000 100644 --- a/src/range_base.h +++ b/src/range_base.h @@ -48,7 +48,7 @@ namespace MultiArrayTools public: virtual ~RangeBase() = default; - + virtual size_t size() const = 0; virtual size_t dim() const = 0; @@ -62,7 +62,7 @@ namespace MultiArrayTools protected: RangeBase() = default; - std::shared_ptr mThis; + std::weak_ptr mThis; }; template diff --git a/src/single_range.cc b/src/single_range.cc index d0d0ee8..048eb4f 100644 --- a/src/single_range.cc +++ b/src/single_range.cc @@ -122,14 +122,16 @@ namespace MultiArrayTools template typename SingleRange::IndexType SingleRange::begin() const { - SingleIndex i( std::dynamic_pointer_cast >( RB::mThis ) ); + SingleIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); return i = 0; } template typename SingleRange::IndexType SingleRange::end() const { - SingleIndex i( std::dynamic_pointer_cast >( RB::mThis ) ); + SingleIndex i( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); return i = size(); } @@ -138,7 +140,8 @@ namespace MultiArrayTools std::shared_ptr SingleRange::index() const { return std::make_shared > - ( std::dynamic_pointer_cast >( RB::mThis ) ); + ( std::dynamic_pointer_cast > + ( std::shared_ptr( RB::mThis ) ) ); } /*