kodi
InfoExpression.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "InfoBool.h"
12 
13 #include <list>
14 #include <stack>
15 #include <utility>
16 #include <vector>
17 
18 class CGUIListItem;
19 
20 namespace INFO
21 {
24 class InfoSingle : public InfoBool
25 {
26 public:
27  InfoSingle(const std::string& expression, int context, unsigned int& refreshCounter)
28  : InfoBool(expression, context, refreshCounter)
29  {
30  }
31  void Initialize(CGUIInfoManager* infoMgr) override;
32 
33  void Update(int contextWindow, const CGUIListItem* item) override;
34 
35 private:
36  int m_condition;
37 };
38 
41 class InfoExpression : public InfoBool
42 {
43 public:
44  InfoExpression(const std::string& expression, int context, unsigned int& refreshCounter)
45  : InfoBool(expression, context, refreshCounter)
46  {
47  }
48  ~InfoExpression() override = default;
49 
50  void Initialize(CGUIInfoManager* infoMgr) override;
51 
52  void Update(int contextWindow, const CGUIListItem* item) override;
53 
54 private:
55  typedef enum
56  {
57  OPERATOR_NONE = 0,
58  OPERATOR_LB, // 1
59  OPERATOR_RB, // 2
60  OPERATOR_OR, // 3
61  OPERATOR_AND, // 4
62  OPERATOR_NOT, // 5
63  } operator_t;
64 
65  typedef enum
66  {
67  NODE_LEAF,
68  NODE_AND,
69  NODE_OR,
70  } node_type_t;
71 
72  // An abstract base class for nodes in the expression tree
73  class InfoSubexpression
74  {
75  public:
76  virtual ~InfoSubexpression(void) = default; // so we can destruct derived classes using a pointer to their base class
77  virtual bool Evaluate(int contextWindow, const CGUIListItem* item) = 0;
78  virtual node_type_t Type() const=0;
79  };
80 
81  typedef std::shared_ptr<InfoSubexpression> InfoSubexpressionPtr;
82 
83  // A leaf node in the expression tree
84  class InfoLeaf : public InfoSubexpression
85  {
86  public:
87  InfoLeaf(InfoPtr info, bool invert) : m_info(std::move(info)), m_invert(invert) {}
88  bool Evaluate(int contextWindow, const CGUIListItem* item) override;
89  node_type_t Type() const override { return NODE_LEAF; }
90 
91  private:
92  InfoPtr m_info;
93  bool m_invert;
94  };
95 
96  // A branch node in the expression tree
97  class InfoAssociativeGroup : public InfoSubexpression
98  {
99  public:
100  InfoAssociativeGroup(node_type_t type, const InfoSubexpressionPtr &left, const InfoSubexpressionPtr &right);
101  void AddChild(const InfoSubexpressionPtr &child);
102  void Merge(const std::shared_ptr<InfoAssociativeGroup>& other);
103  bool Evaluate(int contextWindow, const CGUIListItem* item) override;
104  node_type_t Type() const override { return m_type; }
105 
106  private:
107  node_type_t m_type;
108  std::list<InfoSubexpressionPtr> m_children;
109  };
110 
111  static operator_t GetOperator(char ch);
112  static void OperatorPop(std::stack<operator_t> &operator_stack, bool &invert, std::stack<InfoSubexpressionPtr> &nodes);
113  bool Parse(const std::string &expression);
114  InfoSubexpressionPtr m_expression_tree;
115 };
116 
117 };
Definition: GUIInfoManager.h:56
Definition: GUIListItem.h:29
Class to wrap active boolean conditions.
Definition: InfoExpression.h:24
Definition: ContextMenuItem.h:23
void Update(int contextWindow, const CGUIListItem *item) override
Update the value of this info bool This is called if and only if the info bool is dirty...
Definition: InfoExpression.cpp:26
Base class, wrapping boolean conditions and expressions.
Definition: InfoBool.h:23
Class to wrap active boolean expressions.
Definition: InfoExpression.h:41