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:
parent
665abd0b92
commit
059018191f
5 changed files with 128 additions and 35 deletions
|
@ -17,7 +17,7 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include <loki/Register.h>
|
||||
#include <loki/Sequence.h>
|
||||
|
||||
|
||||
struct Base
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
#include "foo.h"
|
||||
#include <iostream>
|
||||
|
||||
#include <loki/Register.h>
|
||||
|
||||
Base::~Base(){}
|
||||
|
||||
Foo::Foo(){}
|
||||
|
|
|
@ -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");
|
||||
|
||||
foo->foo();
|
||||
boo->foo();
|
||||
// register test
|
||||
{
|
||||
Loki::RegisterOnCreateSet<ClassList> registerAllClasses;
|
||||
Loki::UnRegisterOnDeleteSet<ClassList> 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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue