J
jacob navia
An "observer" is an object that wants to be notified when something of
interest happens in another object.
Practically, within the context of the container library, an observer
is a callback that is called when an operation for which the observer
has subscribed happens.
The observer interface looks like this:
typedef void (*ObserverFunction)(void *ObservedObject,unsigned
operation, void *ExtraInfo[]);
typedef struct tagObserverInterface {
int (*Subscribe)(void *ObservedObject, ObserverFunction callback,
unsigned Operations);
int (*Notify)(void *ObservedObject,unsigned operation,void
*ExtraInfo1,void *ExtraInfo2);
size_t (*Unsubscribe)(void *ObservedObject,ObserverFunction callback);
} ObserverInterface;
extern ObserverInterface iObserver;
The process goes like this:
1) You subscribe to a container requesting to be informed about a set
of operations you want to observe.
2) The container notifies you when one of the operations that you
subscribed to happens.
3) You unsubscribe when you are going out of scope or for whatever
reason. Note that the container can unsubscribe too if it is going
out of scope.
Note that the notifications gives to the observer the maximum
information possible so that there is no need to perform costly
operations to retrieve it. Each operation (Add, Erase, Replace, etc)
will pass a different set of extra information to the observer.
The implementation is already at http://code.google.com/p/ccl. When
an object has no observers, there is still a small overhead to pay:
the test of a bit in the "Flags" field. I haven't been able to
eliminate that "overhead", and that means that all operations that
change the container have this test.
When a message to an observer should be sent the overhead is bigger
than that since the observer must search for all occurrences of the
object in the observer table.
The observer table associates objects with their callbacks. An object
can have several callbacks and a callback can have several objects.
At this stage I would like to have (if possible) feedback on the design
of this. Am I missing something fundamental?
Thanks in advance.
jacob
interest happens in another object.
Practically, within the context of the container library, an observer
is a callback that is called when an operation for which the observer
has subscribed happens.
The observer interface looks like this:
typedef void (*ObserverFunction)(void *ObservedObject,unsigned
operation, void *ExtraInfo[]);
typedef struct tagObserverInterface {
int (*Subscribe)(void *ObservedObject, ObserverFunction callback,
unsigned Operations);
int (*Notify)(void *ObservedObject,unsigned operation,void
*ExtraInfo1,void *ExtraInfo2);
size_t (*Unsubscribe)(void *ObservedObject,ObserverFunction callback);
} ObserverInterface;
extern ObserverInterface iObserver;
The process goes like this:
1) You subscribe to a container requesting to be informed about a set
of operations you want to observe.
2) The container notifies you when one of the operations that you
subscribed to happens.
3) You unsubscribe when you are going out of scope or for whatever
reason. Note that the container can unsubscribe too if it is going
out of scope.
Note that the notifications gives to the observer the maximum
information possible so that there is no need to perform costly
operations to retrieve it. Each operation (Add, Erase, Replace, etc)
will pass a different set of extra information to the observer.
The implementation is already at http://code.google.com/p/ccl. When
an object has no observers, there is still a small overhead to pay:
the test of a bit in the "Flags" field. I haven't been able to
eliminate that "overhead", and that means that all operations that
change the container have this test.
When a message to an observer should be sent the overhead is bigger
than that since the observer must search for all occurrences of the
object in the observer table.
The observer table associates objects with their callbacks. An object
can have several callbacks and a callback can have several objects.
At this stage I would like to have (if possible) feedback on the design
of this. Am I missing something fundamental?
Thanks in advance.
jacob