130. RAII

Question.4

A developer manages peripheral power using RAII:

class PowerGuard {
   Peripheral& periph;
public:
   PowerGuard(Peripheral& p) : periph(p) {
       periph.power_on();
   }
   ~PowerGuard() { periph.power_off(); }
};

void read_sensor(Peripheral& adc) {
   PowerGuard pg(adc);  // ADC powered on
   int val = adc.read();
   process(val);
} // ADC powered off automatically

What firmware benefit does this provide?

Need Help? Refer to the Quick Guide below

Select Answer