Lumiera  0.pre.03
»edit your freedom«
multifact.hpp File Reference

Go to the source code of this file.

Description

Framework for building a configurable factory, to generate families of related objects.

These building blocks are targeted towards the "classical" factory situation: obtaining objects of various kinds, which are related somehow (usually through an common interface). The creation of these objects might be non-trivial, while the number of flavours to be produced and the exact parametrisation isn't known beforehand and needs to be figured out at runtime. As a solution, thus a number of "fabrication lines" is set up, to be selected on invocation through an ID (which may be symbolic, hashed or structural).

Usually, the issue of object and storage management is closely related, while it is desirable to keep the object production logic clean of these rather technical concerns. The implementation built here separates the latter into a policy template invoked as a wrapper, accepting the raw product and either registering it, taking ownership, clone it or use it for more involved wiring. Obviously, the product generated by the installed "fabrication lines" needs to be delivered in a form acceptable by the concrete wrapper; mismatch will be spotted by the compiler on registration of the respective fabrication function.

Singleton generation
For the very common situation of building a family of singleton objects, accessible by ID, there is a convenience shortcut: The nested MultiFact::Singleton template can be instantiated within the context providing the objects (usually a static context). In itself a lib::Depend singleton factory, it automatically registers the singleton access function as "fabrication" function into a suitable MultiFact instance passed in as ctor parameter.
Remarks
this is the second attempt at building a skeleton of the core factory mechanics. The first attempt was pre-C++11, relied on partial specialisations and was hard to understand and maintain. Now, after C++11 the basic task was greatly simplified, relying on rvalue references and variadic templates. However, we still need a specialised factory template to allow for a family of factory functions with common configuration.
See also
multifact-test.cpp
multifact-singleton-test.cpp
SingletonFactory

Definition in file multifact.hpp.

#include "lib/error.hpp"
#include "lib/depend.hpp"
#include "util.hpp"
#include <functional>
#include <utility>
#include <memory>
#include <map>

Classes

struct  Build< TAR >
 Policy: use a custom functor to finish the generated product. More...
 
struct  BuildRefcountPtr< RAW >
 Wrapper taking ownership, by wrapping into smart-ptr. More...
 
struct  Fab< SIG, ID >
 Table of registered production functions for MultiFact. More...
 
struct  FabConfig< TY, Wrapper >
 
struct  FabConfig< RET(ARGS...), Wrapper >
 
class  MultiFact< SIG, ID, Wrapper >
 Factory for creating a family of objects by ID. More...
 
struct  PassAsIs< TAR >
 Dummy "wrapper", to perform the fabrication and return the unaltered product. More...
 
class  MultiFact< SIG, ID, Wrapper >::Singleton< IMP >
 Convenience shortcut for automatically setting up a production line, to fabricate a singleton instance of the given implementation target type (IMP) More...
 
struct  Build< TAR >::Wrapper< RAW >
 

Namespaces

 lib
 Implementation namespace for support and library code.