Ver código fonte

refactor: rename bound_number => limit

Sam Jaffe 2 anos atrás
pai
commit
2760378f96
4 arquivos alterados com 108 adições e 253 exclusões
  1. 0 71
      bound_number.hpp
  2. 85 0
      include/math/limit.h
  3. 21 180
      limit.xcodeproj/project.pbxproj
  4. 2 2
      bound_number.t.h

+ 0 - 71
bound_number.hpp

@@ -1,71 +0,0 @@
-//
-//  bound_number.hpp
-//  utilities
-//
-//  Created by Sam Jaffe on 10/22/13.
-//  Copyright (c) 2013 Sam Jaffe. All rights reserved.
-//
-
-#pragma once
-
-#include <stdexcept>
-
-struct {} check_bounds;
-using check_bounds_t = decltype(check_bounds);
-
-template <class Tp, Tp MINIMUM_VALUE, Tp MAXIMUM_VALUE>
-class bound_number final {
-private:
-public:
-  static_assert(MINIMUM_VALUE <= MAXIMUM_VALUE, "The minimum value must be less than or equal to the maximum");
-  
-  typedef Tp value_type;
-  static constexpr const value_type min = MINIMUM_VALUE;
-  static constexpr const value_type max = MAXIMUM_VALUE;
-  
-  bound_number() = default;
-  bound_number(const bound_number& other) = default;
-  bound_number(bound_number&& other) = default;
-  bound_number& operator=(const bound_number& other) = default;
-  bound_number& operator=(bound_number&& other) = default;
-  ~bound_number() = default;
-  
-  bound_number(Tp const & val) :
-  value(std::max(MINIMUM_VALUE, std::min(MAXIMUM_VALUE, val))) {
-  }
-  
-  bound_number(check_bounds_t, Tp const & val) :
-  value(val) {
-    if ( val < MINIMUM_VALUE || MAXIMUM_VALUE < val ) {
-      throw std::out_of_range{"Must construct a value within range"};
-    }
-  }
-  
-  bound_number& operator --() {
-    if (min < value) { --value; }
-    return *this;
-  }
-  
-  bound_number& operator ++() {
-    if (value < max) { ++value; }
-    return *this;
-  }
-  
-  bound_number operator --(int) {
-    bound_number tmp = *this;
-    operator--();
-    return tmp;
-  }
-  
-  bound_number operator ++(int) {
-    bound_number tmp = *this;
-    operator++();
-    return tmp;
-  }
-  
-  operator Tp() const {
-    return value;
-  }
-private:
-  Tp value;
-};

+ 85 - 0
include/math/limit.h

