zeek/src/Trigger.h
Johanna Amann 6d612ced3d Mark one-parameter constructors as explicit & use override where possible
This commit marks (hopefully) ever one-parameter constructor as explicit.

It also uses override in (hopefully) all circumstances where a virtual
method is overridden.

There are a very few other minor changes - most of them were necessary
to get everything to compile (like one additional constructor). In one
case I changed an implicit operation to an explicit string conversion -
I think the automatically chosen conversion was much more convoluted.

This took longer than I want to admit but not as long as I feared :)
2018-03-27 07:17:32 -07:00

119 lines
3 KiB
C++

#ifndef TRIGGER_H
#define TRIGGER_H
#include <list>
#include <map>
#include "StateAccess.h"
#include "Traverse.h"
// Triggers are the heart of "when" statements: expressions that when
// they become true execute a body of statements.
class TriggerTimer;
class TriggerTraversalCallback;
class Trigger : public NotifierRegistry::Notifier, public BroObj {
public:
// Don't access Trigger objects; they take care of themselves after
// instantiation. Note that if the condition is already true, the
// statements are executed immediately and the object is deleted
// right away.
Trigger(Expr* cond, Stmt* body, Stmt* timeout_stmts, Expr* timeout,
Frame* f, bool is_return, const Location* loc);
~Trigger() override;
// Evaluates the condition. If true, executes the body and deletes
// the object deleted.
//
// Returns the state of condition.
bool Eval();
// Executes timeout code and deletes the object.
void Timeout();
// Return the timeout interval (negative if none was specified).
double TimeoutValue() const
{ return timeout_value; }
// Called if another entity needs to complete its operations first
// in any case before this trigger can proceed.
void Hold() { delayed = true; }
// Complement of Hold().
void Release() { delayed = false; }
// If we evaluate to true, our return value will be passed on
// to the given trigger. Note, automatically calls Hold().
void Attach(Trigger* trigger);
// Cache for return values of delayed function calls.
void Cache(const CallExpr* expr, Val* val);
Val* Lookup(const CallExpr*);
// Disable this trigger completely. Needed because Unref'ing the trigger
// may not immediately delete it as other references may still exist.
void Disable();
bool Disabled() const { return disabled; }
void Describe(ODesc* d) const override
{ d->Add("<trigger>"); }
// Overidden from Notifier. We queue the trigger and evaluate it
// later to avoid race conditions.
void Access(ID* id, const StateAccess& sa) override
{ QueueTrigger(this); }
void Access(Val* val, const StateAccess& sa) override
{ QueueTrigger(this); }
const char* Name() const override;
static void QueueTrigger(Trigger* trigger);
// Evaluates all queued Triggers.
static void EvaluatePending();
struct Stats {
unsigned long total;
unsigned long pending;
};
static void GetStats(Stats* stats);
private:
friend class TriggerTraversalCallback;
friend class TriggerTimer;
void Init();
void Register(ID* id);
void Register(Val* val);
void UnregisterAll();
Expr* cond;
Stmt* body;
Stmt* timeout_stmts;
Expr* timeout;
double timeout_value;
Frame* frame;
bool is_return;
const Location* location;
TriggerTimer* timer;
Trigger* attached;
bool delayed; // true if a function call is currently being delayed
bool disabled;
val_list vals;
id_list ids;
typedef map<const CallExpr*, Val*> ValCache;
ValCache cache;
typedef list<Trigger*> TriggerList;
static TriggerList* pending;
static unsigned long total_triggers;
};
#endif