C++ Utilities

This chapter presents a selection of C++ utilities that are useful for solving recurring problems in microcontroller programming.

  • PDF / 211,524 Bytes
  • 15 Pages / 439.36 x 666.15 pts Page_size
  • 68 Downloads / 206 Views

DOWNLOAD

REPORT


C++ Utilities

This chapter presents a selection of C++ utilities that are useful for solving recurring problems in microcontroller programming.

15.1 The nothing Structure Consider the implementation of the nothing structure below. struct nothing { }; The nothing structure contains no members and encapsulates no functionality whatsoever. Although the nothing structure does not actually do anything itself, it can be quite useful as a place holder for other function and template parameters. Recall the fixed_point class from Sect. 13.2. The constructors of the fixed_point class from integral types perform a left-shift of their input parameter before using it to initialize the internal representation of the fixed-point number. This accounts for the fixed position of the decimal point. For a simplified version of the Q7.8 fixed-point representation, for example, we have something like the following.

// A simplified Q7.8 fixed-point representation. class fixed_point { public: fixed_point(std::uint16_t u) : value(u = (pts_end - 1U)->x) { // We are above the upper x-range. return (pts_end - 1U)->y; } else { // Find interpolation pair with binary search. point_iterator it = std::lower_bound(pts_begin, pts_end, point(x));

// Do const const const const const

the linear interpolation. x_type xn = (it - 1U)->x; x_type delta_xn = it->x - xn; x_type delta_x = x - xn; y_type yn = (it - 1U)->y; y_type delta_yn = it->y - yn;

const y_type delta_y = (delta_x * delta_yn) / delta_xn; return (yn + delta_y) + offset; } } Following some elementary bounds checking, the core of this linear interpolation function uses the std::lower_bound() algorithm to find the pair of interpolation points. The linear_interpolate() subroutine thereby profits from the high efficiency of std::lower_bound() which uses a binary search for sequences having random access iterators. The fourth input parameter to linear_interpolate() called offset has a dual role. It allows an optional non-zero offset to be applied to the result of the linear interpolation. In addition, the offset parameter provides the compiler with enough information to automatically deduce all of the template parameters. The linear_interpolate() subroutine is designed to work particularly well with a template point class type such as the one shown in Sect. 5.4. The lower-bound algorithm tests for inequality using operator