@@ -0,0 +1,85 @@
+//
+//  limit.h
+//  utilities
+//
+//  Created by Sam Jaffe on 10/22/13.
+//  Copyright (c) 2013 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <stdexcept>
+
+namespace math {
+
+struct {} assert_bounds;
+using AssertBounds = decltype(assert_bounds);
+
+template <typename T, T MINIMUM_VALUE, T MAXIMUM_VALUE>
+class Bound final {
+private:
+public:
+  static_assert(MINIMUM_VALUE <= MAXIMUM_VALUE,
+                "The minimum value must be less than or equal to the maximum");
+  
+  typedef Tp value_type;
+  static constexpr const value_type min = MINIMUM_VALUE;
+  static constexpr const value_type max = MAXIMUM_VALUE;
+  
+  constexpr Bound() = default;
+  constexpr Bound(value_type const & val) noexcept
+      : value(std::max(min, std::min(max, val))) {}
+  
+  Bound(AssertBounds, value_type val) : value(val) {
+    if (val < min || max < val) {
+      throw std::out_of_range{"Must construct a value within range"};
+    }
+  }
+  
+  Bound& operator-=(value_type v) { return *this = Bound(value - v); }
+  Bound& operator+=(value_type v) { return *this = Bound(value + v); }
+  Bound& operator--() { return *this -= 1; }
+  Bound& operator++() { return *this += 1; }
+  
+  Bound operator--(int) {
+    Bound tmp = *this;
+    operator--();
+    return tmp;
+  }
+  
+  Bound operator++(int) {
+    Bound tmp = *this;
+    operator++();
+    return tmp;
+  }
+  
+  operator value_type() const { return value; }
+  
+private:
+  value_type value{};
+};
+
+template <typename T, T MINMAX> using SymBound = Bound<T, -MINMAX, +MINMAX>;
+template <typename T, T VAL>
+using UniBound = std::conditional_t<0 <= VAL, Bound<T, 0, VAL>, Bound<T, VAL, 0>>;
+
+template <typename L, L l1, L h1, typename R, R l2, R h2>
+auto operator+(Bound<L, l1, h1> lhs, Bound<R, l2, h2> rhs) {
+  return L(lhs) + R(rhs);
+}
+
+template <typename L, L l1, L h1, typename R, R l2, R h2>
+auto operator-(Bound<L, l1, h1> lhs, Bound<R, l2, h2> rhs) {
+  return L(lhs) - R(rhs);
+}
+
+template <typename L, L l1, L h1, typename R, R l2, R h2>
+auto operator*(Bound<L, l1, h1> lhs, Bound<R, l2, h2> rhs) {
+  return L(lhs) * R(rhs);
+}
+
+template <typename L, L l1, L h1, typename R, R l2, R h2>
+auto operator/(Bound<L, l1, h1> lhs, Bound<R, l2, h2> rhs) {
+  return L(lhs) / R(rhs);
+}
+}

+ 21 - 180
limit.xcodeproj/project.pbxproj

@@ -6,44 +6,18 @@
 	objectVersion = 46;
 	objects = {
 
-/* Begin PBXBuildFile section */
-		CDFB746A1E451F94007D4841 /* limit_tc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDFB745D1E451F61007D4841 /* limit_tc.cpp */; };
-/* End PBXBuildFile section */
-
-/* Begin PBXCopyFilesBuildPhase section */
-		CDFB74611E451F72007D4841 /* CopyFiles */ = {
-			isa = PBXCopyFilesBuildPhase;
-			buildActionMask = 2147483647;
-			dstPath = /usr/share/man/man1/;
-			dstSubfolderSpec = 0;
-			files = (
-			);
-			runOnlyForDeploymentPostprocessing = 1;
-		};
-/* End PBXCopyFilesBuildPhase section */
-
 /* Begin PBXFileReference section */
