Skip to content

Instantly share code, notes, and snippets.

@cdleary
Created January 27, 2014 00:16
Show Gist options
  • Save cdleary/8641252 to your computer and use it in GitHub Desktop.
Save cdleary/8641252 to your computer and use it in GitHub Desktop.
struct Cake {
static Cake Fail;
bool burned();
};
Cake Cake::Fail;
struct Ingredients { bool spoiled(); };
struct BatterAndWhatnot {};
template <class Wrapped>
class CakeMaker
{
public:
Cake makeCake() {
Wrapped *self = static_cast<Wrapped *>(this);
Ingredients ingredients = self->fetchIngredients();
if (ingredients.spoiled())
return Cake::Fail;
BatterAndWhatnot batter = self->mixAndStuff(ingredients);
Cake result = self->bake(batter);
if (result.burned())
return Cake::Fail;
return result;
}
};
class PersonalChef : public CakeMaker<PersonalChef>
{
Ingredients fetchIngredients();
BatterAndWhatnot mixAndStuff(Ingredients);
Cake bake(BatterAndWhatnot);
friend class CakeMaker;
};
int main()
{
PersonalChef chef;
chef.makeCake();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment