libhext: C++ Library Documentation  1.0.12-3ea013c
NegateMatch.h
Go to the documentation of this file.
1 // Copyright 2015, 2016 Thomas Trapp
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEXT_NEGATE_MATCH_H_INCLUDED
16 #define HEXT_NEGATE_MATCH_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::NegateMatch
20 
21 #include "hext/Cloneable.h"
22 #include "hext/Match.h"
23 #include "hext/Visibility.h"
24 
25 #include <memory>
26 #include <vector>
27 
28 #include <gumbo.h>
29 
30 
31 namespace hext {
32 
33 
34 /// Matches HTML nodes for which every given Match returns false.
35 ///
36 /// @par Example:
37 /// ~~~~~~~~~~~~~
38 /// GumboNode * attr_none = ...; // <hr/>
39 /// GumboNode * attr_one = ...; // <h1 class="main-heading"></h1>
40 /// GumboNode * attr_two = ...; // <div id="cart" class="my-cart"></div>
41 ///
42 /// NegateMatch not_one (std::make_unique<AttributeCountMatch>(1));
43 /// NegateMatch not_none(std::make_unique<AttributeCountMatch>(0));
44 ///
45 /// assert(!not_none.matches(attr_none));
46 /// assert( not_none.matches(attr_one));
47 /// assert( not_none.matches(attr_two));
48 ///
49 /// assert( not_one.matches(attr_none));
50 /// assert(!not_one.matches(attr_one));
51 /// assert( not_one.matches(attr_two));
52 /// ~~~~~~~~~~~~~
53 class HEXT_PUBLIC NegateMatch final : public Cloneable<NegateMatch, Match>
54 {
55 public:
56  /// Constructs a NegateMatch that matches nodes for which every contained
57  /// Match returns false.
58  ///
59  /// @param v_matches: An optional vector containing Matches.
60  explicit
61  NegateMatch(std::vector<std::unique_ptr<Match>> v_matches = {}) noexcept;
62 
63  /// Constructs a NegateMatch and appends a Match.
64  ///
65  /// @param match: The Match to append.
66  explicit NegateMatch(std::unique_ptr<Match> match);
67 
68  ~NegateMatch() noexcept override = default;
69  NegateMatch(NegateMatch&& other) noexcept = default;
70  NegateMatch(const NegateMatch& other);
71  NegateMatch& operator=(NegateMatch&& other) noexcept = default;
72  NegateMatch& operator=(const NegateMatch& other);
73 
74  /// Appends a Match.
75  ///
76  /// @param match: The Match to append.
77  void append_match(std::unique_ptr<Match> match);
78 
79  /// Returns true if every contained Match returns false for node.
80  ///
81  /// @param node: A pointer to a GumboNode.
82  bool matches(const GumboNode * node) const override;
83 
84 private:
85  /// A vector containing Matches whose result will be negated.
86  std::vector<std::unique_ptr<Match>> matches_;
87 };
88 
89 
90 } // namespace hext
91 
92 
93 #endif // HEXT_NEGATE_MATCH_H_INCLUDED
94 
Defines template hext::Cloneable.
Declares hext::Match.
Defines HEXT_PUBLIC and HEXT_PRIVATE.
#define HEXT_PUBLIC
Definition: Visibility.h:26
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:37
Abstract base for every Match.
Definition: Match.h:42
Matches HTML nodes for which every given Match returns false.
Definition: NegateMatch.h:54
~NegateMatch() noexcept override=default
NegateMatch(std::unique_ptr< Match > match)
Constructs a NegateMatch and appends a Match.
NegateMatch(std::vector< std::unique_ptr< Match >> v_matches={}) noexcept
Constructs a NegateMatch that matches nodes for which every contained Match returns false.