Samuel Jaffe пре 9 година
комит
07c83b8644
3 измењених фајлова са 344 додато и 0 уклоњено
  1. 258 0
      case-matcher.xcodeproj/project.pbxproj
  2. 36 0
      match.cpp
  3. 50 0
      match.hpp

+ 258 - 0
case-matcher.xcodeproj/project.pbxproj

@@ -0,0 +1,258 @@
+// !$*UTF8*$!
+{
+	archiveVersion = 1;
+	classes = {
+	};
+	objectVersion = 46;
+	objects = {
+
+/* Begin PBXBuildFile section */
+		CD6E2CED1D84BF64002C1FB3 /* match.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD6E2CEB1D84BF64002C1FB3 /* match.cpp */; };
+		CD6E2CEE1D84BF64002C1FB3 /* match.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CD6E2CEC1D84BF64002C1FB3 /* match.hpp */; };
+/* End PBXBuildFile section */
+
+/* Begin PBXFileReference section */
+		CD6E2CDC1D84BF3C002C1FB3 /* libcase-matcher.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = "libcase-matcher.dylib"; sourceTree = BUILT_PRODUCTS_DIR; };
+		CD6E2CEB1D84BF64002C1FB3 /* match.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = match.cpp; sourceTree = "<group>"; };
+		CD6E2CEC1D84BF64002C1FB3 /* match.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = match.hpp; sourceTree = "<group>"; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+		CD6E2CD91D84BF3C002C1FB3 /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+		CD6E2CD31D84BF3C002C1FB3 = {
+			isa = PBXGroup;
+			children = (
+				CD6E2CEA1D84BF45002C1FB3 /* src */,
+				CD6E2CDD1D84BF3C002C1FB3 /* Products */,
+			);
+			sourceTree = "<group>";
+		};
+		CD6E2CDD1D84BF3C002C1FB3 /* Products */ = {
+			isa = PBXGroup;
+			children = (
+				CD6E2CDC1D84BF3C002C1FB3 /* libcase-matcher.dylib */,
+			);
+			name = Products;
+			sourceTree = "<group>";
+		};
+		CD6E2CEA1D84BF45002C1FB3 /* src */ = {
+			isa = PBXGroup;
+			children = (
+				CD6E2CEB1D84BF64002C1FB3 /* match.cpp */,
+				CD6E2CEC1D84BF64002C1FB3 /* match.hpp */,
+			);
+			name = src;
+			sourceTree = "<group>";
+		};
+/* End PBXGroup section */
+
+/* Begin PBXHeadersBuildPhase section */
+		CD6E2CDA1D84BF3C002C1FB3 /* Headers */ = {
+			isa = PBXHeadersBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CD6E2CEE1D84BF64002C1FB3 /* match.hpp in Headers */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXHeadersBuildPhase section */
+
+/* Begin PBXNativeTarget section */
+		CD6E2CDB1D84BF3C002C1FB3 /* case-matcher */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = CD6E2CE71D84BF3C002C1FB3 /* Build configuration list for PBXNativeTarget "case-matcher" */;
+			buildPhases = (
+				CD6E2CD81D84BF3C002C1FB3 /* Sources */,
+				CD6E2CD91D84BF3C002C1FB3 /* Frameworks */,
+				CD6E2CDA1D84BF3C002C1FB3 /* Headers */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = "case-matcher";
+			productName = "case-matcher";
+			productReference = CD6E2CDC1D84BF3C002C1FB3 /* libcase-matcher.dylib */;
+			productType = "com.apple.product-type.library.dynamic";
+		};
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+		CD6E2CD41D84BF3C002C1FB3 /* Project object */ = {
+			isa = PBXProject;
+			attributes = {
+				LastUpgradeCheck = 0720;
+				ORGANIZATIONNAME = "Sam Jaffe";
+				TargetAttributes = {
+					CD6E2CDB1D84BF3C002C1FB3 = {
+						CreatedOnToolsVersion = 7.2.1;
+					};
+				};
+			};
+			buildConfigurationList = CD6E2CD71D84BF3C002C1FB3 /* Build configuration list for PBXProject "case-matcher" */;
+			compatibilityVersion = "Xcode 3.2";
+			developmentRegion = English;
+			hasScannedForEncodings = 0;
+			knownRegions = (
+				en,
+			);
+			mainGroup = CD6E2CD31D84BF3C002C1FB3;
+			productRefGroup = CD6E2CDD1D84BF3C002C1FB3 /* Products */;
+			projectDirPath = "";
+			projectRoot = "";
+			targets = (
+				CD6E2CDB1D84BF3C002C1FB3 /* case-matcher */,
+			);
+		};
+/* End PBXProject section */
+
+/* Begin PBXSourcesBuildPhase section */
+		CD6E2CD81D84BF3C002C1FB3 /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CD6E2CED1D84BF64002C1FB3 /* match.cpp in Sources */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXSourcesBuildPhase section */
+
+/* Begin XCBuildConfiguration section */
+		CD6E2CE51D84BF3C002C1FB3 /* 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;
+		};
+		CD6E2CE61D84BF3C002C1FB3 /* 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;
+		};
+		CD6E2CE81D84BF3C002C1FB3 /* 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;
+		};
+		CD6E2CE91D84BF3C002C1FB3 /* 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 */
+		CD6E2CD71D84BF3C002C1FB3 /* Build configuration list for PBXProject "case-matcher" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD6E2CE51D84BF3C002C1FB3 /* Debug */,
+				CD6E2CE61D84BF3C002C1FB3 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+		CD6E2CE71D84BF3C002C1FB3 /* Build configuration list for PBXNativeTarget "case-matcher" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD6E2CE81D84BF3C002C1FB3 /* Debug */,
+				CD6E2CE91D84BF3C002C1FB3 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+		};
+/* End XCConfigurationList section */
+	};
+	rootObject = CD6E2CD41D84BF3C002C1FB3 /* Project object */;
+}

