简体   繁体   中英

reading message structure from a text file and generate a class based on the it

I have a question. Is it possible for cpp/qt language to do the following ?

1) Read from a text file eg template.txt

struct messageA 
{
  variableA int 0 
  variableB string abc
  variableC double 1.0
}

struct msgB

{ variableD int 0 variableE byte 255 variableF unsignedInt 123

} //... and so on

2) Based on the details from the template.txt, generate a class that will create the variables and the datatype accordingly?

I have a general idea on how to read the the content from the text file and stores the details but I'm having problem to come up with an idea on how to have create the class based on the data I've received.

any tips/hints would be great! thanks !

It's not like in python where you can create classes on runtime; c++ is a compiled language. You can create a source file from reading the file and compile it. Such a technologie is also used by Qt's moc creator. If you then want to include your class in your running environment, you can build a shared library with your class and load the library. Also thee this question.

As user3755692 already mentioned, you can't create classes on runtime. But if you really desperate about idea of dynamically creation of some objects with different fields and methods, you can try a component-based design . It's really effective, but sometimes hard to implement, approach.

It's like you have a dummy object, which is just a container of components , like so:

class BaseObject
{
public:
    void addComponent(IComponent* component);
    Icomponent* getComponent();

    void executeComponent(int id);
    void updateAllComponents();
private:
    vector<IComponent*> _components;
};

And you have various components inherited from IComponent interface(which provides some basic functionality and pure virtual methods), which you can add to your base object.

Components can be anything:

// Component that stores int value
class IntComponent : public IComponent
{
public:
     // This is something inherited from IComponent
    virtual void init()
    {
        integerValue = 0;
    }

public:
    int integerValue;
};

// Component that allows you to print something in console
class PrintingComponent : public IComponent
{
public:
     // This is something inherited from IComponent
    virtual void init()
    {

    }

public:
    void print(const char* phrase)
    {
        cout<<phrase<<endl;
    }
};

You can combine different components in your base object to achieve deserved functionality. And you can create such objects using a some kind of configuration file. Like simple *.txt file:

[OBJECT]
COMPONENT_INT,
COMPONENT_PRINT

[OBJECT]
COMPONENT_INT,
COMPONENT_FILEREADER

Then you will parse it and create two objects with different sets of components, which then can be used inside of your program.

Here's some useful articles about this approach:

1) http://www.randygaul.net/2013/05/20/component-based-engine-design/

2) http://gameprogrammingpatterns.com/component.html

3) Component based game engine design - much useful links here too

Those articles above focused on gamedev mainly, but there's a very good explanations. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM