Skip to content

Commit

Permalink
Adding begin,end,size propagation for containers which support it
Browse files Browse the repository at this point in the history
  • Loading branch information
Przemo committed Oct 7, 2019
1 parent 6226f7b commit 6b8094c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions inc/owned_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ class owned_pointer : std::shared_ptr<_priv::control_block_type>
auto operator->() const -> element_type*;
auto get(std::nothrow_t) const noexcept -> element_type*;

template<typename X = element_type>
auto begin() const -> decltype(std::declval<X>().begin()) { return get()->begin(); }

template<typename X = element_type>
auto end() const -> decltype(std::declval<X>().end()) { return get()->end(); }

template<typename X = element_type>
auto cbegin() const -> decltype(std::declval<X>().cbegin()) { return get()->cbegin(); }

template<typename X = element_type>
auto cend() const -> decltype(std::declval<X>().cend()) { return get()->end(); }

template<typename X = element_type>
auto size() const -> decltype(std::declval<X>().size()) { return get()->size(); }

template<typename T>
operator owned_pointer<T>() const noexcept;

Expand Down
24 changes: 24 additions & 0 deletions ut/owned_pointer_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/
#include <gmock/gmock-generated-matchers.h>
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

Expand Down Expand Up @@ -560,3 +562,25 @@ TEST_F(owned_pointer_ut, testCastingAddressMovement)
take.giveme(*u);
}
}

TEST_F(owned_pointer_ut, testBeginEndAndSizePropagation)
{
std::vector<int> v{};
const auto o = csp::make_owned<std::vector<int>>(1, 2, 3, 4, 5);

std::copy(o.begin(), o.end(), std::back_inserter(v));

ASSERT_THAT(o.size(), ::testing::Eq(5));
ASSERT_THAT(v, ::testing::ElementsAre(1,2,3,4,5));
}

TEST_F(owned_pointer_ut, testCBeginEndAndSizePropagation)
{
std::vector<int> v{};
const auto o = csp::make_owned<std::vector<int>>(1, 2, 3, 4, 5);

std::copy(o.cbegin(), o.cend(), std::back_inserter(v));

ASSERT_THAT(o.size(), ::testing::Eq(5));
ASSERT_THAT(v, ::testing::ElementsAre(1,2,3,4,5));
}

0 comments on commit 6b8094c

Please sign in to comment.