We want a solution in C++ that must be able to do the following:
Given a string of particular type, lets say 'A', we want to find all the types that derives from 'A'.
Instantiate new objects out of the types that are derived from 'A'.
E.g. Lets say we have a class, VehicleEntity. VehicleEntityhas child classes, PassangerCarEntity, TruckEntity, TrainEntity, BoatEntity.
We are unsure what vehicle entities there may be as the a library could be added containing more VehicleEntities. E.g. an AirplaneEntity thaterives from VehicleEntity could be added after deployment.
In the application, when a user wants to select a VehicleEntity, the user should be able to pick any of the entities deriving from VehicleEntity. This includes the PassangerCarEntity, TruckEntity, TrainEntity, BoatEntity and AirplaneEntity.
The user selects an Entity, lets say AirplaneEntity, A new object of type AirplaneEntity must be instantiated.
The following is an concept example in C# of what we want to achieve in C++.
In C# the items for the dropdown list can be retrieved as follows:
1 2 3 4 5 6 7 8 9 10 11
|
Type vehicleEntityType = typeof(VehicleEntity);
List<Type> types = new List<Type>();
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
types.AddRange(assembly.GetTypes().Where(x => vehicleEntityType.IsAssignableFrom(x) && !x.IsGenericType && !x.IsAbstract));
dropDownBox.ItemList = types;
//To instantiate the object:
List<VehicleEntity> vehicleList = new List<VehicleEntity>();
Type newVehicleType = (Type)dropDownBox.SelectedItem;
object newVehicle = Activator.CreateInstance(newVehicleType ); // defrault constructor used, parameterless constructors for vehicles.
vehicleList.Add(newVehicle);
|
We are aware that standard C++ does not contain any Metadata on its objects, and thus it is not possible without a workaround. It does not seem possible with RTTI and boost.Mirror.
I am sure that we are not the only ones that had this problem and that solutions exist.