Explorar o código

Creating a library to represent opaque typedefs. Opaque typedefs introduce type-safety that typedef/using does not, because it is not an alias.

Imaging the following:
using coordinate_t = std::pair<int, int>;
using distance_t = std::pair<int, int>;

coordinate_t move( coordinate_t pos, distance_t step ) { ... }

void test_move_coord_dist( ) {
  coordinate_t pos{0,0};
  distance_t step{5,5};
  TEST_ASSERT_EQUALS( coordinate_t(5,5), move(step, pos) ); // Compiles, PASSES
}
// Because coordinate_t and distance_t are simply type aliases,
// move is actually a function 'move :: pair<int, int> -> pair<int, int> -> pair<int, int>'
// so reversing the order that I add them compiles anyway. The test case
// even passes in certain examples, where only commutative operations are performed.
// On the other hand, if I did:
struct coordinate_t : public opaque_typedef< std::pair<int, int> > {
  using opaque_typedef< std::pair<int, int> >::explicit_opaque_typedef;
}; // and the same for distance_t
// Then 'move(step, pos)' will no longer compile because coordinate_t and
// distance_t are different (incompatible) types

TODO: Provide std::hash/std::equal_to/std::less overloads?
Samuel Jaffe %!s(int64=9) %!d(string=hai) anos
achega
096d74b792
Modificáronse 2 ficheiros con 304 adicións e 0 borrados
  1. 48 0
      opaque_typedef.hpp
  2. 256 0
      opaque_typedef.xcodeproj/project.pbxproj

+ 48 - 0
opaque_typedef.hpp

@@ -0,0 +1,48 @@
+//
+//  opaque_typedef.hpp
+//  opaque-type
+//
+//  Created by Sam Jaffe on 8/16/16.
+//
+
+#pragma once
+
+#define OPAQUE_TYPE_OPERATOR(name, op) \
+  template <typename R, typename O1 = R, typename O2 = R> struct name; \
+  \
+  template <typename R, typename O1, typename O2> \
+  struct name { \
+    friend R operator op(O1 const & lhs, O2 const & rhs) { \
+      return R(lhs.value() op rhs.value()); \
+    } \
+  }; \
+  \
+  template <typename R, typename O2> \
+  struct name<R, R, O2> { \
+    friend R & operator op##=(R & lhs, O2 const & rhs) { \
+      return lhs = R(lhs.value() op rhs.value()); \
+    } \
+    \
+    friend R operator op(R const & lhs, O2 const & rhs) { \
+      return R(lhs.value() op rhs.value()); \
+    } \
+  };
+
+OPAQUE_TYPE_OPERATOR(addition, +)
+OPAQUE_TYPE_OPERATOR(subtraction, -)
+OPAQUE_TYPE_OPERATOR(multiplication, *)
+OPAQUE_TYPE_OPERATOR(division, /)
+
+#undef OPAQUE_TYPE_OPERATOR
+
+template <typename T>
+class opaque_typedef {
+public:
+  opaque_typedef() : _value() {}
+  explicit opaque_typedef(T val) : _value(val) {}
+  explicit operator T() const { return _value; }
+  
+  T value() const { return _value; }
+private:
+  T _value;
+};

+ 256 - 0
opaque_typedef.xcodeproj/project.pbxproj

