diff options
Diffstat (limited to 'src/compat')
-rw-r--r-- | src/compat/attr.hpp | 20 | ||||
-rw-r--r-- | src/compat/cast.cpp | 5 | ||||
-rw-r--r-- | src/compat/cast.hpp | 14 | ||||
-rw-r--r-- | src/compat/fun.hpp | 14 | ||||
-rw-r--r-- | src/compat/fwd.hpp | 27 | ||||
-rw-r--r-- | src/compat/iter.cpp | 5 | ||||
-rw-r--r-- | src/compat/iter.hpp | 66 | ||||
-rw-r--r-- | src/compat/iter_test.cpp | 65 | ||||
-rw-r--r-- | src/compat/memory.cpp | 5 | ||||
-rw-r--r-- | src/compat/memory.hpp | 13 | ||||
-rw-r--r-- | src/compat/nullpo.cpp | 4 | ||||
-rw-r--r-- | src/compat/nullpo.hpp | 29 | ||||
-rw-r--r-- | src/compat/rawmem.cpp | 5 | ||||
-rw-r--r-- | src/compat/rawmem.hpp | 17 | ||||
-rw-r--r-- | src/compat/time_t.cpp (renamed from src/compat/alg.cpp) | 11 | ||||
-rw-r--r-- | src/compat/time_t.hpp (renamed from src/compat/alg.hpp) | 26 |
16 files changed, 245 insertions, 81 deletions
diff --git a/src/compat/attr.hpp b/src/compat/attr.hpp index ea7ba86..238a5d5 100644 --- a/src/compat/attr.hpp +++ b/src/compat/attr.hpp @@ -1,5 +1,4 @@ -#ifndef TMWA_COMPAT_ATTR_HPP -#define TMWA_COMPAT_ATTR_HPP +#pragma once // attr.hpp - Attributes. // // Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com> @@ -19,13 +18,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -# include "../sanity.hpp" +#include "fwd.hpp" -# ifdef __clang__ -# define FALLTHROUGH [[clang::fallthrough]] -# else -# define FALLTHROUGH /* fallthrough */ -# endif - -#endif // TMWA_COMPAT_ATTR_HPP +namespace tmwa +{ +#ifdef __clang__ +# define FALLTHROUGH [[clang::fallthrough]] +#else +# define FALLTHROUGH /* fallthrough */ +#endif +} // namespace tmwa diff --git a/src/compat/cast.cpp b/src/compat/cast.cpp index 43c0b53..482529d 100644 --- a/src/compat/cast.cpp +++ b/src/compat/cast.cpp @@ -19,3 +19,8 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. #include "../poison.hpp" + + +namespace tmwa +{ +} // namespace tmwa diff --git a/src/compat/cast.hpp b/src/compat/cast.hpp index 8db486f..35dbc62 100644 --- a/src/compat/cast.hpp +++ b/src/compat/cast.hpp @@ -1,5 +1,4 @@ -#ifndef TMWA_COMPAT_CAST_HPP -#define TMWA_COMPAT_CAST_HPP +#pragma once // cast.hpp - Change the type of a variable. // // Copyright © 2011-2014 Ben Longbons <b.r.longbons@gmail.com> @@ -19,12 +18,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -# include "../sanity.hpp" +#include "fwd.hpp" -# include <utility> -# include <type_traits> +#include <utility> +#include <type_traits> +namespace tmwa +{ template<class T> const T& const_(T& t) { @@ -68,5 +69,4 @@ typename std::remove_pointer<T>::type *sign_cast(U *u) static_assert(sizeof(T_) == sizeof(U), "sign cast must be same size"); return reinterpret_cast<T_ *>(u); } - -#endif // TMWA_COMPAT_CAST_HPP +} // namespace tmwa diff --git a/src/compat/fun.hpp b/src/compat/fun.hpp index 005f2c5..8073fe7 100644 --- a/src/compat/fun.hpp +++ b/src/compat/fun.hpp @@ -1,5 +1,4 @@ -#ifndef TMWA_COMPAT_FUN_HPP -#define TMWA_COMPAT_FUN_HPP +#pragma once // fun.hpp - Functional placeholders. // // Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com> @@ -19,13 +18,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -# include "../sanity.hpp" +#include "fwd.hpp" -# include <functional> - -# include "../sanity.hpp" +#include <functional> +namespace tmwa +{ namespace ph = std::placeholders; - -#endif // TMWA_COMPAT_FUN_HPP +} // namespace tmwa diff --git a/src/compat/fwd.hpp b/src/compat/fwd.hpp new file mode 100644 index 0000000..45f3c24 --- /dev/null +++ b/src/compat/fwd.hpp @@ -0,0 +1,27 @@ +#pragma once +// compat/fwd.hpp - list of type names for compat libs +// +// Copyright © 2014 Ben Longbons <b.r.longbons@gmail.com> +// +// This file is part of The Mana World (Athena server) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. + +#include "../sanity.hpp" + + +namespace tmwa +{ +// meh, add more when I feel like it +} // namespace tmwa diff --git a/src/compat/iter.cpp b/src/compat/iter.cpp index f0ab0af..b6d6b63 100644 --- a/src/compat/iter.cpp +++ b/src/compat/iter.cpp @@ -19,3 +19,8 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. #include "../poison.hpp" + + +namespace tmwa +{ +} // namespace tmwa diff --git a/src/compat/iter.hpp b/src/compat/iter.hpp index 7793d90..ff146a0 100644 --- a/src/compat/iter.hpp +++ b/src/compat/iter.hpp @@ -1,5 +1,4 @@ -#ifndef TMWA_COMPAT_ITER_HPP -#define TMWA_COMPAT_ITER_HPP +#pragma once // iter.hpp - tools for dealing with iterators // // Copyright © 2012-2014 Ben Longbons <b.r.longbons@gmail.com> @@ -19,11 +18,13 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -# include "../sanity.hpp" +#include "fwd.hpp" -# include <iterator> +#include <iterator> +namespace tmwa +{ /// Simple class to use a pair of iterators with foreach template<class It> class IteratorPair @@ -94,4 +95,59 @@ IteratorPair<ValueIterator<T>> value_range(T b, T e) return {b, e}; } -#endif // TMWA_COMPAT_ITER_HPP + +template<class T, class F, class C> +class FilterIterator +{ + F filter; + C *container; + + using InnerIterator = decltype(std::begin(*container)); + InnerIterator impl; +public: + void post_adv() + { + while (impl != std::end(*container)) + { + if (filter(*impl)) + break; + ++impl; + } + } + + FilterIterator(C *c, F f) + : filter(f), container(c), impl(std::begin(*c)) + { + post_adv(); + } + + void operator ++() + { + ++impl; + post_adv(); + } + + T operator *() + { + return *impl; + } + + friend + bool operator != (FilterIterator l, FilterIterator) + { + return l.impl != std::end(*l.container); + } +}; + +template<class T> +bool is_truthy(T v) +{ + return v; +} + +template<class T, class F=decltype(is_truthy<T>)*, class C> +IteratorPair<FilterIterator<T, F, C>> filter_iterator(C *c, F f=is_truthy<T>) +{ + return {FilterIterator<T, F, C>(c, f), FilterIterator<T, F, C>(c, f)}; +} +} // namespace tmwa diff --git a/src/compat/iter_test.cpp b/src/compat/iter_test.cpp index a07cb0f..6732c47 100644 --- a/src/compat/iter_test.cpp +++ b/src/compat/iter_test.cpp @@ -20,10 +20,15 @@ #include <gtest/gtest.h> -#include "../strings/xstring.hpp" +#include <algorithm> + +#include "../ints/udl.hpp" #include "../poison.hpp" + +namespace tmwa +{ TEST(iterpair, strings) { IteratorPair<ValueIterator<char>> pair = value_range('0', ':'); @@ -33,7 +38,7 @@ TEST(iterpair, strings) TEST(iterpair, signed8) { - IteratorPair<ValueIterator<int8_t>> pair = value_range(int8_t(-128), int8_t(127)); + IteratorPair<ValueIterator<int8_t>> pair = value_range(-128_n8, +127_p8); int8_t arr[255] = { -128, -127, -126, -125, -124, -123, -122, -121, -120, @@ -68,7 +73,7 @@ TEST(iterpair, signed8) TEST(iterpair, unsigned8) { - IteratorPair<ValueIterator<uint8_t>> pair = value_range(uint8_t(0), uint8_t(255)); + IteratorPair<ValueIterator<uint8_t>> pair = value_range(0_u8, 255_u8); uint8_t arr[255] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, @@ -100,3 +105,57 @@ TEST(iterpair, unsigned8) }; EXPECT_TRUE(std::equal(pair.begin(), pair.end(), arr)); } + +static +bool is_odd_ref(int& i) +{ + return i % 2; +} + +TEST(iterpair, filter1) +{ + int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + + int expected_arr[] = {1, 3, 5, 7}; + int *expected_it = expected_arr; + int *expected_end = expected_arr + 4; + + for (int& i : filter_iterator<int&>(&arr, is_odd_ref)) + { + EXPECT_EQ(i, *expected_it); + ++expected_it; + } + EXPECT_EQ(expected_it, expected_end); +} + +TEST(iterpair, filter2) +{ + std::vector<int> vals = {0, 1, 0, 2, 0, 3, 0}; + + int sum = 0, count = 0; + for (int i : filter_iterator<int>(&vals)) + { + sum += i; + count++; + } + EXPECT_EQ(sum, 6); + EXPECT_EQ(count, 3); +} + +TEST(iterpair, filter3) +{ + int one = 1; + int two = 2; + int three = 3; + std::vector<int *> vals = {nullptr, &one, nullptr, &two, nullptr, &three, nullptr}; + + int sum = 0, count = 0; + for (int *i : filter_iterator<int *>(&vals)) + { + sum += *i; + count++; + } + EXPECT_EQ(sum, 6); + EXPECT_EQ(count, 3); +} +} // namespace tmwa diff --git a/src/compat/memory.cpp b/src/compat/memory.cpp index 5db23b2..f9f2c22 100644 --- a/src/compat/memory.cpp +++ b/src/compat/memory.cpp @@ -19,3 +19,8 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. #include "../poison.hpp" + + +namespace tmwa +{ +} // namespace tmwa diff --git a/src/compat/memory.hpp b/src/compat/memory.hpp index 2c0f742..566cc8b 100644 --- a/src/compat/memory.hpp +++ b/src/compat/memory.hpp @@ -1,5 +1,4 @@ -#ifndef TMWA_COMPAT_MEMORY_HPP -#define TMWA_COMPAT_MEMORY_HPP +#pragma once // memory.hpp - I forget ... // // Copyright © 2013-2014 Ben Longbons <b.r.longbons@gmail.com> @@ -19,11 +18,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -# include "../sanity.hpp" +#include "fwd.hpp" -# include <memory> +#include <memory> +#include <type_traits> +namespace tmwa +{ template<class T> struct is_array_of_unknown_bound : std::is_same<T, typename std::remove_extent<T>::type[]> @@ -41,5 +43,4 @@ typename std::enable_if<is_array_of_unknown_bound<T>::value, std::unique_ptr<T, typedef typename std::remove_extent<T>::type E; return std::unique_ptr<E[], D>(new E[sz]()); } - -#endif // TMWA_COMPAT_MEMORY_HPP +} // namespace tmwa diff --git a/src/compat/nullpo.cpp b/src/compat/nullpo.cpp index a31cc34..bb80b27 100644 --- a/src/compat/nullpo.cpp +++ b/src/compat/nullpo.cpp @@ -24,6 +24,9 @@ #include "../poison.hpp" + +namespace tmwa +{ /// Actual output function static void nullpo_info(const char *file, int line, const char *func) @@ -46,3 +49,4 @@ bool nullpo_chk(const char *file, int line, const char *func, nullpo_info(file, line, func); return 1; } +} // namespace tmwa diff --git a/src/compat/nullpo.hpp b/src/compat/nullpo.hpp index 75f8110..5be674a 100644 --- a/src/compat/nullpo.hpp +++ b/src/compat/nullpo.hpp @@ -1,5 +1,4 @@ -#ifndef TMWA_COMPAT_NULLPO_HPP -#define TMWA_COMPAT_NULLPO_HPP +#pragma once // nullpo.hpp - Non-fatal pointer assertions. // // Copyright © ????-2004 Athena Dev Teams @@ -26,23 +25,28 @@ //# define BUG_FREE /// All functions print to standard error (was: standard output) -/// nullpo_ret(cond) - return 0 if given pointer is NULL +/// nullpo_retn(cond) - return nullptr if given pointer is nullptr +/// nullpo_retz(cond) - return 0 if given pointer is nullptr /// nullpo_retv(cond) - just return (function returns void) /// nullpo_retr(rv, cond) - return given value instead -# ifndef BUG_FREE -# define nullpo_retr(ret, t) \ +#ifndef BUG_FREE +# define nullpo_retr(ret, t) \ if (nullpo_chk(__FILE__, __LINE__, __PRETTY_FUNCTION__, t)) \ return ret; -# else // BUG_FREE -# define nullpo_retr(ret, t) /*t*/ -# endif // BUG_FREE +#else // BUG_FREE +# define nullpo_retr(ret, t) /*t*/ +#endif // BUG_FREE -# define nullpo_ret(t) nullpo_retr(0, t) -# define nullpo_retv(t) nullpo_retr(, t) +#define nullpo_retn(t) nullpo_retr(nullptr, t) +#define nullpo_retz(t) nullpo_retr(0, t) +#define nullpo_retv(t) nullpo_retr(, t) -# include "../sanity.hpp" +#include "fwd.hpp" + +namespace tmwa +{ /// Used by macros in this header bool nullpo_chk(const char *file, int line, const char *func, const void *target); @@ -57,5 +61,4 @@ bool nullpo_chk(const char *file, int line, const char *func, T *target) { return nullpo_chk(file, line, func, static_cast<const void *>(target)); } - -#endif // TMWA_COMPAT_NULLPO_HPP +} // namespace tmwa diff --git a/src/compat/rawmem.cpp b/src/compat/rawmem.cpp index 74fdc0b..d322437 100644 --- a/src/compat/rawmem.cpp +++ b/src/compat/rawmem.cpp @@ -19,3 +19,8 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. #include "../poison.hpp" + + +namespace tmwa +{ +} // namespace tmwa diff --git a/src/compat/rawmem.hpp b/src/compat/rawmem.hpp index bbe917c..c271a56 100644 --- a/src/compat/rawmem.hpp +++ b/src/compat/rawmem.hpp @@ -1,5 +1,4 @@ -#ifndef TMWA_COMPAT_RAWMEM_HPP -#define TMWA_COMPAT_RAWMEM_HPP +#pragma once // rawmem.hpp - Ignore poisoning and really frob this memory unsafely. // // Copyright © 2013-2014 Ben Longbons <b.r.longbons@gmail.com> @@ -19,12 +18,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -# include <cstddef> -# include <cstdint> -# include <cstring> +#include <cstddef> +#include <cstdint> +#include <cstring> -# include "../sanity.hpp" +#include "fwd.hpp" + +namespace tmwa +{ inline void really_memcpy(uint8_t *dest, const uint8_t *src, size_t n) { @@ -47,5 +49,4 @@ void really_memset0(uint8_t *dest, size_t n) { memset(dest, '\0', n); } - -#endif // TMWA_COMPAT_RAWMEM_HPP +} // namespace tmwa diff --git a/src/compat/alg.cpp b/src/compat/time_t.cpp index 8cfd00a..ee0bbde 100644 --- a/src/compat/alg.cpp +++ b/src/compat/time_t.cpp @@ -1,7 +1,7 @@ -#include "alg.hpp" -// alg.cpp - Silly math stuff. +#include "time_t.hpp" +// time_t.cpp - time_t with a reliable representation // -// Copyright © 2012 Ben Longbons <b.r.longbons@gmail.com> +// Copyright © 2013-2014 Ben Longbons <b.r.longbons@gmail.com> // // This file is part of The Mana World (Athena server) // @@ -19,3 +19,8 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. #include "../poison.hpp" + + +namespace tmwa +{ +} // namespace tmwa diff --git a/src/compat/alg.hpp b/src/compat/time_t.hpp index adf3f3f..9e7cf25 100644 --- a/src/compat/alg.hpp +++ b/src/compat/time_t.hpp @@ -1,8 +1,7 @@ -#ifndef TMWA_COMPAT_ALG_HPP -#define TMWA_COMPAT_ALG_HPP -// alg.hpp - Silly math stuff. +#pragma once +// time_t.hpp - time_t with a reliable representation // -// Copyright © 2012 Ben Longbons <b.r.longbons@gmail.com> +// Copyright © 2013-2014 Ben Longbons <b.r.longbons@gmail.com> // // This file is part of The Mana World (Athena server) // @@ -19,21 +18,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -# include "../sanity.hpp" +#include "fwd.hpp" -# include <type_traits> +// TODO fix this ordering violation by promoting TimeT here +#include "../mmo/utils.hpp" -template<class A, class B> -typename std::common_type<A, B>::type min(A a, B b) +namespace tmwa { - return a < b ? a : b; -} - -template<class A, class B> -typename std::common_type<A, B>::type max(A a, B b) -{ - return b < a ? a : b; -} - -#endif // TMWA_COMPAT_ALG_HPP +} // namespace tmwa |