Browse Source

logging/backend: Use std::string_view in RemoveBackend() and GetBackend()

These can just use a view to a string since its only comparing against
two names in both cases for matches. This avoids constructing
std::string instances where they aren't necessary.
Lioncash 8 years ago
parent
commit
457d1b4490
2 changed files with 13 additions and 12 deletions
  1. 10 10
      src/common/logging/backend.cpp
  2. 3 2
      src/common/logging/backend.h

+ 10 - 10
src/common/logging/backend.cpp

@@ -48,11 +48,11 @@ public:
         backends.push_back(std::move(backend));
         backends.push_back(std::move(backend));
     }
     }
 
 
-    void RemoveBackend(const std::string& backend_name) {
+    void RemoveBackend(std::string_view backend_name) {
         std::lock_guard<std::mutex> lock(writing_mutex);
         std::lock_guard<std::mutex> lock(writing_mutex);
-        auto it = std::remove_if(backends.begin(), backends.end(), [&backend_name](const auto& i) {
-            return !strcmp(i->GetName(), backend_name.c_str());
-        });
+        const auto it =
+            std::remove_if(backends.begin(), backends.end(),
+                           [&backend_name](const auto& i) { return backend_name == i->GetName(); });
         backends.erase(it, backends.end());
         backends.erase(it, backends.end());
     }
     }
 
 
@@ -64,10 +64,10 @@ public:
         filter = f;
         filter = f;
     }
     }
 
 
-    Backend* GetBackend(const std::string& backend_name) {
-        auto it = std::find_if(backends.begin(), backends.end(), [&backend_name](const auto& i) {
-            return !strcmp(i->GetName(), backend_name.c_str());
-        });
+    Backend* GetBackend(std::string_view backend_name) {
+        const auto it =
+            std::find_if(backends.begin(), backends.end(),
+                         [&backend_name](const auto& i) { return backend_name == i->GetName(); });
         if (it == backends.end())
         if (it == backends.end())
             return nullptr;
             return nullptr;
         return it->get();
         return it->get();
@@ -265,11 +265,11 @@ void AddBackend(std::unique_ptr<Backend> backend) {
     Impl::Instance().AddBackend(std::move(backend));
     Impl::Instance().AddBackend(std::move(backend));
 }
 }
 
 
-void RemoveBackend(const std::string& backend_name) {
+void RemoveBackend(std::string_view backend_name) {
     Impl::Instance().RemoveBackend(backend_name);
     Impl::Instance().RemoveBackend(backend_name);
 }
 }
 
 
-Backend* GetBackend(const std::string& backend_name) {
+Backend* GetBackend(std::string_view backend_name) {
     return Impl::Instance().GetBackend(backend_name);
     return Impl::Instance().GetBackend(backend_name);
 }
 }
 
 

+ 3 - 2
src/common/logging/backend.h

@@ -7,6 +7,7 @@
 #include <cstdarg>
 #include <cstdarg>
 #include <memory>
 #include <memory>
 #include <string>
 #include <string>
+#include <string_view>
 #include <utility>
 #include <utility>
 #include "common/file_util.h"
 #include "common/file_util.h"
 #include "common/logging/filter.h"
 #include "common/logging/filter.h"
@@ -106,9 +107,9 @@ private:
 
 
 void AddBackend(std::unique_ptr<Backend> backend);
 void AddBackend(std::unique_ptr<Backend> backend);
 
 
-void RemoveBackend(const std::string& backend_name);
+void RemoveBackend(std::string_view backend_name);
 
 
-Backend* GetBackend(const std::string& backend_name);
+Backend* GetBackend(std::string_view backend_name);
 
 
 /**
 /**
  * Returns the name of the passed log class as a C-string. Subclasses are separated by periods
  * Returns the name of the passed log class as a C-string. Subclasses are separated by periods