]> git.mxchange.org Git - simgear.git/blob - simgear/props/condition.hxx
Merge branch 'fred/precip' into next
[simgear.git] / simgear / props / condition.hxx
1 /**
2  * \file condition.hxx
3  * Declarations and inline methods for property conditions.
4  * Written by David Megginson, started 2000.
5  * CLO May 2003 - Split out condition specific code.
6  *
7  * This file is in the Public Domain, and comes with no warranty.
8  */
9
10 #ifndef __SG_CONDITION_HXX
11 #define __SG_CONDITION_HXX
12
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>
17
18 \f
19 ////////////////////////////////////////////////////////////////////////
20 // Conditions.
21 ////////////////////////////////////////////////////////////////////////
22
23
24 /**
25  * An encoded condition.
26  *
27  * This class encodes a single condition of some sort, possibly
28  * connected with properties.
29  *
30  * This class should migrate to somewhere more general.
31  */
32 class SGCondition : public SGReferenced
33 {
34 public:
35   SGCondition ();
36   virtual ~SGCondition ();
37   virtual bool test () const = 0;
38 };
39
40
41 /**
42  * Condition for a single property.
43  *
44  * This condition is true only if the property returns a boolean
45  * true value.
46  */
47 class SGPropertyCondition : public SGCondition
48 {
49 public:
50   SGPropertyCondition ( SGPropertyNode *prop_root,
51                         const char * propname  );
52   virtual ~SGPropertyCondition ();
53   virtual bool test () const { return _node->getBoolValue(); }
54 private:
55   SGConstPropertyNode_ptr _node;
56 };
57
58
59 /**
60  * Condition for a 'not' operator.
61  *
62  * This condition is true only if the child condition is false.
63  */
64 class SGNotCondition : public SGCondition
65 {
66 public:
67   SGNotCondition (SGCondition * condition);
68   virtual ~SGNotCondition ();
69   virtual bool test () const;
70 private:
71   SGSharedPtr<SGCondition> _condition;
72 };
73
74
75 /**
76  * Condition for an 'and' group.
77  *
78  * This condition is true only if all of the conditions
79  * in the group are true.
80  */
81 class SGAndCondition : public SGCondition
82 {
83 public:
84   SGAndCondition ();
85   virtual ~SGAndCondition ();
86   virtual bool test () const;
87                                 // transfer pointer ownership
88   virtual void addCondition (SGCondition * condition);
89 private:
90   std::vector<SGSharedPtr<SGCondition> > _conditions;
91 };
92
93
94 /**
95  * Condition for an 'or' group.
96  *
97  * This condition is true if at least one of the conditions in the
98  * group is true.
99  */
100 class SGOrCondition : public SGCondition
101 {
102 public:
103   SGOrCondition ();
104   virtual ~SGOrCondition ();
105   virtual bool test () const;
106                                 // transfer pointer ownership
107   virtual void addCondition (SGCondition * condition);
108 private:
109   std::vector<SGSharedPtr<SGCondition> > _conditions;
110 };
111
112
113 /**
114  * Abstract base class for property comparison conditions.
115  */
116 class SGComparisonCondition : public SGCondition
117 {
118 public:
119   enum Type {
120     LESS_THAN,
121     GREATER_THAN,
122     EQUALS
123   };
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);
133 private:
134   Type _type;
135   bool _reverse;
136   SGConstPropertyNode_ptr _left_property;
137   SGConstPropertyNode_ptr _right_property;
138   SGConstPropertyNode_ptr _right_value;
139 };
140
141
142 /**
143  * Base class for a conditional components.
144  *
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.
148  */
149 class SGConditional : public SGReferenced
150 {
151 public:
152   SGConditional ();
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;
158 private:
159   SGSharedPtr<SGCondition> _condition;
160 };
161
162
163 /**
164  * Global function to make a condition out of properties.
165  *
166  * The top-level is always an implicit 'and' group, whatever the
167  * node's name (it should usually be "condition").
168  *
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.
173  */
174 SGCondition *sgReadCondition( SGPropertyNode *prop_root,
175                               const SGPropertyNode *node );
176
177
178 #endif // __SG_CONDITION_HXX
179