J
jacob navia
An observer is a "must have" for my container library.
For each container you should be able to add an "observer" to it, i.e.
adding a function that will be called when the container is modified.
The callback function should receive a lot of information about what is
being changed, but that is another problem.
As far as I see I have two choices.
(1) Add a slot to the header object. This is simple, but it would mean
that this already heavy objects are bigger by sizeof(void *), not
very exciting considering that in 99.99% of the case you are not
going to use it.
(2) Add some global list to the software that would maintain a list
(or hash table) of containers, each with a list of functions that
should be called when it changes. Then, the flags field that is now
mostly empty could be used to flag any container that posesses an
observer. If that flag is set, after any modification the software
would inspect the list of observers and see if this one is in
that list. Problems with this second solution is that it represents
a multi-threaded bottleneck, is much more expensive since the
global observer lists could be quite long.
Has anyone here any better solution?
By the way, does anyone know how is this implemented in the C++ STL (if
at all)?
For each container you should be able to add an "observer" to it, i.e.
adding a function that will be called when the container is modified.
The callback function should receive a lot of information about what is
being changed, but that is another problem.
As far as I see I have two choices.
(1) Add a slot to the header object. This is simple, but it would mean
that this already heavy objects are bigger by sizeof(void *), not
very exciting considering that in 99.99% of the case you are not
going to use it.
(2) Add some global list to the software that would maintain a list
(or hash table) of containers, each with a list of functions that
should be called when it changes. Then, the flags field that is now
mostly empty could be used to flag any container that posesses an
observer. If that flag is set, after any modification the software
would inspect the list of observers and see if this one is in
that list. Problems with this second solution is that it represents
a multi-threaded bottleneck, is much more expensive since the
global observer lists could be quite long.
Has anyone here any better solution?
By the way, does anyone know how is this implemented in the C++ STL (if
at all)?