-		0E5DFDCB1BB4D3360063976E /* bound_number.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = bound_number.hpp; sourceTree = "<group>"; };
-		CDFB745D1E451F61007D4841 /* limit_tc.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = limit_tc.cpp; sourceTree = "<group>"; };
-		CDFB745E1E451F61007D4841 /* bound_number.t.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = bound_number.t.h; sourceTree = "<group>"; };
-		CDFB74631E451F72007D4841 /* limit_tc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = limit_tc; sourceTree = BUILT_PRODUCTS_DIR; };
+		CDDAA47C28655E6300D5EDD4 /* math */ = {isa = PBXFileReference; lastKnownFileType = folder; name = math; path = include/math; sourceTree = "<group>"; };
+		CDDAA47D28655E8F00D5EDD4 /* limit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = limit.h; sourceTree = "<group>"; };
+		CDFB745E1E451F61007D4841 /* limit_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = limit_test.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
-/* Begin PBXFrameworksBuildPhase section */
-		CDFB74601E451F72007D4841 /* Frameworks */ = {
-			isa = PBXFrameworksBuildPhase;
-			buildActionMask = 2147483647;
-			files = (
-			);
-			runOnlyForDeploymentPostprocessing = 0;
-		};
-/* End PBXFrameworksBuildPhase section */
-
 /* Begin PBXGroup section */
 		0E5DFDC51BB4D3360063976E = {
 			isa = PBXGroup;
 			children = (
-				CDFB745B1E451F42007D4841 /* src */,
+				CDDAA47C28655E6300D5EDD4 /* math */,
+				CDDAA47928655E5C00D5EDD4 /* include */,
 				CDFB745C1E451F45007D4841 /* test */,
 				0E5DFE1C1BB4DB740063976E /* Products */,
 			);
@@ -52,61 +26,41 @@
 		0E5DFE1C1BB4DB740063976E /* Products */ = {
 			isa = PBXGroup;
 			children = (
-				CDFB74631E451F72007D4841 /* limit_tc */,
 			);
 			name = Products;
 			sourceTree = "<group>";
 		};
-		CDFB745B1E451F42007D4841 /* src */ = {
+		CDDAA47928655E5C00D5EDD4 /* include */ = {
 			isa = PBXGroup;
 			children = (
-				0E5DFDCB1BB4D3360063976E /* bound_number.hpp */,
+				CDDAA47A28655E5C00D5EDD4 /* math */,
 			);
-			name = src;
+			path = include;
 			sourceTree = "<group>";
 		};
-		CDFB745C1E451F45007D4841 /* test */ = {
+		CDDAA47A28655E5C00D5EDD4 /* math */ = {
 			isa = PBXGroup;
 			children = (
-				CDFB745E1E451F61007D4841 /* bound_number.t.h */,
-				CDFB745D1E451F61007D4841 /* limit_tc.cpp */,
+				CDDAA47D28655E8F00D5EDD4 /* limit.h */,
 			);
-			name = test;
+			path = math;
 			sourceTree = "<group>";
 		};
-/* End PBXGroup section */
-
-/* Begin PBXNativeTarget section */
-		CDFB74621E451F72007D4841 /* limit_tc */ = {
-			isa = PBXNativeTarget;
-			buildConfigurationList = CDFB74671E451F72007D4841 /* Build configuration list for PBXNativeTarget "limit_tc" */;
-			buildPhases = (
-				CDFB746B1E451FAB007D4841 /* ShellScript */,
-				CDFB745F1E451F72007D4841 /* Sources */,
-				CDFB74601E451F72007D4841 /* Frameworks */,
-				CDFB74611E451F72007D4841 /* CopyFiles */,
-			);
-			buildRules = (
-			);
-			dependencies = (
+		CDFB745C1E451F45007D4841 /* test */ = {
+			isa = PBXGroup;
+			children = (
+				CDFB745E1E451F61007D4841 /* limit_test.cxx */,
 			);
-			name = limit_tc;
-			productName = limit_tc;
-			productReference = CDFB74631E451F72007D4841 /* limit_tc */;
-			productType = "com.apple.product-type.tool";
+			path = test;
+			sourceTree = "<group>";
 		};
-/* End PBXNativeTarget section */
+/* End PBXGroup section */
 
 /* Begin PBXProject section */
 		0E5DFDC61BB4D3360063976E /* Project object */ = {
 			isa = PBXProject;
 			attributes = {
-				LastUpgradeCheck = 1030;
-				TargetAttributes = {
-					CDFB74621E451F72007D4841 = {
-						CreatedOnToolsVersion = 7.2.1;
-					};
-				};
+				LastUpgradeCheck = 1230;
 			};
 			buildConfigurationList = 0E5DFDC91BB4D3360063976E /* Build configuration list for PBXProject "limit" */;
 			compatibilityVersion = "Xcode 3.2";
@@ -121,40 +75,10 @@
 			projectDirPath = "";
 			projectRoot = "";
 			targets = (
-				CDFB74621E451F72007D4841 /* limit_tc */,
 			);
 		};
 /* End PBXProject section */
 
-/* Begin PBXShellScriptBuildPhase section */
-		CDFB746B1E451FAB007D4841 /* ShellScript */ = {
-			isa = PBXShellScriptBuildPhase;
-			buildActionMask = 2147483647;
-			files = (
-			);
-			inputPaths = (
-				"$(SRCROOT)/bound_number.t.h",
-			);
-			outputPaths = (
-				"$(SRCROOT)/limit_tc.cpp",
-			);
-			runOnlyForDeploymentPostprocessing = 0;
-			shellPath = /bin/sh;
-			shellScript = "cxxtestgen --error-printer -o limit_tc.cpp bound_number.t.h";
-		};
-/* End PBXShellScriptBuildPhase section */
-
-/* Begin PBXSourcesBuildPhase section */
-		CDFB745F1E451F72007D4841 /* Sources */ = {
-			isa = PBXSourcesBuildPhase;
-			buildActionMask = 2147483647;
-			files = (
-				CDFB746A1E451F94007D4841 /* limit_tc.cpp in Sources */,
-			);
-			runOnlyForDeploymentPostprocessing = 0;
-		};
-/* End PBXSourcesBuildPhase section */
-
 /* Begin XCBuildConfiguration section */
 		0E5DFDC71BB4D3360063976E /* Debug */ = {
 			isa = XCBuildConfiguration;
@@ -172,6 +96,7 @@
 				CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
 				CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
 				CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
+				CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
 				CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
 				CLANG_WARN_STRICT_PROTOTYPES = YES;
 				CLANG_WARN_SUSPICIOUS_MOVE = YES;
@@ -208,6 +133,7 @@
 				CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
 				CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
 				CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
+				CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
 				CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
 				CLANG_WARN_STRICT_PROTOTYPES = YES;
 				CLANG_WARN_SUSPICIOUS_MOVE = YES;
@@ -226,82 +152,6 @@
 			};
 			name = Release;
 		};
-		CDFB74681E451F72007D4841 /* 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 = "-";
-				DEBUG_INFORMATION_FORMAT = dwarf;
-				ENABLE_STRICT_OBJC_MSGSEND = 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;
-				HEADER_SEARCH_PATHS = /usr/local/include/;
-				MACOSX_DEPLOYMENT_TARGET = 10.10;
-				MTL_ENABLE_DEBUG_INFO = YES;
-				PRODUCT_NAME = "$(TARGET_NAME)";
-			};
-			name = Debug;
-		};
-		CDFB74691E451F72007D4841 /* 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;
-				HEADER_SEARCH_PATHS = /usr/local/include/;
-				MACOSX_DEPLOYMENT_TARGET = 10.10;
-				MTL_ENABLE_DEBUG_INFO = NO;
-				PRODUCT_NAME = "$(TARGET_NAME)";
-			};
-			name = Release;
-		};
 /* End XCBuildConfiguration section */
 
 /* Begin XCConfigurationList section */
@@ -314,15 +164,6 @@
 			defaultConfigurationIsVisible = 0;
 			defaultConfigurationName = Release;
 		};
-		CDFB74671E451F72007D4841 /* Build configuration list for PBXNativeTarget "limit_tc" */ = {
-			isa = XCConfigurationList;
-			buildConfigurations = (
-				CDFB74681E451F72007D4841 /* Debug */,
-				CDFB74691E451F72007D4841 /* Release */,
-			);
-			defaultConfigurationIsVisible = 0;
-			defaultConfigurationName = Release;
-		};
 /* End XCConfigurationList section */
 	};
 	rootObject = 0E5DFDC61BB4D3360063976E /* Project object */;

+ 2 - 2
bound_number.t.h

@@ -1,12 +1,12 @@
 //
-//  bound_number.t.h
+//  limit_test.cxx
 //  limit
 //
 //  Created by Sam Jaffe on 2/3/17.
 //
 #pragma once
 
-#include "bound_number.hpp"
+#include "math/limit.h"
 
 #include <cxxtest/TestSuite.h>