浏览代码

Made STATUS_STRING const char*[]

Tatsuhiro Tsujikawa 13 年之前
父节点
当前提交
b640b830a2
共有 2 个文件被更改,包括 12 次插入11 次删除
  1. 10 8
      src/ServerStat.cc
  2. 2 3
      src/ServerStat.h

+ 10 - 8
src/ServerStat.cc

@@ -46,10 +46,12 @@
 
 namespace aria2 {
 
-const std::string ServerStat::STATUS_STRING[] = {
+namespace {
+const char* STATUS_STRING[] = {
   "OK",
   "ERROR"
 };
+} // namespace
 
 ServerStat::ServerStat(const std::string& hostname, const std::string& protocol)
   : hostname_(hostname),
@@ -161,18 +163,18 @@ void ServerStat::setStatus(STATUS status)
 
 void ServerStat::setStatus(const std::string& status)
 {
-  const std::string* p = std::find(vbegin(STATUS_STRING), vend(STATUS_STRING),
-                                   status);
-  if(p != vend(STATUS_STRING)) {
-    status_ = static_cast<STATUS>(ServerStat::OK+
-                                  std::distance(vbegin(STATUS_STRING), p));
+  for(int i = 0; i < MAX_STATUS; ++i) {
+    if(strcmp(status.c_str(), STATUS_STRING[i]) == 0) {
+      status_ = static_cast<STATUS>(i);
+      break;
+    }
   }
 }
 
 void ServerStat::setStatusInternal(STATUS status)
 {
   A2_LOG_DEBUG(fmt("ServerStat: set status %s for %s (%s)",
-                   STATUS_STRING[status].c_str(),
+                   STATUS_STRING[status],
                    hostname_.c_str(),
                    protocol_.c_str()));
   status_ = status;
@@ -211,7 +213,7 @@ std::string ServerStat::toString() const
              getMultiConnectionAvgSpeed(),
              getLastUpdated().getTime(),
              getCounter(),
-             ServerStat::STATUS_STRING[getStatus()].c_str());
+             STATUS_STRING[getStatus()]);
 }
 
 } // namespace aria2

+ 2 - 3
src/ServerStat.h

@@ -52,10 +52,9 @@ class ServerStat {
 public:
   enum STATUS {
     OK = 0,
-    A2_ERROR
+    A2_ERROR,
+    MAX_STATUS
   };
-  
-  static const std::string STATUS_STRING[];
 
   ServerStat(const std::string& hostname, const std::string& protocol);