Using C++ in

embedded environment

Example: class Disable

It is often necessary to disable the interrupts for a specific part of the program.
How it is done in C or assembly? The solution is very simple: every processor has instructions to do this: one for disable (e.g.: "cli") and one for enable (e.g.: "sei"). These are inline functions in C called "disable()" and "enable()", respectively, generating only one instruction.
The problem is a little more complicated: to be structured, it is better to restore the previous state instead of enable at the end. The corresponding state is usually stored in the status register of the processor which can be read and written.

/* "C" solution: */
unsigned char tmp = SREG; /* Store the current status of the IE flag */
disable();                /* The interrupt is disabled now */
do_something_here();      /* Some uninterruptibe job is done here */
...                       /* More possible instructions... */
SREG = tmp;               /* Restore the status register, including the IE flag */
                          /* The interrupt is enabled again */ 

Of course, the task is done with interrupt disabled can be complicated. A little risk of this part is that if a return instruction is inserted at the middle, then it is possible to forget about restoring the interrupt status. It is the responsibility of the programmer.

How can it be done in C++?

Let's see the following class:
class Disable
    Disable() { status = SREG; disable(); }
    ~Disable() { SREG = status; }
    unsigned char status;

How this class works?
It is very simple: creating an instance stores the current IE status, and disables the interrupt. Destroying it restores the previous state.
How to use it:
...              /* Somwhere in the middle of the code... */
  Disable _d;    /* Instantiate it: the constructor runs here */
  ...            /* Do the job with interrupt is disabled */
}                /* The destructor runs here: restore the interrupt flag */
...              /* Continue the execution with interrupt status restored */ 

About the generated machine code:
In an embedded system it can be very important to generate short and fast code. If you believe or not, gcc generates exactly the same code as in the first example. It cannot be smaller nor faster using any language, including assembly! Note that the temporary variable can be optimized into a register, if enabled and is possible.

The destructor runs automatically, therefore any number of return instructions can be inserted at the middle, the interrupt state is always restored correctly.
It is a very simple but useful example. In a real design, there can be several situations where some kind of "on/off" switch must be handled this way, or something must be restored before return. The solution is safe, because restoring the state is the responsibility of the compiler, and not the programmer. This solution does not need any additional resource!

Using this programming method can produce exactly the same code as its C or assembly equivalent, but the result is simplier and more reliable and comprehensible. Such a method can be used for other similar purposes too: it is easy and does not hurt. :-)