HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Class templates for encapsulation of datasheet limits

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
encapsulationlimitstemplatesforclassdatasheet

Problem

I've written some classes to encapsulate the limits given for datasheet parameters, e.g. for the classic 741 op amp:

As the above example datasheet snippet shows, there are some challenges for representing the values in the "min", "typ" (typical), and "max" columns -- including cases where there is no typical value, and single-sided limits (i.e. there is no minimum and/or no maximum).

Additionally, I need to support limits which require exact integer values (e.g. for representing the limits for an analog-to-digital converter (ADC), which outputs discrete values). Consequently, I've written my classes as templates to support both integral and floating point types (the former is better suited to digital parameters, the latter to analog).

I also provided support for guardbands for tightening the limits in a test/quality control environment.

My compiler at work is the aging VS2005 compiler, so I don't have some of the useful C++11 std utilities like std::numeric_limits::lowest(). I've implemented a few that I need:

/** \file mycpp11std.h */

#include  // std::numeric_limits

namespace mycpp11std {

/** Implementation of enable_if from C++11 `std`, taken from
http://en.cppreference.com/w/cpp/types/enable_if.
*/
template
struct enable_if {};

/** Specialization of enable_if from C++11 `std` for `B = true`.
Taken from http://en.cppreference.com/w/cpp/types/enable_if.
*/
template
struct enable_if { typedef T type; };

/** Implementation of numeric_limits::lowest from C++11 `std`. Returns the most
negative value that can be represented by the type T. */
template 
struct numeric_limits {
    static T lowest() {
        return -std::numeric_limits::max();
    }
};

/** Specialization of numeric_limits::lowest for integers. */
template 
struct numeric_limits::is_integer >::type> {
    static T lowest() {
        return std::numeric_limits::min();
    }
};

} // end namespace mycpp11std


On to the actual class templates:

```
/** \file range.h */

#include // s

Solution

Was my decision to make lowerlimit and upperlimit public the correct
one?

Since lowerlimit and upperlimit are only written by constructor. you can make then const. So even though they are public access, still safe.


Did I handle the implementation of numeric_limits::lowest()
correctly?

You are right. For float, double, they are symmetrical in sign.
the lowest for float and double are:

float   -FLT_MAX
double  -DBL_MAX

Code Snippets

float   -FLT_MAX
double  -DBL_MAX

Context

StackExchange Code Review Q#121463, answer score: 2

Revisions (0)

No revisions yet.