Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accessing namespaces via iterators/pointers?

I am currently facing a design/language problem of sorts. I have 3 files, A,B,and C which have their own namespaces defined as A,B,and C respectively. Each namespace has exactly the same function signatures, only implemented differently (think a REST API call with different but similar APIs). Now my aim is to be able to call these functions from the correct namespace without repeating the function name.

Ideally I’d like to do something like this:

#include <iostream>

#include "A.h"
#include "B.h"
#include "C.h"

enum ns {
    A = 0,
    B = 1,
    C = 2
};
int main() {

    auto NS = {A, B, C}; //these are the namespaces

    if (condition){
        int n = get_index();
        NS[n]::myfunc();
    }
}

where myfunc() is defined, albeit differently, in each source file corresponding to A.h, B.h, and C.h .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can someone tell me whether there is a way to do this, or suggest an alternate design pattern I should adopt? Thanks

EDIT: to explain my problem more concretely, I have a DataReader class which fetches data about say cars, from REST endpoints by vendors A, B, C and preprocesses them. I did think of making a class and subclassing them for different vendors, but then won’t it be too complicated, having a vendor-specific class inside my DataReader class?

>Solution :

You can create a base class with a pure-virtual method myfunc(),
and then make A,B and C (each reside in its namespace) derive from it and implement myfunc.

You can then store them all in an array, and use a polymorphic call the invoke the proper method based on an index:

#include <array>
#include <iostream>
#include <memory>

struct Base {
    virtual ~Base() = default;
    virtual void myfunc() = 0;
};

namespace AAA
{
    struct A : public Base {
        void myfunc() override { std::cout << "A::myfunc()\n"; }
    };
}

namespace BBB
{
    struct B : public Base {
        void myfunc() override { std::cout << "B::myfunc()\n"; }
    };
}

namespace CCC
{
    struct C : public Base {
        void myfunc() override { std::cout << "C::myfunc()\n"; }
    };
}

int main() {
    std::array<std::unique_ptr<Base>, 3> objs = {
        std::make_unique<AAA::A>(),
        std::make_unique<BBB::B>(),
        std::make_unique<CCC::C>()
    };

    objs[0]->myfunc();
    objs[1]->myfunc();
    objs[2]->myfunc();
}

Output:

A::myfunc()
B::myfunc()
C::myfunc()

Live demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading