瀏覽代碼

refactor: use scope_guard instead of custom deleter.

refactor: add Random(Random, Tape) ctor.
Sam Jaffe 2 年之前
父節點
當前提交
d63343b5bc
共有 6 個文件被更改,包括 79 次插入6 次删除
  1. 3 0
      .gitmodules
  2. 1 0
      external/scope_guard
  3. 2 0
      include/random/forwards.h
  4. 3 6
      include/random/random.h
  5. 56 0
      shared_random_generator.xcodeproj/project.pbxproj
  6. 14 0
      src/random.cxx

+ 3 - 0
.gitmodules

@@ -1,3 +1,6 @@
 [submodule "external/expect"]
 	path = external/expect
 	url = ssh://git@gogs.sjaffe.name:3000/sjjaffe/cpp-expect.git
+[submodule "external/scope_guard"]
+	path = external/scope_guard
+	url = ssh://git@gogs.sjaffe.name:3000/sjjaffe/cpp-scope-guard.git

+ 1 - 0
external/scope_guard

@@ -0,0 +1 @@
+Subproject commit dda916e01f91e13a91e20f5628bfe389d557d246

+ 2 - 0
include/random/forwards.h

@@ -8,6 +8,8 @@
 
 #pragma once
 
+class scope_exit;
+
 namespace engine::random {
 class Device;
 class Random;

+ 3 - 6
include/random/random.h

@@ -13,6 +13,8 @@ private:
 
 public:
   Random();
+  Random(Random const &other, Tape & with_tape);
+  Random(Random const &other, std::shared_ptr<Tape> with_tape);
   template <typename P> Random(P const & p) : impl_(std::make_shared<P>(p)) {}
   template <typename P> Random(std::shared_ptr<P> const & p) : impl_(p) {}
   
@@ -28,11 +30,6 @@ public:
    * @param tape The tape to write to
    * @return A scope object that stops recording once it is descructed
    */
-  auto record(std::shared_ptr<Tape> tape) {
-    assert(tape && !tape_);
-    tape_ = tape;
-    auto cleanup = [this](void *) { tape_.reset(); };
-    return std::unique_ptr<void, decltype(cleanup)>(this, cleanup);
-  }
+  scope_exit record(std::shared_ptr<Tape> tape);
 };
 }

+ 56 - 0
shared_random_generator.xcodeproj/project.pbxproj

@@ -75,6 +75,27 @@
 			remoteGlobalIDString = CDD476BC29C5423B00BDB829;
 			remoteInfo = expect;
 		};
