Selaa lähdekoodia

refactor: move formatting helpers into detail/iostream.h

Sam Jaffe 2 viikkoa sitten
vanhempi
commit
f050c8693c
2 muutettua tiedostoa jossa 30 lisäystä ja 30 poistoa
  1. 27 0
      include/jvalidate/detail/iostream.h
  2. 3 30
      include/jvalidate/validation_visitor.h

+ 27 - 0
include/jvalidate/detail/iostream.h

@@ -2,6 +2,7 @@
 
 #include <iostream>
 #include <set>
+#include <sstream>
 #include <unordered_set>
 
 #include <jvalidate/enum.h>
@@ -83,4 +84,30 @@ inline std::ostream & operator<<(std::ostream & os, std::unordered_set<T> const
   }
   return os << ' ' << ']';
 }
+
+template <typename S>
+  requires(std::is_constructible_v<std::string, S>)
+// Optimization to avoid running string-like objects through a
+// std::stringstream in fmtlist.
+static std::string to_string(S const & str) {
+  return std::string(str);
+}
+
+// Format va_args into a single string to annotate or mark an error message
+static std::string to_string(auto const &... args) {
+  std::stringstream ss;
+  using ::jvalidate::operator<<;
+  [[maybe_unused]] int _[] = {(ss << args, 0)...};
+  return ss.str();
+}
+
+// Format an iterable argument into a vector of strings to annotate or mark
+// an error.
+static std::vector<std::string> to_string_list(auto const & arg) {
+  std::vector<std::string> strs;
+  for (auto const & elem : arg) {
+    strs.push_back(::jvalidate::to_string(elem));
+  }
+  return strs;
+}
 }

+ 3 - 30
include/jvalidate/validation_visitor.h

@@ -738,9 +738,9 @@ public:
     return detail::ScopedState(tracking_, StoreResults::ForAnything);
   }
 
-  ANNOTATION_HELPER(error, error, fmt)
-  ANNOTATION_HELPER(annotate, annotate, fmt)
-  ANNOTATION_HELPER(annotate_list, annotate, fmtlist)
+  ANNOTATION_HELPER(error, error, jvalidate::to_string)
+  ANNOTATION_HELPER(annotate, annotate, jvalidate::to_string)
+  ANNOTATION_HELPER(annotate_list, annotate, jvalidate::to_string_list)
 
   bool should_annotate(Status stat) const {
     if (not result_) {
@@ -851,33 +851,6 @@ public:
     }
     return rval;
   }
-
-private:
-  template <typename S>
-    requires(std::is_constructible_v<std::string, S>)
-  // Optimization to avoid running string-like objects through a
-  // std::stringstream in fmtlist.
-  static std::string fmt(S const & str) {
-    return std::string(str);
-  }
-
-  // Format va_args into a single string to annotate or mark an error message
-  static std::string fmt(auto const &... args) {
-    std::stringstream ss;
-    using ::jvalidate::operator<<;
-    [[maybe_unused]] int _[] = {(ss << args, 0)...};
-    return ss.str();
-  }
-
-  // Format an iterable argument into a vector of strings to annotate or mark
-  // an error.
-  static std::vector<std::string> fmtlist(auto const & arg) {
-    std::vector<std::string> strs;
-    for (auto const & elem : arg) {
-      strs.push_back(fmt(elem));
-    }
-    return strs;
-  }
 };
 }