I have a C++ code. It contains a State pattern. This is how I like the main loop looks…
- User
I have a C++ code. It contains a State pattern. This is how I like the main loop looks like for the final user: for(number of simulation) { do { state->execute(); state = state->nextState(); } while (state->getStateName != "ExitState"); } For this reason, I am considering transitions that are state-driven. Then, I have a IConfiguration class that provides getter function. The concrete class is DefaultConfiguration, that contains the major part of the data that is common to all states, while some is state-dependent. For this reason, I create a class called StateConfiguration that handle this specialization using as parameter a string that contains the name of the State. I also write a StateFactory that handles the registration and creation of State. Additionally, I create a class called StateRegistration that registers the State inside the Factory. However, I am finding the best solution to pass DefaultConfiguration or directly the specialized version to the relevant concrete states. Indeed, I like the idea that DefaultConfiguration is created in the main application, to guarantee visibility, however I am looking for a solution such that when provided, even as unique_ptr to strongly guarantee the ownership, to "someone " the final user does not have to care about this management. Please provide alternatives if you see any.
- Assistant
- User
Considering solution 1.
- In this case, MyState should have both a pointer to DefaultConfiguration and to StateConfiguration, because the user will use StateConfiguration but the system needs DefaultConfiguration if it needs to pass it to the next state.
- Additionally, State must not have a constructor but only concrete State. At this stage, how can I handle the lambda inside StateRegistrator for registerState? This call does not work in my opinion: explicit StateRegistrator(std::shared_ptr fullConfig) { if (!fullConfig) throw std::invalid_argument("Invalid or undefined state"); StateFactory::instance().registerState("BaseState", fullConfig -> std::unique_ptr { return std::make_unique(fullConfig); }); }
- At this stage, StateFactory should look like: std::unique_ptr create(const std::string &name, std::shared_ptr fullConfig) const { if (auto it = creators.find(name); it != creators.end()) { return it->second(fullConfig); } throw std::invalid_argument("State not found: " + name); } and in concrete State I should have something like this: std::unique_ptr nextState() override { return StateFactory::instance().create("BaseState", fullConfig); } Such that I need that to own even the pointer to IFullConfiguration to it. Note: in this scenario, the concrete instance of IFullConfiguration will remain one, because within a state-driven transition it acts as a chain-of-pointers. If this statement is correct, this may lead me to refine the consideration, because an alternative could be to pass IFullConfiguration to StateFactory or any other class that acts as centralized actor in this design.
- Assistant
- User
Consider this option: in StateRegistrator class StateRegistrator { explicit StateRegistrator(std::shared_ptr fullConfig) { if (!fullConfig) throw std::invalid_argument("Invalid or undefined state"); StateFactory::instance().registerState("BaseState", fullConfig -> std::unique_ptr { return std::make_unique(fullConfig); }); } }; I can add a call like: StateFactory::instance().linkConfiguration(fullConfig) and in StateFactory something like: void linkConfiguration(std::shared_ptr fullConfig_>) { std::call_once(linked, & { fullConfig = std::move(fullConfig_); }; } and add as private: std::once_flag linked; At this stage, StateFactory owns the ownership of DefaultConfiguration and create function may look like: std::unique_ptr create(const std::string &name) const { if (auto it = creators.find(name); it != creators.end()) { return it->second(fullConfig); } throw std::invalid_argument("State not found: " + name); } and the register function: using CreateFunc = std::function<std::unique_ptr()>;
- Assistant
- User
Ok, almost in (maybe :) : take a look of this code in StateFactory: using CreateFunc = std::function<std::unique_ptr()>;
void registerState(const std::string &name, CreateFunc func) { if (auto it = creators.find(name); it == creators.end()) { creators[name] = func; return; } throw std::invalid_argument("State already registered: " + name); } std::unique_ptr<State> create(const std::string &name) const { if (linked) { if (auto it = creators.find(name); it != creators.end()) { return it->second(fullConfig); // here! } throw std::invalid_argument("State not found: " + name); } else throw std::invalid_argument("Configuration not linked"); }CreateFunc looks how I expect, however to create an instance of concrete State I should provide as argument fullConfig. So, I am considering from a technical point of view how to proceed in such a way.
- Assistant
- User
Yes but registerState in this way does not correspond to my actual design. Indeed, it should be the StateRegistrator that passes the lambda to register states. In this way, you are hardcoding that each time registerState is called a BaseState is created.
- Assistant
- User
Yes, but in this way the idea to have StateFactory that owns a shared pointer to IFullConfiguration falls down because you are passing the configuration directly from the StateRegistrator without using the linkConfiguration method.
- Assistant
- User
Ok, but in this way it is convenient to provide the shared ptr to StateRegistrator and not to StateFactory, it sounds for you?
- Assistant
- User
Ok, but at this state why keep a shred ptr to fullConfig even in Factory?
- Assistant
- User
This is the lambda I am using in StateRegistrator: StateFactory::instance().registerState("BaseState", = -> std::unique_ptr { return std::make_unique(fullConfig); }); However, as discussed, the constructor of State is the default one while the concrete state accepts the IFullConfiguration. Proposal? One solution is to delete the constructor of concrete state and create a constructor of State that accepts the pointer to IFullConfiguration. The other one can be to solved this error: no suitable user-defined conversion from "lambda ->std::unique_ptr<State, std::default_delete>" to "StateFactory::CreateFunc"
- Assistant
- User
Solution 1. Is it the best solution to keep the shared pointer of IFullConfiguration as protected?
- Assistant
- User
Question on Inheritance and Polymorphism: when I have a class with private data, and I inherit from it several instances, like in this case State and BaseState, for each instances of derived class I also create a State class, like each BaseState calls State or not? Additionally, how are private member class of base class handled? For each derived class I have private data?
- Assistant