【问题标题】:Invoke final class constructor from base class从基类调用最终类构造函数
【发布时间】:2015-01-24 10:27:03
【问题描述】:

我有一个异常类如下:

class ExtensionExceptionType;
class Object;

class Exception
{
public:
    explicit Exception () { }

    Exception(                      const std::string &reason ) { PyErr_SetString( _Exc_RuntimeError(), reason.c_str() ); }
    Exception( PyObject* exception, const std::string &reason ) { PyErr_SetString( exception,           reason.c_str() ); }

    Exception( PyObject* exception, Object& reason );

    Exception( ExtensionExceptionType& exception, const std::string& reason );
    Exception( ExtensionExceptionType& exception, Object&            reason );

    void clear() { PyErr_Clear(); } // clear the error -- technically but not philosophically const

    static Object err_type();
    static Object err_value();
    static Object err_trace();

    static Object err_stats( uint32_t i ); // 0 1 2 for {type, value, trace}

    static void wrap( int condition ) {
        if( condition == -1 )
            throw Exception{};
    }
};

// Abstract
class StandardError     : public Exception      { protected: explicit StandardError()    {} };

class LookupError       : public StandardError  { protected: explicit LookupError()      {} };
class ArithmeticError   : public StandardError  { protected: explicit ArithmeticError()  {} };
class EnvironmentError  : public StandardError  { protected: explicit EnvironmentError() {} };

// Concrete (π)

// e.g.
//    class TypeError: public StandardError
//    {
//    public:
//        TypeError (const std::string& reason)
//        : StandardError()
//        {
//            PyErr_SetString( Py::_Exc_TypeError(),reason.c_str() );
//        }
//    };

#define CONCRETE( CLASS, BASE ) \
class CLASS: public BASE \
    { \
    public: \
        CLASS (const std::string& reason) \
        { \
            std::cout << "(Exception.hxx) " #CLASS " from PyCXX (" << reason.c_str() << ") \n"; \
            PyErr_SetString( _Exc_##CLASS(), reason.c_str() ); \
        } \
    };

// it appears that these classes are only for manually RAISING Python errors
// i.e. Raising an exception in the Python runtime
// because if I type something in to the Python console, I can make (e.g.) a KeyError occur, but these classes don't get hit.

CONCRETE( TypeError,            StandardError   )
CONCRETE( IndexError,           LookupError     )
CONCRETE( AttributeError,       StandardError   )
CONCRETE( NameError,            StandardError   )
CONCRETE( RuntimeError,         StandardError   )
CONCRETE( NotImplementedError,  StandardError   )
CONCRETE( SystemError,          StandardError   )
CONCRETE( KeyError,             LookupError     )
CONCRETE( ValueError,           StandardError   )
CONCRETE( OverflowError,        ArithmeticError )
CONCRETE( ZeroDivisionError,    ArithmeticError )
CONCRETE( FloatingPointError,   ArithmeticError )
CONCRETE( MemoryError,          StandardError   )
CONCRETE( SystemExit,           StandardError   )

我刚刚添加了:

    static void wrap( int condition ) {
        if( condition == -1 )
            throw Exception{};
    }

...因为在其他地方有很多场合...

if( SomePythonFunc(...) == -1 ) throw Exception{};

...已经整理成:

Exception.wrap( SomePythonFunc(...) ); // much nicer, I think

不过,也有以下情况:

if( SomePythonFunc(...) == -1 ) throw TypeError{ "foo" };

...我看不到如何执行等效包装。

即写:

TypeError.wrap( SomePythonFunc(...), "foo" );

由于 TypeError : Exception 和 Exception::wrap 是公共的,我可以为 wrap 创建一个可选的第二个参数:

    static void wrap( int condition, string err="default-err" ) {
        if( condition == -1 )
            throw FinalClassConstructor{ err }; // <-- how to do this?
    }

...但是我该如何调用 ::wrap 刚刚被命中的最终类的构造函数?

【问题讨论】:

    标签: c++ exception base-class final-class


    【解决方案1】:

    您尝试做的事情违反了多项 SOLID 原则。您还使您的基类与其后代紧密耦合(!)。你绝对不想要那个。它应该不知道谁继承。

    要做到这一点,请在子类中重写 Wrap 函数。在您的代码中,您将使用 StandardError.Wrap(...

    但老实说,我只是将异常留在代码中。

    (WHATEVER CONDITION -> Throw exception) 在代码中非常好,并且比使用静态方法的另一个异常中的异常更具可读性。

    这是你不应该再重构它的情况。

    【讨论】:

      猜你喜欢
      • 2016-07-19
      • 1970-01-01
      • 2011-02-12
      • 2014-02-19
      • 2011-05-03
      • 2012-01-27
      • 1970-01-01
      相关资源
      最近更新 更多