[openjk] 30/130: Access fields or throw exceptions in templated members via methods only

Simon McVittie smcv at debian.org
Fri Oct 28 11:09:14 UTC 2016


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch debian/master
in repository openjk.

commit 9e6541d35162193208761fe0cfe6d527a27229dd
Author: bibendovsky <bibendovsky at hotmail.com>
Date:   Sat Jul 9 22:39:07 2016 +0300

    Access fields or throw exceptions in templated members via methods only
---
 shared/qcommon/ojk_saved_game.cpp   | 106 ++++++++++++++++++++++++++++++++
 shared/qcommon/ojk_saved_game.h     | 117 ++++++++++++++----------------------
 shared/qcommon/ojk_saved_game_fwd.h |  44 +++++++++++---
 3 files changed, 185 insertions(+), 82 deletions(-)

diff --git a/shared/qcommon/ojk_saved_game.cpp b/shared/qcommon/ojk_saved_game.cpp
index 341f8cb..db70802 100644
--- a/shared/qcommon/ojk_saved_game.cpp
+++ b/shared/qcommon/ojk_saved_game.cpp
@@ -364,6 +364,31 @@ bool SavedGame::write_chunk(
     return true;
 }
 
+SavedGame::Buffer& SavedGame::get_buffer()
+{
+    return io_buffer_;
+}
+
+const SavedGame::Buffer& SavedGame::get_buffer() const
+{
+    return io_buffer_;
+}
+
+int SavedGame::get_buffer_offset() const
+{
+    return static_cast<int>(io_buffer_offset_);
+}
+
+uint8_t* SavedGame::get_current_data()
+{
+    return &io_buffer_[io_buffer_offset_];
+}
+
+const uint8_t* SavedGame::get_current_data() const
+{
+    return &io_buffer_[io_buffer_offset_];
+}
+
 void SavedGame::rename(
     const std::string& old_base_file_name,
     const std::string& new_base_file_name)
@@ -401,6 +426,20 @@ SavedGame& SavedGame::get_instance()
     return result;
 }
 
+void SavedGame::throw_error(
+    const char* message)
+{
+    throw SavedGameException(
+        message);
+}
+
+void SavedGame::throw_error(
+    const std::string& message)
+{
+    throw SavedGameException(
+        message);
+}
+
 void SavedGame::compress(
     const Buffer& src_buffer,
     Buffer& dst_buffer)
@@ -554,5 +593,72 @@ std::string SavedGame::get_chunk_id_string(
     return result;
 }
 
+void SavedGame::check_io_buffer(
+    int item_size,
+    int count)
+{
+    if (item_size <= 0) {
+        throw SavedGameException(
+            "Zero or negative item size.");
+    }
+
+    if (count <= 0) {
+        throw SavedGameException(
+            "Zero or negative count.");
+    }
+
+    const auto data_size = item_size * count;
+
+    if ((io_buffer_offset_ + data_size) > io_buffer_.size()) {
+        throw SavedGameException(
+            "Not enough data.");
+    }
+}
+
+void SavedGame::accomodate_io_buffer(
+    int item_size,
+    int count)
+{
+    if (item_size <= 0) {
+        throw SavedGameException(
+            "Zero or negative item size.");
+    }
+
+    if (count <= 0) {
+        throw SavedGameException(
+            "Zero or negative count.");
+    }
+
+    const auto data_size = item_size * count;
+
+    const auto new_buffer_size = io_buffer_offset_ + data_size;
+
+    io_buffer_.resize(
+        new_buffer_size);
+}
+
+void SavedGame::advance_io_buffer(
+    int item_size,
+    int count)
+{
+    if (item_size <= 0) {
+        throw SavedGameException(
+            "Zero or negative item size.");
+    }
+
+    if (count <= 0) {
+        throw SavedGameException(
+            "Zero or negative count.");
+    }
+
+    const auto data_size = item_size * count;
+    io_buffer_offset_ += data_size;
+}
+
+void SavedGame::reset_io_buffer_offset()
+{
+    io_buffer_offset_ = 0;
+}
+
 
 } // ojk
