Pārlūkot izejas kodu

refactor: use string_utils for string parsing

Sam Jaffe 3 gadi atpakaļ
vecāks
revīzija
a733f9fbb5

+ 36 - 0
cli.xcodeproj/project.pbxproj

@@ -12,6 +12,11 @@
 		CDC748E725313CBC008D9D1D /* cli_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDC748E625313CBC008D9D1D /* cli_test.cxx */; };
 		CDC748E925313D81008D9D1D /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CDC748B425313553008D9D1D /* GoogleMock.framework */; };
 		CDC748F4253141AF008D9D1D /* libstring-utils.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CDC748F125314196008D9D1D /* libstring-utils.a */; };
+		CDCB1D732856747B001C406D /* types.h in Headers */ = {isa = PBXBuildFile; fileRef = CDCB1D692856747B001C406D /* types.h */; };
+		CDCB1D742856747B001C406D /* message.h in Headers */ = {isa = PBXBuildFile; fileRef = CDCB1D6A2856747B001C406D /* message.h */; };
+		CDCB1D752856747B001C406D /* cli.h in Headers */ = {isa = PBXBuildFile; fileRef = CDCB1D6B2856747B001C406D /* cli.h */; };
+		CDCB1D762856747B001C406D /* lambda_cast.h in Headers */ = {isa = PBXBuildFile; fileRef = CDCB1D6C2856747B001C406D /* lambda_cast.h */; };
+		CDCB1D772856747B001C406D /* prompt.h in Headers */ = {isa = PBXBuildFile; fileRef = CDCB1D6D2856747B001C406D /* prompt.h */; };
 		CDF978A525323ADB00204B5C /* prompt_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDF978A425323ADB00204B5C /* prompt_test.cxx */; };
 /* End PBXBuildFile section */
 
@@ -84,6 +89,11 @@
 		CDC748E2253136FD008D9D1D /* cli.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cli.cxx; sourceTree = "<group>"; };
 		CDC748E625313CBC008D9D1D /* cli_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = cli_test.cxx; sourceTree = "<group>"; };
 		CDC748EA25314195008D9D1D /* string-utils.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = "string-utils.xcodeproj"; path = "external/string-utils/string-utils.xcodeproj"; sourceTree = "<group>"; };
+		CDCB1D692856747B001C406D /* types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = types.h; sourceTree = "<group>"; };
+		CDCB1D6A2856747B001C406D /* message.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = message.h; sourceTree = "<group>"; };
+		CDCB1D6B2856747B001C406D /* cli.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cli.h; sourceTree = "<group>"; };
+		CDCB1D6C2856747B001C406D /* lambda_cast.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lambda_cast.h; sourceTree = "<group>"; };
+		CDCB1D6D2856747B001C406D /* prompt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = prompt.h; sourceTree = "<group>"; };
 		CDF978A425323ADB00204B5C /* prompt_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = prompt_test.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
@@ -114,6 +124,7 @@
 				CDC748EA25314195008D9D1D /* string-utils.xcodeproj */,
 				CDC748AC25313553008D9D1D /* GoogleMock.xcodeproj */,
 				CDC748BD25313587008D9D1D /* cli */,
+				CDCB1D672856747B001C406D /* include */,
 				CDC748BB2531357C008D9D1D /* src */,
 				CDC748BC2531357C008D9D1D /* test */,
 				CDC748C325313592008D9D1D /* cli-test */,
@@ -184,6 +195,26 @@
 			name = Products;
 			sourceTree = "<group>";
 		};
+		CDCB1D672856747B001C406D /* include */ = {
+			isa = PBXGroup;
+			children = (
+				CDCB1D682856747B001C406D /* cli */,
+			);
+			path = include;
+			sourceTree = "<group>";
+		};
+		CDCB1D682856747B001C406D /* cli */ = {
+			isa = PBXGroup;
+			children = (
+				CDCB1D692856747B001C406D /* types.h */,
+				CDCB1D6A2856747B001C406D /* message.h */,
+				CDCB1D6B2856747B001C406D /* cli.h */,
+				CDCB1D6C2856747B001C406D /* lambda_cast.h */,
+				CDCB1D6D2856747B001C406D /* prompt.h */,
+			);
+			path = cli;
+			sourceTree = "<group>";
+		};
 /* End PBXGroup section */
 
 /* Begin PBXHeadersBuildPhase section */
@@ -191,6 +222,11 @@
 			isa = PBXHeadersBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDCB1D772856747B001C406D /* prompt.h in Headers */,
