libhext: C++ Library Documentation 1.0.13-b24695d
Loading...
Searching...
No Matches
FunctionValueMatch.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_FUNCTION_VALUE_MATCH_H_INCLUDED
16#define HEXT_FUNCTION_VALUE_MATCH_H_INCLUDED
17
18/// @file
19/// Declares hext::FunctionValueMatch
20
22#include "hext/Cloneable.h"
23#include "hext/Match.h"
24#include "hext/ValueTest.h"
25#include "hext/Visibility.h"
26
27#include <memory>
28
29#include <gumbo.h>
30
31
32namespace hext {
33
34
35/// Matches if the result of applying a given CaptureFunction to an HTML node
36/// passes a ValueTest.
37///
38/// @par Example:
39/// ~~~~~~~~~~~~~
40/// GumboNode * foo = ...; // <div>This is a foo!</div>
41/// GumboNode * bar = ...; // <div>This is a bar!</div>
42///
43/// FunctionValueMatch m_foo(
44/// InnerHtmlBuiltin, // CaptureFunction
45/// std::unique_ptr<ContainsTest>("foo") // ValueTest
46/// );
47/// FunctionValueMatch m_bar(
48/// InnerHtmlBuiltin, // CaptureFunction
49/// std::unique_ptr<ContainsTest>("bar") // ValueTest
50/// );
51///
52/// assert(m_foo.matches(foo));
53/// assert(m_bar.matches(bar));
54///
55/// assert(!m_foo.matches(bar));
56/// assert(!m_bar.matches(foo));
57/// ~~~~~~~~~~~~~
59 : public Cloneable<FunctionValueMatch, Match>
60{
61public:
62 /// Constructs a FunctionValueMatch that matches HTML nodes for which a given
63 /// CaptureFunction returns a result that passes a ValueTest.
64 ///
65 /// @param func: The CaptureFunction that will be applied to an HTML
66 /// node.
67 /// @param value_test: The ValueTest that the result of the given
68 /// CaptureFunction must pass.
70 std::unique_ptr<ValueTest> value_test);
71
72 ~FunctionValueMatch() noexcept override = default;
75 FunctionValueMatch& operator=(FunctionValueMatch&& other) = default;
76 FunctionValueMatch& operator=(const FunctionValueMatch& other);
77
78 /// Returns true if the result of calling the given CaptureFunction with node
79 /// as its first argument passes the given ValueTest.
80 ///
81 /// @param node: A pointer to a GumboNode.
82 bool matches(const GumboNode * node) const override;
83
84private:
85 /// The CaptureFunction that will be applied to an HTML node.
86 CaptureFunction func_;
87
88 /// The ValueTest that the result of the given CaptureFunction must pass.
89 std::unique_ptr<ValueTest> test_;
90};
91
92
93} // namespace hext
94
95
96#endif // HEXT_FUNCTION_VALUE_MATCH_H_INCLUDED
97
Declares hext::CaptureFunction.
Defines template hext::Cloneable.
Declares hext::Match.
Declares hext::ValueTest.
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
Matches if the result of applying a given CaptureFunction to an HTML node passes a ValueTest.
FunctionValueMatch(CaptureFunction func, std::unique_ptr< ValueTest > value_test)
Constructs a FunctionValueMatch that matches HTML nodes for which a given CaptureFunction returns a r...
~FunctionValueMatch() noexcept override=default
Abstract base for every ValueTest.
Definition ValueTest.h:38
std::function< std::string(const GumboNode *)> CaptureFunction
A type of std::function that receives an HTML element and returns a string.