mirror of
https://github.com/ETLCPP/etl.git
synced 2026-05-01 11:29:09 +08:00
Added factory pattern
This commit is contained in:
parent
259dbfa741
commit
7ee5acee18
249
src/factory.h
Normal file
249
src/factory.h
Normal file
@ -0,0 +1,249 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 jwellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __ETL_FACTORY__
|
||||
#define __ETL_FACTORY__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "error_handler.h"
|
||||
#include "exception.h"
|
||||
#include "largest.h"
|
||||
#include "pool.h"
|
||||
#include "type_traits.h"
|
||||
#include "alignment.h"
|
||||
#include "static_assert.h"
|
||||
|
||||
#undef ETL_FILE
|
||||
#define ETL_FILE "40"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
class factory_exception : public etl::exception
|
||||
{
|
||||
public:
|
||||
|
||||
factory_exception(string_type what, string_type file_name, numeric_type line_number)
|
||||
: exception(what, file_name, line_number)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
class factory_cannot_create : public etl::factory_exception
|
||||
{
|
||||
public:
|
||||
|
||||
factory_cannot_create(string_type file_name, numeric_type line_number)
|
||||
: factory_exception(ETL_ERROR_TEXT("factory:cannot create", ETL_FILE"A"), file_name, line_number)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
class factory_did_not_create : public etl::factory_exception
|
||||
{
|
||||
public:
|
||||
|
||||
factory_did_not_create(string_type file_name, numeric_type line_number)
|
||||
: factory_exception(ETL_ERROR_TEXT("factory:did not create", ETL_FILE"B"), file_name, line_number)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
template <const size_t MAX_ITEMS_,
|
||||
typename TBase,
|
||||
typename T1, typename T2 = void, typename T3 = void, typename T4 = void,
|
||||
typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void,
|
||||
typename T9 = void, typename T10 = void, typename T11 = void, typename T12 = void,
|
||||
typename T13 = void, typename T14 = void, typename T15 = void, typename T16 = void>
|
||||
class factory
|
||||
{
|
||||
public:
|
||||
|
||||
static const size_t MAX_ITEMS = MAX_ITEMS_;
|
||||
|
||||
//*************************************************************************
|
||||
/// Creates the object. Default constructor.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
T* create()
|
||||
{
|
||||
STATIC_ASSERT((etl::is_one_of<T, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::value), "Unsupported type");
|
||||
|
||||
ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create));
|
||||
|
||||
T* p = pool.template allocate<T>();
|
||||
new (p) T();
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Creates the object. One parameter constructor.
|
||||
//*************************************************************************
|
||||
template <typename T, typename TP1>
|
||||
T* create(const TP1& p1)
|
||||
{
|
||||
STATIC_ASSERT((etl::is_one_of<T, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::value), "Unsupported type");
|
||||
|
||||
ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create));
|
||||
|
||||
T* p = pool.template allocate<T>();
|
||||
new (p) T(p1);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Creates the object. Two parameter constructor.
|
||||
//*************************************************************************
|
||||
template <typename T, typename TP1, typename TP2>
|
||||
T* create(const TP1& p1, const TP2& p2)
|
||||
{
|
||||
STATIC_ASSERT((etl::is_one_of<T, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::value), "Unsupported type");
|
||||
|
||||
ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create));
|
||||
|
||||
T* p = pool.template allocate<T>();
|
||||
new (p) T(p1, p2);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Creates the object. Three parameter constructor.
|
||||
//*************************************************************************
|
||||
template <typename T, typename TP1, typename TP2, typename TP3>
|
||||
T* create(const TP1& p1, const TP2& p2, const TP3& p3)
|
||||
{
|
||||
STATIC_ASSERT((etl::is_one_of<T, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::value), "Unsupported type");
|
||||
|
||||
ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create));
|
||||
|
||||
T* p = pool.template allocate<T>();
|
||||
new (p) T(p1, p2, p3);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Creates the object. Four parameter constructor.
|
||||
//*************************************************************************
|
||||
template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
|
||||
T* create(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
|
||||
{
|
||||
STATIC_ASSERT((etl::is_one_of<T, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::value), "Unsupported type");
|
||||
|
||||
ETL_ASSERT(!full(), ETL_ERROR(etl::factory_cannot_create));
|
||||
|
||||
T* p = pool.template allocate<T>();
|
||||
new (p) T(p1, p2, p3, p4);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destroys the object.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
void destroy(T* p)
|
||||
{
|
||||
STATIC_ASSERT((etl::is_one_of<T, TBase, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::value), "Unsupported type");
|
||||
|
||||
if (pool.is_in_pool(p))
|
||||
{
|
||||
p->~T();
|
||||
pool.release(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
ETL_ASSERT(false, ETL_ERROR(etl::factory_did_not_create));
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum number of items in the factory.
|
||||
//*************************************************************************
|
||||
size_t max_items() const
|
||||
{
|
||||
return pool.max_items();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the number of free items in the factory.
|
||||
//*************************************************************************
|
||||
size_t available() const
|
||||
{
|
||||
return pool.available();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the number of allocated items in the factory.
|
||||
//*************************************************************************
|
||||
size_t size() const
|
||||
{
|
||||
return pool.size();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Checks to see if there are no allocated items in the factory.
|
||||
/// \return <b>true</b> if there are none allocated.
|
||||
//*************************************************************************
|
||||
bool empty() const
|
||||
{
|
||||
return pool.empty();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Checks to see if there are no free items in the factory.
|
||||
/// \return <b>true</b> if there are none free.
|
||||
//*************************************************************************
|
||||
bool full() const
|
||||
{
|
||||
return pool.full();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// The pool element.
|
||||
union element
|
||||
{
|
||||
char value[etl::largest<T1, T2, T3, T4, T5, T6, T7, T8>::size]; ///< Storage for value type.
|
||||
typename etl::type_with_alignment<etl::largest<T1, T2, T3, T4, T5, T6, T7, T8>::alignment>::type dummy; ///< Dummy item to get correct alignment.
|
||||
};
|
||||
|
||||
etl::pool<element, MAX_ITEMS_> pool;
|
||||
};
|
||||
}
|
||||
|
||||
#undef ETL_FILE
|
||||
|
||||
#endif
|
||||
@ -37,3 +37,4 @@
|
||||
37 task
|
||||
38 message
|
||||
39 message_bus
|
||||
40 factory
|
||||
|
||||
@ -1 +1 @@
|
||||
python -m cogapp -d -e -otype_traits.h -DIsOneOf=16 type_traits_generator.h
|
||||
python -m cogapp -d -e -otype_traits.h -DIsOneOf=17 type_traits_generator.h
|
||||
|
||||
@ -458,7 +458,8 @@ namespace etl
|
||||
typename T1, typename T2 = void, typename T3 = void, typename T4 = void,
|
||||
typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void,
|
||||
typename T9 = void, typename T10 = void, typename T11 = void, typename T12 = void,
|
||||
typename T13 = void, typename T14 = void, typename T15 = void, typename T16 = void>
|
||||
typename T13 = void, typename T14 = void, typename T15 = void, typename T16 = void,
|
||||
typename T17 = void>
|
||||
struct is_one_of
|
||||
{
|
||||
static const bool value =
|
||||
@ -477,7 +478,8 @@ namespace etl
|
||||
etl::is_same<T, T13>::value ||
|
||||
etl::is_same<T, T14>::value ||
|
||||
etl::is_same<T, T15>::value ||
|
||||
etl::is_same<T, T16>::value;
|
||||
etl::is_same<T, T16>::value ||
|
||||
etl::is_same<T, T17>::value;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
|
||||
@ -3768,7 +3768,7 @@
|
||||
<iterator>
|
||||
"static_assert.h"
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_checksum.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_checksum.cpp
|
||||
"UnitTest++.h"
|
||||
<iterator>
|
||||
<string>
|
||||
@ -3879,7 +3879,7 @@
|
||||
1501066981 d:\users\john\documents\programming\github\etl\test\data.h
|
||||
<ostream>
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_endian.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_endian.cpp
|
||||
"UnitTest++.h"
|
||||
<string>
|
||||
"endianness.h"
|
||||
@ -3889,7 +3889,7 @@
|
||||
<string>
|
||||
"enum_type.h"
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_error_handler.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_error_handler.cpp
|
||||
"UnitTest++.h"
|
||||
<Windows.h>
|
||||
<sstream>
|
||||
@ -4062,7 +4062,7 @@
|
||||
<stddef.h>
|
||||
"exception.h"
|
||||
|
||||
1503146751 source:d:\users\john\documents\programming\github\etl\test\test_function.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_function.cpp
|
||||
"UnitTest++.h"
|
||||
"function.h"
|
||||
|
||||
@ -4155,7 +4155,7 @@
|
||||
<stddef.h>
|
||||
"exception.h"
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_maths.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_maths.cpp
|
||||
"UnitTest++.h"
|
||||
"log.h"
|
||||
"power.h"
|
||||
@ -4360,7 +4360,7 @@
|
||||
"exception.h"
|
||||
"error_handler.h"
|
||||
|
||||
1502893398 source:d:\users\john\documents\programming\github\etl\test\test_pool.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_pool.cpp
|
||||
"UnitTest++.h"
|
||||
"ExtraCheckMacros.h"
|
||||
"data.h"
|
||||
@ -4413,7 +4413,7 @@
|
||||
"../exception.h"
|
||||
"../error_handler.h"
|
||||
|
||||
1502118978 source:d:\users\john\documents\programming\github\etl\test\test_type_traits.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_type_traits.cpp
|
||||
"UnitTest++.h"
|
||||
"type_traits.h"
|
||||
<type_traits>
|
||||
@ -5553,9 +5553,9 @@
|
||||
|
||||
1482948766 d:\users\john\documents\programming\github\etl\src\exception.h
|
||||
|
||||
1503146751 d:\users\john\documents\programming\github\etl\src\function.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\function.h
|
||||
|
||||
1503393231 d:\users\john\documents\programming\github\etl\src\nullptr.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\nullptr.h
|
||||
"platform.h"
|
||||
<cstddef>
|
||||
|
||||
@ -5574,7 +5574,7 @@
|
||||
"iterator.h"
|
||||
"type_traits.h"
|
||||
|
||||
1503364831 d:\users\john\documents\programming\github\etl\src\type_traits.h
|
||||
1505160601 d:\users\john\documents\programming\github\etl\src\type_traits.h
|
||||
<stddef.h>
|
||||
"platform.h"
|
||||
"nullptr.h"
|
||||
@ -5584,12 +5584,12 @@
|
||||
<stddef.h>
|
||||
<iterator>
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\alignment.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\alignment.h
|
||||
<stdint.h>
|
||||
"type_traits.h"
|
||||
"static_assert.h"
|
||||
|
||||
1503082182 d:\users\john\documents\programming\github\etl\src\static_assert.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\static_assert.h
|
||||
"platform.h"
|
||||
|
||||
1502552754 d:\users\john\documents\programming\github\etl\src\array.h
|
||||
@ -5604,10 +5604,10 @@
|
||||
"error_handler.h"
|
||||
"algorithm.h"
|
||||
|
||||
1503363970 d:\users\john\documents\programming\github\etl\src\parameter_type.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\parameter_type.h
|
||||
"type_traits.h"
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\integral_limits.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\integral_limits.h
|
||||
<limits.h>
|
||||
<stddef.h>
|
||||
"type_traits.h"
|
||||
@ -5632,7 +5632,7 @@
|
||||
<stdint.h>
|
||||
"log.h"
|
||||
|
||||
1502816074 d:\users\john\documents\programming\github\etl\src\smallest.h
|
||||
1503577754 d:\users\john\documents\programming\github\etl\src\smallest.h
|
||||
<stdint.h>
|
||||
"integral_limits.h"
|
||||
|
||||
@ -5746,7 +5746,7 @@
|
||||
"static_assert.h"
|
||||
"exception.h"
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\deque.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\deque.h
|
||||
<stddef.h>
|
||||
<stdint.h>
|
||||
<iterator>
|
||||
@ -5825,7 +5825,7 @@
|
||||
|
||||
1502705648 count.h"
|
||||
|
||||
1503082182 d:\users\john\documents\programming\github\etl\src\vector.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\vector.h
|
||||
<type_traits>
|
||||
<stddef.h>
|
||||
<stdint.h>
|
||||
@ -5870,7 +5870,7 @@
|
||||
"../ivector.h"
|
||||
"../error_handler.h"
|
||||
|
||||
1503363970 d:\users\john\documents\programming\github\etl\src\forward_list.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\forward_list.h
|
||||
<iterator>
|
||||
<algorithm>
|
||||
<functional>
|
||||
@ -5884,7 +5884,7 @@
|
||||
"type_traits.h"
|
||||
"parameter_type.h"
|
||||
|
||||
1502893398 d:\users\john\documents\programming\github\etl\src\pool.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\pool.h
|
||||
"alignment.h"
|
||||
"array.h"
|
||||
"container.h"
|
||||
@ -5968,7 +5968,7 @@
|
||||
"nullptr.h"
|
||||
"parameter_type.h"
|
||||
|
||||
1502816074 d:\users\john\documents\programming\github\etl\src\largest.h
|
||||
1503577754 d:\users\john\documents\programming\github\etl\src\largest.h
|
||||
"type_traits.h"
|
||||
"smallest.h"
|
||||
"static_assert.h"
|
||||
@ -6213,7 +6213,7 @@
|
||||
|
||||
1479515291 d:\users\john\documents\programming\github\etl\src\visitor.h
|
||||
|
||||
1503393219 d:\users\john\documents\programming\github\etl\src\platform.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\platform.h
|
||||
<stdint.h>
|
||||
<limits.h>
|
||||
|
||||
@ -6349,7 +6349,7 @@
|
||||
"static_assert.h"
|
||||
"algorithm.h"
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_jenkins.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_jenkins.cpp
|
||||
"UnitTest++.h"
|
||||
<iterator>
|
||||
<string>
|
||||
@ -6457,7 +6457,7 @@
|
||||
"../exception.h"
|
||||
"../error_handler.h"
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_murmur3.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_murmur3.cpp
|
||||
"UnitTest++.h"
|
||||
"murmurhash3.h"
|
||||
<iterator>
|
||||
@ -6473,7 +6473,7 @@
|
||||
"binary.h"
|
||||
"error_handler.h"
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_pearson.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_pearson.cpp
|
||||
"UnitTest++.h"
|
||||
<iterator>
|
||||
<string>
|
||||
@ -6482,7 +6482,7 @@
|
||||
<stdint.h>
|
||||
"pearson.h"
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\pearson.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\pearson.h
|
||||
<stdint.h>
|
||||
"platform.h"
|
||||
"static_assert.h"
|
||||
@ -6704,7 +6704,7 @@
|
||||
1482079651 d:\users\john\documents\programming\github\unittest-cpp\unittest++\throwingtestreporter.h
|
||||
"TestReporter.h"
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\char_traits.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\char_traits.h
|
||||
<algorithm>
|
||||
"platform.h"
|
||||
"stdint.h"
|
||||
@ -8808,7 +8808,7 @@
|
||||
<iterator>
|
||||
"type_traits.h"
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\memory.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\memory.h
|
||||
<iterator>
|
||||
<algorithm>
|
||||
"type_traits.h"
|
||||
@ -8818,10 +8818,10 @@
|
||||
<assert.h>
|
||||
"platform.h"
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\src\random.cpp
|
||||
1505152930 source:d:\users\john\documents\programming\github\etl\src\random.cpp
|
||||
"random.h"
|
||||
|
||||
1503082182 d:\users\john\documents\programming\github\etl\src\random.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\random.h
|
||||
<stdint.h>
|
||||
|
||||
1494277861 source:d:\users\john\documents\programming\github\etl\test\test_intrusive_flat_map.cpp
|
||||
@ -8924,7 +8924,7 @@
|
||||
"UnitTest++.h"
|
||||
"iterator.h"
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_memory.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_memory.cpp
|
||||
"UnitTest++.h"
|
||||
"memory.h"
|
||||
"debug_count.h"
|
||||
@ -9248,7 +9248,7 @@
|
||||
"container.h"
|
||||
<iostream>
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\fsm.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\fsm.h
|
||||
<stdint.h>
|
||||
"array.h"
|
||||
"nullptr.h"
|
||||
@ -9261,7 +9261,7 @@
|
||||
|
||||
1502118978 d:\users\john\documents\programming\github\etl\src\user_type.h
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\message_router.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\message_router.h
|
||||
<stdint.h>
|
||||
"message.h"
|
||||
"message_types.h"
|
||||
@ -9270,7 +9270,7 @@
|
||||
"exception.h"
|
||||
"largest.h"
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_message_router.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_message_router.cpp
|
||||
"UnitTest++.h"
|
||||
"ExtraCheckMacros.h"
|
||||
"message_router.h"
|
||||
@ -9315,7 +9315,7 @@
|
||||
"type_traits.h"
|
||||
"function.h"
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\message.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\message.h
|
||||
<stdint.h>
|
||||
"error_handler.h"
|
||||
"exception.h"
|
||||
@ -9335,7 +9335,7 @@
|
||||
"largest.h"
|
||||
"packet.h"
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\message_bus.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\message_bus.h
|
||||
<stdint.h>
|
||||
<algorithm>
|
||||
"algorithm.h"
|
||||
@ -10317,18 +10317,18 @@
|
||||
"DeferredTestReporter.h"
|
||||
<iosfwd>
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\endianness.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\endianness.h
|
||||
<stdint.h>
|
||||
"enum_type.h"
|
||||
|
||||
1503082182 d:\users\john\documents\programming\github\etl\src\sqrt.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\sqrt.h
|
||||
<stddef.h>
|
||||
"type_traits.h"
|
||||
"constant.h"
|
||||
|
||||
1503082181 d:\users\john\documents\programming\github\etl\src\constant.h
|
||||
1505152930 d:\users\john\documents\programming\github\etl\src\constant.h
|
||||
|
||||
1503082182 source:d:\users\john\documents\programming\github\etl\test\test_constant.cpp
|
||||
1505152931 source:d:\users\john\documents\programming\github\etl\test\test_constant.cpp
|
||||
"UnitTest++.h"
|
||||
"constant.h"
|
||||
"integral_limits.h"
|
||||
@ -10341,3 +10341,19 @@
|
||||
<ostream>
|
||||
"parameter_type.h"
|
||||
|
||||
1505164199 source:d:\users\john\documents\programming\github\etl\test\test_factory.cpp
|
||||
"UnitTest++.h"
|
||||
"ExtraCheckMacros.h"
|
||||
"factory.h"
|
||||
<string>
|
||||
|
||||
1505163782 d:\users\john\documents\programming\github\etl\src\factory.h
|
||||
<stdint.h>
|
||||
"error_handler.h"
|
||||
"exception.h"
|
||||
"largest.h"
|
||||
"pool.h"
|
||||
"type_traits.h"
|
||||
"alignment.h"
|
||||
"static_assert.h"
|
||||
|
||||
|
||||
261
test/test_factory.cpp
Normal file
261
test/test_factory.cpp
Normal file
@ -0,0 +1,261 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 jwellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#include "UnitTest++.h"
|
||||
#include "ExtraCheckMacros.h"
|
||||
|
||||
#include "factory.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
bool destructor;
|
||||
|
||||
//***********************************
|
||||
struct Base
|
||||
{
|
||||
Base()
|
||||
{
|
||||
destructor = false;
|
||||
}
|
||||
|
||||
virtual ~Base()
|
||||
{
|
||||
}
|
||||
|
||||
virtual void Set() = 0;
|
||||
};
|
||||
|
||||
//***********************************
|
||||
struct Derived1 : public Base
|
||||
{
|
||||
int i;
|
||||
|
||||
Derived1()
|
||||
: i(0)
|
||||
{
|
||||
}
|
||||
|
||||
~Derived1()
|
||||
{
|
||||
destructor = true;
|
||||
}
|
||||
|
||||
void Set()
|
||||
{
|
||||
i = 1;
|
||||
}
|
||||
};
|
||||
|
||||
//***********************************
|
||||
struct Derived2 : public Base
|
||||
{
|
||||
double d;
|
||||
|
||||
Derived2()
|
||||
: d(0.0)
|
||||
{
|
||||
}
|
||||
|
||||
~Derived2()
|
||||
{
|
||||
destructor = true;
|
||||
}
|
||||
|
||||
void Set()
|
||||
{
|
||||
d = 1.2;
|
||||
}
|
||||
};
|
||||
|
||||
//***********************************
|
||||
struct Derived3 : public Base
|
||||
{
|
||||
std::string s;
|
||||
|
||||
Derived3()
|
||||
: s("constructed")
|
||||
{
|
||||
}
|
||||
|
||||
Derived3(const char* p1)
|
||||
: s("constructed")
|
||||
{
|
||||
s.append(p1);
|
||||
}
|
||||
|
||||
Derived3(const char* p1, const std::string& p2)
|
||||
: s("constructed")
|
||||
{
|
||||
s.append(p1);
|
||||
s.append(p2);
|
||||
}
|
||||
|
||||
Derived3(const char* p1, const std::string& p2, const char* p3)
|
||||
: s("constructed")
|
||||
{
|
||||
s.append(p1);
|
||||
s.append(p2);
|
||||
s.append(p3);
|
||||
}
|
||||
|
||||
Derived3(const char* p1, const std::string& p2, const char* p3, const std::string& p4)
|
||||
: s("constructed")
|
||||
{
|
||||
s.append(p1);
|
||||
s.append(p2);
|
||||
s.append(p3);
|
||||
s.append(p4);
|
||||
}
|
||||
|
||||
~Derived3()
|
||||
{
|
||||
destructor = true;
|
||||
}
|
||||
|
||||
void Set()
|
||||
{
|
||||
s = "set";
|
||||
}
|
||||
};
|
||||
|
||||
typedef etl::factory<4, Base, Derived1, Derived2, Derived3> Factory;
|
||||
|
||||
SUITE(test_factory)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_sizes)
|
||||
{
|
||||
Factory factory;
|
||||
|
||||
size_t ms = Factory::MAX_ITEMS;
|
||||
CHECK_EQUAL(4U, ms);
|
||||
CHECK_EQUAL(4U, factory.max_items());
|
||||
CHECK_EQUAL(4U, factory.available());
|
||||
CHECK_EQUAL(0U, factory.size());
|
||||
CHECK(factory.empty());
|
||||
CHECK(!factory.full());
|
||||
|
||||
factory.create<Derived1>();
|
||||
CHECK_EQUAL(3U, factory.available());
|
||||
CHECK_EQUAL(1U, factory.size());
|
||||
CHECK(!factory.empty());
|
||||
CHECK(!factory.full());
|
||||
|
||||
factory.create<Derived1>();
|
||||
factory.create<Derived1>();
|
||||
factory.create<Derived1>();
|
||||
CHECK_EQUAL(0U, factory.available());
|
||||
CHECK_EQUAL(4U, factory.size());
|
||||
CHECK(!factory.empty());
|
||||
CHECK(factory.full());
|
||||
|
||||
CHECK_THROW(factory.create<Derived1>(), etl::factory_cannot_create);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_create_release)
|
||||
{
|
||||
Factory factory;
|
||||
|
||||
Base* p;
|
||||
|
||||
p = factory.create<Derived1>();
|
||||
Derived1* pd1 = static_cast<Derived1*>(p);
|
||||
CHECK_EQUAL(0, pd1->i);
|
||||
p->Set();
|
||||
CHECK_EQUAL(1, pd1->i);
|
||||
factory.destroy(p);
|
||||
CHECK(destructor);
|
||||
|
||||
destructor = false;
|
||||
p = factory.create<Derived2>();
|
||||
Derived2* pd2 = static_cast<Derived2*>(p);
|
||||
CHECK_EQUAL(0.0, pd2->d);
|
||||
p->Set();
|
||||
CHECK_EQUAL(1.2, pd2->d);
|
||||
factory.destroy(p);
|
||||
CHECK(destructor);
|
||||
|
||||
destructor = false;
|
||||
p = factory.create<Derived3>();
|
||||
Derived3* pd3 = static_cast<Derived3*>(p);
|
||||
CHECK_EQUAL("constructed", pd3->s);
|
||||
p->Set();
|
||||
CHECK_EQUAL("set", pd3->s);
|
||||
factory.destroy(p);
|
||||
CHECK(destructor);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_create_emplace)
|
||||
{
|
||||
Factory factory;
|
||||
|
||||
Base* p;
|
||||
Derived3* pd3;
|
||||
|
||||
p = factory.create<Derived3>("1");
|
||||
pd3 = static_cast<Derived3*>(p);
|
||||
CHECK_EQUAL("constructed1", pd3->s);
|
||||
factory.destroy(p);
|
||||
|
||||
p = factory.create<Derived3>("1", "2");
|
||||
pd3 = static_cast<Derived3*>(p);
|
||||
CHECK_EQUAL("constructed12", pd3->s);
|
||||
factory.destroy(p);
|
||||
|
||||
p = factory.create<Derived3>("1", "2", "3");
|
||||
pd3 = static_cast<Derived3*>(p);
|
||||
CHECK_EQUAL("constructed123", pd3->s);
|
||||
factory.destroy(p);
|
||||
|
||||
p = factory.create<Derived3>("1", "2", "3", "4");
|
||||
pd3 = static_cast<Derived3*>(p);
|
||||
CHECK_EQUAL("constructed1234", pd3->s);
|
||||
factory.destroy(p);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_did_not_create)
|
||||
{
|
||||
Factory factory1;
|
||||
Factory factory2;
|
||||
|
||||
Base* p;
|
||||
|
||||
p = factory1.create<Derived1>();
|
||||
CHECK_NO_THROW(factory1.destroy(p));
|
||||
|
||||
p = factory2.create<Derived1>();
|
||||
CHECK_THROW(factory1.destroy(p), etl::factory_did_not_create);
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -130,6 +130,7 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\src\constant.h" />
|
||||
<ClInclude Include="..\..\src\factory.h" />
|
||||
<ClInclude Include="..\..\src\fsm.h" />
|
||||
<ClInclude Include="..\..\src\fsm_generator.h" />
|
||||
<ClInclude Include="..\..\src\largest_generator.h" />
|
||||
@ -331,6 +332,7 @@
|
||||
<ClCompile Include="..\test_enum_type.cpp" />
|
||||
<ClCompile Include="..\test_error_handler.cpp" />
|
||||
<ClCompile Include="..\test_exception.cpp" />
|
||||
<ClCompile Include="..\test_factory.cpp" />
|
||||
<ClCompile Include="..\test_fixed_iterator.cpp" />
|
||||
<ClCompile Include="..\test_flat_multimap.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
|
||||
@ -483,6 +483,9 @@
|
||||
<ClInclude Include="..\..\src\sqrt.h">
|
||||
<Filter>ETL\Maths</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\factory.h">
|
||||
<Filter>ETL\Patterns</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\main.cpp">
|
||||
@ -830,6 +833,9 @@
|
||||
<ClCompile Include="..\test_parameter_type.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_factory.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\Doxyfile">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user