index

patterns: prototype


Use a launch init pattern to define an example of each kind of plugin.
class   DinnerPlugs { 
public: DinnerPlugs(vector<plug*>&); 
  vector<plug*>*V; 
  inline void F(plug*X)  {V->push_back(X);} 
}; 

vector<Plug*> dinnerProtos; 
DinnerPlugs::DinnerPlugs(vector<plug*>&all) { V=&all; 
  F(new ingredientsUI); 
  F(new        diceUI); 
  F(new    julienneUI); 
  F(new      sauteeUI); 
  F(new       broilUI); 
  //F(new   microwaveUI); //disabled; microwave broken. 
//etc.. 
} 
DinnerPlugs P(dinnerProtos); // launch init. 

//..later, put them on a menu: 

for (uint i=0; i<dinnerProtos.size(); ++i) 
  menuUI->addPayload(dinnerProtos[i]);
It's time to make your script for preparing dinner, so you go to the pop-up menu and construct a recipe. The pop-up callback ends up in a sync function:
void PluginUI::sync(Widget*o) { 
  //... 
  if (o==menuUI) { 
    plug*P = menuUI->selected(); // P = the prototype. 
    adopt(P->copy());            // add to current script. 
    } 
  //... 
}
All plugins know how to copy themselves, but the plugin container has no idea what it just copied, which is great.

nedwaves.com 2017 [171108]