De-virtualised isBinaryExpression() and getResult() on ITransientExpression

This commit is contained in:
Phil Nash 2017-11-27 22:49:26 +03:00
parent da86ddc620
commit e3941a9ad2
2 changed files with 20 additions and 19 deletions

View File

@ -21,18 +21,14 @@ namespace Catch {
ArgT const& m_arg; ArgT const& m_arg;
MatcherT m_matcher; MatcherT m_matcher;
StringRef m_matcherString; StringRef m_matcherString;
bool m_result;
public: public:
MatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef matcherString ) MatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef matcherString )
: m_arg( arg ), : ITransientExpression{ true, matcher.match( arg ) },
m_arg( arg ),
m_matcher( matcher ), m_matcher( matcher ),
m_matcherString( matcherString ), m_matcherString( matcherString )
m_result( matcher.match( arg ) )
{} {}
auto isBinaryExpression() const -> bool override { return true; }
auto getResult() const -> bool override { return m_result; }
void streamReconstructedExpression( std::ostream &os ) const override { void streamReconstructedExpression( std::ostream &os ) const override {
auto matcherAsString = m_matcher.toString(); auto matcherAsString = m_matcher.toString();
os << Catch::Detail::stringify( m_arg ) << ' '; os << Catch::Detail::stringify( m_arg ) << ' ';

View File

@ -24,27 +24,32 @@
namespace Catch { namespace Catch {
struct ITransientExpression { struct ITransientExpression {
virtual auto isBinaryExpression() const -> bool = 0; auto isBinaryExpression() const -> bool { return m_isBinaryExpression; }
virtual auto getResult() const -> bool = 0; auto getResult() const -> bool { return m_result; }
virtual void streamReconstructedExpression( std::ostream &os ) const = 0; virtual void streamReconstructedExpression( std::ostream &os ) const = 0;
// We don't actually need a virtual destructore, but many static analysers ITransientExpression( bool isBinaryExpression, bool result )
: m_isBinaryExpression( isBinaryExpression ),
m_result( result )
{}
// We don't actually need a virtual destructor, but many static analysers
// complain if it's not here :-( // complain if it's not here :-(
virtual ~ITransientExpression(); virtual ~ITransientExpression();
bool m_isBinaryExpression;
bool m_result;
}; };
void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ); void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs );
template<typename LhsT, typename RhsT> template<typename LhsT, typename RhsT>
class BinaryExpr : public ITransientExpression { class BinaryExpr : public ITransientExpression {
bool m_result;
LhsT m_lhs; LhsT m_lhs;
StringRef m_op; StringRef m_op;
RhsT m_rhs; RhsT m_rhs;
auto isBinaryExpression() const -> bool override { return true; }
auto getResult() const -> bool override { return m_result; }
void streamReconstructedExpression( std::ostream &os ) const override { void streamReconstructedExpression( std::ostream &os ) const override {
formatReconstructedExpression formatReconstructedExpression
( os, Catch::Detail::stringify( m_lhs ), m_op, Catch::Detail::stringify( m_rhs ) ); ( os, Catch::Detail::stringify( m_lhs ), m_op, Catch::Detail::stringify( m_rhs ) );
@ -52,7 +57,7 @@ namespace Catch {
public: public:
BinaryExpr( bool comparisonResult, LhsT lhs, StringRef op, RhsT rhs ) BinaryExpr( bool comparisonResult, LhsT lhs, StringRef op, RhsT rhs )
: m_result( comparisonResult ), : ITransientExpression{ true, comparisonResult },
m_lhs( lhs ), m_lhs( lhs ),
m_op( op ), m_op( op ),
m_rhs( rhs ) m_rhs( rhs )
@ -63,15 +68,15 @@ namespace Catch {
class UnaryExpr : public ITransientExpression { class UnaryExpr : public ITransientExpression {
LhsT m_lhs; LhsT m_lhs;
auto isBinaryExpression() const -> bool override { return false; }
auto getResult() const -> bool override { return m_lhs ? true : false; }
void streamReconstructedExpression( std::ostream &os ) const override { void streamReconstructedExpression( std::ostream &os ) const override {
os << Catch::Detail::stringify( m_lhs ); os << Catch::Detail::stringify( m_lhs );
} }
public: public:
UnaryExpr( LhsT lhs ) : m_lhs( lhs ) {} UnaryExpr( LhsT lhs )
: ITransientExpression{ false, lhs ? true : false },
m_lhs( lhs )
{}
}; };