Re: Repository of all objects of a certain type
Hi,
James Kanze schrieb:
Well, the only disadvantage I see is that the factory of
ObjType (or the constructor) must be public. This is a bit
dangerous with respect to data integrity.
I thought the factory was public in your original example. Or
do you mean rather: the constructor must be public
Yes, the latter. Either I need a public constructor some other public
factory method of MyObject.
(which of
course, allows client code to create instances which aren't in
the index). If this is a problem, you can declare
ContainerSupport< MyObject, MyKey > friend of MyObject, and keep
the destructors private.
Exactly.
It works as far as I can see and the constructor of MyObject
is private. I wonder a bit since ContainerSupport<MyObject,
MyKey> is not yet a friend of MyObject when the template is
instantiated.
But it is! The constructor of MyObject is only used in the
functions, not in the class itself, and the functions are only
instantiated when they are actually used, at the point of use.
[...]
Should be fine.
But at least the 17 year old IBM compiler and gcc 3.3.5 ate
it. Maybe because of the same reason as the above dependency.
I wouldn't count on this working with just any 17 year old
compiler. 17 years ago, class instantiation was all or nothing
for most compilers (if they supported templates at all). IBM is
probably an exception---that's where Barton and Nackmann worked,
and they pushed the compiler team considerably (much like Boost
has in recent times). At the time, IBM was considerably in
advance of anyone else with regards to templates.
The IBM VAC++ 3.0 compiler did not cause much harm so far. The only
unsupported pattern I found so far is that it cannot handle temporaries
in conditional expressions. But even in this case a warning told about
that fact.
The ececutables are pretty small and the compilation time is neglectable
in comparsion to g++. I did not find any differences in the behaviour of
the application so far. So I mostly use IBM Visual Age. Only the
warnings of gcc are a bit more sensitive to potentially buggy code
fragments.
I was a bit supprised about the good C++ support too. gcc before 3.x was
quite unuseable with respect to C++, Watcom too. And Borland C++ was
always more like a conglomerate of compiler bugs.
Pretty useful pattern.
Yep. I can't take credit for it, however---Barton and Nackmann
proposed a variant of it well over ten years ago.
I never heard about that before. Maybe because I didn't need it. But it
was not that simple to get the pattern fully thread-safe. I finally got
it by checking whether the intrusive reference counter is non-zero
before returning an element from the index.
Marcel