Browse Source

refactor: some other minor cleanups

Sam Jaffe 3 tháng trước cách đây
mục cha
commit
09576a604b

+ 7 - 3
include/jvalidate/detail/pointer.h

@@ -47,15 +47,19 @@ public:
     };
 
     path.remove_prefix(1);
-    size_t p = path.find('/');
-    for (; p != std::string::npos; path.remove_prefix(p + 1), p = path.find('/')) {
+    // The rules of JSON-Pointer is that if a token were to contain a '/' as a
+    // strict character: then that character would be escaped, using the above
+    // rules. We take advantage of string_view's sliding view to make iteration
+    // easy.
+    for (size_t p = path.find('/'); p != std::string::npos;
+         path.remove_prefix(p + 1), p = path.find('/')) {
       append_with_parse(std::string(path.substr(0, p)));
     }
 
     append_with_parse(std::string(path));
   }
 
-  template <Adapter A> A walk(A document) const {
+  auto walk(Adapter auto document) const {
     for (auto const & token : tokens_) {
       document = std::visit([&document](auto const & next) { return document[next]; }, token);
     }

+ 11 - 20
include/jvalidate/detail/reference.h

@@ -88,26 +88,17 @@ public:
   RootReference const & root() const { return root_; }
   Reference parent() const { return Reference(root_, pointer_.parent()); }
 
-  Reference & operator/=(Pointer const & relative) {
-    pointer_ /= relative;
-    return *this;
-  }
-
-  Reference operator/(Pointer const & relative) const { return Reference(*this) /= relative; }
-
-  Reference & operator/=(std::string_view key) {
-    pointer_ /= key;
-    return *this;
-  }
-
-  Reference operator/(std::string_view key) const { return Reference(*this) /= key; }
-
-  Reference & operator/=(size_t index) {
-    pointer_ /= index;
-    return *this;
-  }
-
-  Reference operator/(size_t index) const { return Reference(*this) /= index; }
+  /**
+   * @brief Delegate function for {@see Pointer::operator/=}, but returning
+   * this Reference.
+   */
+  Reference & operator/=(auto const & in) { return (pointer_ /= in, *this); }
+
+  /**
+   * @brief Delegate function for {@see Pointer::operator/}, but returning
+   * a Reference.
+   */
+  Reference operator/(auto const & in) const { return Reference(*this) /= in; }
 
   friend std::ostream & operator<<(std::ostream & os, Reference const & self) {
     return os << self.root_ << self.pointer_;

+ 3 - 1
include/jvalidate/detail/reference_manager.h

@@ -38,11 +38,13 @@ private:
   DocumentCache<A> & external_;
 
   ReferenceCache references_;
+
   std::map<schema::Version, Vocabulary<A>> vocabularies_;
   std::map<URI, Vocabulary<A>> user_vocabularies_;
+
   std::map<RootReference, A> roots_;
-  std::map<URI, std::map<Anchor, Reference>> dynamic_anchors_;
 
+  std::map<URI, std::map<Anchor, Reference>> dynamic_anchors_;
   DynamicReferenceContext active_dynamic_anchors_;
 
 public:

+ 6 - 5
include/jvalidate/detail/scoped_state.h

@@ -3,10 +3,11 @@
 #include <functional>
 #include <type_traits>
 
-#define CONCAT2(A, B) A##B
-#define CONCAT(A, B) CONCAT2(A, B)
+#define JVALIDATE_CONCAT2(A, B) A##B
+#define JVALIDATE_CONCAT(A, B) JVALIDATE_CONCAT2(A, B)
+
 #define scoped_state(prop, value)                                                                  \
-  auto CONCAT(scoped_state_, __LINE__) = detail::ScopedState(prop, value)
+  auto JVALIDATE_CONCAT(scoped_state_, __LINE__) = detail::ScopedState(prop, value)
 
 namespace jvalidate::detail {
 class ScopedState {
@@ -15,8 +16,8 @@ private:
 
 public:
   template <typename T, typename S>
-  requires(std::is_constructible_v<T, S>) ScopedState(T & prop, S value)
-      : reset_([reset = prop, &prop]() { prop = reset; }) {
+    requires(std::is_constructible_v<T, S>)
+  ScopedState(T & prop, S value) : reset_([reset = prop, &prop]() { prop = reset; }) {
     prop = std::move(value);
   }