From 29c9b658d700705a1eb5bdef81ff9978c0c4e1ca Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 13 Sep 2017 13:17:11 +0100 Subject: [PATCH] Modified factory pattern --- src/factory.h | 110 +++++++++++++++++++++++++++++++----------- test/test_factory.cpp | 17 ++++++- 2 files changed, 96 insertions(+), 31 deletions(-) diff --git a/src/factory.h b/src/factory.h index 677fb770..ae7e0f8a 100644 --- a/src/factory.h +++ b/src/factory.h @@ -3,7 +3,7 @@ The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl -http://www.etlcpp.com +https://www.etlcpp.com Copyright(c) 2017 jwellbelove @@ -78,7 +78,7 @@ namespace etl }; //*************************************************************************** - template ::value), "Unsupported type"); - ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create)); + T* p = nullptr; - T* p = pool.template allocate(); - new (p) T(); + if (!full()) + { + p = pool.template allocate(); + new (p) T(); + } + else + { + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } return p; } @@ -114,10 +121,17 @@ namespace etl { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create)); + T* p = nullptr; - T* p = pool.template allocate(); - new (p) T(p1); + if (!full()) + { + p = pool.template allocate(); + new (p) T(p1); + } + else + { + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } return p; } @@ -130,10 +144,17 @@ namespace etl { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create)); + T* p = nullptr; - T* p = pool.template allocate(); - new (p) T(p1, p2); + if (!full()) + { + p = pool.template allocate(); + new (p) T(p1, p2); + } + else + { + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } return p; } @@ -146,10 +167,17 @@ namespace etl { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create)); + T* p = nullptr; - T* p = pool.template allocate(); - new (p) T(p1, p2, p3); + if (!full()) + { + p = pool.template allocate(); + new (p) T(p1, p2, p3); + } + else + { + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } return p; } @@ -162,10 +190,17 @@ namespace etl { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create)); + T* p = nullptr; - T* p = pool.template allocate(); - new (p) T(p1, p2, p3, p4); + if (!full()) + { + p = pool.template allocate(); + new (p) T(p1, p2, p3, p4); + } + else + { + ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); + } return p; } @@ -173,28 +208,45 @@ namespace etl //************************************************************************* /// Destroys the object. //************************************************************************* - template - void destroy(T* p) + bool destroy(TBase* p) { - STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); - - if (pool.is_in_pool(p)) + if (!empty() && pool.is_in_pool(p)) { - p->~T(); + p->~TBase(); pool.release(p); + return true; } else { ETL_ASSERT(false, ETL_ERROR(etl::factory_did_not_create)); + return false; + } + } + + //************************************************************************* + /// Destroys the object. + //************************************************************************* + bool destroy(const TBase * const p) + { + if (!empty() && pool.is_in_pool(p)) + { + p->~TBase(); + pool.release(p); + return true; + } + else + { + ETL_ASSERT(false, ETL_ERROR(etl::factory_did_not_create)); + return false; } } //************************************************************************* /// Returns the maximum number of items in the factory. //************************************************************************* - size_t max_items() const + size_t max_size() const { - return pool.max_items(); + return pool.max_size(); } //************************************************************************* @@ -236,11 +288,11 @@ namespace etl // The pool element. union element { - char value[etl::largest::size]; ///< Storage for value type. - typename etl::type_with_alignment::alignment>::type dummy; ///< Dummy item to get correct alignment. + char value[etl::largest::size]; ///< Storage for value type. + typename etl::type_with_alignment::alignment>::type dummy; ///< Dummy item to get correct alignment. }; - etl::pool pool; + etl::pool pool; }; } diff --git a/test/test_factory.cpp b/test/test_factory.cpp index 02f7b5ed..b5bdcbea 100644 --- a/test/test_factory.cpp +++ b/test/test_factory.cpp @@ -154,9 +154,9 @@ namespace { Factory factory; - size_t ms = Factory::MAX_ITEMS; + size_t ms = Factory::MAX_SIZE; CHECK_EQUAL(4U, ms); - CHECK_EQUAL(4U, factory.max_items()); + CHECK_EQUAL(4U, factory.max_size()); CHECK_EQUAL(4U, factory.available()); CHECK_EQUAL(0U, factory.size()); CHECK(factory.empty()); @@ -213,6 +213,19 @@ namespace CHECK(destructor); } + //************************************************************************* + TEST(test_create_release_const) + { + Factory factory; + + const Derived1& d = *factory.create(); + + CHECK_EQUAL(0, d.i); + factory.destroy(&d); + CHECK(destructor); + + } + //************************************************************************* TEST(test_create_emplace) {