use foreach in register code, add foreach tests

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1060 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2009-11-21 10:44:08 +00:00
parent 665abd0b92
commit 059018191f
5 changed files with 128 additions and 35 deletions

View file

@ -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<class t> bool RegisterFunction();
template<class T>
bool RegisterFunction();
////////////////////////////////////////////////////////////////////////////////
/// \ingroup RegisterGroup
/// Must be specialized be the user
////////////////////////////////////////////////////////////////////////////////
template<class t> bool UnRegisterFunction();
template<class T>
bool UnRegisterFunction();
namespace Private
{
template<class T>
struct RegisterOnCreate
{
RegisterOnCreate() { RegisterFunction<T>(); }
template< int Index, typename T >
void operator()()
{
RegisterFunction<T>();
}
};
template<class T>
struct UnRegisterOnDelete
{
~UnRegisterOnDelete() { UnRegisterFunction<T>(); }
template< int Index, typename T >
void operator()()
{
UnRegisterFunction<T>();
}
};
template<class T>
struct RegisterOnCreateElement
{
RegisterOnCreate<T> registerObj;
};
template<class T>
struct UnRegisterOnDeleteElement
{
UnRegisterOnDelete<T> unregisterObj;
};
}
////////////////////////////////////////////////////////////////////////////////
/// \class RegisterOnCreateSet
///
@ -80,8 +80,13 @@ namespace Loki
template<typename ElementList>
struct RegisterOnCreateSet
: GenScatterHierarchy<ElementList, Private::RegisterOnCreateElement>
{};
{
RegisterOnCreateSet()
{
Private::RegisterOnCreate d;
ForEachType< ElementList, Private::RegisterOnCreate > dummy(d);
}
};
////////////////////////////////////////////////////////////////////////////////
/// \class UnRegisterOnDeleteSet
@ -94,8 +99,13 @@ namespace Loki
////////////////////////////////////////////////////////////////////////////////
template<typename ElementList>
struct UnRegisterOnDeleteSet
: GenScatterHierarchy<ElementList, Private::UnRegisterOnDeleteElement>
{};
{
~UnRegisterOnDeleteSet()
{
Private::UnRegisterOnDelete d;
ForEachType< ElementList, Private::UnRegisterOnDelete > dummy(d);
}
};
////////////////////////////////////////////////////////////////////////////////

View file

@ -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

View file

@ -17,7 +17,7 @@
#include <string>
#include <loki/Register.h>
#include <loki/Sequence.h>
struct Base

View file

@ -16,6 +16,8 @@
#include "foo.h"
#include <iostream>
#include <loki/Register.h>
Base::~Base(){}
Foo::Foo(){}

View file

@ -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 <loki/Factory.h>
#include <loki/Singleton.h>
#include <loki/Register.h>
#include "loki/ForEachType.h"
#include "classlist.h"
struct NumMeth
{
typedef std::pair<int, std::string> 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<NumMeth::IdxType>& called_for = num_meth.called_for_;
std::string int_typename = typeid( static_cast<int>(0) ).name();
std::string dou_typename = typeid( static_cast<double>(0) ).name();
std::string uin_typename = typeid( static_cast<unsigned int>(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<NumMeth::IdxType>& called_for = num_meth.called_for_;
std::string dou_typename = typeid( static_cast<double>(0) ).name();
std::string str_typename = typeid( dou_typename ).name();
std::string int_typename = typeid( static_cast<int>(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<Base, std::string>
@ -32,24 +98,40 @@ bool registerClass(std::string key, Base*(*creator)() )
return BaseFactory::Instance().Register(key,creator);
}
Loki::RegisterOnCreateSet<ClassList> registerAllClasses;
Loki::UnRegisterOnDeleteSet<ClassList> unregisterAllClasses;
int main()
{
Base* foo = BaseFactory::Instance().CreateObject("Foo");
Base* boo = BaseFactory::Instance().CreateObject("Boo");
// register test
{
Loki::RegisterOnCreateSet<ClassList> registerAllClasses;
Loki::UnRegisterOnDeleteSet<ClassList> unregisterAllClasses;
foo->foo();
boo->foo();
Base* foo = BaseFactory::Instance().CreateObject("Foo");
Base* boo = BaseFactory::Instance().CreateObject("Boo");
delete foo;
delete 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;
}