Browse Source

Add README.md

Sam Jaffe 5 years ago
parent
commit
37e2d39173
2 changed files with 48 additions and 0 deletions
  1. 46 0
      README.md
  2. 2 0
      opaque_typedef.xcodeproj/project.pbxproj

+ 46 - 0
README.md

@@ -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 |

+ 2 - 0
opaque_typedef.xcodeproj/project.pbxproj

@@ -58,6 +58,7 @@
 		CD7048F320C48B30007C944C /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
 		CD70490720C48B75007C944C /* opaque_typedef_test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = opaque_typedef_test.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD70490B20C48B75007C944C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
+		CDAA6D1224E0645100E09BB9 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
 		CDE8546624DF5051006FE7C7 /* opaque_typedef */ = {isa = PBXFileReference; lastKnownFileType = folder; name = opaque_typedef; path = include/opaque_typedef; sourceTree = "<group>"; };
 		CDE8547224DF80EE006FE7C7 /* opaque_typedef_comparable_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = opaque_typedef_comparable_test.cxx; sourceTree = "<group>"; };
 		CDE8547424DF8110006FE7C7 /* opaque_typedef_adhoc_arithmetic_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = opaque_typedef_adhoc_arithmetic_test.cxx; sourceTree = "<group>"; };
@@ -79,6 +80,7 @@
 		CD3C805F1D63EE0000ACC795 = {
 			isa = PBXGroup;
 			children = (
+				CDAA6D1224E0645100E09BB9 /* README.md */,
 				CD7048F320C48B30007C944C /* GoogleMock.xcodeproj */,
 				CDE8546624DF5051006FE7C7 /* opaque_typedef */,
 				CD7048F220C48B19007C944C /* test */,