diff --git a/include/loki/Register.h b/include/loki/Register.h index 7df7408..6f1228e 100644 --- a/include/loki/Register.h +++ b/include/loki/Register.h @@ -17,6 +17,8 @@ #include "TypeManip.h" #include "HierarchyGenerators.h" +#include "ForEachType.h" + /// \defgroup RegisterGroup Register @@ -33,41 +35,39 @@ namespace Loki /// \ingroup RegisterGroup /// Must be specialized be the user //////////////////////////////////////////////////////////////////////////////// - template bool RegisterFunction(); + template + bool RegisterFunction(); //////////////////////////////////////////////////////////////////////////////// /// \ingroup RegisterGroup /// Must be specialized be the user //////////////////////////////////////////////////////////////////////////////// - template bool UnRegisterFunction(); + template + bool UnRegisterFunction(); namespace Private { - template struct RegisterOnCreate { - RegisterOnCreate() { RegisterFunction(); } + template< int Index, typename T > + void operator()() + { + RegisterFunction(); + } }; - template struct UnRegisterOnDelete { - ~UnRegisterOnDelete() { UnRegisterFunction(); } - }; - - template - struct RegisterOnCreateElement - { - RegisterOnCreate registerObj; + template< int Index, typename T > + void operator()() + { + UnRegisterFunction(); + } }; - template - struct UnRegisterOnDeleteElement - { - UnRegisterOnDelete unregisterObj; - }; } + //////////////////////////////////////////////////////////////////////////////// /// \class RegisterOnCreateSet /// @@ -79,9 +79,14 @@ namespace Loki //////////////////////////////////////////////////////////////////////////////// template - struct RegisterOnCreateSet - : GenScatterHierarchy - {}; + struct RegisterOnCreateSet + { + RegisterOnCreateSet() + { + Private::RegisterOnCreate d; + ForEachType< ElementList, Private::RegisterOnCreate > dummy(d); + } + }; //////////////////////////////////////////////////////////////////////////////// /// \class UnRegisterOnDeleteSet @@ -93,9 +98,14 @@ namespace Loki /// see test/Register //////////////////////////////////////////////////////////////////////////////// template - struct UnRegisterOnDeleteSet - : GenScatterHierarchy - {}; + struct UnRegisterOnDeleteSet + { + ~UnRegisterOnDeleteSet() + { + Private::UnRegisterOnDelete d; + ForEachType< ElementList, Private::UnRegisterOnDelete > dummy(d); + } + }; //////////////////////////////////////////////////////////////////////////////// diff --git a/include/loki/StrongPtr.h b/include/loki/StrongPtr.h index f0f3233..c4c9a36 100644 --- a/include/loki/StrongPtr.h +++ b/include/loki/StrongPtr.h @@ -1810,4 +1810,3 @@ namespace std #endif // end file guardian - /// specialization of std::less for StroeTracker@Private@Loki@@@std@@@std@@QBEXABV123@@Z??_C@_1BBK@FOBGIJMK@?$AAs?$AAt?$AAd?$AA?3?$AA?3?$AAl?$AAi?$AAs?$AAt?$AA?$DM?$AAc?$AAl?$AAa?$AAs?$AAs?$AA?5?$AAL?$AAo?$AAk?$AAi?$AA?3?$AA?3?$AAP?$AAr?$AAi?$AAv?$AAa?$AAt?$AAe?$AA?3?$AA?3?$AAL@??_C@_1DI@MDELDGPI@?$AAl?$AAi?$AAs?$AAt?$AA?5?$AAi?$AAt?$AAe?$AAr?$AAa?$AAt?$AAo?$AAr?$AAs?$AA?5?$AAi?$AAn?$AAc?$AAo?$AAm?$AAp?$AAa?$AAt?$AAi?$AAb?$AAl?$AAe?$AA?$AA@??$_Debug_lt_pred@P6A_NPBVLifetimeTracker@Private@Loki@@0@ZPAV123@PAV123@@std@@YA_NP6A_NPBVLifetimeTracker@Private@Loki@@0@ZAAPAV123@2PB_WI@Z diff --git a/test/Register/classlist.h b/test/Register/classlist.h index 2f39859..40069f1 100644 --- a/test/Register/classlist.h +++ b/test/Register/classlist.h @@ -17,7 +17,7 @@ #include -#include +#include struct Base diff --git a/test/Register/foo.cpp b/test/Register/foo.cpp index 9a97e58..f3f2ee9 100644 --- a/test/Register/foo.cpp +++ b/test/Register/foo.cpp @@ -16,6 +16,8 @@ #include "foo.h" #include +#include + Base::~Base(){} Foo::Foo(){} diff --git a/test/Register/main.cpp b/test/Register/main.cpp index bb1d2e6..b5ce62b 100644 --- a/test/Register/main.cpp +++ b/test/Register/main.cpp @@ -1,6 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // The Loki Library -// Copyright (c) 2006 Peter Kümmel +// Copyright (c) 2006,2009 Peter Kümmel +// Copyright (C) 2009 Andy Balaam // Permission to use, copy, modify, distribute and sell this software for any // purpose is hereby granted without fee, provided that the above copyright // notice appear in all copies and that both that copyright notice and this @@ -17,9 +18,74 @@ #include #include +#include +#include "loki/ForEachType.h" #include "classlist.h" + + + +struct NumMeth +{ + typedef std::pair IdxType; + + template< int Index, typename Type > + void operator()() + { + Type tmp; + (void) tmp; + called_for_.push_back( IdxType( Index, typeid( tmp ).name() ) ); + } + + std::vector< IdxType > called_for_; +}; + + +void test_typelist_foreach_forward() +{ + typedef LOKI_TYPELIST_4( int, int, double, unsigned int ) MyTypeList; + + NumMeth num_meth; + Loki::ForEachType< MyTypeList, NumMeth > dummy( num_meth ); + + const std::vector& called_for = num_meth.called_for_; + + std::string int_typename = typeid( static_cast(0) ).name(); + std::string dou_typename = typeid( static_cast(0) ).name(); + std::string uin_typename = typeid( static_cast(0) ).name(); + + assert( called_for.size() == 4 ); + assert( called_for[0] == NumMeth::IdxType( 0, int_typename ) ); + assert( called_for[1] == NumMeth::IdxType( 1, int_typename ) ); + assert( called_for[2] == NumMeth::IdxType( 2, dou_typename ) ); + assert( called_for[3] == NumMeth::IdxType( 3, uin_typename ) ); +} + + +void test_typelist_foreach_backward() +{ + typedef LOKI_TYPELIST_3( double, std::string, int ) MyTypeList; + + NumMeth num_meth; + Loki::ForEachType< MyTypeList, NumMeth, Loki::OrderPolicyBackward > dummy( num_meth ); + + const std::vector& called_for = num_meth.called_for_; + + std::string dou_typename = typeid( static_cast(0) ).name(); + std::string str_typename = typeid( dou_typename ).name(); + std::string int_typename = typeid( static_cast(0) ).name(); + + assert( called_for.size() == 3 ); + assert( called_for[0] == NumMeth::IdxType( 0, int_typename ) ); + assert( called_for[1] == NumMeth::IdxType( 1, str_typename ) ); + assert( called_for[2] == NumMeth::IdxType( 2, dou_typename ) ); +} + + + + + typedef Loki::SingletonHolder < Loki::Factory @@ -32,24 +98,40 @@ bool registerClass(std::string key, Base*(*creator)() ) return BaseFactory::Instance().Register(key,creator); } -Loki::RegisterOnCreateSet registerAllClasses; -Loki::UnRegisterOnDeleteSet unregisterAllClasses; + + + int main() { - Base* foo = BaseFactory::Instance().CreateObject("Foo"); - Base* boo = BaseFactory::Instance().CreateObject("Boo"); - - foo->foo(); - boo->foo(); + // register test + { + Loki::RegisterOnCreateSet registerAllClasses; + Loki::UnRegisterOnDeleteSet unregisterAllClasses; - delete foo; - delete boo; + Base* foo = BaseFactory::Instance().CreateObject("Foo"); + Base* boo = BaseFactory::Instance().CreateObject("Boo"); + + foo->foo(); + boo->foo(); + + delete foo; + delete boo; + + } + + // typelist tests + + test_typelist_foreach_forward(); + test_typelist_foreach_backward(); #if defined(__BORLANDC__) || defined(_MSC_VER) system("PAUSE"); #endif + + return 0; } +