+		CDD2169229CD0B1500A4582C /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = CDD2168729CD0B1500A4582C /* scope_guard.xcodeproj */;
+			proxyType = 2;
+			remoteGlobalIDString = CDD215D129CCFA1E00A4582C;
+			remoteInfo = scope_guard;
+		};
+		CDD2169429CD0B1500A4582C /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = CDD2168729CD0B1500A4582C /* scope_guard.xcodeproj */;
+			proxyType = 2;
+			remoteGlobalIDString = CDD215EA29CCFA5000A4582C;
+			remoteInfo = "scope_guard-test";
+		};
+		CDD2169629CD0B1D00A4582C /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = CDD2168729CD0B1500A4582C /* scope_guard.xcodeproj */;
+			proxyType = 1;
+			remoteGlobalIDString = CDD215D029CCFA1E00A4582C;
+			remoteInfo = scope_guard;
+		};
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
@@ -85,6 +106,7 @@
 		CDD2137C29C76E5600A4582C /* xcode_gtest_helper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xcode_gtest_helper.h; sourceTree = "<group>"; };
 		CDD2138129C76EF500A4582C /* tape_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = tape_test.cxx; sourceTree = "<group>"; };
 		CDD2138329C7723700A4582C /* expect.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = expect.xcodeproj; path = external/expect/expect.xcodeproj; sourceTree = "<group>"; };
+		CDD2168729CD0B1500A4582C /* scope_guard.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = scope_guard.xcodeproj; path = external/scope_guard/scope_guard.xcodeproj; sourceTree = "<group>"; };
 		CDE943B329C75C610086A8CA /* device.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = device.h; sourceTree = "<group>"; };
 		CDE943B429C75C610086A8CA /* random.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = random.h; sourceTree = "<group>"; };
 		CDE943B529C75CD70086A8CA /* forwards.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = forwards.h; sourceTree = "<group>"; };
@@ -152,6 +174,15 @@
 			name = Products;
 			sourceTree = "<group>";
 		};
+		CDD2168829CD0B1500A4582C /* Products */ = {
+			isa = PBXGroup;
+			children = (
+				CDD2169329CD0B1500A4582C /* libscope_guard.a */,
+				CDD2169529CD0B1500A4582C /* scope_guard-test.xctest */,
+			);
+			name = Products;
+			sourceTree = "<group>";
+		};
 		CDE943AD29C75C250086A8CA /* include */ = {
 			isa = PBXGroup;
 			children = (
@@ -177,6 +208,7 @@
 			children = (
 				CD89E50024E6F3E9008167A8 /* GoogleMock.xcodeproj */,
 				CDD2138329C7723700A4582C /* expect.xcodeproj */,
+				CDD2168729CD0B1500A4582C /* scope_guard.xcodeproj */,
 				CDED6A5D21B2F76700AB91D0 /* random */,
 				CDE943AD29C75C250086A8CA /* include */,
 				CDED6A4921B2F5DF00AB91D0 /* src */,
@@ -260,6 +292,7 @@
 			buildRules = (
 			);
 			dependencies = (
+				CDD2169729CD0B1D00A4582C /* PBXTargetDependency */,
 				CDD2138E29C7724200A4582C /* PBXTargetDependency */,
 			);
 			name = shared_random_generator;
@@ -304,6 +337,10 @@
 					ProductGroup = CD89E50124E6F3E9008167A8 /* Products */;
 					ProjectRef = CD89E50024E6F3E9008167A8 /* GoogleMock.xcodeproj */;
 				},
+				{
+					ProductGroup = CDD2168829CD0B1500A4582C /* Products */;
+					ProjectRef = CDD2168729CD0B1500A4582C /* scope_guard.xcodeproj */;
+				},
 			);
 			projectRoot = "";
 			targets = (
@@ -356,6 +393,20 @@
 			remoteRef = CDD2138B29C7723700A4582C /* PBXContainerItemProxy */;
 			sourceTree = BUILT_PRODUCTS_DIR;
 		};
+		CDD2169329CD0B1500A4582C /* libscope_guard.a */ = {
+			isa = PBXReferenceProxy;
+			fileType = archive.ar;
+			path = libscope_guard.a;
+			remoteRef = CDD2169229CD0B1500A4582C /* PBXContainerItemProxy */;
+			sourceTree = BUILT_PRODUCTS_DIR;
+		};
+		CDD2169529CD0B1500A4582C /* scope_guard-test.xctest */ = {
+			isa = PBXReferenceProxy;
+			fileType = wrapper.cfbundle;
+			path = "scope_guard-test.xctest";
+			remoteRef = CDD2169429CD0B1500A4582C /* PBXContainerItemProxy */;
+			sourceTree = BUILT_PRODUCTS_DIR;
+		};
 /* End PBXReferenceProxy section */
 
 /* Begin PBXResourcesBuildPhase section */
@@ -401,6 +452,11 @@
 			name = expect;
 			targetProxy = CDD2138D29C7724200A4582C /* PBXContainerItemProxy */;
 		};
+		CDD2169729CD0B1D00A4582C /* PBXTargetDependency */ = {
+			isa = PBXTargetDependency;
+			name = scope_guard;
+			targetProxy = CDD2169629CD0B1D00A4582C /* PBXContainerItemProxy */;
+		};
 /* End PBXTargetDependency section */
 
 /* Begin XCBuildConfiguration section */

+ 14 - 0
src/random.cxx

@@ -4,6 +4,8 @@
 #include <cfloat>
 #include <random>
 
+#include <scope_guard/scope_guard.hpp>
+
 #include "random/device.h"
 #include "random/tape.h"
 
@@ -42,6 +44,12 @@ private:
 Random::Random()
     : impl_(std::make_shared<DefaultDevice>()) {}
 
+Random::Random(Random const &other, std::shared_ptr<Tape> with_tape)
+    : impl_(other.impl_), tape_(with_tape) {}
+
+Random::Random(Random const &other, Tape & with_tape)
+    : impl_(other.impl_), tape_(&with_tape, [](void*){}) {}
+
 unsigned int Random::exclusive(unsigned int max) const {
   assert(max != 0);
   return EVALUATE(exclusive, max);
@@ -54,4 +62,10 @@ double Random::exclusive(double min, double max) const {
 double Random::inclusive(double min, double max) const {
   return EVALUATE(inclusive, min, max);
 }
+
+scope_exit Random::record(std::shared_ptr<Tape> tape) {
+  assert(tape && !tape_);
+  tape_ = tape;
+  return [this]() { tape_.reset(); };
+}
 }