add CachedFactory, patches for: msvc, build system

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@720 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2006-10-16 09:59:11 +00:00
parent cf3ca512e6
commit 78febe17ec
8 changed files with 522 additions and 452 deletions

View file

@ -34,6 +34,15 @@
#define D( x ) ;
#endif
#ifdef _MSC_VER
#include <time.h>
namespace std
{
typedef ::clock_t clock_t;
int clock(){ return ::clock();}
}
#endif
using std::clock_t;
using std::clock;
using std::bind2nd;
@ -142,7 +151,7 @@ namespace Loki
void cleanVector()
{
clock_t currentTime = clock();
clock_t currentTime = std::clock();
D( cout << "currentTime = " << currentTime<< endl; )
D( cout << "currentTime - lastUpdate = " << currentTime - lastUpdate<< endl; )
if(currentTime - lastUpdate > timeValidity)
@ -191,7 +200,7 @@ namespace Loki
void onCreate()
{
m_vTimes.push_back(clock());
m_vTimes.push_back(std::clock());
}
void onDestroy()
@ -324,7 +333,7 @@ namespace Loki
EH::m_mHitCount[key] = 0;
}
void onFetch(const DT& key)
void onFetch(const DT&)
{
}
@ -376,7 +385,7 @@ namespace Loki
EH::m_mHitCount[key] = 0;
}
void onFetch(const DT& key){}
void onFetch(const DT&){}
// onRelease increments the hit counter associated with the object
// Updating every counters by iterating over the map
@ -428,10 +437,10 @@ namespace Loki
typedef typename std::vector< DT >::iterator iterator;
protected:
void onCreate(const DT& key){
void onCreate(const DT&){
}
void onFetch(const DT& key){
void onFetch(const DT& ){
}
void onRelease(const DT& key){
@ -561,7 +570,7 @@ namespace Loki
template <typename , typename> class EvictionPolicy = EvictRandom,
class StatisticPolicy = NoStatisticPolicy,
template<typename, class> class FactoryErrorPolicy = DefaultFactoryError,
template<typename _T, class = std::allocator<_T> > class Vector = std::vector
class ObjVector = std::vector<AbstractProduct*>
>
class CachedFactory :
protected EncapsulationPolicy<AbstractProduct>,
@ -595,7 +604,7 @@ namespace Loki
typedef typename NP::ProductReturn ProductReturn;
typedef Key< Impl, IdentifierType > Key;
typedef Vector< AbstractProduct* > ObjVector;
//typedef typename Vector ObjVector;
typedef std::map< Key, ObjVector > KeyToObjVectorMap;
typedef std::map< AbstractProduct*, Key > FetchedObjToKeyMap;
@ -612,6 +621,7 @@ namespace Loki
AbstractProduct* const getPointerToObjectInContainer(ObjVector &entry)
{
AbstractProduct* pObject = NULL;
(void) pObject;
if(entry.empty()) // No object available
{ // the object will be created in the calling function.
// It has to be created in the calling function because of
@ -707,7 +717,9 @@ namespace Loki
//assert(outObjects==0);
// debug information
if(!providedObjects.empty())
{
D( cout << "====>> Cache destructor : deleting "<< providedObjects.size()<<" objects <<====" << endl << endl; )
}
// cleaning the Cache
typename FetchedObjToKeyMap::iterator itr;
for(itr=providedObjects.begin(); itr!=providedObjects.end();itr++)

View file

@ -25,6 +25,20 @@
namespace Loki
{
template<
class Factory,
typename IdentifierType
>
class Key;
template<class F, typename I>
bool operator==(const Key<F, I> &k1, const Key<F, I> &k2);
template<class F, typename I>
bool operator<(const Key<F, I> &k1, const Key<F, I> &k2);
/*
* A Key class
*/
@ -304,12 +318,16 @@ namespace Loki
this->p15 = p15;
}
friend bool operator==(const EmptyType &k1, const EmptyType &k2)
{
return true;
}
template<class F, typename I>
friend bool operator==(const Key<F, I> &k1, const Key<F, I> &k2);
friend bool operator==(const Key &k1, const Key &k2)
template<class F, typename I>
friend bool operator<(const Key<F, I> &k1, const Key<F, I> &k2);
};
template<class F, typename I>
bool operator==(const Key<F, I> &k1, const Key<F, I> &k2)
{
if( k1.count != k2.count )
return false;
@ -521,12 +539,10 @@ namespace Loki
}
}
friend bool operator<(const EmptyType &k1, const EmptyType &k2)
{
return false;
}
friend bool operator<(const Key &k1, const Key &k2)
template<class F, typename I>
bool operator<(const Key<F, I> &k1, const Key<F, I> &k2)
{
if( k1.count < k2.count )
return true;
@ -737,7 +753,18 @@ namespace Loki
return false;
}
}
};
bool operator==(const EmptyType&, const EmptyType&)
{
return true;
}
bool operator<(const EmptyType&, const EmptyType&)
{
return false;
}
} // namespace Loki
#endif /*_KEY_H_*/