+ 36 - 0
match.cpp

@@ -0,0 +1,36 @@
+//
+//  match.cpp
+//  case-matcher
+//
+//  Created by Sam Jaffe on 9/10/16.
+//
+#include <iostream>
+
+#include "match.hpp"
+
+struct example {
+  example(long in) : i(in) {}
+  example(example const &) : i(0xDEADBEEF) {}
+  long i;
+};
+
+bool operator==(example const & a, example const & b) {
+  return a.i == b.i;
+}
+
+int main() {
+  example const ex{1};
+  bool b{true};
+  
+  match(ex, b) {
+    with(example{1}, false) {
+      std::cout << "Match 1" << std::endl;
+    }
+    with(example{0xDEADBEEF}, matcher::any) {
+      std::cout << "Match 2" << std::endl;
+    }
+    with(matcher::any, true) {
+      std::cout << "Match 3" << std::endl;
+    }
+  }
+}

+ 50 - 0
match.hpp

@@ -0,0 +1,50 @@
+//
+//  match.hpp
+//  case-matcher
+//
+//  Created by Sam Jaffe on 9/10/16.
+//
+
+#pragma once
+
+#include <tuple>
+
+namespace matcher {
+  struct {} any;
+  using any_t = decltype(any);
+  
+  template <typename T>
+  bool operator==(T const &, any_t) { return true; }
+  template <typename T>
+  bool operator==(any_t, T const &) { return true; }
+  
+  template <typename... Args>
+  struct matcher {
+  public:
+    matcher(Args &&... args) : value(std::forward<Args>(args)...) {}
+    
+    operator bool( ) const { return true; }
+    bool unmatched( ) const { return ! satisfied; }
+    
+    template <typename... NArgs>
+    bool matches(NArgs &&... args) const {
+      return value == std::tuple<NArgs...>(std::forward<NArgs>(args)...);
+    }
+  private:
+    std::tuple<Args...> value;
+    bool satisfied = false;
+  };
+  
+  template <typename... Args>
+  matcher<Args...> make_matcher(Args &&... args) {
+    return matcher<Args...>(std::forward<Args>(args)...);
+  }
+}
+
+#define match( ... ) \
+  if ( auto const & _matcher_local = \
+      ::matcher::make_matcher( __VA_ARGS__ ) )
+
+#define with( ... ) \
+  if ( _matcher_local.unmatched( ) && \
+      _matcher_local.matches( __VA_ARGS__ ) )