Nessuna descrizione

Sam Jaffe 0253681e05 chore: update XCode version 5 mesi fa
include 63e654bfe1 refactor: add support for three-way-comparison 8 mesi fa
opaque_typedef.xcodeproj 0253681e05 chore: update XCode version 5 mesi fa
opaque_typedef_test e52b0ed5bb Updating opaque typedef, including adding support for auto-generation of opaque_typedefs with operators, and optional conversion functions. 7 anni fa
test b95d26c73c Fix spelling error Arithmatic -> Arithmetic 5 anni fa
.clang-format 388e3343e6 Add clang-format support. 5 anni fa
README.md 37e2d39173 Add README.md 5 anni fa

README.md

Opaque Typedef

A library designed to provide smarter handling of typedef/using declarations, whereas the standard typedef provides absolutely no defenses against accidentally switching parameters.

Usage

An opaque typedef is constructed with a using-declaration, a wrapped type, a tag type, and a list of Skills that the type implements.

Example Code Snippet

For example, suppose we're writing a math library, and want to make sure that we don't accidentally pass in radians as degrees or degrees as radians.

// Define the types that we're interested in for degrees and radians
using radian = types::opaque_typedef<double, struct radian_t, types::Comparable,
                                     types::Arithmetic>;
using degree = types::opaque_typedef<double, struct degree_t, types::Comparable,
                                     types::Arithmetic>;

// Provide implicit conversion operators for degree <-> radian
template <> template <> radian::opaque_typedef(degree const & deg)
    : opaque_typedef(deg.get() * M_PI_2 / 90.0) {}
    
template <> template <> degree::opaque_typedef(radian const & rad)
    : opaque_typedef(rad.get() * 90.0 / M_PI_2) {}
    
namespace math {
  // Even if I call math::sin(degree{90}), it'll work correctly
  double sin(radian r) { return std::sin(r.get()); }
}

Caveat

This library is designed for being used as wrappers around primitive types (bool, int#_t, uint#_t, float, double, long double, char), and std::string. While it is possible to place opaque_typedefs around literally anything, it is not advised that you wrap complex types, as providing delegate functions is not an intended feature.

Skills

The opaque_typdef library comes natively with the following 7 skills

Skill Name Operations
EqualityComparable a == b, a != b
Comparable a == b, a != b, a < b, a <= b, a > b, a >= b
Incrementable ++a, a++
Decrementable --a, a--
Addable a + b
Arithmetic a + b, a - b, -a
Numeric a + b, a - b, a * b, a / b, -a