mirror of
https://github.com/zeek/zeek.git
synced 2025-10-12 11:38:20 +00:00
Merge remote-tracking branch 'remotes/origin/topic/robin/gh59-when'
Fixed a few small bugs - Modifiable had an uninitialized member and the Registry looped over a map while deleting elements from it. Fixes GH-319 * remotes/origin/topic/robin/gh59-when: Renaming src/StateAccess.{h,cc} to src/Notifier.{h,cc}. Clean up new code. Remove MutableVal class. Redo API for notifiers. Remove most of MutableVal (but not the class itelf yet) Remove enum Opcode. Remove StateAccess class. Redo NotfifierRegistry to no longer rely on StateAccess. Add new test for when-statement watching global variables. Couple of compile fixes.
This commit is contained in:
commit
b1be8abe4e
22 changed files with 365 additions and 1422 deletions
116
src/Notifier.h
Normal file
116
src/Notifier.h
Normal file
|
@ -0,0 +1,116 @@
|
|||
// See the file "COPYING" in the main distribution directory for copyright.
|
||||
//
|
||||
// A notification framework to inform interested parties of modifications to
|
||||
// selected global objects. To get notified about a change, derive a class
|
||||
// from notifier::Receiver and register the interesting objects with the
|
||||
// notification::Registry.
|
||||
|
||||
#ifndef NOTIFIER_H
|
||||
#define NOTIFIER_H
|
||||
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
#include "util.h"
|
||||
#include "DebugLogger.h"
|
||||
|
||||
namespace notifier {
|
||||
|
||||
class Modifiable;
|
||||
|
||||
/** Interface class for receivers of notifications. */
|
||||
class Receiver {
|
||||
public:
|
||||
Receiver();
|
||||
virtual ~Receiver();
|
||||
|
||||
/**
|
||||
* Callback executed when a register object has been modified.
|
||||
*
|
||||
* @param m object that was modified
|
||||
*/
|
||||
virtual void Modified(Modifiable* m) = 0;
|
||||
};
|
||||
|
||||
/** Singleton class tracking all notification requests globally. */
|
||||
class Registry {
|
||||
public:
|
||||
~Registry();
|
||||
|
||||
/**
|
||||
* Registers a receiver to be informed when a modifiable object has
|
||||
* changed.
|
||||
*
|
||||
* @param m object to track. Does not take ownership, but the object
|
||||
* will automatically unregister itself on destruction.
|
||||
*
|
||||
* @param r receiver to notify on changes. Does not take ownershop,
|
||||
* the receiver must remain valid as long as the registration stays
|
||||
* in place.
|
||||
*/
|
||||
void Register(Modifiable* m, Receiver* r);
|
||||
|
||||
/**
|
||||
* Cancels a receiver's request to be informed about an object's
|
||||
* modification. The arguments to the method must match what was
|
||||
* originally registered.
|
||||
*
|
||||
* @param m object to no loger track.
|
||||
*
|
||||
* @param r receiver to no longer notify.
|
||||
*/
|
||||
void Unregister(Modifiable* m, Receiver* Receiver);
|
||||
|
||||
/**
|
||||
* Cancels any active receiver requests to be informed about a
|
||||
* partilar object's modifications.
|
||||
*
|
||||
* @param m object to no loger track.
|
||||
*/
|
||||
void Unregister(Modifiable* m);
|
||||
|
||||
private:
|
||||
friend class Modifiable;
|
||||
|
||||
// Inform all registered receivers of a modification to an object.
|
||||
// Will be called from the object itself.
|
||||
void Modified(Modifiable* m);
|
||||
|
||||
typedef std::unordered_multimap<Modifiable*, Receiver*> ModifiableMap;
|
||||
ModifiableMap registrations;
|
||||
};
|
||||
|
||||
/**
|
||||
* Singleton object tracking all global notification requests.
|
||||
*/
|
||||
extern Registry registry;
|
||||
|
||||
/**
|
||||
* Base class for objects that can trigger notifications to receivers when
|
||||
* modified.
|
||||
*/
|
||||
class Modifiable {
|
||||
public:
|
||||
/**
|
||||
* Calling this method signals to all registered receivers that the
|
||||
* object has been modified.
|
||||
*/
|
||||
void Modified()
|
||||
{
|
||||
if ( num_receivers )
|
||||
registry.Modified(this);
|
||||
}
|
||||
|
||||
protected:
|
||||
friend class Registry;
|
||||
|
||||
virtual ~Modifiable();
|
||||
|
||||
// Number of currently registered receivers.
|
||||
uint64 num_receivers = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue