C++ – How to make plugable factory work with lua

clua

class data is like this:

struct Base_data
{
public:
    Base_data(){
        protocolname = "Base";
    }
    string protocolname;
};

class HttpData : public Base_data
{
public:
    HttpData(){
        protocolname = "Http";
    }
};

class Professor:

class Base_Professor
{
public:
    void Process(Base_data &data)
    {
        std::map::const_iterator it = ListProfessor.find(data.protocolname);
        if(it == ListProfessor.end())
            cout second->Do(data);
    }

    virtual void Do(Base_data &data){}

    virtual std::string GetProfessorname(){
        return "Base";
    }

    ~Base_Professor(){
        std::map::const_iterator it;
        for(it = ListProfessor.begin(); it != ListProfessor.end(); ++it)
            delete it->second;
    }
    bool Register(Base_Professor *Professor){
        std::map::const_iterator it = ListProfessor.find(Professor->GetProfessorname());
        if(it != ListProfessor.end())
            return false;
        ListProfessor.insert(std::make_pair(Professor->GetProfessorname(), Professor));
    }
private:
    std::map ListProfessor;
};

class HttpProfessor : public Base_Professor
{
public:
    std::string GetProfessorname(){
        return "Http";
    }
    void Do(Base_data &data){
        std::cout

I can add new protocol by inheritant Base_Professor and register new class but I have no ideal how to do that in lua. Do you have any ideal how to do that ?

Best Answer

Lua doesn't provide "classes" out-of-the-box. It has other features, somewhat different.

However you can simulate classes and inheritance functionality by using some of these functionalities (tables & metatables).

If you are not interested in knowing the technical details, you can use some already-built lua library.

I've myself created a library called "MiddleClass" that provides this functionality. You can find a manual in its LÖVE wiki page.

This library provides most of what you need, except for "public" and "private" functionalities (everything is "public"). You may find other libraries on the lua users wiki.