Pārlūkot izejas kodu

fix: permit the default-construction of filter iterators with lambdas

Sam Jaffe 2 gadi atpakaļ
vecāks
revīzija
5ee7b843d7

+ 42 - 0
include/iterator/detail/default_constructible_function_proxy.h

@@ -0,0 +1,42 @@
+//
+//  default_constructible_function_proxy.h
+//  iterator
+//
+//  Created by Sam Jaffe on 4/1/23.
+//  Copyright © 2023 Sam Jaffe. All rights reserved.
+//
+
+#include <optional>include/iterator/detail/default_constructible_function_proxy.h
+
+namespace iterator::detail {
+template <typename F, typename = void>
+class default_constructible_function_proxy {
+public:
+  default_constructible_function_proxy() = default;
+  default_constructible_function_proxy(F func) : func_(func) {}
+
+  template <typename... Args> decltype(auto) operator()(Args &&... args) const {
+    if (!func_) { throw; }
+    return std::invoke(*func_, std::forward<Args>(args)...);
+  }
+
+private:
+  std::optional<F> func_;
+};
+
+template <typename F>
+class default_constructible_function_proxy<
+    F, std::enable_if_t<std::is_default_constructible_v<F>>> {
+
+public:
+  default_constructible_function_proxy() = default;
+  default_constructible_function_proxy(F func) : func_(func) {}
+
+  template <typename... Args> decltype(auto) operator()(Args &&... args) const {
+    return std::invoke(func_, std::forward<Args>(args)...);
+  }
+
+private:
+  F func_;
+};
+}

+ 2 - 1
include/iterator/filter_iterator.h

@@ -9,6 +9,7 @@
 
 #include <functional>
 
+#include <iterator/detail/default_constructible_function_proxy.h>
 #include <iterator/detail/traits.h>
 #include <iterator/end_aware_iterator.h>
 #include <iterator/facade.h>
@@ -57,7 +58,7 @@ public:
   }
 
   end_aware_iterator<Iter> base_;
-  Pred pred_;
+  detail::default_constructible_function_proxy<Pred> pred_;
 };
 
 template <typename C, typename P>

+ 2 - 0
iterator.xcodeproj/project.pbxproj

@@ -71,6 +71,7 @@
 		CD5AEB3329D8956600A390A4 /* capture_iterator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = capture_iterator.h; sourceTree = "<group>"; };
 		CD5AEB3429D897DD00A390A4 /* capture_iterator_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = capture_iterator_test.cxx; sourceTree = "<group>"; };
 		CD6EBE2229D5C93A00F387C1 /* sentinel_iterator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = sentinel_iterator.h; sourceTree = "<group>"; };
+		CD8D0C7B29D8C0BF00443256 /* default_constructible_function_proxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = default_constructible_function_proxy.h; sourceTree = "<group>"; };
 		CDA2B60928581255004D5353 /* libiterator.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libiterator.a; sourceTree = BUILT_PRODUCTS_DIR; };
 		CDA2B6122858128C004D5353 /* forwards.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = forwards.h; sourceTree = "<group>"; };
 		CDA2B6132858128C004D5353 /* facade.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = facade.h; sourceTree = "<group>"; };
@@ -197,6 +198,7 @@
 			isa = PBXGroup;
 			children = (
 				CDA2B61A2858128C004D5353 /* arrow_proxy.h */,
+				CD8D0C7B29D8C0BF00443256 /* default_constructible_function_proxy.h */,
 				CD5AEAE829D86DCD00A390A4 /* facade_traits.h */,
 				CD5AEB3129D8885400A390A4 /* macro.h */,
 				CDA2B6182858128C004D5353 /* recursive_traits.h */,