]> git.mxchange.org Git - simgear.git/blob - simgear/props/condition.hxx
3da63712f081d8d7f03eb057340bb44e5064f94b
[simgear.git] / simgear / props / condition.hxx
1 // condition.hxx - Declarations and inline methods for property conditions.
2 // Written by David Megginson, started 2000.
3 // CLO May 2003 - Split out condition specific code.
4 //
5 // This file is in the Public Domain, and comes with no warranty.
6
7 #ifndef __SG_CONDITION_HXX
8 #define __SG_CONDITION_HXX
9
10 #include <simgear/debug/logstream.hxx>
11 #include <simgear/props/props.hxx>
12 #include <simgear/props/props_io.hxx>
13
14 \f
15 ////////////////////////////////////////////////////////////////////////
16 // Conditions.
17 ////////////////////////////////////////////////////////////////////////
18
19
20 /**
21  * An encoded condition.
22  *
23  * This class encodes a single condition of some sort, possibly
24  * connected with properties.
25  *
26  * This class should migrate to somewhere more general.
27  */
28 class FGCondition
29 {
30 public:
31   FGCondition ();
32   virtual ~FGCondition ();
33   virtual bool test () const = 0;
34 };
35
36
37 /**
38  * Condition for a single property.
39  *
40  * This condition is true only if the property returns a boolean
41  * true value.
42  */
43 class FGPropertyCondition : public FGCondition
44 {
45 public:
46   FGPropertyCondition ( SGPropertyNode *prop_root,
47                         const char * propname  );
48   virtual ~FGPropertyCondition ();
49   virtual bool test () const { return _node->getBoolValue(); }
50 private:
51   const SGPropertyNode * _node;
52 };
53
54
55 /**
56  * Condition for a 'not' operator.
57  *
58  * This condition is true only if the child condition is false.
59  */
60 class FGNotCondition : public FGCondition
61 {
62 public:
63                                 // transfer pointer ownership
64   FGNotCondition (FGCondition * condition);
65   virtual ~FGNotCondition ();
66   virtual bool test () const;
67 private:
68   FGCondition * _condition;
69 };
70
71
72 /**
73  * Condition for an 'and' group.
74  *
75  * This condition is true only if all of the conditions
76  * in the group are true.
77  */
78 class FGAndCondition : public FGCondition
79 {
80 public:
81   FGAndCondition ();
82   virtual ~FGAndCondition ();
83   virtual bool test () const;
84                                 // transfer pointer ownership
85   virtual void addCondition (FGCondition * condition);
86 private:
87   vector<FGCondition *> _conditions;
88 };
89
90
91 /**
92  * Condition for an 'or' group.
93  *
94  * This condition is true if at least one of the conditions in the
95  * group is true.
96  */
97 class FGOrCondition : public FGCondition
98 {
99 public:
100   FGOrCondition ();
101   virtual ~FGOrCondition ();
102   virtual bool test () const;
103                                 // transfer pointer ownership
104   virtual void addCondition (FGCondition * condition);
105 private:
106   vector<FGCondition *> _conditions;
107 };
108
109
110 /**
111  * Abstract base class for property comparison conditions.
112  */
113 class FGComparisonCondition : public FGCondition
114 {
115 public:
116   enum Type {
117     LESS_THAN,
118     GREATER_THAN,
119     EQUALS
120   };
121   FGComparisonCondition (Type type, bool reverse = false);
122   virtual ~FGComparisonCondition ();
123   virtual bool test () const;
124   virtual void setLeftProperty( SGPropertyNode *prop_root,
125                                 const char * propname );
126   virtual void setRightProperty( SGPropertyNode *prop_root,
127                                  const char * propname );
128                                 // will make a local copy
129   virtual void setRightValue (const SGPropertyNode * value);
130 private:
131   Type _type;
132   bool _reverse;
133   const SGPropertyNode * _left_property;
134   const SGPropertyNode * _right_property;
135   const SGPropertyNode * _right_value;
136 };
137
138
139 /**
140  * Base class for a conditional components.
141  *
142  * This class manages the conditions and tests; the component should
143  * invoke the test() method whenever it needs to decide whether to
144  * active itself, draw itself, and so on.
145  */
146 class FGConditional
147 {
148 public:
149   FGConditional ();
150   virtual ~FGConditional ();
151                                 // transfer pointer ownership
152   virtual void setCondition (FGCondition * condition);
153   virtual const FGCondition * getCondition () const { return _condition; }
154   virtual bool test () const;
155 private:
156   FGCondition * _condition;
157 };
158
159
160 /**
161  * Global function to make a condition out of properties.
162  *
163  * The top-level is always an implicit 'and' group, whatever the
164  * node's name (it should usually be "condition").
165  *
166  * @param node The top-level condition node (usually named "condition").
167  * @return A pointer to a newly-allocated condition; it is the
168  *         responsibility of the caller to delete the condition when
169  *         it is no longer needed.
170  */
171 FGCondition * fgReadCondition( SGPropertyNode *prop_root,
172                                const SGPropertyNode *node );
173
174
175 #endif // __SG_CONDITION_HXX
176