Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
lazyevaluation.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_LAZYEVALUATION_HPP
19 #define NURIA_LAZYEVALUATION_HPP
20 
21 #include "essentials.hpp"
22 #include "callback.hpp"
23 #include "variant.hpp"
24 #include <QSharedData>
25 #include <QMetaType>
26 #include <QVariant>
27 #include <QDebug>
28 
29 namespace Nuria {
30 
31 class AbstractConditionEvaluator;
32 class LazyConditionPrivate;
33 class Field;
34 
63 class NURIA_CORE_EXPORT LazyCondition {
64 public:
65 
69  enum Type {
70  Empty = 0,
76  Less,
79  LogicOr
80  };
81 
83  LazyCondition ();
84 
86  ~LazyCondition ();
87 
89  LazyCondition (const Field &field);
90 
92  LazyCondition (const QVariant &single);
93 
95  LazyCondition (const LazyCondition &other);
96 
98  LazyCondition (const QVariant &left, Type type, const QVariant &right);
99 
101  LazyCondition &operator= (const LazyCondition &other);
102 
106  bool isValid () const;
107 
112  Type type () const;
113 
115  const QVariant &left () const;
116 
118  const QVariant &right () const;
119 
121  operator QVariant () const;
122 
128  LazyCondition operator&& (const LazyCondition &other);
129 
135  LazyCondition operator|| (const LazyCondition &other);
136 
140  template< typename ... Args >
141  inline bool operator() (const Args & ... args)
142  { return evaluate (Variant::buildList (args ...)); }
143 
149  bool evaluate (const QVariantList &arguments) const;
150 
161  void compile (AbstractConditionEvaluator *evaluator = nullptr);
162 
163 private:
164  QSharedDataPointer< LazyConditionPrivate > d;
165 };
166 
176 class NURIA_CORE_EXPORT Field {
177 public:
178 
182  enum Type {
184  Empty = 0,
185 
188 
194 
199 
208  Custom = 50
209  };
210 
212  Field ();
213 
215  Field (int type, const QVariant &data);
216 
218  Field (const Field &other);
219 
225  //
226  LazyCondition operator== (const Field &other);
227  LazyCondition operator!= (const Field &other);
228  LazyCondition operator< (const Field &other);
229  LazyCondition operator<= (const Field &other);
230  LazyCondition operator> (const Field &other);
231  LazyCondition operator>= (const Field &other);
232 
233  //
234  template< typename T >
235  LazyCondition operator== (const T &other)
236  { return LazyCondition (toVariant (), LazyCondition::Equal, QVariant::fromValue (other)); }
237 
238  template< typename T >
239  LazyCondition operator!= (const T &other)
240  { return LazyCondition (toVariant (), LazyCondition::NonEqual, QVariant::fromValue (other)); }
241 
242  template< typename T >
243  LazyCondition operator< (const T &other)
244  { return LazyCondition (toVariant (), LazyCondition::Less, QVariant::fromValue (other)); }
245 
246  template< typename T >
247  LazyCondition operator<= (const T &other)
248  { return LazyCondition (toVariant (), LazyCondition::LessEqual, QVariant::fromValue (other)); }
249 
250  template< typename T >
251  LazyCondition operator> (const T &other)
252  { return LazyCondition (toVariant (), LazyCondition::Greater, QVariant::fromValue (other)); }
253 
254  template< typename T >
255  LazyCondition operator>= (const T &other)
256  { return LazyCondition (toVariant (), LazyCondition::GreaterEqual, QVariant::fromValue (other)); }
257 
263  const QVariant &value () const;
264 
270  Type type () const;
271 
276  int customType () const;
277 
283  QVariant toVariant () const;
284 
285 private:
286  Type m_type;
287  QVariant m_value;
288 };
289 
309 class NURIA_CORE_EXPORT TestCall {
310 public:
311 
313  TestCall ();
314 
316  TestCall (const QString &name, const QVariantList &args);
317 
319  TestCall (const Nuria::Callback &callback, const QVariantList &args);
320 
325  bool isNative () const;
326 
331  QString name () const;
332 
337  Nuria::Callback callback () const;
338 
343  const QVariantList &arguments () const;
344 
345 private:
346  QVariant m_method;
347  QVariantList m_args;
348 };
349 
355 template< typename T > inline Field val (const T &value)
356 { return Field (Field::Value, QVariant::fromValue (value)); }
357 
362 NURIA_CORE_EXPORT Field arg (int index);
363 
369 template< typename ... Args >
370 Field test (const QString &method, const Args &... args) {
371  TestCall call (method, Variant::buildList (args ...));
372  return Field (Field::TestCall, QVariant::fromValue (call));
373 }
374 
380 template< typename ... Args >
381 Field test (const Nuria::Callback &method, const Args &... args) {
382  TestCall call (method, Variant::buildList (args ...));
383  return Field (Field::TestCall, QVariant::fromValue (call));
384 }
385 
386 }
387 
392 NURIA_CORE_EXPORT QDebug operator<< (QDebug dbg, const Nuria::LazyCondition &condition);
393 NURIA_CORE_EXPORT QDebug operator<< (QDebug dbg, const Nuria::TestCall &call);
394 NURIA_CORE_EXPORT QDebug operator<< (QDebug dbg, const Nuria::Field &field);
397 //
398 Q_DECLARE_METATYPE(Nuria::LazyCondition)
399 Q_DECLARE_METATYPE(Nuria::TestCall)
400 Q_DECLARE_METATYPE(Nuria::Field)
401 
402 #endif // NURIA_LAZYEVALUATION_HPP
Evaluates to (left > right).
Definition: lazyevaluation.hpp:75
Evaluates to (left <= right).
Definition: lazyevaluation.hpp:78
Field encapsulates a value-field for LazyCondition.
Definition: lazyevaluation.hpp:176
A modern style callback mechanism which can be bound to various method types including slots...
Definition: callback.hpp:141
static QVariantList buildList(const Items &...items)
Definition: variant.hpp:547
Definition: lazyevaluation.hpp:187
LazyCondition offers lazily evaluated conditions for C++.
Definition: lazyevaluation.hpp:63
Evaluates to (left >= right).
Definition: lazyevaluation.hpp:76
The TestCall class encapsulates a call to a test function for LazyCondition.
Definition: lazyevaluation.hpp:309
Evaluates to (left == right).
Definition: lazyevaluation.hpp:73
Evaluates to (left < right).
Definition: lazyevaluation.hpp:77
Invalid instance, evaluates to false.
Definition: lazyevaluation.hpp:71
Evaluates to (left != right).
Definition: lazyevaluation.hpp:74
Type
Definition: lazyevaluation.hpp:182
Evaluates to (left).
Definition: lazyevaluation.hpp:72
The AbstractConditionEvaluator class.
Definition: conditionevaluator.hpp:31
Definition: lazyevaluation.hpp:198
Type
Definition: lazyevaluation.hpp:69
Definition: lazyevaluation.hpp:193