Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use helpers during the construction #26

Closed
ilyaraz opened this issue Jan 9, 2016 · 2 comments
Closed

Use helpers during the construction #26

ilyaraz opened this issue Jan 9, 2016 · 2 comments

Comments

@ilyaraz
Copy link
Member

ilyaraz commented Jan 9, 2016

Need to use the code like below instead of "exponential" case analysis in the wrapper.

#include <iostream>
#include <memory>

class Base {
public:
    virtual void impl() = 0;

    virtual ~Base() {}
};

template<bool u, bool v> class Derived : public Base {
public:
    void impl() {
        std::cout << u << " " << v << std::endl;
    }
};

class Factory {
public:
    static std::shared_ptr<Base> construct(bool u, bool v) {
        if (u) {
            return construct_1<true>(v);
        }
        else {
            return construct_1<false>(v);
        }
    }
private:
    template<bool u> static std::shared_ptr<Base> construct_1(bool v) {
        if (v) {
            return construct_2<u, true>();
        }
        else {
            return construct_2<u, false>();
        }
    }

    template<bool u, bool v> static std::shared_ptr<Base> construct_2() {
        return std::shared_ptr<Base>(new Derived<u, v>);
    }
};

int main() {
    std::shared_ptr<Base> x = Factory::construct(false, true);
    x->impl();
    return 0;
}
@ludwigschmidt
Copy link
Collaborator

Yes, that's roughly what I meant when we talked about how to integrate the second distance function. The helper function construction_helper is basically already one level of this, but you can refactor it to make it better. Let's wait until we are going to add the next case to construct_table and refactor things then.

BTW, not sure if we need shared_ptr for this. unique_ptr should be sufficient and also semantically what we want.

@ludwigschmidt
Copy link
Collaborator

Wrapper constructions was refactored in ba35a64 .

A-Guldborg pushed a commit to duckth/FOENNIX that referenced this issue May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants