In short: State is an alias for a function pointer. State_ is a wrapper class that has a State member and is implicitly converted to that State .
The reason State_ is needed is because there is no way to express a function that returns a pointer to a function of the same type. The wrapper gets rid of self-reference.
Line by line:
struct StateData;
Direct declaration of the StateData class.
struct State_;
The first declaration of the State_ class.
typedef State_ (*State)(StateData&);
This is a bit complicated. It defines State as a type alias for a function pointer, which can point to a function that returns State_ , and takes StateData& as an argument. Functions declared at the end of a fragment can be indicated by a pointer to a function of this type.
In my opinion, the chosen name is very confusing, given that the State_ class already exists. Although I usually oppose Hungarian notation, I would recommend always using a suffix or prefix to indicate a function pointer, like state_fun or state_handler or state_callback ,
struct State_ {
This starts the definition of State_ calss.
State_( State pp ) : p( pp ) { }
This defines the constructor for the class. The argument has a function pointer type that was previously defined. It initializes a member to be announced shortly.
operator State() { return p; }
Member function. More specifically, custom conversion to a function pointer type.
State p;
Declares an element that has been initialized in the constructor.
}; State_ state_start(StateData& d); State_ state_selecting(StateData& d); State_ state_initializing(StateData& d); State_ state_tracking(StateData& d);
Free functions that State can point to.
user2079303
source share