Mercurial > malikania
changeset 164:a3ba00cdaa7c
Common: remove id class, closes #773 @5m
While here, remove weak_array forgotten.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 17 Mar 2018 14:36:14 +0100 |
parents | a99a7db489bd |
children | 3e3040d085b5 |
files | libcommon/CMakeLists.txt libcommon/malikania/id.hpp libcommon/malikania/weak_array.hpp tests/libserver/CMakeLists.txt tests/libserver/id/CMakeLists.txt tests/libserver/id/main.cpp |
diffstat | 6 files changed, 0 insertions(+), 397 deletions(-) [+] |
line wrap: on
line diff
--- a/libcommon/CMakeLists.txt Sat Mar 17 14:26:55 2018 +0100 +++ b/libcommon/CMakeLists.txt Sat Mar 17 14:36:14 2018 +0100 @@ -24,7 +24,6 @@ set( HEADERS ${libmlk-common_SOURCE_DIR}/malikania/game.hpp - ${libmlk-common_SOURCE_DIR}/malikania/id.hpp ${libmlk-common_SOURCE_DIR}/malikania/loader.hpp ${libmlk-common_SOURCE_DIR}/malikania/locator.hpp ${libmlk-common_SOURCE_DIR}/malikania/network_stream.hpp
--- a/libcommon/malikania/id.hpp Sat Mar 17 14:26:55 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,159 +0,0 @@ -/* - * id.hpp -- integer id generator - * - * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef MALIKANIA_ID_HPP -#define MALIKANIA_ID_HPP - -/** - * \file id.hpp - * \brief Integer id generator - */ - -#include <limits> -#include <memory> -#include <queue> -#include <stdexcept> - -namespace mlk { - -template <typename T> -class id; - -/** - * \brief Integer id generator - * - * This class helps generating and release unique integer id that can be used - * anywhere. The ids are generated in a sequence and when an id is released it - * is reused instead of incrementing the next number. - * - * The template can use any integral integer but unsigned are preferred. - * - * The maximum number of id is equal to std::numeric_limits<T>::max - 1. - */ -template <typename T> -class id_generator { -private: - static_assert(std::numeric_limits<T>::is_integer, "id_generator requires integral types"); - - friend class id<T>; - - T m_current{0}; - std::priority_queue<T> m_reusable; - -public: - /** - * Get the next id for that player. - * - * \return the id - * \throw std::out_of_range if no number is available - */ - std::unique_ptr<id<T>> next(); -}; - -/** - * \brief RAII based id owner - * - * This class is similar to a std::lock_guard or std::unique_lock in a way that - * the id is acquired when the object is instanciated and released when - * destroyed. - * - * This class does not take ownership of the id_generator so it must still - * exists when the id is destroyed. - */ -template <typename T> -class id { -private: - friend class id_generator<T>; - - id_generator<T>& m_gen; - T m_id; - - /** - * Construct a new id and take the next number. - * - * \param gen the generator - * \throw any exception if IdGen fails to give an id. - */ - inline id(id_generator<T>& gen, T id) noexcept - : m_gen(gen) - , m_id(id) - { - } - -public: - /** - * Destroy the id and release the number. - */ - ~id() noexcept - { - m_gen.m_reusable.push(m_id); - } - - /** - * Get the number id. - * - * \return the id - */ - inline T value() const noexcept - { - return m_id; - } -}; - -template <typename T> -std::unique_ptr<id<T>> id_generator<T>::next() -{ - T i; - - if (m_reusable.size() > 0) { - i = m_reusable.top(); - m_reusable.pop(); - } else { - if (m_current == std::numeric_limits<T>::max()) { - throw std::out_of_range("no id available"); - } - - i = m_current++; - } - - return std::unique_ptr<id<T>>(new id<T>(*this, i)); -} - -/** - * Typedef for std::uint8_t. - */ -using id8 = id<std::uint8_t>; - -/** - * Typedef for std::uint16_t. - */ -using id16 = id<std::uint16_t>; - -/** - * Typedef for std::uint32_t. - */ -using id32 = id<std::uint32_t>; - -/** - * Typedef for std::uint64_t. - */ -using id64 = id<std::uint64_t>; - -} // !mlk - -#endif // !MALIKANIA_ID_HPP
--- a/libcommon/malikania/weak_array.hpp Sat Mar 17 14:26:55 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,79 +0,0 @@ -/* - * weak_array.hpp -- implement a cache array of weak values - * - * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef MALIKANIA_WEAK_ARRAY_HPP -#define MALIKANIA_WEAK_ARRAY_HPP - -/** - * \file weak_array.hpp - * \brief Implement a cache array of weak values. - */ - -#include <memory> -#include <vector> - -namespace mlk { - -/** - * \brief Implement a cache array of weak values. - */ -template <typename T> -class weak_array : public std::vector<std::weak_ptr<T>> { -public: - /** - * Find a value by a predicate. - * - * The predicate must have the following signature: - * - * ````cpp - * bool f(const std::shared_ptr<T>& value) const noexcept - * ```` - * - * \param pred the predicate - * \return nullptr if not found - */ - template <typename Predicate> - std::shared_ptr<T> find_if(Predicate&& pred) const noexcept - { - for (const auto& value : *this) { - auto sp = value.lock(); - - if (sp && pred(sp)) - return sp; - } - - return nullptr; - } - - /** - * Remove values that have no more referenced. - */ - void purge() - { - for (auto it = this->begin(); it != this->end(); ) { - if (!it->lock()) - it = this->erase(it); - else - it++; - } - } -}; - -} // !mlk - -#endif // !MALIKANIA_WEAK_ARRAY_HPP
--- a/tests/libserver/CMakeLists.txt Sat Mar 17 14:26:55 2018 +0100 +++ b/tests/libserver/CMakeLists.txt Sat Mar 17 14:36:14 2018 +0100 @@ -16,5 +16,4 @@ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # -add_subdirectory(id) add_subdirectory(db/account)
--- a/tests/libserver/id/CMakeLists.txt Sat Mar 17 14:26:55 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -# -# CMakeLists.txt -- CMake build system for malikania -# -# Copyright (c) 2013-2018 David Demelier <markand@malikania.fr> -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# - -malikania_create_test( - NAME id - LIBRARIES libmlk-common - SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp -)
--- a/tests/libserver/id/main.cpp Sat Mar 17 14:26:55 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ -/* - * main.cpp -- test Id - * - * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <cstdint> -#include <thread> - -#define BOOST_TEST_MODULE "Id" -#include <boost/test/unit_test.hpp> - -#include <malikania/id.hpp> - -using namespace mlk; - -/* - * Basic use case. - * ------------------------------------------------------------------ - */ - -class test_id { -protected: - id_generator<unsigned> m_idgen; -}; - -BOOST_FIXTURE_TEST_SUITE(test_id_suite, test_id) - -BOOST_AUTO_TEST_CASE(simple) -{ - auto i0 = m_idgen.next(); - auto i1 = m_idgen.next(); - auto i2 = m_idgen.next(); - - BOOST_REQUIRE_EQUAL(0U, i0->value()); - BOOST_REQUIRE_EQUAL(1U, i1->value()); - BOOST_REQUIRE_EQUAL(2U, i2->value()); -} - -BOOST_AUTO_TEST_CASE(release1) -{ - auto i0 = m_idgen.next(); // 0 - auto i1 = m_idgen.next(); // 1 - auto i2 = m_idgen.next(); // 2 - - i1 = nullptr; - - auto n1 = m_idgen.next(); - auto n2 = m_idgen.next(); - - /* - * 0 and 2 are currently in use. - * - * The next id must be 1 and then 3. - */ - BOOST_REQUIRE_EQUAL(1U, n1->value()); - BOOST_REQUIRE_EQUAL(3U, n2->value()); -} - -BOOST_AUTO_TEST_CASE(release2) -{ - auto i0 = m_idgen.next(); // 0 - auto i1 = m_idgen.next(); // 1 - auto i2 = m_idgen.next(); // 2 - - i1 = nullptr; - i0 = nullptr; - - /* - * Only 2 is in use, next id must be: - * - * - 1 - * - 0 - * - 3 - */ - auto n1 = m_idgen.next(); - auto n2 = m_idgen.next(); - auto n3 = m_idgen.next(); - - BOOST_REQUIRE_EQUAL(1U, n1->value()); - BOOST_REQUIRE_EQUAL(0U, n2->value()); - BOOST_REQUIRE_EQUAL(3U, n3->value()); -} - -/* - * Limit test. - * ------------------------------------------------------------------ - */ - -BOOST_AUTO_TEST_CASE(max) -{ - id_generator<std::int8_t> idgen; - std::vector<std::unique_ptr<id<std::int8_t>>> ids; - - try { - for (int i = 0; i < 127; ++i) { - ids.push_back(idgen.next()); - } - } catch (const std::exception &ex) { - BOOST_FAIL(ex.what()); - } - - BOOST_REQUIRE_EQUAL(127U, ids.size()); - BOOST_REQUIRE_EQUAL(126, ids[126]->value()); -} - -BOOST_AUTO_TEST_CASE(fail) -{ - id_generator<std::int8_t> idgen; - std::vector<std::unique_ptr<id<std::int8_t>>> ids; - - BOOST_REQUIRE_THROW({ - for (int i = 0; i < 200; ++i) { - ids.push_back(idgen.next()); - } - }, std::exception); - - BOOST_REQUIRE_EQUAL(127U, ids.size()); - BOOST_REQUIRE_EQUAL(126, ids[126]->value()); -} - -BOOST_AUTO_TEST_SUITE_END()