2006-10-16 09:31:45 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The Loki Library
|
|
|
|
// Copyright (c) 2006 by Guillaume Chatelet
|
|
|
|
//
|
|
|
|
// Code covered by the MIT License
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
//
|
|
|
|
// The authors make no representations about the suitability of this software
|
|
|
|
// for any purpose. It is provided "as is" without express or implied warranty.
|
|
|
|
//
|
|
|
|
// This code DOES NOT accompany the book:
|
|
|
|
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
|
|
|
|
// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-10-17 19:50:37 +00:00
|
|
|
#ifndef LOKI_CACHEDFACTORY_INC_
|
|
|
|
#define LOKI_CACHEDFACTORY_INC_
|
|
|
|
|
|
|
|
// $Id$
|
2006-10-16 09:31:45 +00:00
|
|
|
|
2006-12-17 21:02:00 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
2006-10-16 09:31:45 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <iterator>
|
|
|
|
#include <map>
|
|
|
|
#include <cassert>
|
|
|
|
#include <loki/Key.h>
|
|
|
|
|
|
|
|
#ifdef DO_EXTRA_LOKI_TESTS
|
|
|
|
#define D( x ) x
|
|
|
|
#else
|
|
|
|
#define D( x ) ;
|
|
|
|
#endif
|
|
|
|
|
2009-01-26 19:45:54 +00:00
|
|
|
#if defined(_MSC_VER) || defined(__CYGWIN__)
|
2006-10-16 09:59:11 +00:00
|
|
|
#include <time.h>
|
2006-11-22 22:56:46 +00:00
|
|
|
#endif
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
2006-10-27 18:05:03 +00:00
|
|
|
* \defgroup FactoriesGroup Factories
|
2006-10-22 22:28:24 +00:00
|
|
|
* \defgroup CachedFactoryGroup Cached Factory
|
2006-10-27 18:05:03 +00:00
|
|
|
* \ingroup FactoriesGroup
|
2006-10-22 22:28:24 +00:00
|
|
|
* \brief CachedFactory provides an extension of a Factory with caching
|
|
|
|
* support.
|
|
|
|
*
|
|
|
|
* Once used objects are returned to the CachedFactory that manages its
|
|
|
|
* destruction.
|
|
|
|
* If your code uses lots of "long to construct/destruct objects" using the
|
|
|
|
* CachedFactory will surely speedup the execution.
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
namespace Loki
|
|
|
|
{
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \defgroup EncapsulationPolicyCachedFactoryGroup Encapsulation policies
|
|
|
|
* \ingroup CachedFactoryGroup
|
|
|
|
* \brief Defines how the object is returned to the client
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \class SimplePointer
|
|
|
|
* \ingroup EncapsulationPolicyCachedFactoryGroup
|
|
|
|
* \brief No encaspulation : returns the pointer
|
|
|
|
*
|
|
|
|
* This implementation does not make any encapsulation.
|
|
|
|
* It simply returns the object's pointer.
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
template<class AbstractProduct>
|
|
|
|
class SimplePointer
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
typedef AbstractProduct* ProductReturn;
|
|
|
|
ProductReturn encapsulate(AbstractProduct* pProduct)
|
|
|
|
{
|
|
|
|
return pProduct;
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractProduct* release(ProductReturn &pProduct)
|
|
|
|
{
|
2006-12-18 19:29:30 +00:00
|
|
|
AbstractProduct* pPointer(pProduct);
|
2006-10-16 09:31:45 +00:00
|
|
|
pProduct=NULL;
|
|
|
|
return pPointer;
|
|
|
|
}
|
|
|
|
const char* name(){return "pointer";}
|
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \defgroup CreationPolicyCachedFactoryGroup Creation policies
|
|
|
|
* \ingroup CachedFactoryGroup
|
|
|
|
* \brief Defines a way to limit the creation operation.
|
|
|
|
*
|
|
|
|
* For instance one may want to be alerted (Exception) when
|
|
|
|
* - Cache has created a more than X object within the last x seconds
|
|
|
|
* - Cache creation rate has increased dramatically
|
|
|
|
* .
|
|
|
|
* which may result from bad caching strategy, or critical overload
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \class NeverCreate
|
|
|
|
* \ingroup CreationPolicyCachedFactoryGroup
|
|
|
|
* \brief Never allows creation. Testing purposes only.
|
|
|
|
*
|
|
|
|
* Using this policy will throw an exception.
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
class NeverCreate
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
struct Exception : public std::exception
|
|
|
|
{
|
|
|
|
const char* what() const throw() { return "NeverFetch Policy : No Fetching allowed"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
bool canCreate()
|
|
|
|
{
|
|
|
|
throw Exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onCreate(){}
|
|
|
|
void onDestroy(){}
|
|
|
|
const char* name(){return "never";}
|
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \class AlwaysCreate
|
|
|
|
* \ingroup CreationPolicyCachedFactoryGroup
|
|
|
|
* \brief Always allows creation.
|
|
|
|
*
|
|
|
|
* Doesn't limit the creation in any way
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
class AlwaysCreate
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
bool canCreate()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onCreate(){}
|
|
|
|
void onDestroy(){}
|
|
|
|
const char* name(){return "always";}
|
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \class RateLimitedCreation
|
|
|
|
* \ingroup CreationPolicyCachedFactoryGroup
|
|
|
|
* \brief Limit in rate.
|
|
|
|
*
|
2006-10-16 09:31:45 +00:00
|
|
|
* This implementation will prevent from Creating more than maxCreation objects
|
|
|
|
* within byTime ms by throwing an exception.
|
2006-10-22 22:28:24 +00:00
|
|
|
* Could be usefull to detect prevent loads (http connection for instance).
|
|
|
|
* Use the setRate method to set the rate parameters.
|
|
|
|
* default is 10 objects in a second.
|
2006-10-16 09:31:45 +00:00
|
|
|
*/
|
2006-11-20 23:43:08 +00:00
|
|
|
// !! CAUTION !!
|
|
|
|
// The std::clock() function is not quite precise
|
|
|
|
// under linux this policy might not work.
|
|
|
|
// TODO : get a better implementation (platform dependant)
|
2006-10-16 09:31:45 +00:00
|
|
|
class RateLimitedCreation
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef std::vector< clock_t > Vector;
|
|
|
|
Vector m_vTimes;
|
|
|
|
unsigned maxCreation;
|
|
|
|
clock_t timeValidity;
|
2006-10-16 10:10:46 +00:00
|
|
|
clock_t lastUpdate;
|
|
|
|
|
2006-10-16 09:31:45 +00:00
|
|
|
void cleanVector()
|
|
|
|
{
|
2006-12-18 19:29:30 +00:00
|
|
|
using namespace std;
|
2006-11-20 23:43:08 +00:00
|
|
|
clock_t currentTime = clock();
|
2006-12-18 19:29:30 +00:00
|
|
|
D( cout << "currentTime = " << currentTime<< endl; )
|
|
|
|
D( cout << "currentTime - lastUpdate = " << currentTime - lastUpdate<< endl; )
|
2006-10-16 09:31:45 +00:00
|
|
|
if(currentTime - lastUpdate > timeValidity)
|
|
|
|
{
|
|
|
|
m_vTimes.clear();
|
2006-12-18 19:29:30 +00:00
|
|
|
D( cout << " is less than time validity " << timeValidity; )
|
|
|
|
D( cout << " so clearing vector" << endl; )
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-12-18 19:29:30 +00:00
|
|
|
D( cout << "Cleaning time less than " << currentTime - timeValidity << endl; )
|
2006-10-16 09:31:45 +00:00
|
|
|
D( displayVector(); )
|
2006-12-18 19:29:30 +00:00
|
|
|
Vector::iterator newEnd = remove_if(m_vTimes.begin(), m_vTimes.end(), bind2nd(less<clock_t>(), currentTime - timeValidity));
|
2006-10-16 09:31:45 +00:00
|
|
|
// this rearrangement might be costly, consider optimization
|
|
|
|
// by calling cleanVector in less used onCreate function
|
|
|
|
// ... although it may not be correct
|
|
|
|
m_vTimes.erase(newEnd, m_vTimes.end());
|
|
|
|
D( displayVector(); )
|
|
|
|
}
|
|
|
|
lastUpdate = currentTime;
|
|
|
|
}
|
2006-12-18 19:29:30 +00:00
|
|
|
#ifdef DO_EXTRA_LOKI_TESTS
|
2006-10-16 09:31:45 +00:00
|
|
|
void displayVector()
|
|
|
|
{
|
2006-11-27 20:45:39 +00:00
|
|
|
std::cout << "Vector : ";
|
|
|
|
copy(m_vTimes.begin(), m_vTimes.end(), std::ostream_iterator<clock_t>(std::cout, " "));
|
|
|
|
std::cout << std::endl;
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
2006-12-18 19:29:30 +00:00
|
|
|
#endif
|
2006-10-16 09:31:45 +00:00
|
|
|
protected:
|
2006-11-20 23:43:08 +00:00
|
|
|
RateLimitedCreation() : maxCreation(10), timeValidity(CLOCKS_PER_SEC), lastUpdate(clock())
|
2006-10-16 09:31:45 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
struct Exception : public std::exception
|
|
|
|
{
|
|
|
|
const char* what() const throw() { return "RateLimitedCreation Policy : Exceeded the authorized creation rate"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
bool canCreate()
|
|
|
|
{
|
|
|
|
cleanVector();
|
|
|
|
if(m_vTimes.size()>maxCreation)
|
|
|
|
throw Exception();
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onCreate()
|
|
|
|
{
|
2006-11-20 23:43:08 +00:00
|
|
|
m_vTimes.push_back(clock());
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDestroy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
const char* name(){return "rate limited";}
|
|
|
|
public:
|
|
|
|
// set the creation rate
|
|
|
|
// No more than maxCreation within byTime milliseconds
|
|
|
|
void setRate(unsigned maxCreation, unsigned byTime)
|
|
|
|
{
|
|
|
|
assert(byTime>0);
|
|
|
|
this->maxCreation = maxCreation;
|
2006-10-16 10:30:16 +00:00
|
|
|
this->timeValidity = static_cast<clock_t>(byTime * CLOCKS_PER_SEC / 1000);
|
2006-11-27 20:45:39 +00:00
|
|
|
D( std::cout << "Setting no more than "<< maxCreation <<" creation within " << this->timeValidity <<" ms"<< std::endl; )
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \class AmountLimitedCreation
|
|
|
|
* \ingroup CreationPolicyCachedFactoryGroup
|
|
|
|
* \brief Limit by number of objects
|
|
|
|
*
|
2006-10-16 09:31:45 +00:00
|
|
|
* This implementation will prevent from Creating more than maxCreation objects
|
|
|
|
* within byTime ms by calling eviction policy.
|
2006-10-22 22:28:24 +00:00
|
|
|
* Use the setRate method to set the rate parameters.
|
|
|
|
* default is 10 objects.
|
2006-10-16 09:31:45 +00:00
|
|
|
*/
|
|
|
|
class AmountLimitedCreation
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
unsigned maxCreation;
|
|
|
|
unsigned created;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
AmountLimitedCreation() : maxCreation(10), created(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool canCreate()
|
|
|
|
{
|
2006-12-18 19:29:30 +00:00
|
|
|
return !(created>=maxCreation);
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onCreate()
|
|
|
|
{
|
|
|
|
++created;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDestroy()
|
|
|
|
{
|
|
|
|
--created;
|
|
|
|
}
|
|
|
|
const char* name(){return "amount limited";}
|
|
|
|
public:
|
|
|
|
// set the creation max amount
|
|
|
|
void setMaxCreation(unsigned maxCreation)
|
|
|
|
{
|
|
|
|
assert(maxCreation>0);
|
|
|
|
this->maxCreation = maxCreation;
|
2006-11-27 20:45:39 +00:00
|
|
|
D( std::cout << "Setting no more than " << maxCreation <<" creation" << std::endl; )
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \defgroup EvictionPolicyCachedFactoryGroup Eviction policies
|
|
|
|
* \ingroup CachedFactoryGroup
|
|
|
|
* \brief Gathers informations about the stored objects and choose a
|
|
|
|
* candidate for eviction.
|
|
|
|
*/
|
|
|
|
|
2006-10-16 09:31:45 +00:00
|
|
|
class EvictionException : public std::exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const char* what() const throw() { return "Eviction Policy : trying to make room but no objects are available"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// The following class is intented to provide helpers to sort
|
|
|
|
// the container that will hold an eviction score
|
|
|
|
template
|
|
|
|
<
|
|
|
|
typename ST, // Score type
|
|
|
|
typename DT // Data type
|
|
|
|
>
|
|
|
|
class EvictionHelper
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
typedef typename std::map< DT, ST > HitMap;
|
|
|
|
typedef typename HitMap::iterator HitMapItr;
|
|
|
|
private:
|
|
|
|
typedef std::pair< ST, DT > SwappedPair;
|
|
|
|
typedef std::multimap< ST, DT > SwappedHitMap;
|
|
|
|
typedef typename SwappedHitMap::iterator SwappedHitMapItr;
|
|
|
|
protected:
|
|
|
|
HitMap m_mHitCount;
|
|
|
|
|
|
|
|
// This function sorts the map according to the score
|
|
|
|
// and returns the lower bound of the sorted container
|
|
|
|
DT& getLowerBound(){
|
2006-11-21 20:31:27 +00:00
|
|
|
assert(!m_mHitCount.empty());
|
2006-12-18 07:20:08 +00:00
|
|
|
// inserting the swapped pair into a multimap
|
2006-10-16 09:31:45 +00:00
|
|
|
SwappedHitMap copyMap;
|
2006-11-21 20:31:27 +00:00
|
|
|
for(HitMapItr itr = m_mHitCount.begin(); itr != m_mHitCount.end(); ++itr)
|
2007-01-03 21:50:07 +00:00
|
|
|
copyMap.insert(SwappedPair((*itr).second, (*itr).first));
|
|
|
|
if((*copyMap.rbegin()).first == 0) // the higher score is 0 ...
|
2006-10-16 09:31:45 +00:00
|
|
|
throw EvictionException(); // there is no key evict
|
2007-01-03 21:50:07 +00:00
|
|
|
return (*copyMap.begin()).second;
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \class EvictLRU
|
|
|
|
* \ingroup EvictionPolicyCachedFactoryGroup
|
|
|
|
* \brief Evicts least accessed objects first.
|
|
|
|
*
|
|
|
|
* Implementation of the Least recent used algorithm as
|
|
|
|
* described in http://en.wikipedia.org/wiki/Page_replacement_algorithms .
|
|
|
|
*
|
|
|
|
* WARNING : If an object is heavily fetched
|
|
|
|
* (more than ULONG_MAX = UINT_MAX = 4294967295U)
|
|
|
|
* it could unfortunately be removed from the cache.
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
template
|
|
|
|
<
|
|
|
|
typename DT, // Data Type (AbstractProduct*)
|
|
|
|
typename ST = unsigned // default data type to use as Score Type
|
|
|
|
>
|
|
|
|
class EvictLRU : public EvictionHelper< ST , DT >
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef EvictionHelper< ST , DT > EH;
|
|
|
|
protected:
|
2006-10-16 10:10:46 +00:00
|
|
|
|
|
|
|
virtual ~EvictLRU(){}
|
|
|
|
|
2006-10-16 09:31:45 +00:00
|
|
|
// OnStore initialize the counter for the new key
|
|
|
|
// If the key already exists, the counter is reseted
|
|
|
|
void onCreate(const DT& key)
|
|
|
|
{
|
|
|
|
EH::m_mHitCount[key] = 0;
|
|
|
|
}
|
|
|
|
|
2006-10-16 09:59:11 +00:00
|
|
|
void onFetch(const DT&)
|
2006-10-16 09:31:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// onRelease increments the hit counter associated with the object
|
|
|
|
void onRelease(const DT& key)
|
|
|
|
{
|
|
|
|
++(EH::m_mHitCount[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDestroy(const DT& key)
|
|
|
|
{
|
|
|
|
EH::m_mHitCount.erase(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function is implemented in Cache and redirected
|
|
|
|
// to the Storage Policy
|
2006-11-22 22:56:46 +00:00
|
|
|
virtual void remove(DT const key)=0;
|
2006-10-16 09:31:45 +00:00
|
|
|
|
|
|
|
// LRU Eviction policy
|
|
|
|
void evict()
|
|
|
|
{
|
2006-11-21 20:31:27 +00:00
|
|
|
remove(EH::getLowerBound());
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
2006-12-18 19:29:30 +00:00
|
|
|
const char* name(){return "LRU";}
|
2006-10-16 09:31:45 +00:00
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \class EvictAging
|
|
|
|
* \ingroup EvictionPolicyCachedFactoryGroup
|
|
|
|
* \brief LRU aware of the time span of use
|
|
|
|
*
|
|
|
|
* Implementation of the Aging algorithm as
|
|
|
|
* described in http://en.wikipedia.org/wiki/Page_replacement_algorithms .
|
|
|
|
*
|
|
|
|
* This method is much more costly than evict LRU so
|
|
|
|
* if you need extreme performance consider switching to EvictLRU
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
template
|
|
|
|
<
|
|
|
|
typename DT, // Data Type (AbstractProduct*)
|
|
|
|
typename ST = unsigned // default data type to use as Score Type
|
|
|
|
>
|
|
|
|
class EvictAging : public EvictionHelper< ST, DT >
|
|
|
|
{
|
|
|
|
private:
|
2007-02-25 14:36:28 +00:00
|
|
|
EvictAging(const EvictAging&);
|
|
|
|
EvictAging& operator=(const EvictAging&);
|
2006-10-16 09:31:45 +00:00
|
|
|
typedef EvictionHelper< ST, DT > EH;
|
|
|
|
typedef typename EH::HitMap HitMap;
|
|
|
|
typedef typename EH::HitMapItr HitMapItr;
|
2006-12-18 07:20:08 +00:00
|
|
|
|
|
|
|
// update the counter
|
|
|
|
template<class T> struct updateCounter : public std::unary_function<T, void>
|
|
|
|
{
|
|
|
|
updateCounter(const DT& key): key_(key){}
|
|
|
|
void operator()(T x)
|
|
|
|
{
|
|
|
|
x.second = (x.first == key_ ? (x.second >> 1) | ( 1 << ((sizeof(ST)-1)*8) ) : x.second >> 1);
|
|
|
|
D( std::cout << x.second << std::endl; )
|
|
|
|
}
|
|
|
|
const DT &key_;
|
2007-02-25 14:36:28 +00:00
|
|
|
updateCounter(const updateCounter& rhs) : key_(rhs.key_){}
|
|
|
|
private:
|
|
|
|
updateCounter& operator=(const updateCounter& rhs);
|
2006-12-18 07:20:08 +00:00
|
|
|
};
|
2006-10-16 09:31:45 +00:00
|
|
|
protected:
|
2007-02-25 14:36:28 +00:00
|
|
|
EvictAging(){}
|
2006-10-16 10:10:46 +00:00
|
|
|
virtual ~EvictAging(){}
|
|
|
|
|
2006-10-16 09:31:45 +00:00
|
|
|
// OnStore initialize the counter for the new key
|
|
|
|
// If the key already exists, the counter is reseted
|
|
|
|
void onCreate(const DT& key){
|
|
|
|
EH::m_mHitCount[key] = 0;
|
|
|
|
}
|
|
|
|
|
2006-10-16 09:59:11 +00:00
|
|
|
void onFetch(const DT&){}
|
2006-10-16 09:31:45 +00:00
|
|
|
|
|
|
|
// onRelease increments the hit counter associated with the object
|
|
|
|
// Updating every counters by iterating over the map
|
|
|
|
// If the key is the key of the fetched object :
|
|
|
|
// the counter is shifted to the right and it's MSB is set to 1
|
|
|
|
// else
|
2006-11-21 20:31:27 +00:00
|
|
|
// the counter is shifted to the left
|
2006-10-16 09:31:45 +00:00
|
|
|
void onRelease(const DT& key)
|
|
|
|
{
|
2006-12-18 07:20:08 +00:00
|
|
|
std::for_each(EH::m_mHitCount.begin(), EH::m_mHitCount.end(), updateCounter< typename HitMap::value_type >(key));
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDestroy(const DT& key)
|
|
|
|
{
|
|
|
|
EH::m_mHitCount.erase(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function is implemented in Cache and redirected
|
|
|
|
// to the Storage Policy
|
2006-11-22 22:56:46 +00:00
|
|
|
virtual void remove(DT const key)=0;
|
2006-10-16 09:31:45 +00:00
|
|
|
|
|
|
|
// LRU with Aging Eviction policy
|
|
|
|
void evict()
|
|
|
|
{
|
2006-11-21 20:31:27 +00:00
|
|
|
remove(EH::getLowerBound());
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
const char* name(){return "LRU with aging";}
|
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \class EvictRandom
|
|
|
|
* \ingroup EvictionPolicyCachedFactoryGroup
|
|
|
|
* \brief Evicts a random object
|
|
|
|
*
|
|
|
|
* Implementation of the Random algorithm as
|
|
|
|
* described in http://en.wikipedia.org/wiki/Page_replacement_algorithms .
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
template
|
|
|
|
<
|
|
|
|
typename DT, // Data Type (AbstractProduct*)
|
|
|
|
typename ST = void // Score Type not used by this policy
|
|
|
|
>
|
|
|
|
class EvictRandom
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::vector< DT > m_vKeys;
|
|
|
|
typedef typename std::vector< DT >::size_type size_type;
|
|
|
|
typedef typename std::vector< DT >::iterator iterator;
|
|
|
|
|
|
|
|
protected:
|
2006-10-16 10:10:46 +00:00
|
|
|
|
2007-05-08 10:48:40 +00:00
|
|
|
virtual ~EvictRandom(){}
|
2006-10-16 10:10:46 +00:00
|
|
|
|
2006-10-16 09:59:11 +00:00
|
|
|
void onCreate(const DT&){
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
|
2006-10-16 09:59:11 +00:00
|
|
|
void onFetch(const DT& ){
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onRelease(const DT& key){
|
|
|
|
m_vKeys.push_back(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDestroy(const DT& key){
|
2006-12-18 07:20:08 +00:00
|
|
|
using namespace std;
|
|
|
|
m_vKeys.erase(remove_if(m_vKeys.begin(), m_vKeys.end(), bind2nd(equal_to< DT >(), key)), m_vKeys.end());
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implemented in Cache and redirected to the Storage Policy
|
2006-11-22 22:56:46 +00:00
|
|
|
virtual void remove(DT const key)=0;
|
2006-10-16 09:31:45 +00:00
|
|
|
|
|
|
|
// Random Eviction policy
|
|
|
|
void evict()
|
|
|
|
{
|
2006-11-21 20:31:27 +00:00
|
|
|
if(m_vKeys.empty())
|
2006-10-16 09:31:45 +00:00
|
|
|
throw EvictionException();
|
2008-08-09 15:35:12 +00:00
|
|
|
size_type random = static_cast<size_type>((m_vKeys.size()*rand())/(static_cast<size_type>(RAND_MAX) + 1));
|
2006-11-21 20:31:27 +00:00
|
|
|
remove(*(m_vKeys.begin()+random));
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
2006-12-18 19:29:30 +00:00
|
|
|
const char* name(){return "random";}
|
2006-10-16 09:31:45 +00:00
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \defgroup StatisticPolicyCachedFactoryGroup Statistic policies
|
|
|
|
* \ingroup CachedFactoryGroup
|
|
|
|
* \brief Gathers information about the cache.
|
|
|
|
*
|
|
|
|
* For debugging purpose this policy proposes to gather informations
|
|
|
|
* about the cache. This could be useful to determine whether the cache is
|
|
|
|
* mandatory or if the policies are well suited to the application.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \class NoStatisticPolicy
|
|
|
|
* \ingroup StatisticPolicyCachedFactoryGroup
|
|
|
|
* \brief Do nothing
|
|
|
|
*
|
|
|
|
* Should be used in release code for better performances
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
class NoStatisticPolicy
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
void onDebug(){}
|
|
|
|
void onFetch(){}
|
|
|
|
void onRelease(){}
|
|
|
|
void onCreate(){}
|
|
|
|
void onDestroy(){}
|
|
|
|
const char* name(){return "no";}
|
|
|
|
};
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* \class SimpleStatisticPolicy
|
|
|
|
* \ingroup StatisticPolicyCachedFactoryGroup
|
|
|
|
* \brief Simple statistics
|
|
|
|
*
|
|
|
|
* Provides the following informations about the cache :
|
|
|
|
* - Created objects
|
|
|
|
* - Fetched objects
|
|
|
|
* - Destroyed objects
|
|
|
|
* - Cache hit
|
|
|
|
* - Cache miss
|
|
|
|
* - Currently allocated
|
|
|
|
* - Currently out
|
|
|
|
* - Cache overall efficiency
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
class SimpleStatisticPolicy
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
unsigned allocated, created, hit, out, fetched;
|
|
|
|
protected:
|
|
|
|
SimpleStatisticPolicy() : allocated(0), created(0), hit(0), out(0), fetched(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDebug()
|
|
|
|
{
|
2006-12-18 19:29:30 +00:00
|
|
|
using namespace std;
|
|
|
|
cout << "############################" << endl;
|
|
|
|
cout << "## About this cache " << this << endl;
|
|
|
|
cout << "## + Created objects : " << created << endl;
|
|
|
|
cout << "## + Fetched objects : " << fetched << endl;
|
|
|
|
cout << "## + Destroyed objects : " << created - allocated << endl;
|
|
|
|
cout << "## + Cache hit : " << hit << endl;
|
|
|
|
cout << "## + Cache miss : " << fetched - hit << endl;
|
|
|
|
cout << "## + Currently allocated : " << allocated << endl;
|
|
|
|
cout << "## + Currently out : " << out << endl;
|
|
|
|
cout << "############################" << endl;
|
2006-10-16 09:31:45 +00:00
|
|
|
if(fetched!=0){
|
2006-12-18 19:29:30 +00:00
|
|
|
cout << "## Overall efficiency " << 100*double(hit)/fetched <<"%"<< endl;
|
|
|
|
cout << "############################" << endl;
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
2006-12-18 19:29:30 +00:00
|
|
|
cout << endl;
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onFetch()
|
|
|
|
{
|
|
|
|
++fetched;
|
|
|
|
++out;
|
|
|
|
++hit;
|
|
|
|
}
|
|
|
|
void onRelease()
|
|
|
|
{
|
|
|
|
--out;
|
|
|
|
}
|
|
|
|
void onCreate()
|
|
|
|
{
|
|
|
|
++created;
|
|
|
|
++allocated;
|
|
|
|
--hit;
|
|
|
|
}
|
|
|
|
void onDestroy()
|
|
|
|
{
|
|
|
|
--allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* name(){return "simple";}
|
|
|
|
public:
|
|
|
|
unsigned getCreated(){return created;}
|
|
|
|
unsigned getFetched(){return fetched;}
|
|
|
|
unsigned getHit(){return hit;}
|
|
|
|
unsigned getMissed(){return fetched - hit;}
|
|
|
|
unsigned getAllocated(){return allocated;}
|
|
|
|
unsigned getOut(){return out;}
|
|
|
|
unsigned getDestroyed(){return created-allocated;}
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Cache Factory definition
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
class CacheException : public std::exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const char* what() const throw() { return "Internal Cache Error"; }
|
|
|
|
};
|
2006-10-22 22:28:24 +00:00
|
|
|
|
2006-10-16 09:31:45 +00:00
|
|
|
/**
|
2006-10-22 22:28:24 +00:00
|
|
|
* \class CachedFactory
|
2006-10-27 18:05:03 +00:00
|
|
|
* \ingroup CachedFactoryGroup
|
2006-10-22 22:28:24 +00:00
|
|
|
* \brief Factory with caching support
|
|
|
|
*
|
|
|
|
* This class acts as a Factory (it creates objects)
|
|
|
|
* but also keeps the already created objects to prevent
|
|
|
|
* long constructions time.
|
|
|
|
*
|
|
|
|
* Note this implementation do not retain ownership.
|
2006-10-16 09:31:45 +00:00
|
|
|
*/
|
|
|
|
template
|
|
|
|
<
|
|
|
|
class AbstractProduct,
|
|
|
|
typename IdentifierType,
|
|
|
|
typename CreatorParmTList = NullType,
|
|
|
|
template<class> class EncapsulationPolicy = SimplePointer,
|
|
|
|
class CreationPolicy = AlwaysCreate,
|
|
|
|
template <typename , typename> class EvictionPolicy = EvictRandom,
|
|
|
|
class StatisticPolicy = NoStatisticPolicy,
|
|
|
|
template<typename, class> class FactoryErrorPolicy = DefaultFactoryError,
|
2006-10-16 09:59:11 +00:00
|
|
|
class ObjVector = std::vector<AbstractProduct*>
|
2006-10-16 09:31:45 +00:00
|
|
|
>
|
|
|
|
class CachedFactory :
|
|
|
|
protected EncapsulationPolicy<AbstractProduct>,
|
|
|
|
public CreationPolicy, public StatisticPolicy, EvictionPolicy< AbstractProduct * , unsigned >
|
|
|
|
{
|
|
|
|
private:
|
2007-05-08 10:48:40 +00:00
|
|
|
typedef Factory< AbstractProduct, IdentifierType, CreatorParmTList, FactoryErrorPolicy> MyFactory;
|
2006-10-16 09:31:45 +00:00
|
|
|
typedef FactoryImpl< AbstractProduct, IdentifierType, CreatorParmTList > Impl;
|
|
|
|
typedef Functor< AbstractProduct* , CreatorParmTList > ProductCreator;
|
|
|
|
typedef EncapsulationPolicy<AbstractProduct> NP;
|
|
|
|
typedef CreationPolicy CP;
|
|
|
|
typedef StatisticPolicy SP;
|
|
|
|
typedef EvictionPolicy< AbstractProduct* , unsigned > EP;
|
|
|
|
|
|
|
|
typedef typename Impl::Parm1 Parm1;
|
|
|
|
typedef typename Impl::Parm2 Parm2;
|
|
|
|
typedef typename Impl::Parm3 Parm3;
|
|
|
|
typedef typename Impl::Parm4 Parm4;
|
|
|
|
typedef typename Impl::Parm5 Parm5;
|
|
|
|
typedef typename Impl::Parm6 Parm6;
|
|
|
|
typedef typename Impl::Parm7 Parm7;
|
|
|
|
typedef typename Impl::Parm8 Parm8;
|
|
|
|
typedef typename Impl::Parm9 Parm9;
|
|
|
|
typedef typename Impl::Parm10 Parm10;
|
|
|
|
typedef typename Impl::Parm11 Parm11;
|
|
|
|
typedef typename Impl::Parm12 Parm12;
|
|
|
|
typedef typename Impl::Parm13 Parm13;
|
|
|
|
typedef typename Impl::Parm14 Parm14;
|
|
|
|
typedef typename Impl::Parm15 Parm15;
|
|
|
|
|
2006-12-17 14:59:06 +00:00
|
|
|
public:
|
2006-10-16 09:31:45 +00:00
|
|
|
typedef typename NP::ProductReturn ProductReturn;
|
2006-12-17 14:59:06 +00:00
|
|
|
private:
|
2007-05-08 10:48:40 +00:00
|
|
|
typedef Key< Impl, IdentifierType > MyKey;
|
|
|
|
typedef std::map< MyKey, ObjVector > KeyToObjVectorMap;
|
|
|
|
typedef std::map< AbstractProduct*, MyKey > FetchedObjToKeyMap;
|
2006-10-16 09:31:45 +00:00
|
|
|
|
2007-05-08 10:48:40 +00:00
|
|
|
MyFactory factory;
|
2006-10-16 09:31:45 +00:00
|
|
|
KeyToObjVectorMap fromKeyToObjVector;
|
|
|
|
FetchedObjToKeyMap providedObjects;
|
|
|
|
unsigned outObjects;
|
|
|
|
|
2007-05-08 10:48:40 +00:00
|
|
|
ObjVector& getContainerFromKey(MyKey key){
|
2006-10-16 09:31:45 +00:00
|
|
|
return fromKeyToObjVector[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractProduct* const getPointerToObjectInContainer(ObjVector &entry)
|
|
|
|
{
|
|
|
|
if(entry.empty()) // No object available
|
2006-12-18 19:29:30 +00:00
|
|
|
{ // the object will be created in the calling function.
|
|
|
|
// It has to be created in the calling function because of
|
|
|
|
// the variable number of parameters for CreateObject(...) method
|
2006-10-16 09:31:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
2006-12-18 19:29:30 +00:00
|
|
|
{ // returning the found object
|
|
|
|
AbstractProduct* pObject(entry.back());
|
|
|
|
assert(pObject!=NULL);
|
|
|
|
entry.pop_back();
|
2006-10-16 09:31:45 +00:00
|
|
|
return pObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-21 20:31:27 +00:00
|
|
|
bool shouldCreateObject(AbstractProduct * const pProduct){
|
2006-10-16 09:31:45 +00:00
|
|
|
if(pProduct!=NULL) // object already exists
|
|
|
|
return false;
|
|
|
|
if(CP::canCreate()==false) // Are we allowed to Create ?
|
|
|
|
EP::evict(); // calling Eviction Policy to clean up
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-11-21 20:31:27 +00:00
|
|
|
void ReleaseObjectFromContainer(ObjVector &entry, AbstractProduct * const object)
|
2006-10-16 09:31:45 +00:00
|
|
|
{
|
|
|
|
entry.push_back(object);
|
|
|
|
}
|
|
|
|
|
2006-11-21 20:31:27 +00:00
|
|
|
void onFetch(AbstractProduct * const pProduct)
|
2006-10-16 09:31:45 +00:00
|
|
|
{
|
|
|
|
SP::onFetch();
|
|
|
|
EP::onFetch(pProduct);
|
|
|
|
++outObjects;
|
|
|
|
}
|
|
|
|
|
2006-11-21 20:31:27 +00:00
|
|
|
void onRelease(AbstractProduct * const pProduct)
|
2006-10-16 09:31:45 +00:00
|
|
|
{
|
|
|
|
SP::onRelease();
|
|
|
|
EP::onRelease(pProduct);
|
|
|
|
--outObjects;
|
|
|
|
}
|
|
|
|
|
2006-11-21 20:31:27 +00:00
|
|
|
void onCreate(AbstractProduct * const pProduct)
|
2006-10-16 09:31:45 +00:00
|
|
|
{
|
|
|
|
CP::onCreate();
|
|
|
|
SP::onCreate();
|
|
|
|
EP::onCreate(pProduct);
|
|
|
|
}
|
|
|
|
|
2006-11-21 20:31:27 +00:00
|
|
|
void onDestroy(AbstractProduct * const pProduct)
|
2006-10-16 09:31:45 +00:00
|
|
|
{
|
|
|
|
CP::onDestroy();
|
|
|
|
SP::onDestroy();
|
|
|
|
EP::onDestroy(pProduct);
|
|
|
|
}
|
2006-12-17 21:02:00 +00:00
|
|
|
|
|
|
|
// delete the object
|
|
|
|
template<class T> struct deleteObject : public std::unary_function<T, void>
|
|
|
|
{
|
|
|
|
void operator()(T x){ delete x; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// delete the objects in the vector
|
|
|
|
template<class T> struct deleteVectorObjects : public std::unary_function<T, void>
|
|
|
|
{
|
|
|
|
void operator()(T x){
|
|
|
|
ObjVector &vec(x.second);
|
|
|
|
std::for_each(vec.begin(), vec.end(), deleteObject< typename ObjVector::value_type>());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// delete the keys of the map
|
|
|
|
template<class T> struct deleteMapKeys : public std::unary_function<T, void>
|
|
|
|
{
|
|
|
|
void operator()(T x){ delete x.first; }
|
|
|
|
};
|
|
|
|
|
2006-10-16 09:31:45 +00:00
|
|
|
protected:
|
2006-11-21 20:31:27 +00:00
|
|
|
virtual void remove(AbstractProduct * const pProduct)
|
2006-10-16 09:31:45 +00:00
|
|
|
{
|
|
|
|
typename FetchedObjToKeyMap::iterator fetchedItr = providedObjects.find(pProduct);
|
|
|
|
if(fetchedItr!=providedObjects.end()) // object is unreleased.
|
|
|
|
throw CacheException();
|
|
|
|
bool productRemoved = false;
|
|
|
|
typename KeyToObjVectorMap::iterator objVectorItr;
|
|
|
|
typename ObjVector::iterator objItr;
|
2006-11-21 20:31:27 +00:00
|
|
|
for(objVectorItr=fromKeyToObjVector.begin();objVectorItr!=fromKeyToObjVector.end();++objVectorItr)
|
2006-10-16 09:31:45 +00:00
|
|
|
{
|
2007-01-03 21:50:07 +00:00
|
|
|
ObjVector &v((*objVectorItr).second);
|
2006-11-27 20:45:39 +00:00
|
|
|
objItr = remove_if(v.begin(), v.end(), std::bind2nd(std::equal_to<AbstractProduct*>(), pProduct));
|
2006-10-16 09:31:45 +00:00
|
|
|
if(objItr != v.end()) // we found the vector containing pProduct and removed it
|
|
|
|
{
|
|
|
|
onDestroy(pProduct); // warning policies we are about to destroy an object
|
|
|
|
v.erase(objItr, v.end()); // real removing
|
|
|
|
productRemoved = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(productRemoved==false)
|
|
|
|
throw CacheException(); // the product is not in the cache ?!
|
|
|
|
delete pProduct; // deleting it
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
CachedFactory() : factory(), fromKeyToObjVector(), providedObjects(), outObjects(0)
|
|
|
|
{
|
|
|
|
}
|
2006-10-22 22:28:24 +00:00
|
|
|
|
2006-10-16 09:31:45 +00:00
|
|
|
~CachedFactory()
|
|
|
|
{
|
2006-12-17 21:02:00 +00:00
|
|
|
using namespace std;
|
2006-10-16 09:31:45 +00:00
|
|
|
// debug information
|
2006-12-17 21:02:00 +00:00
|
|
|
SP::onDebug();
|
|
|
|
// cleaning the Cache
|
2006-12-18 07:20:08 +00:00
|
|
|
for_each(fromKeyToObjVector.begin(), fromKeyToObjVector.end(),
|
|
|
|
deleteVectorObjects< typename KeyToObjVectorMap::value_type >()
|
|
|
|
);
|
2006-10-16 09:31:45 +00:00
|
|
|
if(!providedObjects.empty())
|
2006-10-16 09:59:11 +00:00
|
|
|
{
|
2006-12-17 21:02:00 +00:00
|
|
|
// The factory is responsible for the creation and destruction of objects.
|
|
|
|
// If objects are out during the destruction of the Factory : deleting anyway.
|
|
|
|
// This might not be a good idea. But throwing an exception in a destructor is
|
|
|
|
// considered as a bad pratice and asserting might be too much.
|
|
|
|
// What to do ? Leaking memory or corrupting in use pointers ? hmm...
|
|
|
|
D( cout << "====>> Cache destructor : deleting "<< providedObjects.size()<<" in use objects <<====" << endl << endl; )
|
|
|
|
for_each(providedObjects.begin(), providedObjects.end(),
|
|
|
|
deleteMapKeys< typename FetchedObjToKeyMap::value_type >()
|
|
|
|
);
|
2006-10-16 09:59:11 +00:00
|
|
|
}
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////
|
|
|
|
// Acts as the proxy pattern and //
|
|
|
|
// forwards factory methods //
|
|
|
|
///////////////////////////////////
|
|
|
|
|
|
|
|
bool Register(const IdentifierType& id, ProductCreator creator)
|
|
|
|
{
|
|
|
|
return factory.Register(id, creator);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class PtrObj, typename CreaFn>
|
|
|
|
bool Register(const IdentifierType& id, const PtrObj& p, CreaFn fn)
|
|
|
|
{
|
|
|
|
return factory.Register(id, p, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Unregister(const IdentifierType& id)
|
|
|
|
{
|
|
|
|
return factory.Unregister(id);
|
|
|
|
}
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/// Return the registered ID in this Factory
|
2006-11-21 20:31:27 +00:00
|
|
|
std::vector<IdentifierType>& RegisteredIds()
|
2006-10-16 09:31:45 +00:00
|
|
|
{
|
|
|
|
return factory.RegisteredIds();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6, Parm7 p7 )
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6,p7);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6,key.p7);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6, Parm7 p7, Parm8 p8)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6,p7,p8);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6,key.p7,key.p8);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6,p7,p8,p9);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6,key.p7,key.p8,key.p9);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9,Parm10 p10)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6,key.p7,key.p8,key.p9,key.p10);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10,
|
|
|
|
Parm11 p11)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6,key.p7,key.p8,key.p9,key.p10,key.p11);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10,
|
|
|
|
Parm11 p11, Parm12 p12)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6,key.p7,key.p8,key.p9,key.p10,key.p11,key.p12);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10,
|
|
|
|
Parm11 p11, Parm12 p12, Parm13 p13)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6,key.p7,key.p8,key.p9,key.p10,key.p11,key.p12
|
|
|
|
,key.p13);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10,
|
|
|
|
Parm11 p11, Parm12 p12, Parm13 p13, Parm14 p14)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6,key.p7,key.p8,key.p9,key.p10,key.p11,key.p12
|
|
|
|
,key.p13,key.p14);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductReturn CreateObject(const IdentifierType& id,
|
|
|
|
Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
|
|
|
|
Parm6 p6, Parm7 p7, Parm8 p8, Parm9 p9, Parm10 p10,
|
|
|
|
Parm11 p11, Parm12 p12, Parm13 p13, Parm14 p14, Parm15 p15)
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
MyKey key(id,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15);
|
2006-10-16 09:31:45 +00:00
|
|
|
AbstractProduct *pProduct(getPointerToObjectInContainer(getContainerFromKey(key)));
|
|
|
|
if(shouldCreateObject(pProduct))
|
|
|
|
{
|
|
|
|
pProduct = factory.CreateObject(key.id,key.p1,key.p2,key.p3
|
|
|
|
,key.p4,key.p5,key.p6,key.p7,key.p8,key.p9,key.p10,key.p11,key.p12
|
|
|
|
,key.p13,key.p14,key.p15);
|
|
|
|
onCreate(pProduct);
|
|
|
|
}
|
|
|
|
onFetch(pProduct);
|
|
|
|
providedObjects[pProduct] = key;
|
|
|
|
return NP::encapsulate(pProduct);
|
|
|
|
}
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/// Use this function to release the object
|
|
|
|
/**
|
2006-11-27 20:45:39 +00:00
|
|
|
* if execution brakes in this function then you tried
|
2006-10-22 22:28:24 +00:00
|
|
|
* to release an object that wasn't provided by this Cache
|
|
|
|
* ... which is bad :-)
|
|
|
|
*/
|
2006-10-16 09:31:45 +00:00
|
|
|
void ReleaseObject(ProductReturn &object)
|
|
|
|
{
|
|
|
|
AbstractProduct* pProduct(NP::release(object));
|
|
|
|
typename FetchedObjToKeyMap::iterator itr = providedObjects.find(pProduct);
|
|
|
|
if(itr == providedObjects.end())
|
|
|
|
throw CacheException();
|
|
|
|
onRelease(pProduct);
|
2007-01-03 21:50:07 +00:00
|
|
|
ReleaseObjectFromContainer(getContainerFromKey((*itr).second), pProduct);
|
2006-10-16 09:31:45 +00:00
|
|
|
providedObjects.erase(itr);
|
|
|
|
}
|
|
|
|
|
2006-10-22 22:28:24 +00:00
|
|
|
/// display the cache configuration
|
2006-10-16 09:31:45 +00:00
|
|
|
void displayCacheType()
|
|
|
|
{
|
2006-12-18 19:29:30 +00:00
|
|
|
using namespace std;
|
|
|
|
cout << "############################" << endl;
|
|
|
|
cout << "## Cache configuration" << endl;
|
|
|
|
cout << "## + Encapsulation " << NP::name() << endl;
|
|
|
|
cout << "## + Creating " << CP::name() << endl;
|
|
|
|
cout << "## + Eviction " << EP::name() << endl;
|
|
|
|
cout << "## + Statistics " << SP::name() << endl;
|
|
|
|
cout << "############################" << endl;
|
2006-10-16 09:31:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace Loki
|
2006-10-17 19:31:51 +00:00
|
|
|
|
|
|
|
#endif // end file guardian
|
|
|
|
|