MultiFact: implement second use case (smart pointers)

This commit is contained in:
Fischlurch 2014-09-14 00:36:36 +02:00
parent 177eb0fab3
commit 372edbfc85
2 changed files with 43 additions and 6 deletions

View file

@ -89,6 +89,8 @@ namespace lib {
template<typename TAR>
struct PassAsIs
{
typedef TAR RawType;
typedef TAR BareType;
typedef TAR ResultType;
template<class FUN, typename... ARGS>
@ -107,7 +109,9 @@ namespace lib {
template<typename TAR>
struct BuildRefcountPtr
{
typedef std::shared_ptr<TAR> ResultType;
using RawType = typename std::remove_pointer<TAR>::type;
using BareType = RawType *;
using ResultType = std::shared_ptr<RawType>;
template<class FUN, typename... ARGS>
ResultType
@ -169,11 +173,11 @@ namespace lib {
>
struct FabConfig
{
using FabProduct = TY;
using WrapFunctor = Wrapper<TY>;
using BareProduct = typename WrapFunctor::BareType;
using WrappedProduct = typename WrapFunctor::ResultType;
typedef FabProduct SIG_Fab(void);
typedef BareProduct SIG_Fab(void);
};
@ -249,13 +253,13 @@ namespace lib {
class Singleton
: lib::Depend<IMP>
{
typedef lib::Depend<IMP> SingFac;
typedef lib::Depend<IMP> SingleFac;
Creator
createSingleton_accessFunction()
{
return std::bind (&SingFac::operator()
, static_cast<SingFac*>(this));
return std::bind (&SingleFac::operator()
, static_cast<SingleFac*>(this));
}
public:

View file

@ -200,6 +200,39 @@ namespace test{
void
produce_smart_pointers()
{
using TestFactory = factory::MuttiFac<Interface, theID, factory::BuildRefcountPtr>;
using PIfa = shared_ptr<Interface>;
TestFactory theFact;
// the first "production line" is wired to a free function
theFact.defineProduction (ONE, [] { return new Implementation<ONE>; });
theFact.defineProduction (TWO, [] { return new Implementation<TWO>; });
theFact.defineProduction (THR, [] { return new Implementation<THR>; });
theFact.defineProduction (FOU, [] { return new Implementation<FOU>; });
CHECK (!isnil (theFact));
PIfa p1 = theFact(ONE);
PIfa p2 = theFact(TWO);
PIfa p3 = theFact(THR);
PIfa p4 = theFact(FOU);
PIfa p11 = theFact(ONE);
CHECK ("Impl-1" == string(*p1));
CHECK ("Impl-2" == string(*p2));
CHECK ("Impl-3" == string(*p3));
CHECK ("Impl-4" == string(*p4));
CHECK ("Impl-1" == string(*p11));
CHECK (!isSameObject(*p1, *p11));
PIfa p12(p11);
CHECK (isSameObject(*p11, *p12));
CHECK ("Impl-1" == string(*p12));
CHECK (1 == p1.use_count());
CHECK (2 == p11.use_count());
CHECK (2 == p12.use_count());
}