I am implementing a C ++ program that can programmatically create objects specified by an input file that provides class names and arguments passed to the constructors.
Classes are derived from a common base class, but their constructor signature changes.
They are declared as follows:
class Base { ... } class Class1 : Base { Class1(int a1, int a2); } class Class2 : Base { Class2(int a1, int a2, int a3); } ... and so on...
Argument types should not be int, in fact they can be any built-in type or complex, custom type.
Entering program data may look like this in the form of JSON:
[ { "Class1": ["arg11", "arg12"] }, { "Class2": ["arg21", "arg22", "arg23"] }, ...and so on... ]
Reading through Boost.Functional / Factory docs seems like it could solve my problem if it werenโt for the fact that in my application the constructor signature is changing (heterogeneity restriction). The Boost.Function / Factory approach is the normalization of constructor signatures, however this is not possible in my application.
In a dynamic language such as Python, this would be pretty trivial: obj = klass(*args) where klass = Class1 and args = ["arg11, "arg12"] .
So, how could a factory template with a heterogeneous constraint be implemented in C ++?
Are there other libraries besides Boost that I overlooked that might be useful?
Is it possible to implement this so that the only dependency is the standard library (i.e. there is no Boost)?
Also, in the case where the constructor argument is of a complex type, so that it must be specifically constructed from its JSON representation, how does it affect the complexity of the problem?