Finish MultiFact for now; use it to set up a table of HandlingPatterns

This commit is contained in:
Fischlurch 2009-10-01 01:00:21 +02:00
parent b7204e05ed
commit 7e4a984ed4
3 changed files with 59 additions and 34 deletions

View file

@ -46,7 +46,6 @@
namespace lib { namespace lib {
namespace factory { namespace factory {
using util::contains;
/** /**
* Dummy "wrapper", * Dummy "wrapper",
@ -60,9 +59,11 @@ namespace lib {
PType wrap (TAR& object) { return object; } PType wrap (TAR& object) { return object; }
}; };
/** /**
* Repository of registered production lines. * Table of registered production functions for MultiFact.
* @todo write type comment * Each stored function can be accessed by ID and is able
* to fabricate a specific object, which is assignable to TY
*/ */
template<typename TY, typename ID> template<typename TY, typename ID>
struct Fab struct Fab
@ -74,7 +75,7 @@ namespace lib {
FactoryFunc& FactoryFunc&
select (ID id) select (ID id)
{ {
if (!contains (producerTable_,id)) if (!contains (id))
throw lumiera::error::Invalid("unknown factory product requested."); throw lumiera::error::Invalid("unknown factory product requested.");
return producerTable_[id]; return producerTable_[id];
@ -86,6 +87,12 @@ namespace lib {
producerTable_[id] = fun; producerTable_[id] = fun;
} }
/* === diagnostics === */
bool empty () const { return producerTable_.empty(); }
bool contains (ID id) const { return util::contains (producerTable_,id); }
private: private:
std::map<ID, FactoryFunc> producerTable_; std::map<ID, FactoryFunc> producerTable_;
}; };
@ -93,7 +100,14 @@ namespace lib {
/** /**
* @todo write type comment * Factory for creating a family of objects by ID.
* The actual factory functions are to be installed
* from the usage site through calls to #defineProduction .
* Each generated object will be treated by the Wrapper template,
* allowing for the generation of smart-ptrs. The embedded class
* Singleton allows to build a family of singleton objects; it is
* to be instantiated at the call site and acts as singleton factory,
* accessible through a MultiFact instance as frontend.
*/ */
template< typename TY template< typename TY
, typename ID , typename ID
@ -108,6 +122,7 @@ namespace lib {
_Fab funcTable_; _Fab funcTable_;
public: public:
Product Product
operator() (ID id) operator() (ID id)
@ -116,6 +131,7 @@ namespace lib {
return wrap (func()); return wrap (func());
} }
/** to set up a production line, /** to set up a production line,
* associated with a specific ID * associated with a specific ID
*/ */
@ -126,17 +142,17 @@ namespace lib {
funcTable_.defineProduction (id, fun); funcTable_.defineProduction (id, fun);
} }
/** /**
* Convenience shortcut for automatically setting up * Convenience shortcut for automatically setting up
* a production line, fabricating a singleton instance * a production line, fabricating a singleton instance
* of the given target type (TAR) * of the given target type (TAR)
*/ */
template<class TAR> template<class IMP>
class Singleton class Singleton
: lib::Singleton<TAR> : lib::Singleton<IMP>
{ {
typedef lib::Singleton<TAR> SingFac; typedef lib::Singleton<IMP> SingFac;
// typedef std::tr1::function<TAR&(void)> AccessSingleton_Func;
Creator Creator
createSingleton_accessFunction() createSingleton_accessFunction()
@ -151,13 +167,30 @@ namespace lib {
factory.defineProduction(id, createSingleton_accessFunction()); factory.defineProduction(id, createSingleton_accessFunction());
} }
}; };
/* === diagnostics === */
bool empty () const { return funcTable_.empty(); }
bool contains (ID id) const { return funcTable_.contains (id); }
}; };
} // namespace factory } // namespace factory
//using factory::Factory;
/**
* Standard configuration of the family-of-object factory
* @todo this is rather guesswork... find out what the best and most used configuration could be....
*/
template< typename TY
, typename ID
>
class MultiFact
: public factory::MultiFact<TY,ID, factory::PassReference>
{ };
} // namespace lib } // namespace lib

View file

