
Pavel and I have been discussing a sibling class to the singleton, namely a multiton. Such a class would manage a unique object instance for each key provided to GetInst. It should be able to use many of the same policies that the singleton uses. Pavel thinks that it could be useful to allow GetInst to take any number of parameters. Would this actually be useful, and if so, how could such a design be achieved? -Jason

"Jason Hise" wrote:
Pavel and I have been discussing a sibling class to the singleton, namely a multiton. Such a class would manage a unique object instance for each key provided to GetInst. It should be able to use many of the same policies that the singleton uses.
Pavel thinks that it could be useful to allow GetInst to take any number of parameters. Would this actually be useful, and if so, how could such a design be achieved?
The idea is to have library which allows to create "parametrized singletons", like: City& c1 = City::get_inst("Belgium", "Ghent"); ... City& c2 = City::get_inst("Belgium", "Ghent"); assert(&c1 == &c2); The name "Multiton" was already used with similar library in Ruby. The problem is what syntax to use to define Multiton, like: template < typename T, typename Parameters = TYPELIST2(std::string, std::string), typename Lifetime = ...
class Multiton { .... }; and how to 'generate' flat structure from the typelist. (The structure would be then used as key in lookup map.) /Pavel

On 01/24/2005 04:04 AM, Pavel Vozenilek wrote: [snip]
The problem is what syntax to use to define Multiton, like:
template < typename T, typename Parameters = TYPELIST2(std::string, std::string), typename Lifetime = ...
class Multiton { .... };
and how to 'generate' flat structure from the typelist. (The structure would be then used as key in lookup map.)
Use: reverse_fold<Parameters,empty_base,SingletonNode> described here: http://www.boost.org/libs/mpl/doc/refmanual/reverse-fold.html as a superclass of Multiton. The instantiations of last argument would define the singleton's for the various elements in the typelist while inheriting the succeeding ones in the typelist. Accessing the ith-singleton can be done with the code in: get_side_type.zip in http://boost-sandbox.sourceforge.net/vault/ . The code in get_ith_head_test.cpp can be used as a model for making a get<i> member function for accessing the i-th singleton. HTH. Regards, Larry

On 01/24/2005 05:38 AM, Larry Evans wrote:
HTH.
OOPS. Sorry. I responded too soon. I thought the Parameters were the various types of singletons, but they're parameters to a key in the lookup list. I guess in that case, you could do the same thing but just define an operator< for the reverse_fold result by defining individual operator<'s for elements in the typelist and use Multiton::operator< for an element by element comparison. HTH. Larry
participants (3)
-
Jason Hise
-
Larry Evans
-
Pavel Vozenilek