@@ -0,0 +1,256 @@
+// !$*UTF8*$!
+{
+	archiveVersion = 1;
+	classes = {
+	};
+	objectVersion = 46;
+	objects = {
+
+/* Begin PBXBuildFile section */
+		CD3C80781D63F2CC00ACC795 /* opaque_typedef.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3C80771D63F2CC00ACC795 /* opaque_typedef.cpp */; };
+/* End PBXBuildFile section */
+
+/* Begin PBXFileReference section */
+		CD3C80681D63EE0000ACC795 /* libopaque_typedef.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = "libopaque_typedef.dylib"; sourceTree = BUILT_PRODUCTS_DIR; };
+		CD3C80761D63EE7800ACC795 /* opaque_typedef.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = opaque_typedef.hpp; sourceTree = "<group>"; };
+		CD3C80771D63F2CC00ACC795 /* opaque_typedef.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = opaque_typedef.cpp; sourceTree = "<group>"; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+		CD3C80651D63EE0000ACC795 /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+		CD3C805F1D63EE0000ACC795 = {
+			isa = PBXGroup;
+			children = (
+				CD3C806A1D63EE0000ACC795 /* src */,
+				CD3C80691D63EE0000ACC795 /* Products */,
+			);
+			sourceTree = "<group>";
+		};
+		CD3C80691D63EE0000ACC795 /* Products */ = {
+			isa = PBXGroup;
+			children = (
+				CD3C80681D63EE0000ACC795 /* libopaque_typedef.dylib */,
+			);
+			name = Products;
+			sourceTree = "<group>";
+		};
+		CD3C806A1D63EE0000ACC795 /* src */ = {
+			isa = PBXGroup;
+			children = (
+				CD3C80761D63EE7800ACC795 /* opaque_typedef.hpp */,
+				CD3C80771D63F2CC00ACC795 /* opaque_typedef.cpp */,
+			);
+			name = src;
+			sourceTree = "<group>";
+		};
+/* End PBXGroup section */
+
+/* Begin PBXHeadersBuildPhase section */
+		CD3C80661D63EE0000ACC795 /* Headers */ = {
+			isa = PBXHeadersBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXHeadersBuildPhase section */
+
+/* Begin PBXNativeTarget section */
+		CD3C80671D63EE0000ACC795 /* opaque_typedef */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = CD3C80731D63EE0000ACC795 /* Build configuration list for PBXNativeTarget "opaque_typedef" */;
+			buildPhases = (
+				CD3C80641D63EE0000ACC795 /* Sources */,
+				CD3C80651D63EE0000ACC795 /* Frameworks */,
+				CD3C80661D63EE0000ACC795 /* Headers */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = "opaque_typedef";
+			productName = "opaque_typedef";
+			productReference = CD3C80681D63EE0000ACC795 /* libopaque_typedef.dylib */;
+			productType = "com.apple.product-type.library.dynamic";
+		};
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+		CD3C80601D63EE0000ACC795 /* Project object */ = {
+			isa = PBXProject;
+			attributes = {
+				LastUpgradeCheck = 0720;
+				ORGANIZATIONNAME = "Sam Jaffe";
+				TargetAttributes = {
+					CD3C80671D63EE0000ACC795 = {
+						CreatedOnToolsVersion = 7.2.1;
+					};
+				};
+			};
+			buildConfigurationList = CD3C80631D63EE0000ACC795 /* Build configuration list for PBXProject "opaque_typedef" */;
+			compatibilityVersion = "Xcode 3.2";
+			developmentRegion = English;
+			hasScannedForEncodings = 0;
+			knownRegions = (
+				en,
+			);
+			mainGroup = CD3C805F1D63EE0000ACC795;
+			productRefGroup = CD3C80691D63EE0000ACC795 /* Products */;
+			projectDirPath = "";
+			projectRoot = "";
+			targets = (
+				CD3C80671D63EE0000ACC795 /* opaque_typedef */,
+			);
+		};
+/* End PBXProject section */
+
+/* Begin PBXSourcesBuildPhase section */
+		CD3C80641D63EE0000ACC795 /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CD3C80781D63F2CC00ACC795 /* opaque_typedef.cpp in Sources */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXSourcesBuildPhase section */
+
+/* Begin XCBuildConfiguration section */
+		CD3C80711D63EE0000ACC795 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				CODE_SIGN_IDENTITY = "-";
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = dwarf;
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				ENABLE_TESTABILITY = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu99;
+				GCC_DYNAMIC_NO_PIC = NO;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_OPTIMIZATION_LEVEL = 0;
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"DEBUG=1",
+					"$(inherited)",
+				);
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				MACOSX_DEPLOYMENT_TARGET = 10.10;
+				MTL_ENABLE_DEBUG_INFO = YES;
+				ONLY_ACTIVE_ARCH = YES;
+				SDKROOT = macosx;
+			};
+			name = Debug;
+		};
+		CD3C80721D63EE0000ACC795 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				CODE_SIGN_IDENTITY = "-";
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+				ENABLE_NS_ASSERTIONS = NO;
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu99;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				MACOSX_DEPLOYMENT_TARGET = 10.10;
+				MTL_ENABLE_DEBUG_INFO = NO;
+				SDKROOT = macosx;
+			};
+			name = Release;
+		};
+		CD3C80741D63EE0000ACC795 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				DYLIB_COMPATIBILITY_VERSION = 1;
+				DYLIB_CURRENT_VERSION = 1;
+				EXECUTABLE_PREFIX = lib;
+				GCC_ENABLE_CPP_EXCEPTIONS = YES;
+				GCC_ENABLE_CPP_RTTI = YES;
+				GCC_SYMBOLS_PRIVATE_EXTERN = YES;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Debug;
+		};
+		CD3C80751D63EE0000ACC795 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				DYLIB_COMPATIBILITY_VERSION = 1;
+				DYLIB_CURRENT_VERSION = 1;
+				EXECUTABLE_PREFIX = lib;
+				GCC_ENABLE_CPP_EXCEPTIONS = YES;
+				GCC_ENABLE_CPP_RTTI = YES;
+				GCC_SYMBOLS_PRIVATE_EXTERN = YES;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Release;
+		};
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+		CD3C80631D63EE0000ACC795 /* Build configuration list for PBXProject "opaque_typedef" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD3C80711D63EE0000ACC795 /* Debug */,
+				CD3C80721D63EE0000ACC795 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+		CD3C80731D63EE0000ACC795 /* Build configuration list for PBXNativeTarget "opaque_typedef" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD3C80741D63EE0000ACC795 /* Debug */,
+				CD3C80751D63EE0000ACC795 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+		};
+/* End XCConfigurationList section */
+	};
+	rootObject = CD3C80601D63EE0000ACC795 /* Project object */;
+}