Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-10-12 20:59:32


Due to the fact that to get a bachelors degree in computer science at my
university I need 4 semesters of a foreign language, I currently have
run out of time to keep working on singleton. I have no idea why C++
couldn't count as my foreign language... for whatever reason I just have
to learn French instead :-P. I definitely plan to resume work on this
library in the future, but in order to avoid keeping everyone else
waiting I am uploading the current code to the sandbox. I think that
although I have not yet had time to create html docs, the comments
document the functionality reasonably well. The only real things
missing are some sort of timeout_lifetime and multiton_ptr. As a
jumping off point, here is some sample code to demonstrate example usage:

#include <boost/singleton_ptr.hpp>

class MyClass
{
public:
    void DoSomething ( ) { }
};

typedef boost::singleton_ptr < MyClass > MySingletonPtr;

void foo ( )
{
    MySingletonPtr ptr;
    ptr->DoSomething ( );
}

singleton_ptr uses named template parameters to specialize
functionality. The following policies are provided:

factory - determines how instances are created and destroyed, also
responsible for memory allocation
threading - determines how member access is serialized
lifetime - determines how instance is automatically destroyed
storage - determines where and how the singleton policies themselves are
stored
name - allows multiple singletons of the same type to coexist, even if
their other policies aren't different.
       also used to specify segment names in the case of shmem.

you may specify any or all of these, in any order, as follows:

using boost::singleton_ptr;

using boost::singleton::name;
using boost::singleton::factory;
using boost::singleton::threading;
using boost::singleton::lifetime;
using boost::singleton::storage;

typedef boost::singleton_ptr
<
    MyType,
    name < /*instance name here*/ >
    factory < /*factory policy here*/ >,
    threading < /*threading policy here*/ >,
    lifetime < /*lifetime policy here*/ >,
    storage < /*storage policy here*/ >
> MySingletonPtr;

When you declare a singleton_ptr, you can also throw manual_creation,
lazy_creation, or greedy_creation anywhere into the policy list. This
is a special argument, and multiple singleton_ptr types which differ
only by this specification will still point to the same instance (this
cannot be said about any of the other arguments). Choosing one of these
allows you to control when pointers of that type will perform automatic
creation (if the instance does not yet already exist).

Since there is not yet any official documentation, I encourage everyone
to use me as documentation and pummel me with questions :) If you
wonder if something can or can't be done with the library, want
clarification on some of its features, or have ideas for code
improvement please let me know.

The code is in the sandbox under the "Patterns" folder.

-Jason


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk