Boost logo

Boost :

From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2005-05-05 12:59:14


"Tom Brinkman" wrote:

> In the singleton documentation it states that "singletons" could/should
> replace all global variables in an application. Does this comment
> apply to global vectors as well.
>
> I have a vector of boost::any which I've declared
> static in a resource class.
>
> <code below>
>
> struct resource
> {
> static std::vector<boost::any> resources;
> ...<more>...
> };
>
> std::vector<boost::any> resource::resouces;
>
> How would one incorporate this global vector into
> the singleton library to take advantage of its facilities.
>
The wording should be less definitive, IMHO.

a) By wrapping the vector in a class and then deriving
    singleton from result.

struct resource : basic_singleton<resource>
{
    std::vector<boost::any> any_resources;
};

....
resource::pointer ptr;
ptr->any_resources.push_back(...);

b) Or if you only want to destroy the resource at certain
    moment when the application closes, you may do:

// global variable but destroyed before singletons with higher longevity
shared_ptr <vector<any> > > my_resources ( new vector<any>() );
singleton::store_resource ( my_resources );

/Pavel


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