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.
5 // This file is in the Public Domain, and comes with no warranty.
7 #ifndef __SG_CONDITION_HXX
8 #define __SG_CONDITION_HXX
10 #include <simgear/debug/logstream.hxx>
11 #include <simgear/props/props.hxx>
12 #include <simgear/props/props_io.hxx>
15 ////////////////////////////////////////////////////////////////////////
17 ////////////////////////////////////////////////////////////////////////
21 * An encoded condition.
23 * This class encodes a single condition of some sort, possibly
24 * connected with properties.
26 * This class should migrate to somewhere more general.
32 virtual ~FGCondition ();
33 virtual bool test () const = 0;
38 * Condition for a single property.
40 * This condition is true only if the property returns a boolean
43 class FGPropertyCondition : public FGCondition
46 FGPropertyCondition ( SGPropertyNode *prop_root,
47 const char * propname );
48 virtual ~FGPropertyCondition ();
49 virtual bool test () const { return _node->getBoolValue(); }
51 const SGPropertyNode * _node;
56 * Condition for a 'not' operator.
58 * This condition is true only if the child condition is false.
60 class FGNotCondition : public FGCondition
63 // transfer pointer ownership
64 FGNotCondition (FGCondition * condition);
65 virtual ~FGNotCondition ();
66 virtual bool test () const;
68 FGCondition * _condition;
73 * Condition for an 'and' group.
75 * This condition is true only if all of the conditions
76 * in the group are true.
78 class FGAndCondition : public FGCondition
82 virtual ~FGAndCondition ();
83 virtual bool test () const;
84 // transfer pointer ownership
85 virtual void addCondition (FGCondition * condition);
87 vector<FGCondition *> _conditions;
92 * Condition for an 'or' group.
94 * This condition is true if at least one of the conditions in the
97 class FGOrCondition : public FGCondition
101 virtual ~FGOrCondition ();
102 virtual bool test () const;
103 // transfer pointer ownership
104 virtual void addCondition (FGCondition * condition);
106 vector<FGCondition *> _conditions;
111 * Abstract base class for property comparison conditions.
113 class FGComparisonCondition : public FGCondition
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);
133 const SGPropertyNode * _left_property;
134 const SGPropertyNode * _right_property;
135 const SGPropertyNode * _right_value;
140 * Base class for a conditional components.
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.
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;
156 FGCondition * _condition;
161 * Global function to make a condition out of properties.
163 * The top-level is always an implicit 'and' group, whatever the
164 * node's name (it should usually be "condition").
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.
171 FGCondition * fgReadCondition( SGPropertyNode *prop_root,
172 const SGPropertyNode *node );
175 #endif // __SG_CONDITION_HXX