Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members

s11n::phoenix< BaseType, ContextType, InitializerType > Struct Template Reference

phoenix is class for holding singleton-style instances of BaseType objects. More...

#include <phoenix.h>

List of all members.

Public Types

typedef ContextType context_type
 context_type is unused by this class, but might be useful for type identification at some point.

typedef BaseType base_type
 The BaseType parameterized type.

typedef InitializerType initializer_type
 The functor type used to initialize this phoenixed object.


Static Public Member Functions

base_typeinstance ()
 Returns a shared instance of this object.


Detailed Description

template<typename BaseType, typename ContextType = BaseType, typename InitializerType = no_op_phoenix_initializer>
struct s11n::phoenix< BaseType, ContextType, InitializerType >

phoenix is class for holding singleton-style instances of BaseType objects.

Rather than requiring that BaseType be a Singleton type, phoenix subclasses BaseType to add the phoenix-like capabilities. Phoenixing makes the shared object post-main() safe, in terms of object destruction order.

Parameterized on:

Whether or not BaseType is technically a singleton depends on entirely BaseType itself. This class is more often used to provide easy access to context-dependent shared objects, rather than pure singletons. The phoenix class itself is a true Singleton, but each combination of template arguments provides a different Singleton *type*, so the end effect is "context singletons."

This is another attempt to solve the classic Keyboard-Console-Log problem, as discussed at length in Modern C++ Design. It relies on sane behaviour in the C library's atexit() function, which, as is shown in MC++D, is not the case on all systems. That said, the phoenix-specific behaviours are undefined on those systems, which is only to say that it might not be post-main() safe.

Caveats:

i am not 100% clear on all of the implications of this implementation's approach... my gut tells me i'm missing some significant bits. i mean, it can't have been this straightforward to solve ;). The very nature of the Phoenix Singleton problem makes it difficult to reliably test in real-world applications. That said, i have seen a objects be successfully phoenixed and atexit()ed, so it is known to at least "basically" work.

There's a paper about "context singletons", this class, and some of it's implications, at:

http://s11n.net/misccode/context_singletons.html

[Much later: i've gotten more re-use out of this class than probably any other single class i've ever written.]

Definition at line 120 of file phoenix.h.


Member Function Documentation

template<typename BaseType, typename ContextType = BaseType, typename InitializerType = no_op_phoenix_initializer>
base_type& s11n::phoenix< BaseType, ContextType, InitializerType >::instance  )  [inline, static]
 

Returns a shared instance of this object.

The instance() method will always return the same address, though it is potentially possible (post-main()) that the actual object living at that address is different from previous calls.

It is never a good idea to hold on to the returned reference for the life of an object, as that bypasses the phoenixing capabilities.

If you ever delete it you're on you're own. That's a Bad Idea.

Definition at line 152 of file phoenix.h.

References s11n::phoenix< BaseType, ContextType, InitializerType >::base_type, and s11n::phoenix< BaseType, ContextType, InitializerType >::initializer_type.


The documentation for this struct was generated from the following file:
Generated on Wed Jul 28 16:04:15 2004 for s11n by doxygen 1.3.7