@ -44,7 +44,7 @@
//#include "pre.hpp" //#include "pre.hpp"
#include "lib/error.hpp" #include "lib/error.hpp"
#include "lib/singleton-subclass.hpp" #include "lib/multifact.hpp"
#include "proc/control/handling-pattern.hpp" #include "proc/control/handling-pattern.hpp"
#include "proc/control/command-impl.hpp" #include "proc/control/command-impl.hpp"
#include "include/lifecycle.h" #include "include/lifecycle.h"
@ -156,39 +156,27 @@ namespace control {
/* ======== Handling Pattern Table ========== */ /* ======== Handling Pattern Table ========== */
typedef lib::SingletonSub<HandlingPattern> SingletonFac; typedef lib::MultiFact<HandlingPattern, HandlingPattern::ID> HandlingPatternFactory;
/** Table of available command handling patterns */ /** Table of available command handling patterns */
vector<SingletonFac> patternTable; HandlingPatternFactory patternTable;
HandlingPatternFactory::Singleton<InvokeSyncNoThrow> holder1 (patternTable, HandlingPattern::SYNC);
HandlingPatternFactory::Singleton<InvokeSyncThrow> holder2 (patternTable, HandlingPattern::SYNC_THROW);
HandlingPatternFactory::Singleton<InvokeAsync> holder3 (patternTable, HandlingPattern::ASYNC);
/** access the singleton instance for a given ID */ /** access the singleton instance for a given ID */
inline HandlingPattern const& inline HandlingPattern const&
getPatternInstance (size_t id) getPatternInstance (HandlingPattern::ID id)
{ {
REQUIRE (id < patternTable.size()); REQUIRE (patternTable.contains(id));
return patternTable[id] ();
}
/** populate the handling pattern table.
* This init-function will be invoked each time
* a new session is created or loaded.
*/
void
prepareCommandHandlingPatterns()
{
using lib::singleton::UseSubclass;
patternTable[HandlingPattern::SYNC ] = SingletonFac(UseSubclass<InvokeSyncNoThrow>()); return patternTable (id);
// patternTable[HandlingPattern::SYNC_THROW] = SingletonFac(UseSubclass<InvokeSyncThrow>());
// patternTable[HandlingPattern::ASYNC ] = SingletonFac(UseSubclass<InvokeAsync>());
} }
lumiera::LifecycleHook _schedule (lumiera::ON_GLOBAL_INIT, &prepareCommandHandlingPatterns);
} // (END) definition of concrete handling patterns } // (END) definition of concrete handling patterns

View file

@ -37,8 +37,8 @@ namespace test{
using boost::lexical_cast; using boost::lexical_cast;
using lib::test::showSizeof; using lib::test::showSizeof;
//using util::isnil;
using util::isSameObject; using util::isSameObject;
using util::isnil;
using std::ostream; using std::ostream;
using std::string; using std::string;
using std::cout; using std::cout;
@ -46,6 +46,7 @@ namespace test{
using lumiera::error::LUMIERA_ERROR_INVALID; using lumiera::error::LUMIERA_ERROR_INVALID;
namespace { // hierarchy of test dummy objects namespace { // hierarchy of test dummy objects
struct Interface struct Interface
@ -80,8 +81,10 @@ namespace test{
static theID getTypeID() { return ii; } static theID getTypeID() { return ii; }
}; };
/** Factory instance for the tests... */
TestFactory theFact; TestFactory theFact;
// Configure the products to be fabricated....
TestFactory::Singleton<Implementation<ONE> > holder1 (theFact,ONE); TestFactory::Singleton<Implementation<ONE> > holder1 (theFact,ONE);
TestFactory::Singleton<Implementation<TWO> > holder2 (theFact,TWO); TestFactory::Singleton<Implementation<TWO> > holder2 (theFact,TWO);
TestFactory::Singleton<Implementation<THR> > holder3 (theFact,THR); TestFactory::Singleton<Implementation<THR> > holder3 (theFact,THR);
@ -116,6 +119,7 @@ namespace test{
ASSERT (isSameObject(o1,o2)); ASSERT (isSameObject(o1,o2));
TestFactory anotherFact; TestFactory anotherFact;
ASSERT (isnil (anotherFact));
VERIFY_ERROR (INVALID, anotherFact(ONE) ); VERIFY_ERROR (INVALID, anotherFact(ONE) );
TestFactory::Singleton<Implementation<ONE> > anotherSingletonHolder (anotherFact,ONE); TestFactory::Singleton<Implementation<ONE> > anotherSingletonHolder (anotherFact,ONE);