While reading a book about modern C++, I have seen a code snippet that confused me. The code has been written to set up PWM (16 bit Timer) for 8 bit AVR microcontroller. The code is like that:
class pwm_base : private util::noncopyable
{
public:
typedef std::uint_fast16_t duty_type;
pwm_base(const duty_type resol,
const duty_type duty = 0U) : resolution(resol),
counter (0U),
duty_cycle(duty),
shadow (duty) { }
duty_type get_resolution() const { return resolution; }
void set_duty(const duty_type duty)
{
// Set a new duty cycle in the shadow register.
mcal::irq::disable_all();
shadow = static_cast<std::uint_fast8_t>((std::min)(duty, get_resolution())); // ???? (1)
mcal::irq::enable_all();
}
duty_type get_duty() const
{
// Retrieve the duty cycle.
mcal::irq::disable_all();
const volatile std::uint_fast8_t the_duty = duty_cycle; // ???? (2)
mcal::irq::enable_all();
return the_duty;
}
virtual void service() = 0;
protected:
const duty_type resolution;
duty_type counter;
duty_type duty_cycle;
duty_type shadow;
};
I have problems with lines indicated by ????
. It is clearly seen that both, shadow
and duty_cycle
have been defined as uint_fast16_t
. So, it means that they have to be at least 16 bits. If it is the case, why does the author downcast the result of min method to uint_fast8_t
instead of casting to uint_fast16_t
at line ???? (1)
? And also, why does at line ???? (2)
he downcast again the variable to uint_fast8_t
even if the function return type is uint_fast16_t
? Are these downcastings required? What are their purposes?