A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd

Factory

This template class allows instances of multiple derived types to be constructed from a common store.
It may support up to 16 types and MAX_SIZE instances.

Note: The caller of the factory's create becomes the owner of the instance. It must call destroy.

template <const size_t MAX_SIZE,
          typename T1,                   typename T2  = type_id_pair<>,
          typename T3  = type_id_pair<>, typename T4  = type_id_pair<>,
          typename T5  = type_id_pair<>, typename T6  = type_id_pair<>,  
          typename T7  = type_id_pair<>, typename T8  = type_id_pair<>,
          typename T9  = type_id_pair<>, typename T10 = type_id_pair<>,
          typename T11 = type_id_pair<>, typename T12 = type_id_pair<>,
          typename T13 = type_id_pair<>, typename T14 = type_id_pair<>,
          typename T15 = type_id_pair<>, typename T16 = type_id_pair<> >

type_id_pair is a defined in type_lookup.h. It defines a type and its associated id.


Constants

MAX_SIZE
The maximum number of instances that may be created.

Member functions

template <typename T>
T* create()

template <typename T, typename TP1>
T* create(const TP1& p1)

template <typename T, typename TP1, typename TP2>
T* create(const TP1& p1, const TP2& p2)

template <typename T, typename TP1, typename TP2, typename TP3>
T* create(const TP1& p1, const TP2& p2, const TP3& p3)

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)
Creates an instance of T with zero to four constructor parameters. The parameters are forwarded to T's constructor.
Returns a pointer to the constructed instance or nullptr.
If T if not one of those declared in the template parameter list then a compile time error will occur.
If T cannot be created due to there being no spare capacity then an etl::factory_cannot_create is emitted. If
exceptions are not configured to be thrown then the function returns nullptr.

bool destroy(const T* const p)
Destroys the instance pointed to by p.
Compile time error if T is not one of, or not a base of, one the supported types.
If the factory is empty or p does not belong to it then an etl::factory_did_not_create is emitted.
If exceptions are not configured to be thrown then the function returns true for success and false for failure.

size_t max_size() const
Returns the maximum number of items in the factory.

size_t available() const
Returns the number of free items in the factory.

size_t size() const
Returns the number of allocated items in the factory.

bool empty() const
Checks to see if there are no allocated items in the factory.

bool full() const
Checks to see if there are no free items in the factory.

Example


//***********************************
struct Base
{
  virtual ~Base()
  {
  }
};

//***********************************
struct Derived1 : public Base
{
  enum
  {
    ID = 1
  };
};

//***********************************
struct Derived2 : public Base
{
  enum
  {
    ID = 2
  };

  Derived2(int a_)
    : a(a_)
  {
  }

  int a;
};

//***********************************
struct Derived3 : public Base
{
  enum
  {
    ID = 3
  };

  Derived3(int a_, double b_)
    : a(a_),
      b(b_)
  {
  }

  int a;
  double b;
}

//***********************************
struct Derived4 : public Base
{
  enum
  {
    ID = 4
  };
};

//***********************************
struct NonDerived
{
  enum
  {
    ID = 5
  };
};


typedef etl::type_id_pair<Derived1, Derived1::ID>      TypeD1;
typedef etl::type_id_pair<Derived2, Derived2::ID>      TypeD2;
typedef etl::type_id_pair<Derived3, Derived3::ID>      TypeD3;
typedef etl::type_id_pair<NonDerived3, NonDerived::ID> TypeND;


// A factory that can create up to three instances of Derived1, Derived2, Derived3 and NonDerived.
// Notice that the types can be declared in any order.
typedef etl::factory<3, TypeD1, TypeND, TypeD2, TypeD3> Factory;

Factory factory;

// Create an instance of Derived1.
Derived1* pb1 = factory.create_from_type<Derived1>();

// Create an instance of Derived2 from its id, with constructor parameter 3.
Derived2* pb2 = factory.create_from_id<Derived2::ID>(3);

// Create an instance of Derived3 with constructor parameters 4 and 1.2.
Derived3* pb3 = factory.create_from_type<Derived3>(4, 1.2);

// Create an instance of Derived4.
Derived4* pb4 = factory.create_from_type<Derived4>(); // Compile time error.

// Create an instance of Derived4 from an id.
pb4 = factory.create_from_id<Derived4::ID>(); // Compile time error.

// Create an instance of NonDerived.
NonDerived* pbnb = factory.create_from_type<NonDerived>();

// Destroy the instances.
factory.destroy(pb2);
factory.destroy(pb1);
factory.destroy(pb3);
factory.destroy(pbnb);

factory.h