diff --git a/src/cache.cc b/src/cache.cc index da6ea88..7458087 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -7,8 +7,6 @@ #include "cache.hh" #include "state.hh" -Cache::Cache() {} - Cache::~Cache() {} std::pair DynamicCache::get(const State & state) { @@ -49,13 +47,12 @@ std::size_t FixedCache::_get_index(const std::tuple PersistentCache::get(const State & state) { @@ -106,9 +103,9 @@ std::string PersistentCache::_encode_key(const State & state) const { auto [key1, key2, key3] = state.get_keys(); std::string result{sizeof(key1) + sizeof(key2) + sizeof(key3), 0, std::string::allocator_type{}}; - std::copy_n(reinterpret_cast(&key1), sizeof(key1), result.begin()); - std::copy_n(reinterpret_cast(&key2), sizeof(key2), result.begin() + sizeof(key1)); - std::copy_n(reinterpret_cast(&key3), sizeof(key3), result.begin() + sizeof(key1) + sizeof(key2)); + std::copy_n(reinterpret_cast(&key1), sizeof(key1), result.begin()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + std::copy_n(reinterpret_cast(&key2), sizeof(key2), result.begin() + sizeof(key1)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + std::copy_n(reinterpret_cast(&key3), sizeof(key3), result.begin() + sizeof(key1) + sizeof(key2)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) return result; } @@ -119,8 +116,8 @@ std::string PersistentCache::_encode_value(int value, Milliframes frames) const std::string result{sizeof(int64_t) * 2, 0, std::string::allocator_type{}}; - std::copy(reinterpret_cast(&value1), reinterpret_cast(&value1) + sizeof(value1), result.begin()); - std::copy(reinterpret_cast(&value2), reinterpret_cast(&value2) + sizeof(value2), result.begin() + sizeof(value1)); + std::copy(reinterpret_cast(&value1), reinterpret_cast(&value1) + sizeof(value1), result.begin()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast, cppcoreguidelines-pro-bounds-pointer-arithmetic) + std::copy(reinterpret_cast(&value2), reinterpret_cast(&value2) + sizeof(value2), result.begin() + sizeof(value1)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast, cppcoreguidelines-pro-bounds-pointer-arithmetic) return result; } @@ -129,8 +126,8 @@ std::pair PersistentCache::_decode_value(const std::string & d int64_t value1{0}; int64_t value2{0}; - std::copy(data.begin(), data.begin() + sizeof(value1), reinterpret_cast(&value1)); - std::copy(data.begin() + sizeof(value1), data.begin() + sizeof(value1) + sizeof(value2), reinterpret_cast(&value2)); + std::copy(data.begin(), data.begin() + sizeof(value1), reinterpret_cast(&value1)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + std::copy(data.begin() + sizeof(value1), data.begin() + sizeof(value1) + sizeof(value2), reinterpret_cast(&value2)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast, cppcoreguidelines-pro-bounds-pointer-arithmetic) return std::make_pair(static_cast(value1), Milliframes{value2}); } diff --git a/src/cache.hh b/src/cache.hh index 0f457d2..bf124c2 100644 --- a/src/cache.hh +++ b/src/cache.hh @@ -20,7 +20,7 @@ enum class CacheType { class Cache { public: - Cache(); + Cache() = default; Cache(const Cache &) = delete; Cache & operator=(const Cache &) = delete; @@ -41,7 +41,7 @@ class DynamicCache : public Cache { class FixedCache : public Cache { public: - FixedCache(std::size_t size); + explicit FixedCache(std::size_t size); std::pair get(const State & state) override; void set(const State & state, int value, Milliframes frames) override; @@ -56,7 +56,7 @@ class FixedCache : public Cache { class PersistentCache : public Cache { public: - PersistentCache(const std::string & filename); + explicit PersistentCache(const std::string & filename); ~PersistentCache() override; std::pair get(const State & state) override; @@ -67,8 +67,9 @@ class PersistentCache : public Cache { std::string _encode_value(int value, Milliframes frames) const; std::pair _decode_value(const std::string & data) const; - leveldb::DB * _db; - std::size_t _states; + leveldb::Options _options{}; + leveldb::DB * _db{nullptr}; + std::size_t _states{0}; }; #endif // ROSA_CACHE_HH