WIP: stubbed factory functions
This commit is contained in:
parent
319da4bff6
commit
7000a40602
4 changed files with 88 additions and 27 deletions
|
|
@ -35,8 +35,8 @@ This code is heavily inspired by
|
|||
#define LIB_DEPEND_H
|
||||
|
||||
|
||||
#include "lib/nobug-init.hpp"
|
||||
#include "lib/sync-classlock.hpp"
|
||||
#include "lib/dependency-factory.hpp"
|
||||
|
||||
namespace lib {
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ namespace lib {
|
|||
* constitute a memory barrier, such as to force any memory writes happening \em within
|
||||
* the singleton ctor to be flushed and visible to other threads when releasing the lock?
|
||||
* To my understanding, the answer is yes. See
|
||||
* [posix](http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap04.html#tag_04_10)
|
||||
* [POSIX](http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap04.html#tag_04_10)
|
||||
* @param SI the class of the Singleton instance
|
||||
* @param Create policy defining how to create/destroy the instance
|
||||
* @param Life policy defining how to manage Singleton Lifecycle
|
||||
|
|
@ -61,10 +61,9 @@ namespace lib {
|
|||
class Depend
|
||||
{
|
||||
typedef ClassLock<SI> SyncLock;
|
||||
typedef DependencyFactory<SI> Factory;
|
||||
|
||||
static SI* volatile instance;
|
||||
static Factory factory;
|
||||
static DependencyFactory factory;
|
||||
|
||||
|
||||
public:
|
||||
|
|
@ -81,7 +80,7 @@ namespace lib {
|
|||
SyncLock guard;
|
||||
|
||||
if (!instance)
|
||||
instance = factory.buildInstance();
|
||||
instance = static_cast<SI*> (factory.buildInstance());
|
||||
}
|
||||
ENSURE (instance);
|
||||
return *instance;
|
||||
|
|
@ -111,22 +110,27 @@ namespace lib {
|
|||
dropReplacement()
|
||||
{
|
||||
SyncLock guard;
|
||||
factory.discardMock (instance); // EX_FREE
|
||||
instance = factory.restore (); // EX_SANE
|
||||
factory.restore (instance); // EX_FREE
|
||||
}
|
||||
|
||||
|
||||
typedef typename Factory::InstanceConstructor Constructor;
|
||||
typedef DependencyFactory::InstanceConstructor Constructor;
|
||||
|
||||
Depend (Constructor ctor = buildSingleton<SI>());
|
||||
Depend (Constructor ctor = buildSingleton<SI>())
|
||||
{
|
||||
factory.installConstructorFunction (ctor);
|
||||
}
|
||||
|
||||
private:
|
||||
// standard copy operations applicable
|
||||
};
|
||||
|
||||
|
||||
// Storage for SingletonFactory's static fields...
|
||||
template<class SI>
|
||||
SI* volatile Depend<SI>::instance;
|
||||
SI* volatile Depend<SI>::instance;
|
||||
|
||||
template<class SI>
|
||||
DependencyFactory Depend<SI>::factory;
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -13,36 +13,91 @@
|
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
====================================================================
|
||||
This code is heavily inspired by
|
||||
The Loki Library (loki-lib/trunk/include/loki/Singleton.h)
|
||||
Copyright (c) 2001 by Andrei Alexandrescu
|
||||
Loki code accompanies the book:
|
||||
Alexandrescu, Andrei. "Modern C++ Design: Generic Programming
|
||||
and Design Patterns Applied".
|
||||
Copyright (c) 2001. Addison-Wesley. ISBN 0201704315
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#ifndef LIB_DEPENDENCY_FACTORY_H
|
||||
#define LIB_DEPENDENCY_FACTORY_H
|
||||
|
||||
|
||||
#include "lib/depend.hpp"
|
||||
|
||||
//#include "lib/nobug-init.hpp"
|
||||
#include "lib/nobug-init.hpp"
|
||||
//#include "lib/sync-classlock.hpp"
|
||||
|
||||
namespace lib {
|
||||
|
||||
/** */
|
||||
/**
|
||||
* Factory to generate and manage service objects classified by type.
|
||||
*/
|
||||
class DependencyFactory
|
||||
{
|
||||
public:
|
||||
void*
|
||||
buildInstance()
|
||||
{
|
||||
UNIMPLEMENTED("invoke the ctor function");
|
||||
}
|
||||
|
||||
void
|
||||
deconfigure (void* existingInstance)
|
||||
{
|
||||
UNIMPLEMENTED("deregister and destroy a managed product");
|
||||
}
|
||||
|
||||
template<class TAR>
|
||||
void
|
||||
takeOwnership (TAR* newInstance)
|
||||
{
|
||||
UNIMPLEMENTED("enrol existing instance and prepare deleter function");
|
||||
}
|
||||
|
||||
template<class TAR>
|
||||
void
|
||||
restore (TAR* volatile & activeInstance)
|
||||
{
|
||||
UNIMPLEMENTED("disable and destroy temporary shadowing instance and restore the dormant original instance");
|
||||
}
|
||||
|
||||
template<class TAR>
|
||||
void
|
||||
shaddow (TAR* volatile & activeInstance)
|
||||
{
|
||||
UNIMPLEMENTED("set up a temporary replacement, allowing to restore the original later");
|
||||
}
|
||||
|
||||
|
||||
typedef void* (*InstanceConstructor)(void);
|
||||
|
||||
void
|
||||
installConstructorFunction (InstanceConstructor ctor)
|
||||
{
|
||||
UNIMPLEMENTED("set up constructor function, unless already configured");
|
||||
}
|
||||
|
||||
template<class TAR>
|
||||
static void*
|
||||
createSingletonInstance()
|
||||
{
|
||||
UNIMPLEMENTED("trampoline function to create a singleton");
|
||||
}
|
||||
|
||||
|
||||
template<class TAR>
|
||||
friend InstanceConstructor
|
||||
buildSingleton()
|
||||
{
|
||||
return & createSingletonInstance<TAR>;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace lib
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ namespace test{
|
|||
struct Sub
|
||||
: TestTargetObj
|
||||
{
|
||||
static uint created = 0;
|
||||
static uint created;
|
||||
uint instanceID_;
|
||||
|
||||
Sub()
|
||||
|
|
@ -60,6 +60,8 @@ namespace test{
|
|||
+ TestTargetObj::operator string();
|
||||
}
|
||||
};
|
||||
uint Sub::created = 0;
|
||||
|
||||
|
||||
struct SubSub
|
||||
: Sub
|
||||
|
|
@ -181,7 +183,7 @@ namespace test{
|
|||
static SubSubSub*
|
||||
customFactoryFunction (void)
|
||||
{
|
||||
SubSubSub* newObject = DependencyFactory::createSingleton<SubSubSub>();
|
||||
SubSubSub* newObject = static_cast<SubSubSub*> (DependencyFactory::createSingletonInstance<SubSubSub>());
|
||||
newObject->instanceID_ = MAX_ID + 10;
|
||||
return newObject;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ namespace test{
|
|||
|
||||
|
||||
inline
|
||||
TestTargetObj::~TestTargetObj() throw()
|
||||
TestTargetObj::~TestTargetObj() ///< EX_FREE
|
||||
{
|
||||
delete heapData_;
|
||||
delete[] heapArray_;
|
||||
|
|
|
|||
Loading…
Reference in a new issue