C ++, creating classes at runtime

I have a query, I have a set of flat files (say file1, file2, etc.) containing column names and native data types. (how values ​​are stored and can be read in C ++ is elementary) for example. a file with a flat file1 can have data such as col1_name = id, col1_type = integer, col2_name = name, col2_type = string, etc.

Therefore, for each flat file I need to create a C ++ data structure (i.e. 1 data structure with flat files = 1), where the name of the member variable matches the name of the column, and its data type will have its own C ++ data type such as int, float, string, etc. depending on the type of column in the flat file. above, for example: my flat file 1 should give me below ads

class file1{ int id; string Name; }; 

Is there a way to write C ++ code where the generated binary will read a flat file and create a data structure based on the file (the class name will be the same as the file name with a flat file). All classes created using these flat files will share the functionality of the getter and setter member functions.

Let me know if you have done something similar before or have any idea about it.

+6
source share
6 answers

No, not directly. C ++ is a compiled language. The code for each class is generated by the compiler.

You will need a two-step process. First write a program that reads these files and translates them into a .cpp file. Second, pass these .cpp files to the compiler.

+8
source

No, not easy (see other answers for reasons why not).

I would suggest looking at Python instead of this problem. The Python type system , combined with its ideas for using try / except easier to do with data analysis.

If you really have to use C ++, you can find a solution using the dynamic properties function of the Qt QObject class in conjunction with QVariant . Although this will do what you want, I would add a warning that this is becoming difficult and may complicate your task too much.

+7
source

C ++ classes are pure compile-time concepts and have no meaning at run time, so they cannot be created. However you can just go with

 std::vector<std::string> fields; 

and analyze as necessary in your access functions.

+5
source

No, but from what I can say, you should be able to store the names of several columns. What you can do is to have a map or unordered_map member variable that you can index with a string - the column name - and get some data (like a column object or something else). So you can do

 obj.Columns["Name"] 
+4
source

I'm not sure if there is a design template, but if your list of possible type names is finite and known at compile time, can you declare all these classes in your program before starting, and then just instantiate them based on the data in the files?

0
source

What you really want is a field whose exact nature changes at runtime.

There are several approaches, including Boost.Any , but due to the static nature of a C ++ type system, only 2 are recommended, and both require an advance understanding of all the possible data types that may be required.

The first approach is typical:

  • Object base type
  • Int , String , Date any derived types

and the use of polymorphism.

The second requires a bit of Boost magic: boost::variant<int, std::string, date> .

After you have a part of the β€œvariant”, you need to complete a visit to distinguish between the various possible types. Typical visitors to the traditional object-oriented approach, or just a combination of boost::static_visitor<> and boost::apply_visitor to select boost.

It is pretty simple.

0
source

Source: https://habr.com/ru/post/905529/


All Articles