diff --git a/shared/qcommon/ojk_saved_game.h b/shared/qcommon/ojk_saved_game.h
index 4635736..d7c617a 100644
--- a/shared/qcommon/ojk_saved_game.h
+++ b/shared/qcommon/ojk_saved_game.h
@@ -20,56 +20,9 @@ namespace ojk {
 // I/O buffer manipulation.
 
 template<typename T>
-void SavedGame::check_io_buffer(
-    int count)
+T SavedGame::cast_buffer()
 {
-    if (count <= 0) {
-        throw SavedGameException(
-            "Zero or negative count.");
-    }
-
-    const auto data_size = sizeof(T) * count;
-
-    if ((io_buffer_offset_ + data_size) > io_buffer_.size()) {
-        throw SavedGameException(
-            "Not enough data.");
-    }
-}
-
-template<typename T>
-void SavedGame::accomodate_io_buffer(
-    int count)
-{
-    if (count <= 0) {
-        throw SavedGameException(
-            "Zero or negative count.");
-    }
-
-    const auto data_size = sizeof(T) * count;
-
-    const auto new_buffer_size = io_buffer_offset_ + data_size;
-
-    io_buffer_.resize(
-        new_buffer_size);
-}
-
-template<typename T>
-T SavedGame::cast_io_buffer()
-{
-    return reinterpret_cast<T>(io_buffer_[io_buffer_offset_]);
-}
-
-template<typename T>
-void SavedGame::advance_io_buffer(
-    int count)
-{
-    if (count <= 0) {
-        throw SavedGameException(
-            "Zero or negative count.");
-    }
-
-    const auto data_size = sizeof(T) * count;
-    io_buffer_offset_ += data_size;
+    return reinterpret_cast<T>(*get_current_data());
 }
 
 // I/O buffer manipulation.
@@ -121,7 +74,7 @@ bool SavedGame::write_chunk(
     const ChunkId chunk_id,
     const TSrc& src_value)
 {
-    io_buffer_offset_ = 0;
+    reset_io_buffer_offset();
 
     write<TDst>(
         src_value);
@@ -136,7 +89,7 @@ bool SavedGame::write_chunk(
     const TSrc* src_values,
     int src_count)
 {
-    io_buffer_offset_ = 0;
+    reset_io_buffer_offset();
 
     write<TDst>(
         src_values,
@@ -197,16 +150,18 @@ void SavedGame::read(
     TDst& dst_value,
     BooleanTag)
 {
-    constexpr auto src_size = sizeof(TSrc);
+    constexpr auto src_size = static_cast<int>(sizeof(TSrc));
 
-    check_io_buffer<TSrc>();
+    check_io_buffer(
+        src_size);
 
-    dst_value = (cast_io_buffer<const TSrc&>() != 0);
+    dst_value = (cast_buffer<const TSrc&>() != 0);
 
     // FIXME Byte order
     //
 
-    advance_io_buffer<TSrc>();
+    advance_io_buffer(
+        src_size);
 }
 
 template<typename TSrc, typename TDst>
@@ -214,14 +169,18 @@ void SavedGame::read(
     TDst& dst_value,
     NumericTag)
 {
-    check_io_buffer<TSrc>();
+    constexpr auto src_size = static_cast<int>(sizeof(TSrc));
+
+    check_io_buffer(
+        src_size);
 
-    dst_value = static_cast<TDst>(cast_io_buffer<const TSrc&>());
+    dst_value = static_cast<TDst>(cast_buffer<const TSrc&>());
 
     // FIXME Byte order
     //
 
-    advance_io_buffer<TSrc>();
+    advance_io_buffer(
+        src_size);
 }
 
 template<typename TSrc, typename TDst>
@@ -254,7 +213,7 @@ void SavedGame::read(
     TDst& dst_value,
     ClassTag)
 {
-    throw SavedGameException(
+    throw_error(
         "Not implemented.");
 }
 
@@ -298,12 +257,12 @@ void SavedGame::read(
         "Unsupported types.");
 
     if (!dst_values) {
-        throw SavedGameException(
+        throw_error(
             "Null pointer.");
     }
 
     if (dst_count < 0) {
-        throw SavedGameException(
+        throw_error(
             "Negative count.");
     }
 
@@ -352,18 +311,22 @@ void SavedGame::read(
     int dst_count,
     InplaceTag)
 {
-    check_io_buffer<TDst>(
+    constexpr auto dst_size = static_cast<int>(sizeof(TDst));
+
+    check_io_buffer(
+        dst_size,
         dst_count);
 
     std::uninitialized_copy_n(
-        &cast_io_buffer<const TDst&>(),
+        &cast_buffer<const TDst&>(),
         dst_count,
         dst_values);
 
     // FIXME Byte order
     //
 
-    advance_io_buffer<TDst>(
+    advance_io_buffer(
+        dst_size,
         dst_count);
 }
 
@@ -437,14 +400,18 @@ void SavedGame::write(
     const TSrc& src_value,
     NumericTag)
 {
-    accomodate_io_buffer<TSrc>();
+    constexpr auto src_size = static_cast<int>(sizeof(TSrc));
+
+    accomodate_io_buffer(
+        src_size);
 
-    cast_io_buffer<TDst&>() = static_cast<TDst>(src_value);
+    cast_buffer<TDst&>() = static_cast<TDst>(src_value);
 
     // FIXME Byte order
     //
 
-    advance_io_buffer<TSrc>();
+    advance_io_buffer(
+        src_size);
 }
 
 template<typename TDst, typename TSrc>
@@ -470,7 +437,7 @@ void SavedGame::write(
     const TSrc& src_value,
     ClassTag)
 {
-    throw SavedGameException(
+    throw_error(
         "Not implemented.");
 }
 
@@ -514,12 +481,12 @@ void SavedGame::write(
         "Unsupported types.");
 
     if (!src_values) {
-        throw SavedGameException(
+        throw_error(
             "Null pointer.");
     }
 
     if (src_count < 0) {
-        throw SavedGameException(
+        throw_error(
             "Negative count.");
     }
 
@@ -568,18 +535,22 @@ void SavedGame::write(
     int src_count,
     InplaceTag)
 {
-    accomodate_io_buffer<TSrc>(
+    constexpr auto src_size = static_cast<int>(sizeof(TSrc));
+
+    accomodate_io_buffer(
+        src_size,
         src_count);
 
     std::uninitialized_copy_n(
         src_values,
         src_count,
-        &cast_io_buffer<TSrc&>());
+        &cast_buffer<TSrc&>());
 
     // FIXME Byte order
     //
 
-    advance_io_buffer<TSrc>(
+    advance_io_buffer(
+        src_size,
         src_count);
 }
 
diff --git a/shared/qcommon/ojk_saved_game_fwd.h b/shared/qcommon/ojk_saved_game_fwd.h
index feef931..937b8c6 100644
--- a/shared/qcommon/ojk_saved_game_fwd.h
+++ b/shared/qcommon/ojk_saved_game_fwd.h
@@ -11,7 +11,6 @@
 #include <cstdint>
 #include <string>
 #include <vector>
-#include "ojk_saved_game_exception.h"
 
 
 namespace ojk {
@@ -21,6 +20,7 @@ class SavedGame
 {
 public:
     using ChunkId = uint32_t;
+    using Buffer = std::vector<uint8_t>;
 
 
     SavedGame();
@@ -110,6 +110,24 @@ public:
         int src_count);
 
 
+    // Returns an I/O buffer.
+    Buffer& get_buffer();
+
+    // Returns an I/O buffer.
+    const Buffer& get_buffer() const;
+
+    // Returns an I/O buffer offset;
+    int get_buffer_offset() const;
+
+    uint8_t* get_current_data();
+
+    const uint8_t* get_current_data() const;
+
+    // Casts referenced data at current position into a specified type.
+    template<typename T>
+    T cast_buffer();
+
+
     // Renames a saved game file.
     static void rename(
         const std::string& old_base_file_name,
@@ -124,7 +142,6 @@ public:
 
 
 private:
-    using Buffer = std::vector<uint8_t>;
     using BufferOffset = Buffer::size_type;
     using Paths = std::vector<std::string>;
 
@@ -158,6 +175,15 @@ private:
     bool is_write_failed_;
 
 
+    // Throws an exception.
+    static void throw_error(
+        const char* message);
+
+    // Throws an exception.
+    static void throw_error(
+        const std::string& message);
+
+
     // Compresses data.
     static void compress(
         const Buffer& src_buffer,
@@ -177,29 +203,29 @@ private:
         bool is_open);
 
 
+    // Returns a string representation of a chunk id.
     static std::string get_chunk_id_string(
         uint32_t chunk_id);
 
 
     // Checks if there is enough data for reading in the I/O buffer.
-    template<typename T>
     void check_io_buffer(
+        int item_size,
         int count = 1);
 
     // Resizes the I/O buffer according to desire size of data to write.
-    template<typename T>
     void accomodate_io_buffer(
+        int item_size,
         int count = 1);
 
-    // Casts I/O buffer data at the current offset.
-    template<typename T>
-    T cast_io_buffer();
-
     // Advances the current I/O buffer offset.
-    template<typename T>
     void advance_io_buffer(
+        int item_size,
         int count = 1);
 
+    // Resets I/O buffer offset to zero.
+    void reset_io_buffer_offset();
+
 
     template<typename TSrc, typename TDst>
     void read(

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/openjk.git



More information about the Pkg-games-commits mailing list