
I am trying to come up with a mechanism which allows member functions of the singleton to be called if the instance exists, and which do nothing if the instance does not exist (no creation is attempted, the operation is simply skipped). My current idea is a member function of the singleton pointer named zombie_call, which would be used something like: ptr.zombie_call ( &MySingletonType::SomeFunc, param1, param2, ... ); The only problem I have is what to do if the instance does not exist and the member function has a return value. Should I just return a default-constructed object in these cases? (And what should happen if the return type does not have a constructor taking no arguments?) Should I append a parameter to the end of zombie_call which is returned if the instance does not exist, and have it default to a default-constructed object? Should I append a parameter that is a reference to the return type, and just assign the return value to it if the operation is successful? (And what happens if the return type is already a reference?) Should I just enforce that member functions called with zombie_call return void? Also, is there any way to make the syntax of the call a bit nicer? I was originally hoping to have a separate pointer type called zombie_ptr, which just behaved in this manner when functions were accessed via operator->, but I couldn't think of a non-intrusive way to pull it off... Any and all thoughts are appreciated. -Jason

Jason Hise wrote:
I am trying to come up with a mechanism which allows member functions of the singleton to be called if the instance exists, and which do nothing if the instance does not exist (no creation is attempted, the operation is simply skipped). My current idea is a member function of the singleton pointer named zombie_call, which would be used something like:
ptr.zombie_call ( &MySingletonType::SomeFunc, param1, param2, ... );
The only problem I have is what to do if the instance does not exist and the member function has a return value. Should I just return a default-constructed object in these cases? (And what should happen if the return type does not have a constructor taking no arguments?) Should I append a parameter to the end of zombie_call which is returned if the instance does not exist, and have it default to a default-constructed object? Should I append a parameter that is a reference to the return type, and just assign the return value to it if the operation is successful? (And what happens if the return type is already a reference?) Should I just enforce that member functions called with zombie_call return void?
Also, is there any way to make the syntax of the call a bit nicer? I was originally hoping to have a separate pointer type called zombie_ptr, which just behaved in this manner when functions were accessed via operator->, but I couldn't think of a non-intrusive way to pull it off...
Any and all thoughts are appreciated. Regarding how a call to the singleton class' static "Instance()" method should be handled when no instance of the singleton exists, couldn't the class take a "lifetime" policy class template parameter which determines whether a new instance is constructed or an exception should be thrown?

Jason Kankiewicz wrote:
Regarding how a call to the singleton class' static "Instance()" method should be handled when no instance of the singleton exists
There is no instance method. I use smart pointers which always point to the same unique instance as the access mechanism: singleton_ptr < SingletonType, Name > ptr; ptr->SomeFunc ( ); // SomeFunc is a member function of type SingletonType
, couldn't the class take a "lifetime" policy class template parameter which determines whether a new instance is constructed or an exception should be thrown?
I am planning on providing multiple different pointer types to handle this so that different pointer types to the same instance can exist in the same program where needed. Specifically, the types I am considering are strong_ptr, lazy_ptr, weak_ptr, and zombie_ptr. The idea is that strong_ptr will create the instance if it does not already exist when the pointer is constructed, and will create it if needed when dereferenced as well. lazy_ptr will just create the instance if need be when dereferenced, and weak_ptr will never create the instance automatically. If the instance does not exist when the weak_ptr is dereferenced an exception will be thrown. I wanted zombie_ptr to be like a weak_ptr in that it never would attempt automatic instance creation, but when dereferenced instead of throwing an exception it would just do nothing and pretend that the call succeeded. I don't know how to allow this with pointer-to-member syntax, however, which is why I was considering adding the zombie_call method to the other pointer types. Rob Stewart wrote:
I am trying to come up with a mechanism which allows member functions of the singleton to be called if the instance exists, and which do nothing if the instance does not exist (no creation is attempted, the operation is simply skipped).
What is the use case for this?
Perhaps there is some sort of logging singleton that sends messages across a network or some other expensive-to-connect-to place. When the program is shutting down, there may be destructors that wish to log information, but not if doing so results in recreating an expensive singleton. Especially if there is a chance that construction of that singleton instance can throw an exception (like a failure to connect). A zombie_call method would be the perfect way to ensure that such messages are only logged when convenient. -Jason

