|
|
@@ -0,0 +1,46 @@
|
|
|
+# 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 `Skill`s 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.
|
|
|
+
|
|
|
+```cpp
|
|
|
+// 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 |
|