diff --git a/src/catch2/matchers/catch_matchers.hpp b/src/catch2/matchers/catch_matchers.hpp index 7a77a62c..373e2a67 100644 --- a/src/catch2/matchers/catch_matchers.hpp +++ b/src/catch2/matchers/catch_matchers.hpp @@ -37,14 +37,18 @@ namespace Matchers { template - struct MatcherBase : MatcherUntypedBase { + class MatcherBase : public MatcherUntypedBase { + public: virtual bool match( T const& arg ) const = 0; }; namespace Detail { template - struct MatchAllOf final : MatcherBase { + class MatchAllOf final : public MatcherBase { + std::vector const*> m_matchers; + + public: MatchAllOf() = default; MatchAllOf(MatchAllOf const&) = delete; MatchAllOf& operator=(MatchAllOf const&) = delete; @@ -83,9 +87,6 @@ namespace Matchers { rhs.m_matchers.insert(rhs.m_matchers.begin(), &lhs); return CATCH_MOVE(rhs); } - - private: - std::vector const*> m_matchers; }; //! lvalue overload is intentionally deleted, users should @@ -98,7 +99,9 @@ namespace Matchers { MatchAllOf operator&& (MatcherBase const& lhs, MatchAllOf const& rhs) = delete; template - struct MatchAnyOf final : MatcherBase { + class MatchAnyOf final : public MatcherBase { + std::vector const*> m_matchers; + public: MatchAnyOf() = default; MatchAnyOf(MatchAnyOf const&) = delete; MatchAnyOf& operator=(MatchAnyOf const&) = delete; @@ -136,9 +139,6 @@ namespace Matchers { rhs.m_matchers.insert(rhs.m_matchers.begin(), &lhs); return CATCH_MOVE(rhs); } - - private: - std::vector const*> m_matchers; }; //! lvalue overload is intentionally deleted, users should @@ -151,8 +151,10 @@ namespace Matchers { MatchAnyOf operator|| (MatcherBase const& lhs, MatchAnyOf const& rhs) = delete; template - struct MatchNotOf final : MatcherBase { + class MatchNotOf final : public MatcherBase { + MatcherBase const& m_underlyingMatcher; + public: explicit MatchNotOf( MatcherBase const& underlyingMatcher ): m_underlyingMatcher( underlyingMatcher ) {} @@ -164,9 +166,6 @@ namespace Matchers { std::string describe() const override { return "not " + m_underlyingMatcher.toString(); } - - private: - MatcherBase const& m_underlyingMatcher; }; } // namespace Detail diff --git a/src/catch2/matchers/catch_matchers_floating_point.hpp b/src/catch2/matchers/catch_matchers_floating_point.hpp index dc0fa8f7..99572e43 100644 --- a/src/catch2/matchers/catch_matchers_floating_point.hpp +++ b/src/catch2/matchers/catch_matchers_floating_point.hpp @@ -17,7 +17,8 @@ namespace Matchers { enum class FloatingPointKind : uint8_t; } - struct WithinAbsMatcher final : MatcherBase { + class WithinAbsMatcher final : public MatcherBase { + public: WithinAbsMatcher(double target, double margin); bool match(double const& matchee) const override; std::string describe() const override; @@ -26,8 +27,11 @@ namespace Matchers { double m_margin; }; - struct WithinUlpsMatcher final : MatcherBase { - WithinUlpsMatcher(double target, uint64_t ulps, Detail::FloatingPointKind baseType); + class WithinUlpsMatcher final : public MatcherBase { + public: + WithinUlpsMatcher( double target, + uint64_t ulps, + Detail::FloatingPointKind baseType ); bool match(double const& matchee) const override; std::string describe() const override; private: @@ -42,8 +46,9 @@ namespace Matchers { // |lhs - rhs| <= epsilon * max(fabs(lhs), fabs(rhs)), then we get // the same result if we do this for floats, as if we do this for // doubles that were promoted from floats. - struct WithinRelMatcher final : MatcherBase { - WithinRelMatcher(double target, double epsilon); + class WithinRelMatcher final : public MatcherBase { + public: + WithinRelMatcher( double target, double epsilon ); bool match(double const& matchee) const override; std::string describe() const override; private: diff --git a/src/catch2/matchers/catch_matchers_string.hpp b/src/catch2/matchers/catch_matchers_string.hpp index 549e2d85..7f961c45 100644 --- a/src/catch2/matchers/catch_matchers_string.hpp +++ b/src/catch2/matchers/catch_matchers_string.hpp @@ -26,39 +26,46 @@ namespace Matchers { std::string m_str; }; - struct StringMatcherBase : MatcherBase { - StringMatcherBase( StringRef operation, CasedString const& comparator ); - std::string describe() const override; - + class StringMatcherBase : public MatcherBase { + protected: CasedString m_comparator; StringRef m_operation; + + public: + StringMatcherBase( StringRef operation, + CasedString const& comparator ); + std::string describe() const override; }; - struct StringEqualsMatcher final : StringMatcherBase { + class StringEqualsMatcher final : public StringMatcherBase { + public: StringEqualsMatcher( CasedString const& comparator ); bool match( std::string const& source ) const override; }; - struct StringContainsMatcher final : StringMatcherBase { + class StringContainsMatcher final : public StringMatcherBase { + public: StringContainsMatcher( CasedString const& comparator ); bool match( std::string const& source ) const override; }; - struct StartsWithMatcher final : StringMatcherBase { + class StartsWithMatcher final : public StringMatcherBase { + public: StartsWithMatcher( CasedString const& comparator ); bool match( std::string const& source ) const override; }; - struct EndsWithMatcher final : StringMatcherBase { + class EndsWithMatcher final : public StringMatcherBase { + public: EndsWithMatcher( CasedString const& comparator ); bool match( std::string const& source ) const override; }; - struct RegexMatcher final : MatcherBase { + class RegexMatcher final : public MatcherBase { + std::string m_regex; + CaseSensitive m_caseSensitivity; + + public: RegexMatcher( std::string regex, CaseSensitive caseSensitivity ); bool match( std::string const& matchee ) const override; std::string describe() const override; - - private: - std::string m_regex; - CaseSensitive m_caseSensitivity; }; //! Creates matcher that accepts strings that are exactly equal to `str` diff --git a/src/catch2/matchers/catch_matchers_templated.hpp b/src/catch2/matchers/catch_matchers_templated.hpp index 200ca74b..2673a3af 100644 --- a/src/catch2/matchers/catch_matchers_templated.hpp +++ b/src/catch2/matchers/catch_matchers_templated.hpp @@ -19,7 +19,8 @@ namespace Catch { namespace Matchers { - struct MatcherGenericBase : MatcherUntypedBase { + class MatcherGenericBase : public MatcherUntypedBase { + public: MatcherGenericBase() = default; virtual ~MatcherGenericBase(); // = default; @@ -119,7 +120,8 @@ namespace Matchers { template - struct MatchAllOfGeneric final : MatcherGenericBase { + class MatchAllOfGeneric final : public MatcherGenericBase { + public: MatchAllOfGeneric(MatchAllOfGeneric const&) = delete; MatchAllOfGeneric& operator=(MatchAllOfGeneric const&) = delete; MatchAllOfGeneric(MatchAllOfGeneric&&) = default; @@ -137,7 +139,11 @@ namespace Matchers { return describe_multi_matcher(" and "_sr, m_matchers, std::index_sequence_for{}); } - std::array m_matchers; + // Has to be public to enable the concatenating operators + // below, because they are not friend of the RHS, only LHS, + // and thus cannot access private fields of RHS + std::array m_matchers; + //! Avoids type nesting for `GenericAllOf && GenericAllOf` case template @@ -169,7 +175,8 @@ namespace Matchers { template - struct MatchAnyOfGeneric final : MatcherGenericBase { + class MatchAnyOfGeneric final : public MatcherGenericBase { + public: MatchAnyOfGeneric(MatchAnyOfGeneric const&) = delete; MatchAnyOfGeneric& operator=(MatchAnyOfGeneric const&) = delete; MatchAnyOfGeneric(MatchAnyOfGeneric&&) = default; @@ -187,7 +194,11 @@ namespace Matchers { return describe_multi_matcher(" or "_sr, m_matchers, std::index_sequence_for{}); } - std::array m_matchers; + + // Has to be public to enable the concatenating operators + // below, because they are not friend of the RHS, only LHS, + // and thus cannot access private fields of RHS + std::array m_matchers; //! Avoids type nesting for `GenericAnyOf || GenericAnyOf` case template @@ -218,7 +229,10 @@ namespace Matchers { template - struct MatchNotOfGeneric final : MatcherGenericBase { + class MatchNotOfGeneric final : public MatcherGenericBase { + MatcherT const& m_matcher; + + public: MatchNotOfGeneric(MatchNotOfGeneric const&) = delete; MatchNotOfGeneric& operator=(MatchNotOfGeneric const&) = delete; MatchNotOfGeneric(MatchNotOfGeneric&&) = default; @@ -239,8 +253,6 @@ namespace Matchers { friend MatcherT const& operator ! (MatchNotOfGeneric const& matcher) { return matcher.m_matcher; } - private: - MatcherT const& m_matcher; }; } // namespace Detail diff --git a/src/catch2/matchers/catch_matchers_vector.hpp b/src/catch2/matchers/catch_matchers_vector.hpp index 00ecbc33..b9a02f57 100644 --- a/src/catch2/matchers/catch_matchers_vector.hpp +++ b/src/catch2/matchers/catch_matchers_vector.hpp @@ -17,8 +17,10 @@ namespace Catch { namespace Matchers { template - struct VectorContainsElementMatcher final : MatcherBase> { + class VectorContainsElementMatcher final : public MatcherBase> { + T const& m_comparator; + public: VectorContainsElementMatcher(T const& comparator): m_comparator(comparator) {} @@ -35,13 +37,13 @@ namespace Matchers { std::string describe() const override { return "Contains: " + ::Catch::Detail::stringify( m_comparator ); } - - T const& m_comparator; }; template - struct ContainsMatcher final : MatcherBase> { + class ContainsMatcher final : public MatcherBase> { + std::vector const& m_comparator; + public: ContainsMatcher(std::vector const& comparator): m_comparator( comparator ) {} @@ -67,13 +69,13 @@ namespace Matchers { std::string describe() const override { return "Contains: " + ::Catch::Detail::stringify( m_comparator ); } - - std::vector const& m_comparator; }; template - struct EqualsMatcher final : MatcherBase> { + class EqualsMatcher final : public MatcherBase> { + std::vector const& m_comparator; + public: EqualsMatcher(std::vector const& comparator): m_comparator( comparator ) {} @@ -93,12 +95,14 @@ namespace Matchers { std::string describe() const override { return "Equals: " + ::Catch::Detail::stringify( m_comparator ); } - std::vector const& m_comparator; }; template - struct ApproxMatcher final : MatcherBase> { + class ApproxMatcher final : public MatcherBase> { + std::vector const& m_comparator; + mutable Catch::Approx approx = Catch::Approx::custom(); + public: ApproxMatcher(std::vector const& comparator): m_comparator( comparator ) {} @@ -129,13 +133,13 @@ namespace Matchers { approx.scale(static_cast(newScale)); return *this; } - - std::vector const& m_comparator; - mutable Catch::Approx approx = Catch::Approx::custom(); }; template - struct UnorderedEqualsMatcher final : MatcherBase> { + class UnorderedEqualsMatcher final : public MatcherBase> { + std::vector const& m_target; + + public: UnorderedEqualsMatcher(std::vector const& target): m_target(target) {} @@ -149,15 +153,12 @@ namespace Matchers { std::string describe() const override { return "UnorderedEquals: " + ::Catch::Detail::stringify(m_target); } - private: - std::vector const& m_target; }; // The following functions create the actual matcher objects. // This allows the types to be inferred - //! Creates a matcher that matches vectors that contain all elements in `comparator` template, typename AllocMatch = AllocComp> ContainsMatcher Contains( std::vector const& comparator ) { diff --git a/src/catch2/matchers/internal/catch_matchers_impl.hpp b/src/catch2/matchers/internal/catch_matchers_impl.hpp index 2acbec1f..5d00ad7d 100644 --- a/src/catch2/matchers/internal/catch_matchers_impl.hpp +++ b/src/catch2/matchers/internal/catch_matchers_impl.hpp @@ -36,7 +36,7 @@ namespace Catch { namespace Matchers { template - struct MatcherBase; + class MatcherBase; } using StringMatcher = Matchers::MatcherBase;