View file

@ -1,7 +1,7 @@
[Project]
FileName=CachedFactory.dev
Name=Factory
UnitCount=2
UnitCount=3
Type=1
Ver=1
ObjFiles=
@ -47,9 +47,6 @@ ProductName=
ProductVersion=
AutoIncBuildNr=0
[Unit1]
FileName=CachedFactoryTest.cpp
CompileCpp=1
@ -59,3 +56,24 @@ Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Unit2]
FileName=..\..\src\SmallObj.cpp
CompileCpp=1
Folder=Factory
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Unit3]
FileName=..\..\src\Singleton.cpp
CompileCpp=1
Folder=Factory
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

View file

@ -2,7 +2,7 @@
<VisualStudioProject
ProjectType="Visual C++"
Version="8,00"
Name="Factory"
Name="CachedFactory"
ProjectGUID="{925D5863-2F77-41B7-96F1-CC814762C40F}"
RootNamespace="CachedFactory"
Keyword="Win32Proj"
@ -169,17 +169,25 @@
</References>
<Files>
<File
RelativePath=".\CachedFactoryTest.cpp"
RelativePath="..\..\include\loki\CachedFactory.h"
>
</File>
<File
RelativePath="..\..\include\loki\CachedFactory.h"
RelativePath=".\CachedFactoryTest.cpp"
>
</File>
<File
RelativePath="..\..\include\loki\Key.h"
>
</File>
<File
RelativePath="..\..\src\Singleton.cpp"
>
</File>
<File
RelativePath="..\..\src\SmallObj.cpp"
>
</File>
</Files>
<Globals>
</Globals>

View file

@ -106,9 +106,9 @@ void testCacheOverhead(){
typedef CachedFactory< AbstractProduct, int, NullType, SimplePointer, AmountLimitedCreation, EvictRandom > CRandomEvict;
typedef CachedFactory< AbstractProduct, int, NullType, SimplePointer, AmountLimitedCreation, EvictLRU > CLRUEvict;
typedef CachedFactory< AbstractProduct, int, NullType, SimplePointer, AmountLimitedCreation, EvictAging > CAgingEvict;
bool test1 = dispResult("Random policy", unitTestCacheOverhead< CRandomEvict >(loop));
bool test2 = dispResult("LRU policy", unitTestCacheOverhead< CLRUEvict >(loop));
bool test3 = dispResult("Aging policy", unitTestCacheOverhead< CAgingEvict >(loop));
bool test1 = dispResult("Random policy", unitTestCacheOverhead< CRandomEvict >(loop));(void)test1;
bool test2 = dispResult("LRU policy", unitTestCacheOverhead< CLRUEvict >(loop));(void)test2;
bool test3 = dispResult("Aging policy", unitTestCacheOverhead< CAgingEvict >(loop));(void)test3;
}
void unitTestCachePerformance(int loop){
typedef CachedFactory< AbstractProduct, int > CCache;
@ -148,7 +148,7 @@ std::clock_t typicalUse( Cache &CC, unsigned objectKind, unsigned maxObjectCount
std::clock_t start, end;
try{
// Registering objects
for(int i=0;i<objectKind;i++)
for(size_t i=0;i<objectKind;i++)
CC.Register(i, createProductNull);
// Simulating real use
start = std::clock();
@ -312,6 +312,7 @@ bool testRelease(){
AbstractProduct *pProduct = NULL;
CC.ReleaseObject(pProduct);
cout << "Caching failed" << endl;
return false;
}
@ -397,7 +398,7 @@ void reliabilityTests()
dispText("One or more test have failed");
}
int main(int argc, char **argv)
int main(int , char **)
{
try{
performanceTests();

View file

@ -1,7 +1,7 @@
include ../Makefile.common
BIN := Factory$(BIN_SUFFIX)
SRC := Factory.cpp
BIN := CachedFactoryTest$(BIN_SUFFIX)
SRC := CachedFactoryTest.cpp
OBJ := $(SRC:.cpp=.o)
.PHONY: all clean

View file

@ -1,7 +1,7 @@
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W4 -wd4710 -I"." -I"..\..\include" CachedFactoryTest.cpp.cpp
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W4 -wd4710 -I"." -I"..\..\include" CachedFactoryTest.cpp
link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"main-msvc.exe" ..\..\lib\loki.lib CachedFactoryTest.cpp.obj
link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"main-msvc.exe" ..\..\lib\loki.lib CachedFactoryTest.obj
del *.obj

View file

@ -1,4 +1,8 @@
cd CachedFactory
call make.msvc.bat
cd ..
cd DeletableSingleton
call make.msvc.bat
cd ..