Kaynağa Gözat

Improved byte handling

- replaced ls_std::byte* with ls_std::byte_field to
have a null terminated field of bytes
- adjusted interfaces
- adjusted implementation
- adjusted tests
Patrick 4 yıl önce
ebeveyn
işleme
28702268ec

+ 4 - 1
source/base/Types.hpp

@@ -3,15 +3,18 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-07
- * Changed:         2020-08-07
+ * Changed:         2020-08-23
  *
  * */
 
 #ifndef TYPES_HPP
 #define TYPES_HPP
 
+#include <string>
+
 namespace ls_std {
   using byte = char;
+  using byte_field = std::string;
 }
 
 #endif

+ 10 - 10
source/boxing/Integer.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-07
- * Changed:         2020-08-22
+ * Changed:         2020-08-23
  *
  * */
 
@@ -247,9 +247,9 @@ void ls_std::Integer::operator--()
   this->value -= 1;
 }
 
-ls_std::byte * ls_std::Integer::load()
+ls_std::byte_field ls_std::Integer::load()
 {
-  ls_std::byte* data {};
+  ls_std::byte_field data {};
 
   if(this->storable != nullptr && this->serializable != nullptr) {
     data = this->storable->load();
@@ -259,12 +259,12 @@ ls_std::byte * ls_std::Integer::load()
   return data;
 }
 
-const ls_std::byte * ls_std::Integer::marshal()
+ls_std::byte_field ls_std::Integer::marshal()
 {
-  ls_std::byte* data {};
+  ls_std::byte_field data {};
 
   if(this->serializable != nullptr) {
-    data = (char*) this->serializable->marshal();
+    data = this->serializable->marshal();
   }
 
   return data;
@@ -275,11 +275,11 @@ void ls_std::Integer::parse(std::string _parseText)
   this->value = std::stoi(_parseText);
 }
 
-void ls_std::Integer::save(ls_std::byte *_data)
+void ls_std::Integer::save(const ls_std::byte_field& _data)
 {
   if(this->serializable != nullptr) {
-    if(_data == nullptr) {
-      this->storable->save((char *) this->serializable->marshal());
+    if(_data.empty()) {
+      this->storable->save(this->serializable->marshal());
     } else {
       this->storable->save(_data);
     }
@@ -291,7 +291,7 @@ std::string ls_std::Integer::toString()
   return std::to_string(this->value);
 }
 
-void ls_std::Integer::unmarshal(const ls_std::byte *_data)
+void ls_std::Integer::unmarshal(const ls_std::byte_field& _data)
 {
   if(this->serializable != nullptr) {
     this->serializable->unmarshal(_data);

+ 5 - 5
source/boxing/Integer.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-07
- * Changed:         2020-08-22
+ * Changed:         2020-08-23
  *
  * */
 
@@ -105,12 +105,12 @@ namespace ls_std {
 
       // implementation
 
-      ls_std::byte* load() override;
-      const ls_std::byte* marshal() override;
+      ls_std::byte_field load() override;
+      ls_std::byte_field marshal() override;
       void parse(std::string _parseText) override;
-      void save(ls_std::byte* _data) override;
+      void save(const ls_std::byte_field& _data) override;
       std::string toString() override;
-      void unmarshal(const ls_std::byte* _data) override;
+      void unmarshal(const ls_std::byte_field& _data) override;
 
       // additional functionality
 

+ 2 - 2
source/io/FileOutputStream.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2020-08-21
+ * Changed:         2020-08-25
  *
  * */
 
@@ -34,7 +34,7 @@ void ls_std::FileOutputStream::close()
   this->_close();
 }
 
-bool ls_std::FileOutputStream::write(const ls_std::byte *_data)
+bool ls_std::FileOutputStream::write(const ls_std::byte_field& _data)
 {
   bool succeeded {};
 

+ 2 - 2
source/io/FileOutputStream.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2020-08-21
+ * Changed:         2020-08-25
  *
  * */
 
@@ -24,7 +24,7 @@ namespace ls_std {
       ~FileOutputStream();
 
       void close();
-      bool write(const ls_std::byte* _data) override;
+      bool write(const ls_std::byte_field& _data) override;
 
     private:
 

+ 3 - 3
source/io/FileReader.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-19
+ * Changed:         2020-08-25
  *
  * */
 
@@ -18,7 +18,7 @@ file(_file)
   ls_std::FileReader::_init(_file);
 }
 
-ls_std::byte * ls_std::FileReader::read()
+ls_std::byte_field ls_std::FileReader::read()
 {
   ls_std::byte* data;
   std::ifstream inputStream {this->file.getAbsoluteFilePath(), std::ifstream::binary};
@@ -31,7 +31,7 @@ ls_std::byte * ls_std::FileReader::read()
   }
 
   inputStream.close();
-  return data;
+  return ls_std::byte_field {data, (size_t) this->file.getSize()};
 }
 
 void ls_std::FileReader::reset(File &_file)

+ 2 - 2
source/io/FileReader.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-19
+ * Changed:         2020-08-25
  *
  * */
 
@@ -21,7 +21,7 @@ namespace ls_std {
       explicit FileReader(File& _file);
       ~FileReader() = default;
 
-      ls_std::byte* read() override;
+      ls_std::byte_field read() override;
       void reset(File& _file);
 
     private:

+ 2 - 2
source/io/FileWriter.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-20
+ * Changed:         2020-08-25
  *
  * */
 
@@ -24,7 +24,7 @@ void ls_std::FileWriter::reset(File &_file)
   this->file = _file;
 }
 
-bool ls_std::FileWriter::write(const ls_std::byte* _data)
+bool ls_std::FileWriter::write(const ls_std::byte_field& _data)
 {
   std::ofstream outputStream {};
   outputStream.open(this->file.getAbsoluteFilePath());

+ 2 - 2
source/io/FileWriter.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-19
+ * Changed:         2020-08-25
  *
  * */
 
@@ -22,7 +22,7 @@ namespace ls_std {
       ~FileWriter() = default;
 
       void reset(File& _file);
-      bool write(const ls_std::byte* _data) override;
+      bool write(const ls_std::byte_field& _data) override;
 
     private:
 

+ 2 - 2
source/io/IReader.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-17
+ * Changed:         2020-08-23
  *
  * */
 
@@ -19,7 +19,7 @@ namespace ls_std {
       IReader() = default;
       ~IReader() = default;
 
-      virtual ls_std::byte* read() = 0;
+      virtual ls_std::byte_field read() = 0;
   };
 }
 

+ 3 - 3
source/io/IStorable.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-19
- * Changed:         2020-08-22
+ * Changed:         2020-08-23
  *
  * */
 
@@ -18,8 +18,8 @@ namespace ls_std {
       IStorable() = default;
       ~IStorable() = default;
 
-      virtual ls_std::byte* load() = 0;
-      virtual void save(ls_std::byte* _data) = 0;
+      virtual ls_std::byte_field load() = 0;
+      virtual void save(const ls_std::byte_field& _data) = 0;
   };
 }
 

+ 2 - 2
source/io/IWriter.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-17
+ * Changed:         2020-08-23
  *
  * */
 
@@ -20,7 +20,7 @@ namespace ls_std {
       IWriter() = default;
       ~IWriter() = default;
 
-      virtual bool write(const ls_std::byte* _data) = 0;
+      virtual bool write(const ls_std::byte_field& _data) = 0;
   };
 }
 

+ 3 - 3
source/io/StorableFile.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-19
- * Changed:         2020-08-22
+ * Changed:         2020-08-25
  *
  * */
 
@@ -21,7 +21,7 @@ std::shared_ptr<ls_std::File> ls_std::StorableFile::getFile()
   return this->file;
 }
 
-ls_std::byte* ls_std::StorableFile::load()
+ls_std::byte_field ls_std::StorableFile::load()
 {
   ls_std::FileReader reader {*this->file};
   return reader.read();
@@ -32,7 +32,7 @@ void ls_std::StorableFile::reset(const std::string &_path)
   this->_init(_path);
 }
 
-void ls_std::StorableFile::save(ls_std::byte* _data)
+void ls_std::StorableFile::save(const ls_std::byte_field& _data)
 {
   ls_std::FileWriter writer {*this->file};
   writer.write(_data);

+ 3 - 3
source/io/StorableFile.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-19
- * Changed:         2020-08-22
+ * Changed:         2020-08-25
  *
  * */
 
@@ -23,9 +23,9 @@ namespace ls_std {
       ~StorableFile() = default;
 
       std::shared_ptr<ls_std::File> getFile();
-      ls_std::byte* load() override;
+      ls_std::byte_field load() override;
       void reset(const std::string& _path);
-      void save(ls_std::byte* _data) override;
+      void save(const ls_std::byte_field& _data) override;
 
     private:
 

+ 3 - 3
source/serialization/ISerializable.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-21
- * Changed:         2020-08-21
+ * Changed:         2020-08-23
  *
  * */
 
@@ -19,8 +19,8 @@ namespace ls_std {
       ISerializable() = default;
       ~ISerializable() = default;
 
-      virtual const ls_std::byte* marshal() = 0;
-      virtual void unmarshal(const ls_std::byte* _data) = 0;
+      virtual ls_std::byte_field marshal() = 0;
+      virtual void unmarshal(const ls_std::byte_field& _data) = 0;
   };
 }
 

+ 3 - 3
source/serialization/boxing/SerializableJSONInteger.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-21
- * Changed:         2020-08-21
+ * Changed:         2020-08-23
  *
  * */
 
@@ -13,7 +13,7 @@ ls_std::SerializableJSONInteger::SerializableJSONInteger(std::shared_ptr<ls_std:
 integer(std::move(_integer))
 {}
 
-const ls_std::byte * ls_std::SerializableJSONInteger::marshal()
+ls_std::byte_field ls_std::SerializableJSONInteger::marshal()
 {
   this->_update();
   auto* data = new char[this->jsonObject.dump().size() + 1];
@@ -22,7 +22,7 @@ const ls_std::byte * ls_std::SerializableJSONInteger::marshal()
   return data;
 }
 
-void ls_std::SerializableJSONInteger::unmarshal(const ls_std::byte *_data)
+void ls_std::SerializableJSONInteger::unmarshal(const ls_std::byte_field& _data)
 {
   std::string jsonString = std::string(_data);
   this->jsonObject = nlohmann::json::parse(jsonString);

+ 3 - 3
source/serialization/boxing/SerializableJSONInteger.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-21
- * Changed:         2020-08-22
+ * Changed:         2020-08-23
  *
  * */
 
@@ -22,8 +22,8 @@ namespace ls_std {
       explicit SerializableJSONInteger(std::shared_ptr<ls_std::Integer> _integer);
       ~SerializableJSONInteger() = default;
 
-      const ls_std::byte* marshal() override;
-      void unmarshal(const ls_std::byte* _data) override;
+      ls_std::byte_field marshal() override;
+      void unmarshal(const ls_std::byte_field& _data) override;
 
     private:
 

+ 3 - 3
test/cases/boxing/IntegerTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2020-08-22
+ * Changed:         2020-08-25
  *
  * */
 
@@ -385,10 +385,10 @@ namespace {
     auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
     x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
 
-    ASSERT_STREQ(R"({"class":"Integer","value":3})", x->marshal());
+    ASSERT_STREQ(R"({"class":"Integer","value":3})", x->marshal().c_str());
 
     *x = 17;
-    ASSERT_STREQ(R"({"class":"Integer","value":17})", x->marshal());
+    ASSERT_STREQ(R"({"class":"Integer","value":17})", x->marshal().c_str());
   }
 
   TEST_F(IntegerTest, parse)

+ 4 - 7
test/cases/io/FileReaderTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-18
- * Changed:         2020-08-19
+ * Changed:         2020-08-25
  *
  * */
 
@@ -30,8 +30,7 @@ namespace {
     std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
     std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
 
-    ls_std::byte* data = reader.read();
-    std::string content {data, (size_t) file.getSize()};
+    ls_std::byte_field content = reader.read();
     ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
   }
 
@@ -42,8 +41,7 @@ namespace {
     std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
     std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
 
-    ls_std::byte* data = reader.read();
-    std::string content {data, (size_t) file.getSize()};
+    ls_std::byte_field content = reader.read();
     ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
 
     ls_std::File anotherFile {TestHelper::getResourcesFolderLocation() + "list_test/bla.txt"};
@@ -51,8 +49,7 @@ namespace {
     expectedUnix = "nothing to say!" + ls_std::NewLine::getUnixNewLine();
     expectedWindows = "nothing to say!" + ls_std::NewLine::getWindowsNewLine();
 
-    data = reader.read();
-    content = {data, (size_t) anotherFile.getSize()};
+    content = reader.read();
     ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
   }
 }

+ 8 - 8
test/cases/io/StorableFileTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-19
- * Changed:         2020-08-22
+ * Changed:         2020-08-25
  *
  * */
 
@@ -34,7 +34,7 @@ namespace {
   TEST_F(StorableFileTest, load)
   {
     ls_std::StorableFile storableFile {this->fileLocation};
-    std::string content = {storableFile.load(), (size_t) storableFile.getFile()->getSize()};
+    ls_std::byte_field content = storableFile.load();
 
     std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
     std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
@@ -45,7 +45,7 @@ namespace {
   TEST_F(StorableFileTest, reset)
   {
     ls_std::StorableFile storableFile {this->fileLocation};
-    std::string content = {storableFile.load(), (size_t) storableFile.getFile()->getSize()};
+    ls_std::byte_field content = storableFile.load();
 
     std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
     std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
@@ -56,7 +56,7 @@ namespace {
 
     std::string anotherFileLocation = TestHelper::getResourcesFolderLocation() + "list_test/bla.txt";
     storableFile.reset(anotherFileLocation);
-    content = {storableFile.load(), (size_t) storableFile.getFile()->getSize()};
+    content = storableFile.load();
 
     expectedUnix = "nothing to say!" + ls_std::NewLine::getUnixNewLine();
     expectedWindows = "nothing to say!" + ls_std::NewLine::getWindowsNewLine();
@@ -71,11 +71,11 @@ namespace {
     file.createNewFile();
 
     ls_std::StorableFile storableFile {path};
-    std::string textUnix = "Testing save method!" + ls_std::NewLine::getUnixNewLine();
-    std::string textWindows = "Testing save method!" + ls_std::NewLine::getWindowsNewLine();
+    ls_std::byte_field textUnix = "Testing save method!" + ls_std::NewLine::getUnixNewLine();
+    ls_std::byte_field textWindows = "Testing save method!" + ls_std::NewLine::getWindowsNewLine();
 
-    storableFile.save((char*) textUnix.c_str());
-    std::string content = storableFile.load();
+    storableFile.save(textUnix);
+    ls_std::byte_field content = storableFile.load();
     ASSERT_TRUE(content == textUnix || content == textWindows);
 
     file.remove();

+ 2 - 2
test/cases/serialization/boxing/SerializableJSONIntegerTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-21
- * Changed:         2020-08-21
+ * Changed:         2020-08-25
  *
  * */
 
@@ -26,7 +26,7 @@ namespace {
   {
     ls_std::Integer x {3};
     ls_std::SerializableJSONInteger serializable {std::make_shared<ls_std::Integer>(x)};
-    std::string content = serializable.marshal();
+    ls_std::byte_field content = serializable.marshal();
 
     ASSERT_STREQ(R"({"class":"Integer","value":3})", content.c_str());
   }