+				CDCB1D752856747B001C406D /* cli.h in Headers */,
+				CDCB1D742856747B001C406D /* message.h in Headers */,
+				CDCB1D762856747B001C406D /* lambda_cast.h in Headers */,
+				CDCB1D732856747B001C406D /* types.h in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 0 - 52
cli.xcodeproj/xcshareddata/xcschemes/cli-test.xcscheme

@@ -1,52 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<Scheme
-   LastUpgradeVersion = "1240"
-   version = "1.3">
-   <BuildAction
-      parallelizeBuildables = "YES"
-      buildImplicitDependencies = "YES">
-   </BuildAction>
-   <TestAction
-      buildConfiguration = "Debug"
-      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
-      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
-      shouldUseLaunchSchemeArgsEnv = "YES">
-      <Testables>
-         <TestableReference
-            skipped = "NO">
-            <BuildableReference
-               BuildableIdentifier = "primary"
-               BlueprintIdentifier = "CDC748C125313592008D9D1D"
-               BuildableName = "cli-test.xctest"
-               BlueprintName = "cli-test"
-               ReferencedContainer = "container:cli.xcodeproj">
-            </BuildableReference>
-         </TestableReference>
-      </Testables>
-   </TestAction>
-   <LaunchAction
-      buildConfiguration = "Debug"
-      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
-      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
-      launchStyle = "0"
-      useCustomWorkingDirectory = "NO"
-      ignoresPersistentStateOnLaunch = "NO"
-      debugDocumentVersioning = "YES"
-      debugServiceExtension = "internal"
-      allowLocationSimulation = "YES">
-   </LaunchAction>
-   <ProfileAction
-      buildConfiguration = "Release"
-      shouldUseLaunchSchemeArgsEnv = "YES"
-      savedToolIdentifier = ""
-      useCustomWorkingDirectory = "NO"
-      debugDocumentVersioning = "YES">
-   </ProfileAction>
-   <AnalyzeAction
-      buildConfiguration = "Debug">
-   </AnalyzeAction>
-   <ArchiveAction
-      buildConfiguration = "Release"
-      revealArchiveInOrganizer = "YES">
-   </ArchiveAction>
-</Scheme>

+ 2 - 1
cli.xcodeproj/xcshareddata/xcschemes/cli.xcscheme

@@ -26,7 +26,8 @@
       buildConfiguration = "Debug"
       selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
       selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
-      shouldUseLaunchSchemeArgsEnv = "YES">
+      shouldUseLaunchSchemeArgsEnv = "YES"
+      codeCoverageEnabled = "YES">
       <Testables>
          <TestableReference
             skipped = "NO">

+ 1 - 1
external/string-utils

@@ -1 +1 @@
-Subproject commit db456dac7f214b13b48d6953720509972b8e72e0
+Subproject commit 057f98396ed68a63603e25dc98d981dd9232d44f

+ 13 - 14
include/cli/cli.h

@@ -15,6 +15,8 @@
 #include <unordered_map>
 #include <vector>
 
+#include <string_utils/cast.h>
+
 #include "lambda_cast.h"
 #include "types.h"
 
@@ -50,36 +52,33 @@ public:
   }
   
   void run() const;
-  template <typename T, size_t I>
-  static auto from_string(std::string const & value) {
-    using E = std::tuple_element_t<I, T>;
-    using V = std::remove_cv_t<std::remove_reference_t<E>>;
-    return from_string<V>(value);
-  }
   
 private:
   bool active() const;
-  template <typename T> static T from_string(std::string const &);
 };
 
 template <typename... Args, size_t... Is>
 void cli_invoke(std::function<void(Args...)> cb, cli::args_t const & args,
                 std::index_sequence<Is...>) {
-  using tuple = std::tuple<Args...>;
-  cb(cli::from_string<tuple, Is>(args[Is])...);
+  std::tuple<std::decay_t<Args>...> object;
+  if (string_utils::cast(args, object)) {
+    cb(std::move(std::get<Is>(object))...);
+  }
 }
 
 template <typename... Args>
 cli & cli::register_callback(std::string const & handle,
                              std::function<void(Args...)> cb) {
-  arguments_.emplace(handle, sizeof...(Args));
-  callbacks_.emplace(handle, [=](args_t const & args) {
-    if (sizeof...(Args) > args.size()) {
+  constexpr size_t N = sizeof...(Args);
+  arguments_.emplace(handle, N);
+  callbacks_.emplace(handle, [=](args_t args) {
+    if (N > args.size()) {
       std::cerr << "Missing Args in command '" << handle << "', " <<
-          sizeof...(Args) << " required, but " << args.size() << " input\n";
+          N << " required, but " << args.size() << " input\n";
       return;
     }
-    cli_invoke(cb, args, std::make_index_sequence<sizeof...(Args)>());
+    args.resize(N);
+    cli_invoke(cb, args, std::make_index_sequence<N>());
   });
   return *this;
 }

+ 0 - 8
src/cli.cxx

@@ -47,12 +47,4 @@ void cli::run() const {
   }
 }
 
-template <> std::string cli::from_string(std::string const & str) {
-  return str;
-}
-
-template <> int cli::from_string(std::string const & str) {
-  return std::stoi(str);
-}
-
 }

+ 1 - 0
test/xcode_gtest_helper.h

@@ -12,6 +12,7 @@
 
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wquoted-include-in-framework-header"
+#pragma clang diagnostic ignored "-Wcomma"
 
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>