2009-09-30 02:26:32 +02:00
|
|
|
/*
|
|
|
|
|
MULTIFACT.hpp - flexible family-of-object factory template
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** @file multifact.hpp
|
|
|
|
|
** Building blocks of a configurable factory, generating families of related objects.
|
|
|
|
|
**
|
|
|
|
|
** @todo WIP-WIP-WIP ... currently collecting various bits of implementation here. TICKET #277
|
|
|
|
|
**
|
2009-10-27 05:13:38 +01:00
|
|
|
** @see multifact-test.cpp
|
2009-09-30 02:26:32 +02:00
|
|
|
** @see SingletonFactory
|
|
|
|
|
** @see lib::factory::Factory
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LIB_MULTIFACT_H
|
|
|
|
|
#define LIB_MULTIFACT_H
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/error.hpp"
|
2009-09-30 18:18:30 +02:00
|
|
|
#include "lib/singleton.hpp"
|
2009-09-30 23:57:45 +02:00
|
|
|
#include "util.hpp"
|
2009-09-30 02:26:32 +02:00
|
|
|
|
|
|
|
|
#include <tr1/functional>
|
2009-10-28 04:45:17 +01:00
|
|
|
#include <tr1/memory>
|
2009-09-30 23:22:28 +02:00
|
|
|
#include <map>
|
2009-09-30 02:26:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace factory {
|
|
|
|
|
|
2009-09-30 23:57:45 +02:00
|
|
|
|
2009-09-30 18:18:30 +02:00
|
|
|
/**
|
|
|
|
|
* Dummy "wrapper",
|
|
|
|
|
* just returning a target-ref
|
|
|
|
|
*/
|
|
|
|
|
template<typename TAR>
|
|
|
|
|
struct PassReference
|
|
|
|
|
{
|
2009-10-28 04:45:17 +01:00
|
|
|
typedef TAR& RType;
|
2009-09-30 18:18:30 +02:00
|
|
|
typedef TAR& PType;
|
|
|
|
|
|
2009-10-28 04:45:17 +01:00
|
|
|
PType wrap (RType object) { return object; }
|
2009-09-30 18:18:30 +02:00
|
|
|
};
|
|
|
|
|
|
2009-10-01 01:00:21 +02:00
|
|
|
|
2009-10-28 04:45:17 +01:00
|
|
|
/**
|
|
|
|
|
* Wrapper taking ownership,
|
|
|
|
|
* by wrapping into smart-ptr
|
|
|
|
|
*/
|
|
|
|
|
template<typename TAR>
|
|
|
|
|
struct BuildRefcountPtr
|
|
|
|
|
{
|
|
|
|
|
typedef TAR* RType;
|
|
|
|
|
typedef std::tr1::shared_ptr<TAR> PType;
|
|
|
|
|
|
|
|
|
|
PType wrap (RType ptr) { return PType (ptr); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-27 05:13:38 +01:00
|
|
|
template<typename TY>
|
|
|
|
|
struct FabTraits
|
|
|
|
|
{
|
|
|
|
|
typedef TY RawProduct;
|
2009-10-28 04:45:17 +01:00
|
|
|
typedef RawProduct FabSig(void);
|
2009-10-27 05:13:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-30 02:26:32 +02:00
|
|
|
/**
|
2009-10-01 01:00:21 +02:00
|
|
|
* Table of registered production functions for MultiFact.
|
|
|
|
|
* Each stored function can be accessed by ID and is able
|
|
|
|
|
* to fabricate a specific object, which is assignable to TY
|
2009-09-30 02:26:32 +02:00
|
|
|
*/
|
|
|
|
|
template<typename TY, typename ID>
|
2009-09-30 23:22:28 +02:00
|
|
|
struct Fab
|
2009-09-30 02:26:32 +02:00
|
|
|
{
|
2009-10-28 04:45:17 +01:00
|
|
|
typedef typename FabTraits<TY>::FabSig Signature;
|
2009-10-27 05:13:38 +01:00
|
|
|
typedef std::tr1::function<Signature> FactoryFunc;
|
2009-09-30 18:18:30 +02:00
|
|
|
|
2009-09-30 23:22:28 +02:00
|
|
|
|
2009-09-30 23:57:45 +02:00
|
|
|
FactoryFunc&
|
2009-10-27 05:13:38 +01:00
|
|
|
select (ID const& id)
|
2009-09-30 02:26:32 +02:00
|
|
|
{
|
2009-10-01 01:00:21 +02:00
|
|
|
if (!contains (id))
|
2009-09-30 23:57:45 +02:00
|
|
|
throw lumiera::error::Invalid("unknown factory product requested.");
|
|
|
|
|
|
2009-09-30 23:22:28 +02:00
|
|
|
return producerTable_[id];
|
2009-09-30 02:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-30 23:57:45 +02:00
|
|
|
void
|
2009-10-27 05:13:38 +01:00
|
|
|
defineProduction (ID const& id, FactoryFunc fun)
|
2009-09-30 02:26:32 +02:00
|
|
|
{
|
2009-09-30 23:22:28 +02:00
|
|
|
producerTable_[id] = fun;
|
2009-09-30 02:26:32 +02:00
|
|
|
}
|
2009-09-30 23:22:28 +02:00
|
|
|
|
2009-10-01 01:00:21 +02:00
|
|
|
|
|
|
|
|
/* === diagnostics === */
|
|
|
|
|
|
|
|
|
|
bool empty () const { return producerTable_.empty(); }
|
|
|
|
|
bool contains (ID id) const { return util::contains (producerTable_,id); }
|
|
|
|
|
|
2009-09-30 23:22:28 +02:00
|
|
|
private:
|
|
|
|
|
std::map<ID, FactoryFunc> producerTable_;
|
2009-09-30 02:26:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-10-01 01:00:21 +02:00
|
|
|
* 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.
|
2009-09-30 02:26:32 +02:00
|
|
|
*/
|
|
|
|
|
template< typename TY
|
|
|
|
|
, typename ID
|
|
|
|
|
, template<class> class Wrapper
|
|
|
|
|
>
|
|
|
|
|
class MultiFact
|
2009-10-27 05:13:38 +01:00
|
|
|
: Wrapper<typename FabTraits<TY>::RawProduct>
|
2009-09-30 02:26:32 +02:00
|
|
|
{
|
2009-10-28 04:45:17 +01:00
|
|
|
typedef Wrapper<typename FabTraits<TY>::RawProduct> _Wrap;
|
|
|
|
|
typedef typename FabTraits<TY>::RawProduct RawType;
|
|
|
|
|
typedef typename _Wrap::PType WrappedProduct;
|
|
|
|
|
typedef typename _Wrap::RType FabProduct;
|
|
|
|
|
typedef Fab<FabProduct,ID> _Fab;
|
2009-09-30 02:26:32 +02:00
|
|
|
|
2009-09-30 23:57:45 +02:00
|
|
|
_Fab funcTable_;
|
|
|
|
|
|
2009-10-28 04:45:17 +01:00
|
|
|
|
2009-10-27 05:13:38 +01:00
|
|
|
protected:
|
|
|
|
|
typedef typename _Fab::FactoryFunc Creator;
|
2009-10-28 04:45:17 +01:00
|
|
|
typedef WrappedProduct Product;
|
2009-10-27 05:13:38 +01:00
|
|
|
|
|
|
|
|
Creator&
|
|
|
|
|
selectProducer (ID const& id)
|
|
|
|
|
{
|
|
|
|
|
return funcTable_.select(id);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-01 01:00:21 +02:00
|
|
|
|
2009-09-30 02:26:32 +02:00
|
|
|
public:
|
|
|
|
|
Product
|
2009-10-27 05:13:38 +01:00
|
|
|
operator() (ID const& id)
|
2009-09-30 02:26:32 +02:00
|
|
|
{
|
2009-10-27 05:13:38 +01:00
|
|
|
Creator& func = selectProducer (id);
|
2009-09-30 02:26:32 +02:00
|
|
|
return wrap (func());
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-01 01:00:21 +02:00
|
|
|
|
2009-09-30 23:57:45 +02:00
|
|
|
/** to set up a production line,
|
|
|
|
|
* associated with a specific ID
|
2009-09-30 02:26:32 +02:00
|
|
|
*/
|
2009-09-30 23:57:45 +02:00
|
|
|
template<typename FUNC>
|
|
|
|
|
void
|
|
|
|
|
defineProduction (ID id, FUNC fun)
|
2009-09-30 02:26:32 +02:00
|
|
|
{
|
2009-09-30 23:57:45 +02:00
|
|
|
funcTable_.defineProduction (id, fun);
|
|
|
|
|
}
|
2009-09-30 02:26:32 +02:00
|
|
|
|
2009-10-01 01:00:21 +02:00
|
|
|
|
2009-09-30 18:18:30 +02:00
|
|
|
/**
|
|
|
|
|
* Convenience shortcut for automatically setting up
|
|
|
|
|
* a production line, fabricating a singleton instance
|
2009-10-24 00:23:22 +02:00
|
|
|
* of the given target type (IMP)
|
2009-09-30 18:18:30 +02:00
|
|
|
*/
|
2009-10-01 01:00:21 +02:00
|
|
|
template<class IMP>
|
2009-09-30 23:57:45 +02:00
|
|
|
class Singleton
|
2009-10-01 01:00:21 +02:00
|
|
|
: lib::Singleton<IMP>
|
2009-09-30 18:18:30 +02:00
|
|
|
{
|
2009-10-01 01:00:21 +02:00
|
|
|
typedef lib::Singleton<IMP> SingFac;
|
2009-09-30 18:18:30 +02:00
|
|
|
|
2009-09-30 23:57:45 +02:00
|
|
|
Creator
|
|
|
|
|
createSingleton_accessFunction()
|
2009-09-30 18:18:30 +02:00
|
|
|
{
|
2009-09-30 23:57:45 +02:00
|
|
|
return std::tr1::bind (&SingFac::operator()
|
|
|
|
|
, static_cast<SingFac*>(this));
|
2009-09-30 23:22:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2009-09-30 23:57:45 +02:00
|
|
|
Singleton (MultiFact& factory, ID id)
|
2009-09-30 23:22:28 +02:00
|
|
|
{
|
2009-09-30 23:57:45 +02:00
|
|
|
factory.defineProduction(id, createSingleton_accessFunction());
|
2009-09-30 18:18:30 +02:00
|
|
|
}
|
|
|
|
|
};
|
2009-10-01 01:00:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* === diagnostics === */
|
|
|
|
|
|
|
|
|
|
bool empty () const { return funcTable_.empty(); }
|
|
|
|
|
bool contains (ID id) const { return funcTable_.contains (id); }
|
2009-09-30 02:26:32 +02:00
|
|
|
};
|
2009-09-30 18:18:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-30 02:26:32 +02:00
|
|
|
} // namespace factory
|
|
|
|
|
|
2009-10-01 01:00:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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>
|
|
|
|
|
{ };
|
2009-09-30 02:26:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lib
|
|
|
|
|
#endif
|