|
Boost : |
From: Jason Hise (chaos_at_[hidden])
Date: 2005-02-14 23:54:11
Pavel Vozenilek wrote:
>Maybe function
>
>template<.....>
>T* unsafe_get_pointer(singleton<T>::pointer&) { ... }
>
>would be enough. Having one more class
>for just one function feels as overkill.
>
>Local need for raw pointer should not mean
>possibly global change in design.
>
>/Pavel
>
I'm terribly torn on this issue.
Use of a raw singleton pointer is dangerous if it remains in use for a
long period of time, specifically long enough for the instance to have a
possibility of being destroyed. If the singleton uses a real locking
policy the code using the raw pointer would have to lock the operations
manually.
On the other hand, a raw pointer is the only really feasible way to pass
a singleton pointer as a pointer to a non singleton base class. Example:
class LoggerBase
{
public:
virtual void Log ( const char * msg ) = 0;
};
class GlobalLog : public singleton < GlobalLog >, public LoggerBase
{
public:
virtual void Log ( const char * msg )
{
// write to file or something
}
};
void PerformOperation ( vector < LoggerBase * > listeners, /*other
operands*/ )
{
if ( shit_happens )
{
// use lambda and for_each to write a message to each log in
listeners
// (I don't know the lambda library so I can't write this code
myself. I should take next weekend to learn it, it looks really handy)
}
}
Thus, at this point my thought is that the C++ motto is 'trust the
programmer', so I'm thinking I should just provide the unsafe_raw_ptr (
) member in the singleton, surround it with comments in all caps, and
document the heck out of it.
-Jason
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk