Original Post
In my current C++ project I have a Device object. From this Device object I subclass a great number of devices. I store all of these devices in separate std::map objects with one map per subclass. The subclassed devices are produced by a factory. In the base class I have several routines to set various states of each device. Each device must have a unique ID upon creation. How would you implement this scenario?
Some ideas I had:
(1) Store all devices in one map. Store type information and cast to the appropriate subclass. The main advantage here is that I maintain one master list of all devices. However, the devil on my shoulder says this practice is frowned upon because of the casting.
(2) Make a separate map for each subclass but have no centralized list. The main advantage here is that no casts are needed. A big disadvantage is that I have as many lists as I do device types. If I want to add a device I have to check against all lists to see if the device already exists.
(3) Like (2) but I have an additional map that does nothing but record existing ID names. When a device is added/removed two maps need updating. Updating two locations to maintain state could be problematic if code complexity increases. I could add an interface which wraps the action of adding/removing objects to both lists.
(4) Something like (1) and (2) combined. Have a master list of IDevice* and also maintain specific subclassed lists. If I need to perform a base class operation I just use the main device list. However, now I have two pointers and I worry about leaks and/or dangling pointers.
(5) Have some sort of nested map or multi_map structure.
I'm guessing there's a stronger solution than the above. What would you recommend?
Thanks!
Some ideas I had:
(1) Store all devices in one map
(2) Make a separate map for each subclass but have no centralized list. The main advantage here is that no casts are needed. A big disadvantage is that I have as many lists as I do device types. If I want to add a device I have to check against all lists to see if the device already exists.
(3) Like (2) but I have an additional map that does nothing but record existing ID names. When a device is added/removed two maps need updating. Updating two locations to maintain state could be problematic if code complexity increases. I could add an interface which wraps the action of adding/removing objects to both lists.
(4) Something like (1) and (2) combined. Have a master list of IDevice* and also maintain specific subclassed lists. If I need to perform a base class operation I just use the main device list. However, now I have two pointers and I worry about leaks and/or dangling pointers.
(5) Have some sort of nested map or multi_map structure.
I'm guessing there's a stronger solution than the above. What would you recommend?
Thanks!