Solid

predicate.h
1/*
2 SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#ifndef SOLID_PREDICATE_H
8#define SOLID_PREDICATE_H
9
10#include <QSet>
11#include <QVariant>
12
13#include <solid/solid_export.h>
14
15#include <solid/deviceinterface.h>
16
17namespace Solid
18{
19class Device;
20
21/**
22 * @class Solid::Predicate predicate.h <Solid/Predicate>
23 *
24 * This class implements predicates for devices.
25 *
26 * A predicate is a logical condition that a given device can match or not.
27 * It's a constraint about the value a property must have in a given device
28 * interface, or any combination (conjunction, disjunction) of such
29 * constraints.
30 *
31 * A predicate can be:
32 * - a single comparison, or
33 * - a conjunction ("AND") of exactly two predicates, or
34 * - a disjunction ("OR") of exactly two predicates.
35 *
36 * Since these can be nested, it is possible to express "a StorageVolume
37 * that is not ignored AND that StorageVolume contains a FileSystem
38 * AND that StorageVolume is removable". Since conjunctions use exactly
39 * two predicates (and this expression has three), square brackets are
40 * used to group the nested predicates when writing them out in full:
41 *
42 * ```
43 * [ [ StorageVolume.ignored == false AND StorageVolume.usage == 'FileSystem' ]
44 * AND StorageVolume.removable == true ]
45 * ```
46 *
47 * Predicates can be constructed programmatically by creating single
48 * comparisons with the Predicate constructor, and then building
49 * conjunctions with `operator&` and disjunctions with `operator|`.
50 *
51 * Predicates can be constructed from a string by calling `fromString()`
52 * which parses the given string and returns a predicate. If there
53 * are any errors in parsing the string, an empty predicate is returned;
54 * use `isValid()` to detect whether that is the case.
55 *
56 * The string language is described exactly in `predicate_parser.y`,
57 * but boils down to:
58 *
59 * - a single comparison is written as `<interface>.<property> == <value>`
60 * - a single bitmask check is written as `<interface>.<property> & <value>`
61 * - a conjunction is written as `[ <predicate> AND <predicate> ]`
62 * - a disjunction is written as `[ <predicate> OR <predicate> ]`
63 *
64 * Note the mandatory use of `[` and `]` around conjunctions and disjunctions.
65 */
66class SOLID_EXPORT Predicate
67{
68public:
69 /**
70 * The comparison operator which can be used for matching within the predicate.
71 *
72 * - Equals, the property and the value will match for strict equality
73 * - Mask, the property and the value will match if the bitmasking is not null
74 */
75 enum ComparisonOperator { Equals, Mask };
76
77 /**
78 * The predicate type which controls how the predicate is handled
79 *
80 * - PropertyCheck, the predicate contains a comparison that needs to be matched using a ComparisonOperator
81 * - Conjunction, the two contained predicates need to be true for this predicate to be true
82 * - Disjunction, either of the two contained predicates may be true for this predicate to be true
83 * - InterfaceCheck, the device type is compared
84 */
85 enum Type { PropertyCheck, Conjunction, Disjunction, InterfaceCheck };
86
87 /**
88 * Constructs an invalid predicate.
89 */
90 Predicate();
91
92 /**
93 * Copy constructor.
94 *
95 * @param other the predicate to copy
96 */
97 Predicate(const Predicate &other);
98
99 /**
100 * Constructs a predicate matching the value of a property in
101 * a given device interface.
102 *
103 * @param ifaceType the device interface type the device must have
104 * @param property the property name of the device interface
105 * @param value the value the property must have to make the device match
106 * @param compOperator the operator to apply between the property and the value when matching
107 */
108 Predicate(const DeviceInterface::Type &ifaceType, const QString &property, const QVariant &value, ComparisonOperator compOperator = Equals);
109
110 /**
111 * Constructs a predicate matching the value of a property in
112 * a given device interface.
113 *
114 * @param ifaceName the name of the device interface the device must have
115 * @param property the property name of the device interface
116 * @param value the value the property must have to make the device match
117 * @param compOperator the operator to apply between the property and the value when matching
118 */
119 Predicate(const QString &ifaceName, const QString &property, const QVariant &value, ComparisonOperator compOperator = Equals);
120
121 /**
122 * Constructs a predicate matching devices being of a particular device interface
123 *
124 * @param ifaceType the device interface the device must have
125 */
126 explicit Predicate(const DeviceInterface::Type &ifaceType);
127
128 /**
129 * Constructs a predicate matching devices being of a particular device interface
130 *
131 * @param ifaceName the name of the device interface the device must have
132 */
133 explicit Predicate(const QString &ifaceName);
134
135 /**
136 * Destroys a Predicate object.
137 */
138 ~Predicate();
139
140 /**
141 * Assignment operator.
142 *
143 * @param other the predicate to assign
144 * @return this predicate after having assigned 'other' to it
145 */
146 Predicate &operator=(const Predicate &other);
147
148 /**
149 * 'And' operator.
150 *
151 * @param other the second operand
152 * @return a new 'and' predicate having 'this' and 'other' as operands
153 */
154 Predicate operator&(const Predicate &other);
155
156 /**
157 * 'AndEquals' operator.
158 *
159 * @param other the second operand
160 * @return assigns to 'this' a new 'and' predicate having 'this' and 'other' as operands
161 */
162 Predicate &operator&=(const Predicate &other);
163
164 /**
165 * 'Or' operator.
166 *
167 * @param other the second operand
168 * @return a new 'or' predicate having 'this' and 'other' as operands
169 */
170 Predicate operator|(const Predicate &other);
171
172 /**
173 * 'OrEquals' operator.
174 *
175 * @param other the second operand
176 * @return assigns to 'this' a new 'or' predicate having 'this' and 'other' as operands
177 */
178 Predicate &operator|=(const Predicate &other);
179
180 /**
181 * Indicates if the predicate is valid.
182 *
183 * Predicate() is the only invalid predicate.
184 *
185 * @return true if the predicate is valid, false otherwise
186 */
187 bool isValid() const;
188
189 /**
190 * Checks if a device matches the predicate.
191 *
192 * @param device the device to match against the predicate
193 * @return true if the given device matches the predicate, false otherwise
194 */
195 bool matches(const Device &device) const;
196
197 /**
198 * Retrieves the device interface types used in this predicate.
199 *
200 * @return all the device interface types used in this predicate
201 */
202 QSet<DeviceInterface::Type> usedTypes() const;
203
204 /**
205 * Converts the predicate to its string form.
206 *
207 * @return a string representation of the predicate
208 */
209 QString toString() const;
210
211 /**
212 * Converts a string to a predicate.
213 *
214 * @param predicate the string to convert
215 * @return a new valid predicate if the given string is syntactically
216 * correct, Predicate() otherwise
217 */
218 static Predicate fromString(const QString &predicate);
219
220 /**
221 * Retrieves the predicate type, used to determine how to handle the predicate
222 *
223 * @since 4.4
224 * @return the predicate type
225 */
226 Type type() const;
227
228 /**
229 * Retrieves the interface type.
230 *
231 * @note This is only valid for InterfaceCheck and PropertyCheck types
232 * @since 4.4
233 * @return a device interface type used by the predicate
234 */
235 DeviceInterface::Type interfaceType() const;
236
237 /**
238 * Retrieves the property name used when retrieving the value to compare against
239 *
240 * @note This is only valid for the PropertyCheck type
241 * @since 4.4
242 * @return a property name
243 */
244 QString propertyName() const;
245
246 /**
247 * Retrieves the value used when comparing a devices property to see if it matches the predicate
248 *
249 * @note This is only valid for the PropertyCheck type
250 * @since 4.4
251 * @return the value used
252 */
253 QVariant matchingValue() const;
254
255 /**
256 * Retrieves the comparison operator used to compare a property's value
257 *
258 * @since 4.4
259 * @note This is only valid for Conjunction and Disjunction types
260 * @return the comparison operator used
261 */
262 ComparisonOperator comparisonOperator() const;
263
264 /**
265 * A smaller, inner predicate which is the first to appear and is compared with the second one
266 *
267 * @since 4.4
268 * @note This is only valid for Conjunction and Disjunction types
269 * @return The predicate used for the first operand
270 */
271 Predicate firstOperand() const;
272
273 /**
274 * A smaller, inner predicate which is the second to appear and is compared with the first one
275 *
276 * @since 4.4
277 * @note This is only valid for Conjunction and Disjunction types
278 * @return The predicate used for the second operand
279 */
280 Predicate secondOperand() const;
281
282private:
283 class Private;
284 Private *const d;
285};
286}
287
288#endif
Type
This enum type defines the type of device interface that a Device can have.
This class allows applications to deal with devices available in the underlying system.
This class implements predicates for devices.
ComparisonOperator
The comparison operator which can be used for matching within the predicate.
Definition predicate.h:75
Type
The predicate type which controls how the predicate is handled.
Definition predicate.h:85
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.