Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-01-24 15:40:46


AMDG

Robert Dailey wrote:
> Hi,
>
> I have a singleton class called InputRegistry. Before the class is
> used, the user must call:
>
> InputRegistry::Initialize();
>
> I would like this function to take a parameter that defines the type
> of a critical section object, which is unknown at compile time
> (template). I need to store this type in my InputRegistry class so
> that the unknown critical section type can be constructed later. How
> can I do this without making InputRegistry a template class? If I make
> InputRegistry a template, that basically means I need to redesign the
> class, as the singleton pattern would not apply if it were a template
> class.
>
> I'm hoping boost can help here. Thanks in advance.

I would expect the solution to look something like this (warning untested)

struct critical_section_base {
    virtual void lock() = 0;
    virtual void unlock() = 0;
};

template<class T>
struct critical_section_wrapper {
public:
    virtual void lock() { t.lock(); }
    virtual void unlock() { t.unlock(); }
private:
    T t;
};

class InputRegistry {
public:
    template<class T>
    static void Initialize() {
        critical_section_generator =
boost::lambda::new_<critical_section_wrapper<T> >();
    }
private:
    static boost::function<critical_section_base*()>
critical_section_generator;
};

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net