xref: /freebsd/contrib/googletest/googletest/include/gtest/gtest-matchers.h (revision 28f6c2f292806bf31230a959bc4b19d7081669a7)
1*28f6c2f2SEnji Cooper // Copyright 2007, Google Inc.
2*28f6c2f2SEnji Cooper // All rights reserved.
3*28f6c2f2SEnji Cooper //
4*28f6c2f2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5*28f6c2f2SEnji Cooper // modification, are permitted provided that the following conditions are
6*28f6c2f2SEnji Cooper // met:
7*28f6c2f2SEnji Cooper //
8*28f6c2f2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9*28f6c2f2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10*28f6c2f2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11*28f6c2f2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12*28f6c2f2SEnji Cooper // in the documentation and/or other materials provided with the
13*28f6c2f2SEnji Cooper // distribution.
14*28f6c2f2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15*28f6c2f2SEnji Cooper // contributors may be used to endorse or promote products derived from
16*28f6c2f2SEnji Cooper // this software without specific prior written permission.
17*28f6c2f2SEnji Cooper //
18*28f6c2f2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*28f6c2f2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*28f6c2f2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*28f6c2f2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*28f6c2f2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*28f6c2f2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*28f6c2f2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*28f6c2f2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*28f6c2f2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*28f6c2f2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*28f6c2f2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*28f6c2f2SEnji Cooper 
30*28f6c2f2SEnji Cooper // The Google C++ Testing and Mocking Framework (Google Test)
31*28f6c2f2SEnji Cooper //
32*28f6c2f2SEnji Cooper // This file implements just enough of the matcher interface to allow
33*28f6c2f2SEnji Cooper // EXPECT_DEATH and friends to accept a matcher argument.
34*28f6c2f2SEnji Cooper 
35*28f6c2f2SEnji Cooper // IWYU pragma: private, include "gtest/gtest.h"
36*28f6c2f2SEnji Cooper // IWYU pragma: friend gtest/.*
37*28f6c2f2SEnji Cooper // IWYU pragma: friend gmock/.*
38*28f6c2f2SEnji Cooper 
39*28f6c2f2SEnji Cooper #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
40*28f6c2f2SEnji Cooper #define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
41*28f6c2f2SEnji Cooper 
42*28f6c2f2SEnji Cooper #include <atomic>
43*28f6c2f2SEnji Cooper #include <functional>
44*28f6c2f2SEnji Cooper #include <memory>
45*28f6c2f2SEnji Cooper #include <ostream>
46*28f6c2f2SEnji Cooper #include <string>
47*28f6c2f2SEnji Cooper #include <type_traits>
48*28f6c2f2SEnji Cooper 
49*28f6c2f2SEnji Cooper #include "gtest/gtest-printers.h"
50*28f6c2f2SEnji Cooper #include "gtest/internal/gtest-internal.h"
51*28f6c2f2SEnji Cooper #include "gtest/internal/gtest-port.h"
52*28f6c2f2SEnji Cooper 
53*28f6c2f2SEnji Cooper // MSVC warning C5046 is new as of VS2017 version 15.8.
54*28f6c2f2SEnji Cooper #if defined(_MSC_VER) && _MSC_VER >= 1915
55*28f6c2f2SEnji Cooper #define GTEST_MAYBE_5046_ 5046
56*28f6c2f2SEnji Cooper #else
57*28f6c2f2SEnji Cooper #define GTEST_MAYBE_5046_
58*28f6c2f2SEnji Cooper #endif
59*28f6c2f2SEnji Cooper 
60*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(
61*28f6c2f2SEnji Cooper     4251 GTEST_MAYBE_5046_ /* class A needs to have dll-interface to be used by
62*28f6c2f2SEnji Cooper                               clients of class B */
63*28f6c2f2SEnji Cooper     /* Symbol involving type with internal linkage not defined */)
64*28f6c2f2SEnji Cooper 
65*28f6c2f2SEnji Cooper namespace testing {
66*28f6c2f2SEnji Cooper 
67*28f6c2f2SEnji Cooper // To implement a matcher Foo for type T, define:
68*28f6c2f2SEnji Cooper //   1. a class FooMatcherMatcher that implements the matcher interface:
69*28f6c2f2SEnji Cooper //     using is_gtest_matcher = void;
70*28f6c2f2SEnji Cooper //     bool MatchAndExplain(const T&, std::ostream*);
71*28f6c2f2SEnji Cooper //       (MatchResultListener* can also be used instead of std::ostream*)
72*28f6c2f2SEnji Cooper //     void DescribeTo(std::ostream*);
73*28f6c2f2SEnji Cooper //     void DescribeNegationTo(std::ostream*);
74*28f6c2f2SEnji Cooper //
75*28f6c2f2SEnji Cooper //   2. a factory function that creates a Matcher<T> object from a
76*28f6c2f2SEnji Cooper //      FooMatcherMatcher.
77*28f6c2f2SEnji Cooper 
78*28f6c2f2SEnji Cooper class MatchResultListener {
79*28f6c2f2SEnji Cooper  public:
80*28f6c2f2SEnji Cooper   // Creates a listener object with the given underlying ostream.  The
81*28f6c2f2SEnji Cooper   // listener does not own the ostream, and does not dereference it
82*28f6c2f2SEnji Cooper   // in the constructor or destructor.
MatchResultListener(::std::ostream * os)83*28f6c2f2SEnji Cooper   explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
84*28f6c2f2SEnji Cooper   virtual ~MatchResultListener() = 0;  // Makes this class abstract.
85*28f6c2f2SEnji Cooper 
86*28f6c2f2SEnji Cooper   // Streams x to the underlying ostream; does nothing if the ostream
87*28f6c2f2SEnji Cooper   // is NULL.
88*28f6c2f2SEnji Cooper   template <typename T>
89*28f6c2f2SEnji Cooper   MatchResultListener& operator<<(const T& x) {
90*28f6c2f2SEnji Cooper     if (stream_ != nullptr) *stream_ << x;
91*28f6c2f2SEnji Cooper     return *this;
92*28f6c2f2SEnji Cooper   }
93*28f6c2f2SEnji Cooper 
94*28f6c2f2SEnji Cooper   // Returns the underlying ostream.
stream()95*28f6c2f2SEnji Cooper   ::std::ostream* stream() { return stream_; }
96*28f6c2f2SEnji Cooper 
97*28f6c2f2SEnji Cooper   // Returns true if and only if the listener is interested in an explanation
98*28f6c2f2SEnji Cooper   // of the match result.  A matcher's MatchAndExplain() method can use
99*28f6c2f2SEnji Cooper   // this information to avoid generating the explanation when no one
100*28f6c2f2SEnji Cooper   // intends to hear it.
IsInterested()101*28f6c2f2SEnji Cooper   bool IsInterested() const { return stream_ != nullptr; }
102*28f6c2f2SEnji Cooper 
103*28f6c2f2SEnji Cooper  private:
104*28f6c2f2SEnji Cooper   ::std::ostream* const stream_;
105*28f6c2f2SEnji Cooper 
106*28f6c2f2SEnji Cooper   MatchResultListener(const MatchResultListener&) = delete;
107*28f6c2f2SEnji Cooper   MatchResultListener& operator=(const MatchResultListener&) = delete;
108*28f6c2f2SEnji Cooper };
109*28f6c2f2SEnji Cooper 
110*28f6c2f2SEnji Cooper inline MatchResultListener::~MatchResultListener() = default;
111*28f6c2f2SEnji Cooper 
112*28f6c2f2SEnji Cooper // An instance of a subclass of this knows how to describe itself as a
113*28f6c2f2SEnji Cooper // matcher.
114*28f6c2f2SEnji Cooper class GTEST_API_ MatcherDescriberInterface {
115*28f6c2f2SEnji Cooper  public:
116*28f6c2f2SEnji Cooper   virtual ~MatcherDescriberInterface() = default;
117*28f6c2f2SEnji Cooper 
118*28f6c2f2SEnji Cooper   // Describes this matcher to an ostream.  The function should print
119*28f6c2f2SEnji Cooper   // a verb phrase that describes the property a value matching this
120*28f6c2f2SEnji Cooper   // matcher should have.  The subject of the verb phrase is the value
121*28f6c2f2SEnji Cooper   // being matched.  For example, the DescribeTo() method of the Gt(7)
122*28f6c2f2SEnji Cooper   // matcher prints "is greater than 7".
123*28f6c2f2SEnji Cooper   virtual void DescribeTo(::std::ostream* os) const = 0;
124*28f6c2f2SEnji Cooper 
125*28f6c2f2SEnji Cooper   // Describes the negation of this matcher to an ostream.  For
126*28f6c2f2SEnji Cooper   // example, if the description of this matcher is "is greater than
127*28f6c2f2SEnji Cooper   // 7", the negated description could be "is not greater than 7".
128*28f6c2f2SEnji Cooper   // You are not required to override this when implementing
129*28f6c2f2SEnji Cooper   // MatcherInterface, but it is highly advised so that your matcher
130*28f6c2f2SEnji Cooper   // can produce good error messages.
DescribeNegationTo(::std::ostream * os)131*28f6c2f2SEnji Cooper   virtual void DescribeNegationTo(::std::ostream* os) const {
132*28f6c2f2SEnji Cooper     *os << "not (";
133*28f6c2f2SEnji Cooper     DescribeTo(os);
134*28f6c2f2SEnji Cooper     *os << ")";
135*28f6c2f2SEnji Cooper   }
136*28f6c2f2SEnji Cooper };
137*28f6c2f2SEnji Cooper 
138*28f6c2f2SEnji Cooper // The implementation of a matcher.
139*28f6c2f2SEnji Cooper template <typename T>
140*28f6c2f2SEnji Cooper class MatcherInterface : public MatcherDescriberInterface {
141*28f6c2f2SEnji Cooper  public:
142*28f6c2f2SEnji Cooper   // Returns true if and only if the matcher matches x; also explains the
143*28f6c2f2SEnji Cooper   // match result to 'listener' if necessary (see the next paragraph), in
144*28f6c2f2SEnji Cooper   // the form of a non-restrictive relative clause ("which ...",
145*28f6c2f2SEnji Cooper   // "whose ...", etc) that describes x.  For example, the
146*28f6c2f2SEnji Cooper   // MatchAndExplain() method of the Pointee(...) matcher should
147*28f6c2f2SEnji Cooper   // generate an explanation like "which points to ...".
148*28f6c2f2SEnji Cooper   //
149*28f6c2f2SEnji Cooper   // Implementations of MatchAndExplain() should add an explanation of
150*28f6c2f2SEnji Cooper   // the match result *if and only if* they can provide additional
151*28f6c2f2SEnji Cooper   // information that's not already present (or not obvious) in the
152*28f6c2f2SEnji Cooper   // print-out of x and the matcher's description.  Whether the match
153*28f6c2f2SEnji Cooper   // succeeds is not a factor in deciding whether an explanation is
154*28f6c2f2SEnji Cooper   // needed, as sometimes the caller needs to print a failure message
155*28f6c2f2SEnji Cooper   // when the match succeeds (e.g. when the matcher is used inside
156*28f6c2f2SEnji Cooper   // Not()).
157*28f6c2f2SEnji Cooper   //
158*28f6c2f2SEnji Cooper   // For example, a "has at least 10 elements" matcher should explain
159*28f6c2f2SEnji Cooper   // what the actual element count is, regardless of the match result,
160*28f6c2f2SEnji Cooper   // as it is useful information to the reader; on the other hand, an
161*28f6c2f2SEnji Cooper   // "is empty" matcher probably only needs to explain what the actual
162*28f6c2f2SEnji Cooper   // size is when the match fails, as it's redundant to say that the
163*28f6c2f2SEnji Cooper   // size is 0 when the value is already known to be empty.
164*28f6c2f2SEnji Cooper   //
165*28f6c2f2SEnji Cooper   // You should override this method when defining a new matcher.
166*28f6c2f2SEnji Cooper   //
167*28f6c2f2SEnji Cooper   // It's the responsibility of the caller (Google Test) to guarantee
168*28f6c2f2SEnji Cooper   // that 'listener' is not NULL.  This helps to simplify a matcher's
169*28f6c2f2SEnji Cooper   // implementation when it doesn't care about the performance, as it
170*28f6c2f2SEnji Cooper   // can talk to 'listener' without checking its validity first.
171*28f6c2f2SEnji Cooper   // However, in order to implement dummy listeners efficiently,
172*28f6c2f2SEnji Cooper   // listener->stream() may be NULL.
173*28f6c2f2SEnji Cooper   virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;
174*28f6c2f2SEnji Cooper 
175*28f6c2f2SEnji Cooper   // Inherits these methods from MatcherDescriberInterface:
176*28f6c2f2SEnji Cooper   //   virtual void DescribeTo(::std::ostream* os) const = 0;
177*28f6c2f2SEnji Cooper   //   virtual void DescribeNegationTo(::std::ostream* os) const;
178*28f6c2f2SEnji Cooper };
179*28f6c2f2SEnji Cooper 
180*28f6c2f2SEnji Cooper namespace internal {
181*28f6c2f2SEnji Cooper 
182*28f6c2f2SEnji Cooper // A match result listener that ignores the explanation.
183*28f6c2f2SEnji Cooper class DummyMatchResultListener : public MatchResultListener {
184*28f6c2f2SEnji Cooper  public:
DummyMatchResultListener()185*28f6c2f2SEnji Cooper   DummyMatchResultListener() : MatchResultListener(nullptr) {}
186*28f6c2f2SEnji Cooper 
187*28f6c2f2SEnji Cooper  private:
188*28f6c2f2SEnji Cooper   DummyMatchResultListener(const DummyMatchResultListener&) = delete;
189*28f6c2f2SEnji Cooper   DummyMatchResultListener& operator=(const DummyMatchResultListener&) = delete;
190*28f6c2f2SEnji Cooper };
191*28f6c2f2SEnji Cooper 
192*28f6c2f2SEnji Cooper // A match result listener that forwards the explanation to a given
193*28f6c2f2SEnji Cooper // ostream.  The difference between this and MatchResultListener is
194*28f6c2f2SEnji Cooper // that the former is concrete.
195*28f6c2f2SEnji Cooper class StreamMatchResultListener : public MatchResultListener {
196*28f6c2f2SEnji Cooper  public:
StreamMatchResultListener(::std::ostream * os)197*28f6c2f2SEnji Cooper   explicit StreamMatchResultListener(::std::ostream* os)
198*28f6c2f2SEnji Cooper       : MatchResultListener(os) {}
199*28f6c2f2SEnji Cooper 
200*28f6c2f2SEnji Cooper  private:
201*28f6c2f2SEnji Cooper   StreamMatchResultListener(const StreamMatchResultListener&) = delete;
202*28f6c2f2SEnji Cooper   StreamMatchResultListener& operator=(const StreamMatchResultListener&) =
203*28f6c2f2SEnji Cooper       delete;
204*28f6c2f2SEnji Cooper };
205*28f6c2f2SEnji Cooper 
206*28f6c2f2SEnji Cooper struct SharedPayloadBase {
207*28f6c2f2SEnji Cooper   std::atomic<int> ref{1};
RefSharedPayloadBase208*28f6c2f2SEnji Cooper   void Ref() { ref.fetch_add(1, std::memory_order_relaxed); }
UnrefSharedPayloadBase209*28f6c2f2SEnji Cooper   bool Unref() { return ref.fetch_sub(1, std::memory_order_acq_rel) == 1; }
210*28f6c2f2SEnji Cooper };
211*28f6c2f2SEnji Cooper 
212*28f6c2f2SEnji Cooper template <typename T>
213*28f6c2f2SEnji Cooper struct SharedPayload : SharedPayloadBase {
SharedPayloadSharedPayload214*28f6c2f2SEnji Cooper   explicit SharedPayload(const T& v) : value(v) {}
SharedPayloadSharedPayload215*28f6c2f2SEnji Cooper   explicit SharedPayload(T&& v) : value(std::move(v)) {}
216*28f6c2f2SEnji Cooper 
DestroySharedPayload217*28f6c2f2SEnji Cooper   static void Destroy(SharedPayloadBase* shared) {
218*28f6c2f2SEnji Cooper     delete static_cast<SharedPayload*>(shared);
219*28f6c2f2SEnji Cooper   }
220*28f6c2f2SEnji Cooper 
221*28f6c2f2SEnji Cooper   T value;
222*28f6c2f2SEnji Cooper };
223*28f6c2f2SEnji Cooper 
224*28f6c2f2SEnji Cooper // An internal class for implementing Matcher<T>, which will derive
225*28f6c2f2SEnji Cooper // from it.  We put functionalities common to all Matcher<T>
226*28f6c2f2SEnji Cooper // specializations here to avoid code duplication.
227*28f6c2f2SEnji Cooper template <typename T>
228*28f6c2f2SEnji Cooper class MatcherBase : private MatcherDescriberInterface {
229*28f6c2f2SEnji Cooper  public:
230*28f6c2f2SEnji Cooper   // Returns true if and only if the matcher matches x; also explains the
231*28f6c2f2SEnji Cooper   // match result to 'listener'.
MatchAndExplain(const T & x,MatchResultListener * listener)232*28f6c2f2SEnji Cooper   bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
233*28f6c2f2SEnji Cooper     GTEST_CHECK_(vtable_ != nullptr);
234*28f6c2f2SEnji Cooper     return vtable_->match_and_explain(*this, x, listener);
235*28f6c2f2SEnji Cooper   }
236*28f6c2f2SEnji Cooper 
237*28f6c2f2SEnji Cooper   // Returns true if and only if this matcher matches x.
Matches(const T & x)238*28f6c2f2SEnji Cooper   bool Matches(const T& x) const {
239*28f6c2f2SEnji Cooper     DummyMatchResultListener dummy;
240*28f6c2f2SEnji Cooper     return MatchAndExplain(x, &dummy);
241*28f6c2f2SEnji Cooper   }
242*28f6c2f2SEnji Cooper 
243*28f6c2f2SEnji Cooper   // Describes this matcher to an ostream.
DescribeTo(::std::ostream * os)244*28f6c2f2SEnji Cooper   void DescribeTo(::std::ostream* os) const final {
245*28f6c2f2SEnji Cooper     GTEST_CHECK_(vtable_ != nullptr);
246*28f6c2f2SEnji Cooper     vtable_->describe(*this, os, false);
247*28f6c2f2SEnji Cooper   }
248*28f6c2f2SEnji Cooper 
249*28f6c2f2SEnji Cooper   // Describes the negation of this matcher to an ostream.
DescribeNegationTo(::std::ostream * os)250*28f6c2f2SEnji Cooper   void DescribeNegationTo(::std::ostream* os) const final {
251*28f6c2f2SEnji Cooper     GTEST_CHECK_(vtable_ != nullptr);
252*28f6c2f2SEnji Cooper     vtable_->describe(*this, os, true);
253*28f6c2f2SEnji Cooper   }
254*28f6c2f2SEnji Cooper 
255*28f6c2f2SEnji Cooper   // Explains why x matches, or doesn't match, the matcher.
ExplainMatchResultTo(const T & x,::std::ostream * os)256*28f6c2f2SEnji Cooper   void ExplainMatchResultTo(const T& x, ::std::ostream* os) const {
257*28f6c2f2SEnji Cooper     StreamMatchResultListener listener(os);
258*28f6c2f2SEnji Cooper     MatchAndExplain(x, &listener);
259*28f6c2f2SEnji Cooper   }
260*28f6c2f2SEnji Cooper 
261*28f6c2f2SEnji Cooper   // Returns the describer for this matcher object; retains ownership
262*28f6c2f2SEnji Cooper   // of the describer, which is only guaranteed to be alive when
263*28f6c2f2SEnji Cooper   // this matcher object is alive.
GetDescriber()264*28f6c2f2SEnji Cooper   const MatcherDescriberInterface* GetDescriber() const {
265*28f6c2f2SEnji Cooper     if (vtable_ == nullptr) return nullptr;
266*28f6c2f2SEnji Cooper     return vtable_->get_describer(*this);
267*28f6c2f2SEnji Cooper   }
268*28f6c2f2SEnji Cooper 
269*28f6c2f2SEnji Cooper  protected:
MatcherBase()270*28f6c2f2SEnji Cooper   MatcherBase() : vtable_(nullptr), buffer_() {}
271*28f6c2f2SEnji Cooper 
272*28f6c2f2SEnji Cooper   // Constructs a matcher from its implementation.
273*28f6c2f2SEnji Cooper   template <typename U>
MatcherBase(const MatcherInterface<U> * impl)274*28f6c2f2SEnji Cooper   explicit MatcherBase(const MatcherInterface<U>* impl)
275*28f6c2f2SEnji Cooper       : vtable_(nullptr), buffer_() {
276*28f6c2f2SEnji Cooper     Init(impl);
277*28f6c2f2SEnji Cooper   }
278*28f6c2f2SEnji Cooper 
279*28f6c2f2SEnji Cooper   template <typename M, typename = typename std::remove_reference<
280*28f6c2f2SEnji Cooper                             M>::type::is_gtest_matcher>
MatcherBase(M && m)281*28f6c2f2SEnji Cooper   MatcherBase(M&& m) : vtable_(nullptr), buffer_() {  // NOLINT
282*28f6c2f2SEnji Cooper     Init(std::forward<M>(m));
283*28f6c2f2SEnji Cooper   }
284*28f6c2f2SEnji Cooper 
MatcherBase(const MatcherBase & other)285*28f6c2f2SEnji Cooper   MatcherBase(const MatcherBase& other)
286*28f6c2f2SEnji Cooper       : vtable_(other.vtable_), buffer_(other.buffer_) {
287*28f6c2f2SEnji Cooper     if (IsShared()) buffer_.shared->Ref();
288*28f6c2f2SEnji Cooper   }
289*28f6c2f2SEnji Cooper 
290*28f6c2f2SEnji Cooper   MatcherBase& operator=(const MatcherBase& other) {
291*28f6c2f2SEnji Cooper     if (this == &other) return *this;
292*28f6c2f2SEnji Cooper     Destroy();
293*28f6c2f2SEnji Cooper     vtable_ = other.vtable_;
294*28f6c2f2SEnji Cooper     buffer_ = other.buffer_;
295*28f6c2f2SEnji Cooper     if (IsShared()) buffer_.shared->Ref();
296*28f6c2f2SEnji Cooper     return *this;
297*28f6c2f2SEnji Cooper   }
298*28f6c2f2SEnji Cooper 
MatcherBase(MatcherBase && other)299*28f6c2f2SEnji Cooper   MatcherBase(MatcherBase&& other)
300*28f6c2f2SEnji Cooper       : vtable_(other.vtable_), buffer_(other.buffer_) {
301*28f6c2f2SEnji Cooper     other.vtable_ = nullptr;
302*28f6c2f2SEnji Cooper   }
303*28f6c2f2SEnji Cooper 
304*28f6c2f2SEnji Cooper   MatcherBase& operator=(MatcherBase&& other) {
305*28f6c2f2SEnji Cooper     if (this == &other) return *this;
306*28f6c2f2SEnji Cooper     Destroy();
307*28f6c2f2SEnji Cooper     vtable_ = other.vtable_;
308*28f6c2f2SEnji Cooper     buffer_ = other.buffer_;
309*28f6c2f2SEnji Cooper     other.vtable_ = nullptr;
310*28f6c2f2SEnji Cooper     return *this;
311*28f6c2f2SEnji Cooper   }
312*28f6c2f2SEnji Cooper 
~MatcherBase()313*28f6c2f2SEnji Cooper   ~MatcherBase() override { Destroy(); }
314*28f6c2f2SEnji Cooper 
315*28f6c2f2SEnji Cooper  private:
316*28f6c2f2SEnji Cooper   struct VTable {
317*28f6c2f2SEnji Cooper     bool (*match_and_explain)(const MatcherBase&, const T&,
318*28f6c2f2SEnji Cooper                               MatchResultListener*);
319*28f6c2f2SEnji Cooper     void (*describe)(const MatcherBase&, std::ostream*, bool negation);
320*28f6c2f2SEnji Cooper     // Returns the captured object if it implements the interface, otherwise
321*28f6c2f2SEnji Cooper     // returns the MatcherBase itself.
322*28f6c2f2SEnji Cooper     const MatcherDescriberInterface* (*get_describer)(const MatcherBase&);
323*28f6c2f2SEnji Cooper     // Called on shared instances when the reference count reaches 0.
324*28f6c2f2SEnji Cooper     void (*shared_destroy)(SharedPayloadBase*);
325*28f6c2f2SEnji Cooper   };
326*28f6c2f2SEnji Cooper 
IsShared()327*28f6c2f2SEnji Cooper   bool IsShared() const {
328*28f6c2f2SEnji Cooper     return vtable_ != nullptr && vtable_->shared_destroy != nullptr;
329*28f6c2f2SEnji Cooper   }
330*28f6c2f2SEnji Cooper 
331*28f6c2f2SEnji Cooper   // If the implementation uses a listener, call that.
332*28f6c2f2SEnji Cooper   template <typename P>
333*28f6c2f2SEnji Cooper   static auto MatchAndExplainImpl(const MatcherBase& m, const T& value,
334*28f6c2f2SEnji Cooper                                   MatchResultListener* listener)
335*28f6c2f2SEnji Cooper       -> decltype(P::Get(m).MatchAndExplain(value, listener->stream())) {
336*28f6c2f2SEnji Cooper     return P::Get(m).MatchAndExplain(value, listener->stream());
337*28f6c2f2SEnji Cooper   }
338*28f6c2f2SEnji Cooper 
339*28f6c2f2SEnji Cooper   template <typename P>
340*28f6c2f2SEnji Cooper   static auto MatchAndExplainImpl(const MatcherBase& m, const T& value,
341*28f6c2f2SEnji Cooper                                   MatchResultListener* listener)
342*28f6c2f2SEnji Cooper       -> decltype(P::Get(m).MatchAndExplain(value, listener)) {
343*28f6c2f2SEnji Cooper     return P::Get(m).MatchAndExplain(value, listener);
344*28f6c2f2SEnji Cooper   }
345*28f6c2f2SEnji Cooper 
346*28f6c2f2SEnji Cooper   template <typename P>
DescribeImpl(const MatcherBase & m,std::ostream * os,bool negation)347*28f6c2f2SEnji Cooper   static void DescribeImpl(const MatcherBase& m, std::ostream* os,
348*28f6c2f2SEnji Cooper                            bool negation) {
349*28f6c2f2SEnji Cooper     if (negation) {
350*28f6c2f2SEnji Cooper       P::Get(m).DescribeNegationTo(os);
351*28f6c2f2SEnji Cooper     } else {
352*28f6c2f2SEnji Cooper       P::Get(m).DescribeTo(os);
353*28f6c2f2SEnji Cooper     }
354*28f6c2f2SEnji Cooper   }
355*28f6c2f2SEnji Cooper 
356*28f6c2f2SEnji Cooper   template <typename P>
GetDescriberImpl(const MatcherBase & m)357*28f6c2f2SEnji Cooper   static const MatcherDescriberInterface* GetDescriberImpl(
358*28f6c2f2SEnji Cooper       const MatcherBase& m) {
359*28f6c2f2SEnji Cooper     // If the impl is a MatcherDescriberInterface, then return it.
360*28f6c2f2SEnji Cooper     // Otherwise use MatcherBase itself.
361*28f6c2f2SEnji Cooper     // This allows us to implement the GetDescriber() function without support
362*28f6c2f2SEnji Cooper     // from the impl, but some users really want to get their impl back when
363*28f6c2f2SEnji Cooper     // they call GetDescriber().
364*28f6c2f2SEnji Cooper     // We use std::get on a tuple as a workaround of not having `if constexpr`.
365*28f6c2f2SEnji Cooper     return std::get<(
366*28f6c2f2SEnji Cooper         std::is_convertible<decltype(&P::Get(m)),
367*28f6c2f2SEnji Cooper                             const MatcherDescriberInterface*>::value
368*28f6c2f2SEnji Cooper             ? 1
369*28f6c2f2SEnji Cooper             : 0)>(std::make_tuple(&m, &P::Get(m)));
370*28f6c2f2SEnji Cooper   }
371*28f6c2f2SEnji Cooper 
372*28f6c2f2SEnji Cooper   template <typename P>
GetVTable()373*28f6c2f2SEnji Cooper   const VTable* GetVTable() {
374*28f6c2f2SEnji Cooper     static constexpr VTable kVTable = {&MatchAndExplainImpl<P>,
375*28f6c2f2SEnji Cooper                                        &DescribeImpl<P>, &GetDescriberImpl<P>,
376*28f6c2f2SEnji Cooper                                        P::shared_destroy};
377*28f6c2f2SEnji Cooper     return &kVTable;
378*28f6c2f2SEnji Cooper   }
379*28f6c2f2SEnji Cooper 
380*28f6c2f2SEnji Cooper   union Buffer {
381*28f6c2f2SEnji Cooper     // Add some types to give Buffer some common alignment/size use cases.
382*28f6c2f2SEnji Cooper     void* ptr;
383*28f6c2f2SEnji Cooper     double d;
384*28f6c2f2SEnji Cooper     int64_t i;
385*28f6c2f2SEnji Cooper     // And add one for the out-of-line cases.
386*28f6c2f2SEnji Cooper     SharedPayloadBase* shared;
387*28f6c2f2SEnji Cooper   };
388*28f6c2f2SEnji Cooper 
Destroy()389*28f6c2f2SEnji Cooper   void Destroy() {
390*28f6c2f2SEnji Cooper     if (IsShared() && buffer_.shared->Unref()) {
391*28f6c2f2SEnji Cooper       vtable_->shared_destroy(buffer_.shared);
392*28f6c2f2SEnji Cooper     }
393*28f6c2f2SEnji Cooper   }
394*28f6c2f2SEnji Cooper 
395*28f6c2f2SEnji Cooper   template <typename M>
IsInlined()396*28f6c2f2SEnji Cooper   static constexpr bool IsInlined() {
397*28f6c2f2SEnji Cooper     return sizeof(M) <= sizeof(Buffer) && alignof(M) <= alignof(Buffer) &&
398*28f6c2f2SEnji Cooper            std::is_trivially_copy_constructible<M>::value &&
399*28f6c2f2SEnji Cooper            std::is_trivially_destructible<M>::value;
400*28f6c2f2SEnji Cooper   }
401*28f6c2f2SEnji Cooper 
402*28f6c2f2SEnji Cooper   template <typename M, bool = MatcherBase::IsInlined<M>()>
403*28f6c2f2SEnji Cooper   struct ValuePolicy {
GetValuePolicy404*28f6c2f2SEnji Cooper     static const M& Get(const MatcherBase& m) {
405*28f6c2f2SEnji Cooper       // When inlined along with Init, need to be explicit to avoid violating
406*28f6c2f2SEnji Cooper       // strict aliasing rules.
407*28f6c2f2SEnji Cooper       const M* ptr =
408*28f6c2f2SEnji Cooper           static_cast<const M*>(static_cast<const void*>(&m.buffer_));
409*28f6c2f2SEnji Cooper       return *ptr;
410*28f6c2f2SEnji Cooper     }
InitValuePolicy411*28f6c2f2SEnji Cooper     static void Init(MatcherBase& m, M impl) {
412*28f6c2f2SEnji Cooper       ::new (static_cast<void*>(&m.buffer_)) M(impl);
413*28f6c2f2SEnji Cooper     }
414*28f6c2f2SEnji Cooper     static constexpr auto shared_destroy = nullptr;
415*28f6c2f2SEnji Cooper   };
416*28f6c2f2SEnji Cooper 
417*28f6c2f2SEnji Cooper   template <typename M>
418*28f6c2f2SEnji Cooper   struct ValuePolicy<M, false> {
419*28f6c2f2SEnji Cooper     using Shared = SharedPayload<M>;
420*28f6c2f2SEnji Cooper     static const M& Get(const MatcherBase& m) {
421*28f6c2f2SEnji Cooper       return static_cast<Shared*>(m.buffer_.shared)->value;
422*28f6c2f2SEnji Cooper     }
423*28f6c2f2SEnji Cooper     template <typename Arg>
424*28f6c2f2SEnji Cooper     static void Init(MatcherBase& m, Arg&& arg) {
425*28f6c2f2SEnji Cooper       m.buffer_.shared = new Shared(std::forward<Arg>(arg));
426*28f6c2f2SEnji Cooper     }
427*28f6c2f2SEnji Cooper     static constexpr auto shared_destroy = &Shared::Destroy;
428*28f6c2f2SEnji Cooper   };
429*28f6c2f2SEnji Cooper 
430*28f6c2f2SEnji Cooper   template <typename U, bool B>
431*28f6c2f2SEnji Cooper   struct ValuePolicy<const MatcherInterface<U>*, B> {
432*28f6c2f2SEnji Cooper     using M = const MatcherInterface<U>;
433*28f6c2f2SEnji Cooper     using Shared = SharedPayload<std::unique_ptr<M>>;
434*28f6c2f2SEnji Cooper     static const M& Get(const MatcherBase& m) {
435*28f6c2f2SEnji Cooper       return *static_cast<Shared*>(m.buffer_.shared)->value;
436*28f6c2f2SEnji Cooper     }
437*28f6c2f2SEnji Cooper     static void Init(MatcherBase& m, M* impl) {
438*28f6c2f2SEnji Cooper       m.buffer_.shared = new Shared(std::unique_ptr<M>(impl));
439*28f6c2f2SEnji Cooper     }
440*28f6c2f2SEnji Cooper 
441*28f6c2f2SEnji Cooper     static constexpr auto shared_destroy = &Shared::Destroy;
442*28f6c2f2SEnji Cooper   };
443*28f6c2f2SEnji Cooper 
444*28f6c2f2SEnji Cooper   template <typename M>
445*28f6c2f2SEnji Cooper   void Init(M&& m) {
446*28f6c2f2SEnji Cooper     using MM = typename std::decay<M>::type;
447*28f6c2f2SEnji Cooper     using Policy = ValuePolicy<MM>;
448*28f6c2f2SEnji Cooper     vtable_ = GetVTable<Policy>();
449*28f6c2f2SEnji Cooper     Policy::Init(*this, std::forward<M>(m));
450*28f6c2f2SEnji Cooper   }
451*28f6c2f2SEnji Cooper 
452*28f6c2f2SEnji Cooper   const VTable* vtable_;
453*28f6c2f2SEnji Cooper   Buffer buffer_;
454*28f6c2f2SEnji Cooper };
455*28f6c2f2SEnji Cooper 
456*28f6c2f2SEnji Cooper }  // namespace internal
457*28f6c2f2SEnji Cooper 
458*28f6c2f2SEnji Cooper // A Matcher<T> is a copyable and IMMUTABLE (except by assignment)
459*28f6c2f2SEnji Cooper // object that can check whether a value of type T matches.  The
460*28f6c2f2SEnji Cooper // implementation of Matcher<T> is just a std::shared_ptr to const
461*28f6c2f2SEnji Cooper // MatcherInterface<T>.  Don't inherit from Matcher!
462*28f6c2f2SEnji Cooper template <typename T>
463*28f6c2f2SEnji Cooper class Matcher : public internal::MatcherBase<T> {
464*28f6c2f2SEnji Cooper  public:
465*28f6c2f2SEnji Cooper   // Constructs a null matcher.  Needed for storing Matcher objects in STL
466*28f6c2f2SEnji Cooper   // containers.  A default-constructed matcher is not yet initialized.  You
467*28f6c2f2SEnji Cooper   // cannot use it until a valid value has been assigned to it.
468*28f6c2f2SEnji Cooper   explicit Matcher() {}  // NOLINT
469*28f6c2f2SEnji Cooper 
470*28f6c2f2SEnji Cooper   // Constructs a matcher from its implementation.
471*28f6c2f2SEnji Cooper   explicit Matcher(const MatcherInterface<const T&>* impl)
472*28f6c2f2SEnji Cooper       : internal::MatcherBase<T>(impl) {}
473*28f6c2f2SEnji Cooper 
474*28f6c2f2SEnji Cooper   template <typename U>
475*28f6c2f2SEnji Cooper   explicit Matcher(
476*28f6c2f2SEnji Cooper       const MatcherInterface<U>* impl,
477*28f6c2f2SEnji Cooper       typename std::enable_if<!std::is_same<U, const U&>::value>::type* =
478*28f6c2f2SEnji Cooper           nullptr)
479*28f6c2f2SEnji Cooper       : internal::MatcherBase<T>(impl) {}
480*28f6c2f2SEnji Cooper 
481*28f6c2f2SEnji Cooper   template <typename M, typename = typename std::remove_reference<
482*28f6c2f2SEnji Cooper                             M>::type::is_gtest_matcher>
483*28f6c2f2SEnji Cooper   Matcher(M&& m) : internal::MatcherBase<T>(std::forward<M>(m)) {}  // NOLINT
484*28f6c2f2SEnji Cooper 
485*28f6c2f2SEnji Cooper   // Implicit constructor here allows people to write
486*28f6c2f2SEnji Cooper   // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes
487*28f6c2f2SEnji Cooper   Matcher(T value);  // NOLINT
488*28f6c2f2SEnji Cooper };
489*28f6c2f2SEnji Cooper 
490*28f6c2f2SEnji Cooper // The following two specializations allow the user to write str
491*28f6c2f2SEnji Cooper // instead of Eq(str) and "foo" instead of Eq("foo") when a std::string
492*28f6c2f2SEnji Cooper // matcher is expected.
493*28f6c2f2SEnji Cooper template <>
494*28f6c2f2SEnji Cooper class GTEST_API_ Matcher<const std::string&>
495*28f6c2f2SEnji Cooper     : public internal::MatcherBase<const std::string&> {
496*28f6c2f2SEnji Cooper  public:
497*28f6c2f2SEnji Cooper   Matcher() = default;
498*28f6c2f2SEnji Cooper 
499*28f6c2f2SEnji Cooper   explicit Matcher(const MatcherInterface<const std::string&>* impl)
500*28f6c2f2SEnji Cooper       : internal::MatcherBase<const std::string&>(impl) {}
501*28f6c2f2SEnji Cooper 
502*28f6c2f2SEnji Cooper   template <typename M, typename = typename std::remove_reference<
503*28f6c2f2SEnji Cooper                             M>::type::is_gtest_matcher>
504*28f6c2f2SEnji Cooper   Matcher(M&& m)  // NOLINT
505*28f6c2f2SEnji Cooper       : internal::MatcherBase<const std::string&>(std::forward<M>(m)) {}
506*28f6c2f2SEnji Cooper 
507*28f6c2f2SEnji Cooper   // Allows the user to write str instead of Eq(str) sometimes, where
508*28f6c2f2SEnji Cooper   // str is a std::string object.
509*28f6c2f2SEnji Cooper   Matcher(const std::string& s);  // NOLINT
510*28f6c2f2SEnji Cooper 
511*28f6c2f2SEnji Cooper   // Allows the user to write "foo" instead of Eq("foo") sometimes.
512*28f6c2f2SEnji Cooper   Matcher(const char* s);  // NOLINT
513*28f6c2f2SEnji Cooper };
514*28f6c2f2SEnji Cooper 
515*28f6c2f2SEnji Cooper template <>
516*28f6c2f2SEnji Cooper class GTEST_API_ Matcher<std::string>
517*28f6c2f2SEnji Cooper     : public internal::MatcherBase<std::string> {
518*28f6c2f2SEnji Cooper  public:
519*28f6c2f2SEnji Cooper   Matcher() = default;
520*28f6c2f2SEnji Cooper 
521*28f6c2f2SEnji Cooper   explicit Matcher(const MatcherInterface<const std::string&>* impl)
522*28f6c2f2SEnji Cooper       : internal::MatcherBase<std::string>(impl) {}
523*28f6c2f2SEnji Cooper   explicit Matcher(const MatcherInterface<std::string>* impl)
524*28f6c2f2SEnji Cooper       : internal::MatcherBase<std::string>(impl) {}
525*28f6c2f2SEnji Cooper 
526*28f6c2f2SEnji Cooper   template <typename M, typename = typename std::remove_reference<
527*28f6c2f2SEnji Cooper                             M>::type::is_gtest_matcher>
528*28f6c2f2SEnji Cooper   Matcher(M&& m)  // NOLINT
529*28f6c2f2SEnji Cooper       : internal::MatcherBase<std::string>(std::forward<M>(m)) {}
530*28f6c2f2SEnji Cooper 
531*28f6c2f2SEnji Cooper   // Allows the user to write str instead of Eq(str) sometimes, where
532*28f6c2f2SEnji Cooper   // str is a string object.
533*28f6c2f2SEnji Cooper   Matcher(const std::string& s);  // NOLINT
534*28f6c2f2SEnji Cooper 
535*28f6c2f2SEnji Cooper   // Allows the user to write "foo" instead of Eq("foo") sometimes.
536*28f6c2f2SEnji Cooper   Matcher(const char* s);  // NOLINT
537*28f6c2f2SEnji Cooper };
538*28f6c2f2SEnji Cooper 
539*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW
540*28f6c2f2SEnji Cooper // The following two specializations allow the user to write str
541*28f6c2f2SEnji Cooper // instead of Eq(str) and "foo" instead of Eq("foo") when a absl::string_view
542*28f6c2f2SEnji Cooper // matcher is expected.
543*28f6c2f2SEnji Cooper template <>
544*28f6c2f2SEnji Cooper class GTEST_API_ Matcher<const internal::StringView&>
545*28f6c2f2SEnji Cooper     : public internal::MatcherBase<const internal::StringView&> {
546*28f6c2f2SEnji Cooper  public:
547*28f6c2f2SEnji Cooper   Matcher() = default;
548*28f6c2f2SEnji Cooper 
549*28f6c2f2SEnji Cooper   explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)
550*28f6c2f2SEnji Cooper       : internal::MatcherBase<const internal::StringView&>(impl) {}
551*28f6c2f2SEnji Cooper 
552*28f6c2f2SEnji Cooper   template <typename M, typename = typename std::remove_reference<
553*28f6c2f2SEnji Cooper                             M>::type::is_gtest_matcher>
554*28f6c2f2SEnji Cooper   Matcher(M&& m)  // NOLINT
555*28f6c2f2SEnji Cooper       : internal::MatcherBase<const internal::StringView&>(std::forward<M>(m)) {
556*28f6c2f2SEnji Cooper   }
557*28f6c2f2SEnji Cooper 
558*28f6c2f2SEnji Cooper   // Allows the user to write str instead of Eq(str) sometimes, where
559*28f6c2f2SEnji Cooper   // str is a std::string object.
560*28f6c2f2SEnji Cooper   Matcher(const std::string& s);  // NOLINT
561*28f6c2f2SEnji Cooper 
562*28f6c2f2SEnji Cooper   // Allows the user to write "foo" instead of Eq("foo") sometimes.
563*28f6c2f2SEnji Cooper   Matcher(const char* s);  // NOLINT
564*28f6c2f2SEnji Cooper 
565*28f6c2f2SEnji Cooper   // Allows the user to pass absl::string_views or std::string_views directly.
566*28f6c2f2SEnji Cooper   Matcher(internal::StringView s);  // NOLINT
567*28f6c2f2SEnji Cooper };
568*28f6c2f2SEnji Cooper 
569*28f6c2f2SEnji Cooper template <>
570*28f6c2f2SEnji Cooper class GTEST_API_ Matcher<internal::StringView>
571*28f6c2f2SEnji Cooper     : public internal::MatcherBase<internal::StringView> {
572*28f6c2f2SEnji Cooper  public:
573*28f6c2f2SEnji Cooper   Matcher() = default;
574*28f6c2f2SEnji Cooper 
575*28f6c2f2SEnji Cooper   explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)
576*28f6c2f2SEnji Cooper       : internal::MatcherBase<internal::StringView>(impl) {}
577*28f6c2f2SEnji Cooper   explicit Matcher(const MatcherInterface<internal::StringView>* impl)
578*28f6c2f2SEnji Cooper       : internal::MatcherBase<internal::StringView>(impl) {}
579*28f6c2f2SEnji Cooper 
580*28f6c2f2SEnji Cooper   template <typename M, typename = typename std::remove_reference<
581*28f6c2f2SEnji Cooper                             M>::type::is_gtest_matcher>
582*28f6c2f2SEnji Cooper   Matcher(M&& m)  // NOLINT
583*28f6c2f2SEnji Cooper       : internal::MatcherBase<internal::StringView>(std::forward<M>(m)) {}
584*28f6c2f2SEnji Cooper 
585*28f6c2f2SEnji Cooper   // Allows the user to write str instead of Eq(str) sometimes, where
586*28f6c2f2SEnji Cooper   // str is a std::string object.
587*28f6c2f2SEnji Cooper   Matcher(const std::string& s);  // NOLINT
588*28f6c2f2SEnji Cooper 
589*28f6c2f2SEnji Cooper   // Allows the user to write "foo" instead of Eq("foo") sometimes.
590*28f6c2f2SEnji Cooper   Matcher(const char* s);  // NOLINT
591*28f6c2f2SEnji Cooper 
592*28f6c2f2SEnji Cooper   // Allows the user to pass absl::string_views or std::string_views directly.
593*28f6c2f2SEnji Cooper   Matcher(internal::StringView s);  // NOLINT
594*28f6c2f2SEnji Cooper };
595*28f6c2f2SEnji Cooper #endif  // GTEST_INTERNAL_HAS_STRING_VIEW
596*28f6c2f2SEnji Cooper 
597*28f6c2f2SEnji Cooper // Prints a matcher in a human-readable format.
598*28f6c2f2SEnji Cooper template <typename T>
599*28f6c2f2SEnji Cooper std::ostream& operator<<(std::ostream& os, const Matcher<T>& matcher) {
600*28f6c2f2SEnji Cooper   matcher.DescribeTo(&os);
601*28f6c2f2SEnji Cooper   return os;
602*28f6c2f2SEnji Cooper }
603*28f6c2f2SEnji Cooper 
604*28f6c2f2SEnji Cooper // The PolymorphicMatcher class template makes it easy to implement a
605*28f6c2f2SEnji Cooper // polymorphic matcher (i.e. a matcher that can match values of more
606*28f6c2f2SEnji Cooper // than one type, e.g. Eq(n) and NotNull()).
607*28f6c2f2SEnji Cooper //
608*28f6c2f2SEnji Cooper // To define a polymorphic matcher, a user should provide an Impl
609*28f6c2f2SEnji Cooper // class that has a DescribeTo() method and a DescribeNegationTo()
610*28f6c2f2SEnji Cooper // method, and define a member function (or member function template)
611*28f6c2f2SEnji Cooper //
612*28f6c2f2SEnji Cooper //   bool MatchAndExplain(const Value& value,
613*28f6c2f2SEnji Cooper //                        MatchResultListener* listener) const;
614*28f6c2f2SEnji Cooper //
615*28f6c2f2SEnji Cooper // See the definition of NotNull() for a complete example.
616*28f6c2f2SEnji Cooper template <class Impl>
617*28f6c2f2SEnji Cooper class PolymorphicMatcher {
618*28f6c2f2SEnji Cooper  public:
619*28f6c2f2SEnji Cooper   explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}
620*28f6c2f2SEnji Cooper 
621*28f6c2f2SEnji Cooper   // Returns a mutable reference to the underlying matcher
622*28f6c2f2SEnji Cooper   // implementation object.
623*28f6c2f2SEnji Cooper   Impl& mutable_impl() { return impl_; }
624*28f6c2f2SEnji Cooper 
625*28f6c2f2SEnji Cooper   // Returns an immutable reference to the underlying matcher
626*28f6c2f2SEnji Cooper   // implementation object.
627*28f6c2f2SEnji Cooper   const Impl& impl() const { return impl_; }
628*28f6c2f2SEnji Cooper 
629*28f6c2f2SEnji Cooper   template <typename T>
630*28f6c2f2SEnji Cooper   operator Matcher<T>() const {
631*28f6c2f2SEnji Cooper     return Matcher<T>(new MonomorphicImpl<const T&>(impl_));
632*28f6c2f2SEnji Cooper   }
633*28f6c2f2SEnji Cooper 
634*28f6c2f2SEnji Cooper  private:
635*28f6c2f2SEnji Cooper   template <typename T>
636*28f6c2f2SEnji Cooper   class MonomorphicImpl : public MatcherInterface<T> {
637*28f6c2f2SEnji Cooper    public:
638*28f6c2f2SEnji Cooper     explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
639*28f6c2f2SEnji Cooper 
640*28f6c2f2SEnji Cooper     void DescribeTo(::std::ostream* os) const override { impl_.DescribeTo(os); }
641*28f6c2f2SEnji Cooper 
642*28f6c2f2SEnji Cooper     void DescribeNegationTo(::std::ostream* os) const override {
643*28f6c2f2SEnji Cooper       impl_.DescribeNegationTo(os);
644*28f6c2f2SEnji Cooper     }
645*28f6c2f2SEnji Cooper 
646*28f6c2f2SEnji Cooper     bool MatchAndExplain(T x, MatchResultListener* listener) const override {
647*28f6c2f2SEnji Cooper       return impl_.MatchAndExplain(x, listener);
648*28f6c2f2SEnji Cooper     }
649*28f6c2f2SEnji Cooper 
650*28f6c2f2SEnji Cooper    private:
651*28f6c2f2SEnji Cooper     const Impl impl_;
652*28f6c2f2SEnji Cooper   };
653*28f6c2f2SEnji Cooper 
654*28f6c2f2SEnji Cooper   Impl impl_;
655*28f6c2f2SEnji Cooper };
656*28f6c2f2SEnji Cooper 
657*28f6c2f2SEnji Cooper // Creates a matcher from its implementation.
658*28f6c2f2SEnji Cooper // DEPRECATED: Especially in the generic code, prefer:
659*28f6c2f2SEnji Cooper //   Matcher<T>(new MyMatcherImpl<const T&>(...));
660*28f6c2f2SEnji Cooper //
661*28f6c2f2SEnji Cooper // MakeMatcher may create a Matcher that accepts its argument by value, which
662*28f6c2f2SEnji Cooper // leads to unnecessary copies & lack of support for non-copyable types.
663*28f6c2f2SEnji Cooper template <typename T>
664*28f6c2f2SEnji Cooper inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
665*28f6c2f2SEnji Cooper   return Matcher<T>(impl);
666*28f6c2f2SEnji Cooper }
667*28f6c2f2SEnji Cooper 
668*28f6c2f2SEnji Cooper // Creates a polymorphic matcher from its implementation.  This is
669*28f6c2f2SEnji Cooper // easier to use than the PolymorphicMatcher<Impl> constructor as it
670*28f6c2f2SEnji Cooper // doesn't require you to explicitly write the template argument, e.g.
671*28f6c2f2SEnji Cooper //
672*28f6c2f2SEnji Cooper //   MakePolymorphicMatcher(foo);
673*28f6c2f2SEnji Cooper // vs
674*28f6c2f2SEnji Cooper //   PolymorphicMatcher<TypeOfFoo>(foo);
675*28f6c2f2SEnji Cooper template <class Impl>
676*28f6c2f2SEnji Cooper inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {
677*28f6c2f2SEnji Cooper   return PolymorphicMatcher<Impl>(impl);
678*28f6c2f2SEnji Cooper }
679*28f6c2f2SEnji Cooper 
680*28f6c2f2SEnji Cooper namespace internal {
681*28f6c2f2SEnji Cooper // Implements a matcher that compares a given value with a
682*28f6c2f2SEnji Cooper // pre-supplied value using one of the ==, <=, <, etc, operators.  The
683*28f6c2f2SEnji Cooper // two values being compared don't have to have the same type.
684*28f6c2f2SEnji Cooper //
685*28f6c2f2SEnji Cooper // The matcher defined here is polymorphic (for example, Eq(5) can be
686*28f6c2f2SEnji Cooper // used to match an int, a short, a double, etc).  Therefore we use
687*28f6c2f2SEnji Cooper // a template type conversion operator in the implementation.
688*28f6c2f2SEnji Cooper //
689*28f6c2f2SEnji Cooper // The following template definition assumes that the Rhs parameter is
690*28f6c2f2SEnji Cooper // a "bare" type (i.e. neither 'const T' nor 'T&').
691*28f6c2f2SEnji Cooper template <typename D, typename Rhs, typename Op>
692*28f6c2f2SEnji Cooper class ComparisonBase {
693*28f6c2f2SEnji Cooper  public:
694*28f6c2f2SEnji Cooper   explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {}
695*28f6c2f2SEnji Cooper 
696*28f6c2f2SEnji Cooper   using is_gtest_matcher = void;
697*28f6c2f2SEnji Cooper 
698*28f6c2f2SEnji Cooper   template <typename Lhs>
699*28f6c2f2SEnji Cooper   bool MatchAndExplain(const Lhs& lhs, std::ostream*) const {
700*28f6c2f2SEnji Cooper     return Op()(lhs, Unwrap(rhs_));
701*28f6c2f2SEnji Cooper   }
702*28f6c2f2SEnji Cooper   void DescribeTo(std::ostream* os) const {
703*28f6c2f2SEnji Cooper     *os << D::Desc() << " ";
704*28f6c2f2SEnji Cooper     UniversalPrint(Unwrap(rhs_), os);
705*28f6c2f2SEnji Cooper   }
706*28f6c2f2SEnji Cooper   void DescribeNegationTo(std::ostream* os) const {
707*28f6c2f2SEnji Cooper     *os << D::NegatedDesc() << " ";
708*28f6c2f2SEnji Cooper     UniversalPrint(Unwrap(rhs_), os);
709*28f6c2f2SEnji Cooper   }
710*28f6c2f2SEnji Cooper 
711*28f6c2f2SEnji Cooper  private:
712*28f6c2f2SEnji Cooper   template <typename T>
713*28f6c2f2SEnji Cooper   static const T& Unwrap(const T& v) {
714*28f6c2f2SEnji Cooper     return v;
715*28f6c2f2SEnji Cooper   }
716*28f6c2f2SEnji Cooper   template <typename T>
717*28f6c2f2SEnji Cooper   static const T& Unwrap(std::reference_wrapper<T> v) {
718*28f6c2f2SEnji Cooper     return v;
719*28f6c2f2SEnji Cooper   }
720*28f6c2f2SEnji Cooper 
721*28f6c2f2SEnji Cooper   Rhs rhs_;
722*28f6c2f2SEnji Cooper };
723*28f6c2f2SEnji Cooper 
724*28f6c2f2SEnji Cooper template <typename Rhs>
725*28f6c2f2SEnji Cooper class EqMatcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> {
726*28f6c2f2SEnji Cooper  public:
727*28f6c2f2SEnji Cooper   explicit EqMatcher(const Rhs& rhs)
728*28f6c2f2SEnji Cooper       : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {}
729*28f6c2f2SEnji Cooper   static const char* Desc() { return "is equal to"; }
730*28f6c2f2SEnji Cooper   static const char* NegatedDesc() { return "isn't equal to"; }
731*28f6c2f2SEnji Cooper };
732*28f6c2f2SEnji Cooper template <typename Rhs>
733*28f6c2f2SEnji Cooper class NeMatcher
734*28f6c2f2SEnji Cooper     : public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> {
735*28f6c2f2SEnji Cooper  public:
736*28f6c2f2SEnji Cooper   explicit NeMatcher(const Rhs& rhs)
737*28f6c2f2SEnji Cooper       : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {}
738*28f6c2f2SEnji Cooper   static const char* Desc() { return "isn't equal to"; }
739*28f6c2f2SEnji Cooper   static const char* NegatedDesc() { return "is equal to"; }
740*28f6c2f2SEnji Cooper };
741*28f6c2f2SEnji Cooper template <typename Rhs>
742*28f6c2f2SEnji Cooper class LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> {
743*28f6c2f2SEnji Cooper  public:
744*28f6c2f2SEnji Cooper   explicit LtMatcher(const Rhs& rhs)
745*28f6c2f2SEnji Cooper       : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {}
746*28f6c2f2SEnji Cooper   static const char* Desc() { return "is <"; }
747*28f6c2f2SEnji Cooper   static const char* NegatedDesc() { return "isn't <"; }
748*28f6c2f2SEnji Cooper };
749*28f6c2f2SEnji Cooper template <typename Rhs>
750*28f6c2f2SEnji Cooper class GtMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> {
751*28f6c2f2SEnji Cooper  public:
752*28f6c2f2SEnji Cooper   explicit GtMatcher(const Rhs& rhs)
753*28f6c2f2SEnji Cooper       : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {}
754*28f6c2f2SEnji Cooper   static const char* Desc() { return "is >"; }
755*28f6c2f2SEnji Cooper   static const char* NegatedDesc() { return "isn't >"; }
756*28f6c2f2SEnji Cooper };
757*28f6c2f2SEnji Cooper template <typename Rhs>
758*28f6c2f2SEnji Cooper class LeMatcher
759*28f6c2f2SEnji Cooper     : public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> {
760*28f6c2f2SEnji Cooper  public:
761*28f6c2f2SEnji Cooper   explicit LeMatcher(const Rhs& rhs)
762*28f6c2f2SEnji Cooper       : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {}
763*28f6c2f2SEnji Cooper   static const char* Desc() { return "is <="; }
764*28f6c2f2SEnji Cooper   static const char* NegatedDesc() { return "isn't <="; }
765*28f6c2f2SEnji Cooper };
766*28f6c2f2SEnji Cooper template <typename Rhs>
767*28f6c2f2SEnji Cooper class GeMatcher
768*28f6c2f2SEnji Cooper     : public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> {
769*28f6c2f2SEnji Cooper  public:
770*28f6c2f2SEnji Cooper   explicit GeMatcher(const Rhs& rhs)
771*28f6c2f2SEnji Cooper       : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {}
772*28f6c2f2SEnji Cooper   static const char* Desc() { return "is >="; }
773*28f6c2f2SEnji Cooper   static const char* NegatedDesc() { return "isn't >="; }
774*28f6c2f2SEnji Cooper };
775*28f6c2f2SEnji Cooper 
776*28f6c2f2SEnji Cooper template <typename T, typename = typename std::enable_if<
777*28f6c2f2SEnji Cooper                           std::is_constructible<std::string, T>::value>::type>
778*28f6c2f2SEnji Cooper using StringLike = T;
779*28f6c2f2SEnji Cooper 
780*28f6c2f2SEnji Cooper // Implements polymorphic matchers MatchesRegex(regex) and
781*28f6c2f2SEnji Cooper // ContainsRegex(regex), which can be used as a Matcher<T> as long as
782*28f6c2f2SEnji Cooper // T can be converted to a string.
783*28f6c2f2SEnji Cooper class MatchesRegexMatcher {
784*28f6c2f2SEnji Cooper  public:
785*28f6c2f2SEnji Cooper   MatchesRegexMatcher(const RE* regex, bool full_match)
786*28f6c2f2SEnji Cooper       : regex_(regex), full_match_(full_match) {}
787*28f6c2f2SEnji Cooper 
788*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW
789*28f6c2f2SEnji Cooper   bool MatchAndExplain(const internal::StringView& s,
790*28f6c2f2SEnji Cooper                        MatchResultListener* listener) const {
791*28f6c2f2SEnji Cooper     return MatchAndExplain(std::string(s), listener);
792*28f6c2f2SEnji Cooper   }
793*28f6c2f2SEnji Cooper #endif  // GTEST_INTERNAL_HAS_STRING_VIEW
794*28f6c2f2SEnji Cooper 
795*28f6c2f2SEnji Cooper   // Accepts pointer types, particularly:
796*28f6c2f2SEnji Cooper   //   const char*
797*28f6c2f2SEnji Cooper   //   char*
798*28f6c2f2SEnji Cooper   //   const wchar_t*
799*28f6c2f2SEnji Cooper   //   wchar_t*
800*28f6c2f2SEnji Cooper   template <typename CharType>
801*28f6c2f2SEnji Cooper   bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
802*28f6c2f2SEnji Cooper     return s != nullptr && MatchAndExplain(std::string(s), listener);
803*28f6c2f2SEnji Cooper   }
804*28f6c2f2SEnji Cooper 
805*28f6c2f2SEnji Cooper   // Matches anything that can convert to std::string.
806*28f6c2f2SEnji Cooper   //
807*28f6c2f2SEnji Cooper   // This is a template, not just a plain function with const std::string&,
808*28f6c2f2SEnji Cooper   // because absl::string_view has some interfering non-explicit constructors.
809*28f6c2f2SEnji Cooper   template <class MatcheeStringType>
810*28f6c2f2SEnji Cooper   bool MatchAndExplain(const MatcheeStringType& s,
811*28f6c2f2SEnji Cooper                        MatchResultListener* /* listener */) const {
812*28f6c2f2SEnji Cooper     const std::string s2(s);
813*28f6c2f2SEnji Cooper     return full_match_ ? RE::FullMatch(s2, *regex_)
814*28f6c2f2SEnji Cooper                        : RE::PartialMatch(s2, *regex_);
815*28f6c2f2SEnji Cooper   }
816*28f6c2f2SEnji Cooper 
817*28f6c2f2SEnji Cooper   void DescribeTo(::std::ostream* os) const {
818*28f6c2f2SEnji Cooper     *os << (full_match_ ? "matches" : "contains") << " regular expression ";
819*28f6c2f2SEnji Cooper     UniversalPrinter<std::string>::Print(regex_->pattern(), os);
820*28f6c2f2SEnji Cooper   }
821*28f6c2f2SEnji Cooper 
822*28f6c2f2SEnji Cooper   void DescribeNegationTo(::std::ostream* os) const {
823*28f6c2f2SEnji Cooper     *os << "doesn't " << (full_match_ ? "match" : "contain")
824*28f6c2f2SEnji Cooper         << " regular expression ";
825*28f6c2f2SEnji Cooper     UniversalPrinter<std::string>::Print(regex_->pattern(), os);
826*28f6c2f2SEnji Cooper   }
827*28f6c2f2SEnji Cooper 
828*28f6c2f2SEnji Cooper  private:
829*28f6c2f2SEnji Cooper   const std::shared_ptr<const RE> regex_;
830*28f6c2f2SEnji Cooper   const bool full_match_;
831*28f6c2f2SEnji Cooper };
832*28f6c2f2SEnji Cooper }  // namespace internal
833*28f6c2f2SEnji Cooper 
834*28f6c2f2SEnji Cooper // Matches a string that fully matches regular expression 'regex'.
835*28f6c2f2SEnji Cooper // The matcher takes ownership of 'regex'.
836*28f6c2f2SEnji Cooper inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
837*28f6c2f2SEnji Cooper     const internal::RE* regex) {
838*28f6c2f2SEnji Cooper   return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
839*28f6c2f2SEnji Cooper }
840*28f6c2f2SEnji Cooper template <typename T = std::string>
841*28f6c2f2SEnji Cooper PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
842*28f6c2f2SEnji Cooper     const internal::StringLike<T>& regex) {
843*28f6c2f2SEnji Cooper   return MatchesRegex(new internal::RE(std::string(regex)));
844*28f6c2f2SEnji Cooper }
845*28f6c2f2SEnji Cooper 
846*28f6c2f2SEnji Cooper // Matches a string that contains regular expression 'regex'.
847*28f6c2f2SEnji Cooper // The matcher takes ownership of 'regex'.
848*28f6c2f2SEnji Cooper inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
849*28f6c2f2SEnji Cooper     const internal::RE* regex) {
850*28f6c2f2SEnji Cooper   return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
851*28f6c2f2SEnji Cooper }
852*28f6c2f2SEnji Cooper template <typename T = std::string>
853*28f6c2f2SEnji Cooper PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
854*28f6c2f2SEnji Cooper     const internal::StringLike<T>& regex) {
855*28f6c2f2SEnji Cooper   return ContainsRegex(new internal::RE(std::string(regex)));
856*28f6c2f2SEnji Cooper }
857*28f6c2f2SEnji Cooper 
858*28f6c2f2SEnji Cooper // Creates a polymorphic matcher that matches anything equal to x.
859*28f6c2f2SEnji Cooper // Note: if the parameter of Eq() were declared as const T&, Eq("foo")
860*28f6c2f2SEnji Cooper // wouldn't compile.
861*28f6c2f2SEnji Cooper template <typename T>
862*28f6c2f2SEnji Cooper inline internal::EqMatcher<T> Eq(T x) {
863*28f6c2f2SEnji Cooper   return internal::EqMatcher<T>(x);
864*28f6c2f2SEnji Cooper }
865*28f6c2f2SEnji Cooper 
866*28f6c2f2SEnji Cooper // Constructs a Matcher<T> from a 'value' of type T.  The constructed
867*28f6c2f2SEnji Cooper // matcher matches any value that's equal to 'value'.
868*28f6c2f2SEnji Cooper template <typename T>
869*28f6c2f2SEnji Cooper Matcher<T>::Matcher(T value) {
870*28f6c2f2SEnji Cooper   *this = Eq(value);
871*28f6c2f2SEnji Cooper }
872*28f6c2f2SEnji Cooper 
873*28f6c2f2SEnji Cooper // Creates a monomorphic matcher that matches anything with type Lhs
874*28f6c2f2SEnji Cooper // and equal to rhs.  A user may need to use this instead of Eq(...)
875*28f6c2f2SEnji Cooper // in order to resolve an overloading ambiguity.
876*28f6c2f2SEnji Cooper //
877*28f6c2f2SEnji Cooper // TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x))
878*28f6c2f2SEnji Cooper // or Matcher<T>(x), but more readable than the latter.
879*28f6c2f2SEnji Cooper //
880*28f6c2f2SEnji Cooper // We could define similar monomorphic matchers for other comparison
881*28f6c2f2SEnji Cooper // operations (e.g. TypedLt, TypedGe, and etc), but decided not to do
882*28f6c2f2SEnji Cooper // it yet as those are used much less than Eq() in practice.  A user
883*28f6c2f2SEnji Cooper // can always write Matcher<T>(Lt(5)) to be explicit about the type,
884*28f6c2f2SEnji Cooper // for example.
885*28f6c2f2SEnji Cooper template <typename Lhs, typename Rhs>
886*28f6c2f2SEnji Cooper inline Matcher<Lhs> TypedEq(const Rhs& rhs) {
887*28f6c2f2SEnji Cooper   return Eq(rhs);
888*28f6c2f2SEnji Cooper }
889*28f6c2f2SEnji Cooper 
890*28f6c2f2SEnji Cooper // Creates a polymorphic matcher that matches anything >= x.
891*28f6c2f2SEnji Cooper template <typename Rhs>
892*28f6c2f2SEnji Cooper inline internal::GeMatcher<Rhs> Ge(Rhs x) {
893*28f6c2f2SEnji Cooper   return internal::GeMatcher<Rhs>(x);
894*28f6c2f2SEnji Cooper }
895*28f6c2f2SEnji Cooper 
896*28f6c2f2SEnji Cooper // Creates a polymorphic matcher that matches anything > x.
897*28f6c2f2SEnji Cooper template <typename Rhs>
898*28f6c2f2SEnji Cooper inline internal::GtMatcher<Rhs> Gt(Rhs x) {
899*28f6c2f2SEnji Cooper   return internal::GtMatcher<Rhs>(x);
900*28f6c2f2SEnji Cooper }
901*28f6c2f2SEnji Cooper 
902*28f6c2f2SEnji Cooper // Creates a polymorphic matcher that matches anything <= x.
903*28f6c2f2SEnji Cooper template <typename Rhs>
904*28f6c2f2SEnji Cooper inline internal::LeMatcher<Rhs> Le(Rhs x) {
905*28f6c2f2SEnji Cooper   return internal::LeMatcher<Rhs>(x);
906*28f6c2f2SEnji Cooper }
907*28f6c2f2SEnji Cooper 
908*28f6c2f2SEnji Cooper // Creates a polymorphic matcher that matches anything < x.
909*28f6c2f2SEnji Cooper template <typename Rhs>
910*28f6c2f2SEnji Cooper inline internal::LtMatcher<Rhs> Lt(Rhs x) {
911*28f6c2f2SEnji Cooper   return internal::LtMatcher<Rhs>(x);
912*28f6c2f2SEnji Cooper }
913*28f6c2f2SEnji Cooper 
914*28f6c2f2SEnji Cooper // Creates a polymorphic matcher that matches anything != x.
915*28f6c2f2SEnji Cooper template <typename Rhs>
916*28f6c2f2SEnji Cooper inline internal::NeMatcher<Rhs> Ne(Rhs x) {
917*28f6c2f2SEnji Cooper   return internal::NeMatcher<Rhs>(x);
918*28f6c2f2SEnji Cooper }
919*28f6c2f2SEnji Cooper }  // namespace testing
920*28f6c2f2SEnji Cooper 
921*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251 5046
922*28f6c2f2SEnji Cooper 
923*28f6c2f2SEnji Cooper #endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
924