3 * Declarations and inline methods for property conditions.
4 * Written by David Megginson, started 2000.
5 * CLO May 2003 - Split out condition specific code.
7 * This file is in the Public Domain, and comes with no warranty.
10 #ifndef __SG_CONDITION_HXX
11 #define __SG_CONDITION_HXX
13 #include <simgear/debug/logstream.hxx>
14 #include <simgear/props/props.hxx>
15 #include <simgear/props/props_io.hxx>
16 #include <simgear/structure/SGReferenced.hxx>
19 ////////////////////////////////////////////////////////////////////////
21 ////////////////////////////////////////////////////////////////////////
25 * An encoded condition.
27 * This class encodes a single condition of some sort, possibly
28 * connected with properties.
30 * This class should migrate to somewhere more general.
32 class SGCondition : public SGReferenced
36 virtual ~SGCondition ();
37 virtual bool test () const = 0;
42 * Condition for a single property.
44 * This condition is true only if the property returns a boolean
47 class SGPropertyCondition : public SGCondition
50 SGPropertyCondition ( SGPropertyNode *prop_root,
51 const char * propname );
52 virtual ~SGPropertyCondition ();
53 virtual bool test () const { return _node->getBoolValue(); }
55 SGConstPropertyNode_ptr _node;
60 * Condition for a 'not' operator.
62 * This condition is true only if the child condition is false.
64 class SGNotCondition : public SGCondition
67 SGNotCondition (SGCondition * condition);
68 virtual ~SGNotCondition ();
69 virtual bool test () const;
71 SGSharedPtr<SGCondition> _condition;
76 * Condition for an 'and' group.
78 * This condition is true only if all of the conditions
79 * in the group are true.
81 class SGAndCondition : public SGCondition
85 virtual ~SGAndCondition ();
86 virtual bool test () const;
87 // transfer pointer ownership
88 virtual void addCondition (SGCondition * condition);
90 std::vector<SGSharedPtr<SGCondition> > _conditions;
95 * Condition for an 'or' group.
97 * This condition is true if at least one of the conditions in the
100 class SGOrCondition : public SGCondition
104 virtual ~SGOrCondition ();
105 virtual bool test () const;
106 // transfer pointer ownership
107 virtual void addCondition (SGCondition * condition);
109 std::vector<SGSharedPtr<SGCondition> > _conditions;
114 * Abstract base class for property comparison conditions.
116 class SGComparisonCondition : public SGCondition
124 SGComparisonCondition (Type type, bool reverse = false);
125 virtual ~SGComparisonCondition ();
126 virtual bool test () const;
127 virtual void setLeftProperty( SGPropertyNode *prop_root,
128 const char * propname );
129 virtual void setRightProperty( SGPropertyNode *prop_root,
130 const char * propname );
131 // will make a local copy
132 virtual void setRightValue (const SGPropertyNode * value);
136 SGConstPropertyNode_ptr _left_property;
137 SGConstPropertyNode_ptr _right_property;
138 SGConstPropertyNode_ptr _right_value;
143 * Base class for a conditional components.
145 * This class manages the conditions and tests; the component should
146 * invoke the test() method whenever it needs to decide whether to
147 * active itself, draw itself, and so on.
149 class SGConditional : public SGReferenced
153 virtual ~SGConditional ();
154 // transfer pointer ownership
155 virtual void setCondition (SGCondition * condition);
156 virtual const SGCondition * getCondition () const { return _condition; }
157 virtual bool test () const;
159 SGSharedPtr<SGCondition> _condition;
164 * Global function to make a condition out of properties.
166 * The top-level is always an implicit 'and' group, whatever the
167 * node's name (it should usually be "condition").
169 * @param node The top-level condition node (usually named "condition").
170 * @return A pointer to a newly-allocated condition; it is the
171 * responsibility of the caller to delete the condition when
172 * it is no longer needed.
174 SGCondition *sgReadCondition( SGPropertyNode *prop_root,
175 const SGPropertyNode *node );
178 #endif // __SG_CONDITION_HXX