1

I'm writing a dll, which contains a c++ class definition, and a core program based on the proxy pattern, as described in this tutorial: http://www.linuxjournal.com/article/3687

Specifically, this dll, after is loaded on the core program, will fill its definition including the class's name, method's name, and method's function pointer in the data structure of the core program.

However, I want to modify this pattern for the different kinds of classes, instead of only one base kind in the article, which is the reason why I use the function pointer.

My program is as following:

//the base class
class common_object{
};

//this factory is used for storing constructor for each c++ class in the dll.
typedef common_object *maker_t();
extern map< string, maker_t* > factory;

//store all the methods of a class
//string: method's name
typedef map<string, proxy::method> method_map;
//string: class's name
extern map<string, method_map> class_map_;

// our global factory
template<typename T>
class proxy {
public:
    typedef int (T::*mfp)(lua_State *L);
    typedef struct {
        const char *class_name;
        const char *method_name;
        mfp mfunc;
    } method;

    proxy() {
        std::cout << "circle proxy" << endl;
        // fill method table with methods from class T
        // initialize method information for the circle class
        method_map method_map_;
        for (method *m = T::methods;m->method_name; m++) {
            /* edited by Snaily: shouldn't it be const RegType *l ... ? */
            method m1;
            m1.class_name = T::className;
            m1.method_name = m->method_name;
            m1.mfunc = m->mfunc;
            method_map_[m1.method_name] = m1;
        }
        //register the circle class' description
        class_map_[T::class_name] = method_map_;
    }
};

In this program, I extern two data structs of the core problem:

  1. class_map: contain all the class in the dll loaded in the core program. method_map:
  2. method_map: contains all the methods'description for each class. the relation between class_map and method is one-to-many. However, I run into a problem of declaration order of class_map_ and method_map. Specifically, I extern class_map outside the class Proxy, but I also have to define the method structure inside this class. I tried to use forward declaration in the following link: When can I use a forward declaration?, but it doesn't work.

I hope to see your solution about my problem. Thanks so much

Community
  • 1
  • 1
khanhhh89
  • 317
  • 5
  • 19

0 Answers0