Date: Tue, 26 Jul 2005 13:10:17 -0400 From: Jason Hise <chaos@ezequal.com> Reply-To: boost@lists.boost.org Sender: boost-bounces@lists.boost.org
I am trying to come up with a mechanism which allows member functions of the singleton to be called if the instance exists, and which do nothing if the instance does not exist (no creation is attempted, the operation is simply skipped). My current idea is a member function of the singleton pointer named zombie_call, which would be used something like:
ptr.zombie_call ( &MySingletonType::SomeFunc, param1, param2, ... );
The only problem I have is what to do if the instance does not exist and the member function has a return value. Should I just return a default-constructed object in these cases? (And what should happen if the return type does not have a constructor taking no arguments?) Should I append a parameter to the end of zombie_call which is returned if the instance does not exist, and have it default to a default-constructed object? Should I append a parameter that is a reference to the return type, and just assign the return value to it if the operation is successful? (And what happens if the return type is already a reference?) Should I just enforce that member functions called with zombie_call return void?
Also, is there any way to make the syntax of the call a bit nicer? I was originally hoping to have a separate pointer type called zombie_ptr, which just behaved in this manner when functions were accessed via operator->, but I couldn't think of a non-intrusive way to pull it off...
Any and all thoughts are appreciated.
-Jason
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Trying again. C-c C-c isn't C-x C-x! From: Jason Hise <chaos@ezequal.com>
I am trying to come up with a mechanism which allows member functions of the singleton to be called if the instance exists, and which do nothing if the instance does not exist (no creation is attempted, the operation is simply skipped).
What is the use case for this? -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Jason Hise wrote:
I am trying to come up with a mechanism which allows member functions of the singleton to be called if the instance exists, and which do nothing if the instance does not exist (no creation is attempted, the operation is simply skipped). My current idea is a member function of the singleton pointer named zombie_call, which would be used something like:
ptr.zombie_call ( &MySingletonType::SomeFunc, param1, param2, ... );
The only problem I have is what to do if the instance does not exist and the member function has a return value.
Use Boost.Optional: http://www.boost.org/libs/optional/doc/optional.html HTH Fernando Cacciola SciSoft

Fernando Cacciola wrote:
Jason Hise wrote:
I am trying to come up with a mechanism which allows member functions of the singleton to be called if the instance exists, and which do nothing if the instance does not exist (no creation is attempted, the operation is simply skipped).
[...]
Use Boost.Optional:
I don't see how this helps... optional does not appear to allow member functions to be called when the object does not exist. Instead, it simply asserts that the object does exist before the -> operator returns. For my hypothetical zombie_ptr, I want to make usage when the instance does not exist a no-op, not an error. -Jason

Jason Hise wrote:
Fernando Cacciola wrote:
Jason Hise wrote:
I am trying to come up with a mechanism which allows member functions of the singleton to be called if the instance exists, and which do nothing if the instance does not exist (no creation is attempted, the operation is simply skipped).
[...]
Use Boost.Optional:
I don't see how this helps... optional does not appear to allow member functions to be called when the object does not exist. Instead, it simply asserts that the object does exist before the -> operator returns. For my hypothetical zombie_ptr, I want to make usage when the instance does not exist a no-op, not an error.
if ( ptr != null ) return optional<result_type>(ptr->member_function(...)); else return optional<result_type>(); Fernando Cacciola SciSoft
participants (4)
-
Fernando Cacciola
-
Jason Hise
-
Jason Kankiewicz
-
Rob Stewart