ysiraichi / enfield

An OpenQASM source-to-source Compiler.
GNU General Public License v2.0
20 stars 8 forks source link

Refactoring Interfaces #30

Closed ysiraichi closed 6 years ago

ysiraichi commented 6 years ago

Fix #29 . Because of non-trivial default constructors, it was tricky to parameterize and use the Strategy pattern. One simple solution to it was creating Setter methods for the arguments in the constructor, and any virtual function that needed it was made protected and wrapped in a public function where we would check for whether we did set or not. In other words, we transformed:

class A {
    public:
        A(int a) : mA(a) {}
        virtual void foo() = 0;
};

into:

class A {
    public:
        A() : mA(0) {}
        void foo() { checkASet(); fooImpl(); }
    protected:
        void checkASet() { if (mA == 0) { /* Error not set */ } }
        virtual void fooImpl() = 0;
};