浏览代码

Use std::pair instead of writing on references

Jonas Pflanzer 5 年之前
父节点
当前提交
acb2607ab2

+ 1 - 1
daemon/include/FileManager.h

@@ -86,7 +86,7 @@ public:
    * Opens get file if it exists and reports the amount of chunks
    * @return true - file is open | false - file is not open
    */
-  virtual bool openGetFile(const std::string &filename, int &chunks);
+  virtual std::pair<bool, int> openGetFile(const std::string &filename);
 
   /**
    * Closes file

+ 6 - 5
daemon/src/FileManager.cpp

@@ -34,7 +34,8 @@ bool FileManager::openPutFile(const std::string &filename) {
   return true;
 }
 
-bool FileManager::openGetFile(const std::string &filename, int &chunks) {
+std::pair<bool, int>
+FileManager::openGetFile(const std::__cxx11::string &filename) {
   this->getBaseFileName = filename;
   std::string file = this->fileDirectory;
   file.append(filename);
@@ -43,14 +44,14 @@ bool FileManager::openGetFile(const std::string &filename, int &chunks) {
 
   if (this->getFile.is_open() == 0) {
     // file does not exist or cannot be opened
-    return false;
+    return std::pair<bool, int>(false, -1);
   } else {
     this->getFileSize = this->getFile.tellg();
-    chunks = this->getFileSize / max_data_length +
-             (this->getFileSize % max_data_length == 0 ? 0 : 1);
+    int chunks = this->getFileSize / max_data_length +
+                 (this->getFileSize % max_data_length == 0 ? 0 : 1);
 
     this->getFile.seekg(std::ios::beg);
-    return true;
+    return std::pair<bool, int>(true, chunks);
   }
 }
 

+ 5 - 3
daemon/src/JsonCommander.cpp

@@ -271,9 +271,11 @@ JsonCommander::Response JsonCommander::executeGet(const Json::Value &message) {
     // accept request
     response.action = send;
 
-    bool opened = fileManager.openGetFile(message["file"].asString(),
-                                          this->getFileRemaining);
-    if (opened) {
+    std::pair<bool, int> opened =
+        fileManager.openGetFile(message["file"].asString());
+
+    if (opened.first) {
+      this->getFileRemaining = opened.second;
       response.json["accept"] = true;
       response.json["chunks"] = this->getFileRemaining;
       response.json["error"] = "";

+ 1 - 1
daemon/test/FileManagerMock.h

@@ -11,7 +11,7 @@
  */
 class FileManagerMock : public FileManager {
 public:
-  MOCK_METHOD(bool, openGetFile, (const std::string &filename, int &chunks), (override));
+  MOCK_METHOD((std::pair<bool, int>), openGetFile, (const std::string &filename), (override));
   MOCK_METHOD(bool, openPutFile, (const std::string &filename), (override));
 
   MOCK_METHOD(bool, isDownloading, (), (override));

+ 12 - 15
daemon/test/JsonCommanderTest.cpp

@@ -380,9 +380,9 @@ TEST(Get, Positive) {
   message["command"] = command;
   message["file"] = filename;
 
-  EXPECT_CALL(fileManager, openGetFile(testing::_, testing::_))
-      .WillOnce(testing::DoAll(testing::SetArgReferee<1, int>(3),
-                               testing::Return(true)));
+  const int chunks = 3;
+  EXPECT_CALL(fileManager, openGetFile(testing::_))
+      .WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
 
   JsonCommander::Response response = jsonCommander.execute(message);
 
@@ -390,7 +390,7 @@ TEST(Get, Positive) {
   EXPECT_EQ(response.json["command"].asString(), command);
   EXPECT_TRUE(response.json["accept"].asBool());
   EXPECT_EQ(response.json["file"].asString(), filename);
-  EXPECT_TRUE(response.json["chunks"].asInt() > 0);
+  EXPECT_EQ(response.json["chunks"].asInt(), chunks);
   EXPECT_EQ(response.json["error"].asString(), "");
 }
 
@@ -405,8 +405,8 @@ TEST(Get, Negative) {
   message["command"] = command;
   message["file"] = filename;
 
-  EXPECT_CALL(fileManager, openGetFile(testing::_, testing::_))
-      .WillOnce(testing::Return(false));
+  EXPECT_CALL(fileManager, openGetFile(testing::_))
+      .WillOnce(testing::Return(std::pair<bool, int>(false, -1)));
 
   JsonCommander::Response response = jsonCommander.execute(message);
 
@@ -431,9 +431,8 @@ TEST(Getdata, Positive) {
   message["file"] = filename;
 
   const int chunks = 3;
-  EXPECT_CALL(fileManager, openGetFile(testing::_, testing::_))
-      .WillOnce(testing::DoAll(testing::SetArgReferee<1, int>(chunks),
-                               testing::Return(true)));
+  EXPECT_CALL(fileManager, openGetFile(testing::_))
+      .WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
 
   JsonCommander::Response response = jsonCommander.execute(message);
 
@@ -490,9 +489,8 @@ TEST(Getdata, Cancle) {
   message["file"] = filename;
 
   const int chunks = 3;
-  EXPECT_CALL(fileManager, openGetFile(testing::_, testing::_))
-      .WillOnce(testing::DoAll(testing::SetArgReferee<1, int>(chunks),
-                               testing::Return(true)));
+  EXPECT_CALL(fileManager, openGetFile(testing::_))
+      .WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
 
   JsonCommander::Response response = jsonCommander.execute(message);
 
@@ -565,9 +563,8 @@ TEST(Getdata, WrongChunk) {
   message["file"] = filename;
 
   const int chunks = 3;
-  EXPECT_CALL(fileManager, openGetFile(testing::_, testing::_))
-      .WillOnce(testing::DoAll(testing::SetArgReferee<1, int>(chunks),
-                               testing::Return(true)));
+  EXPECT_CALL(fileManager, openGetFile(testing::_))
+      .WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
 
   JsonCommander::Response response = jsonCommander.execute(message);