Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes

Global< ObjectType > Class Template Reference

Manager of global objects. More...

#include <keyvalue/util/Global.h>

List of all members.

Static Public Member Functions

static shared_ptr< ObjectType > get ()
 Gets registered object.
static void set (shared_ptr< ObjectType > object)
 Registers a new object.

Private Member Functions

 Global ()
 Global ()

Static Private Member Functions

static GlobalgetInstance ()
 Gets the unique instance of this class.

Private Attributes

shared_ptr< ObjectType > object_

Detailed Description

template<typename ObjectType>
class keyvalue::util::Global< ObjectType >

Manager of global objects.

Some objects could be global variables and some types could be singletons (e.g. Repository). However, both global variables and singletons are heavily criticized since they introduce state into the application making testing harder. Additionally, static/global objects adds complications to multi-threaded applications. This template class is an attempt to deal with this dilemma.

The instantiation for a given type manages a pointer to an object of this type and provides a global access to it.

Normally, users change the managed at most once, at initialization time. Testers, however might change it to point to mock objects.

Each instantiation of this template class is a singleton which is very simple and supposedly bug free and (hopefully in the future) thread-safe. However, the unique instance is not accessible externally. Only static methods, giving access to the pointer managed by the unique instance, are available.

ObjectType : The type of object managed.
Consider thread-safety issues.

Constructor & Destructor Documentation

Global (  )  [private]


The general implementation calls ObjectType's default constructor to build the object that the managed pointer points to. This behavior can be changed by specializations. For instance, Global<logger::Logger>::Global() sets the initial object to be a logger::StdLogger with some specified level.

We emphasize that when the constructor's default behavior is not adequate for one's needs, then only the constructor needs to be declared and implemented by specializations. (See keyvalue/sys/logger/Logger.h and keyvalue/sys/logger/Logger.cpp.)

Global (  )  [private]

Recall that the default implementation of Global<ObjectType>'s constructor build the global ObjectType by calling its default constructor. Since frontend::FrontEnd is abstract the default behavior is not adequate. Therefore, we need to declare and implement a specialization.

Here the specialization is declared but not implemented. Actually its implementation comes togheter with frontend::FrontEnd's (concrete) derived class.

Member Function Documentation

shared_ptr< ObjectType > get (  )  [static]

Gets registered object.

Debug build throws an exception if the internal pointer has not been previously initialized. Release build has undefined behavior in this case.

A pointer to registered object.
keyvalue::LogicError (Debug build only) If internal pointer has not been initialized.
void set ( shared_ptr< ObjectType >  object  )  [static]

Registers a new object.

object : Pointer to the object to be registered.
Global< ObjectType > & getInstance (  )  [static, private]

Gets the unique instance of this class.

A reference to the unique instance.