Branch data Line data Source code
1 : : // $Id: Trigger.h 2359 2005-12-21 23:55:32Z vern $
2 : :
3 : : #ifndef TRIGGER_H
4 : : #define TRIGGER_H
5 : :
6 : : #include <list>
7 : : #include <map>
8 : :
9 : : #include "StateAccess.h"
10 : : #include "Traverse.h"
11 : :
12 : : // Triggers are the heart of "when" statements: expressions that when
13 : : // they become true execute a body of statements.
14 : :
15 : : class TriggerTimer;
16 : : class TriggerTraversalCallback;
17 : :
18 : : class Trigger : public NotifierRegistry::Notifier, public BroObj {
19 : : public:
20 : : // Don't access Trigger objects; they take care of themselves after
21 : : // instantiation. Note that if the condition is already true, the
22 : : // statements are executed immediately and the object is deleted
23 : : // right away.
24 : : Trigger(Expr* cond, Stmt* body, Stmt* timeout_stmts, Expr* timeout,
25 : : Frame* f, bool is_return, const Location* loc);
26 : : ~Trigger();
27 : :
28 : : // Evaluates the condition. If true, executes the body and deletes
29 : : // the object deleted.
30 : : //
31 : : // Returns the state of condition.
32 : : bool Eval();
33 : :
34 : : // Executes timeout code and deletes the object.
35 : : void Timeout();
36 : :
37 : : // Called if another entity needs to complete its operations first
38 : : // in any case before this trigger can proceed.
39 : 0 : void Hold() { delayed = true; }
40 : :
41 : : // Complement of Hold().
42 : 0 : void Release() { delayed = false; }
43 : :
44 : : // If we evaluate to true, our return value will be passed on
45 : : // to the given trigger. Note, automatically calls Hold().
46 : : void Attach(Trigger* trigger);
47 : :
48 : : // Cache for return values of delayed function calls.
49 : : void Cache(const CallExpr* expr, Val* val);
50 : : Val* Lookup(const CallExpr*);
51 : :
52 : : // Disable this trigger completely. Needed because Unref'ing the trigger
53 : : // may not immediately delete it as other references may still exist.
54 : 0 : void Disable() { disabled = true; }
55 : :
56 : 0 : virtual void Describe(ODesc* d) const { d->Add("<trigger>"); }
57 : :
58 : : // Overidden from Notifier. We queue the trigger and evaluate it
59 : : // later to avoid race conditions.
60 : 0 : virtual void Access(ID* id, const StateAccess& sa)
61 : 0 : { QueueTrigger(this); }
62 : 0 : virtual void Access(Val* val, const StateAccess& sa)
63 : 0 : { QueueTrigger(this); }
64 : :
65 : : virtual const char* Name();
66 : :
67 : : static void QueueTrigger(Trigger* trigger);
68 : :
69 : : // Evaluates all queued Triggers.
70 : : static void EvaluatePending();
71 : :
72 : : private:
73 : : friend class TriggerTraversalCallback;
74 : : friend class TriggerTimer;
75 : :
76 : : void Init();
77 : : void DeleteTrigger();
78 : : void Register(ID* id);
79 : : void Register(Val* val);
80 : : void UnregisterAll();
81 : :
82 : : Expr* cond;
83 : : Stmt* body;
84 : : Stmt* timeout_stmts;
85 : : Expr* timeout;
86 : : Frame* frame;
87 : : bool is_return;
88 : : const Location* location;
89 : :
90 : : TriggerTimer* timer;
91 : : Trigger* attached;
92 : :
93 : : bool delayed; // true if a function call is currently being delayed
94 : : bool disabled;
95 : :
96 : : val_list vals;
97 : : id_list ids;
98 : :
99 : : typedef map<const CallExpr*, Val*> ValCache;
100 : : ValCache cache;
101 : :
102 : : typedef list<Trigger*> TriggerList;
103 : : static TriggerList* pending;
104 : : };
105 : :
106 : : #endif
|