How to dynamically declare structure from text in runtime
-
Hello, I want to build an simple application that it takes data from UDP and parses it with given struct. Application will not know about the structure at the beginning. It will read structure from the text file selected by the user and create it dynamically.
I found that once a type declared in compile time, we can create an instance of it at run time using QMetaType class methods. But I couldn't find any way how can I declare a structure type and register it in run time. For below example MyStruct will be declared in file.
Do you have any suggestions?
//Example
int id = qRegisterMetaType<MyStruct>();
QMetaType type = QMetaType::fromName("MyStruct");
if (type.isValid()) {
void *ptr= type.create();
ptr-> ....;
...
} -
Hello, I want to build an simple application that it takes data from UDP and parses it with given struct. Application will not know about the structure at the beginning. It will read structure from the text file selected by the user and create it dynamically.
I found that once a type declared in compile time, we can create an instance of it at run time using QMetaType class methods. But I couldn't find any way how can I declare a structure type and register it in run time. For below example MyStruct will be declared in file.
Do you have any suggestions?
//Example
int id = qRegisterMetaType<MyStruct>();
QMetaType type = QMetaType::fromName("MyStruct");
if (type.isValid()) {
void *ptr= type.create();
ptr-> ....;
...
}@brsmnc
In C++ you cannot declarestructorclassat runtime, only at compile time. It's a strongly-typed language.You could do something like this more from Python if you wanted to change to that.
It is unclear why exactly you want to produce a "type" or
structdynamically. Even if you could create the type at runtime, how would that be useful given that you would not have any code compiled which used it?? You can manage without in C++, depends what you're really trying to achieve which makes you think you need some dynamically-created type. -
Hello, I want to build an simple application that it takes data from UDP and parses it with given struct. Application will not know about the structure at the beginning. It will read structure from the text file selected by the user and create it dynamically.
I found that once a type declared in compile time, we can create an instance of it at run time using QMetaType class methods. But I couldn't find any way how can I declare a structure type and register it in run time. For below example MyStruct will be declared in file.
Do you have any suggestions?
//Example
int id = qRegisterMetaType<MyStruct>();
QMetaType type = QMetaType::fromName("MyStruct");
if (type.isValid()) {
void *ptr= type.create();
ptr-> ....;
...
}@brsmnc said in How to dynamically declare structure from text in runtime:
I want to build an simple application that it takes data from UDP and parses it with given struct
Why not create (declare) the struct(s) and read your data and save it in your struct(s)?
You could useQByteArray,QVariantetc... or pre-declare one struct/class for every "type" you might have (if there are not too many).