summaryrefslogtreecommitdiff
path: root/src/generic
diff options
context:
space:
mode:
Diffstat (limited to 'src/generic')
-rw-r--r--src/generic/array.cpp26
-rw-r--r--src/generic/array.hpp118
-rw-r--r--src/generic/array_test.cpp162
-rw-r--r--src/generic/db.cpp5
-rw-r--r--src/generic/db.hpp15
-rw-r--r--src/generic/dumb_ptr.cpp26
-rw-r--r--src/generic/dumb_ptr.hpp272
-rw-r--r--src/generic/enum.cpp5
-rw-r--r--src/generic/enum.hpp82
-rw-r--r--src/generic/fwd.hpp30
-rw-r--r--src/generic/intern-pool.cpp5
-rw-r--r--src/generic/intern-pool.hpp24
-rw-r--r--src/generic/intern-pool_test.cpp22
-rw-r--r--src/generic/matrix.cpp5
-rw-r--r--src/generic/matrix.hpp15
-rw-r--r--src/generic/md5.cpp9
-rw-r--r--src/generic/md5.hpp21
-rw-r--r--src/generic/md5_test.cpp18
-rw-r--r--src/generic/oops.cpp48
-rw-r--r--src/generic/oops.hpp40
-rw-r--r--src/generic/oops_test.cpp54
-rw-r--r--src/generic/operators.cpp5
-rw-r--r--src/generic/operators.hpp11
-rw-r--r--src/generic/random.cpp4
-rw-r--r--src/generic/random.hpp15
-rw-r--r--src/generic/random.t.hpp11
-rw-r--r--src/generic/random2.hpp17
27 files changed, 916 insertions, 149 deletions
diff --git a/src/generic/array.cpp b/src/generic/array.cpp
new file mode 100644
index 0000000..3063569
--- /dev/null
+++ b/src/generic/array.cpp
@@ -0,0 +1,26 @@
+#include "array.hpp"
+// array.cpp - A simple bounds-checked array.
+//
+// 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 "../poison.hpp"
+
+
+namespace tmwa
+{
+} // namespace tmwa
diff --git a/src/generic/array.hpp b/src/generic/array.hpp
new file mode 100644
index 0000000..dccb91e
--- /dev/null
+++ b/src/generic/array.hpp
@@ -0,0 +1,118 @@
+#pragma once
+// array.hpp - A simple bounds-checked array.
+//
+// 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 "fwd.hpp"
+
+#include <cassert>
+#include <cstddef>
+
+#include "oops.hpp"
+
+
+namespace tmwa
+{
+template<class I, I be, I en>
+struct ExclusiveIndexing
+{
+ using index_type = I;
+ constexpr static size_t index_to_offset(index_type idx)
+ { return static_cast<size_t>(idx) - static_cast<size_t>(be); }
+ constexpr static index_type offset_to_index(size_t off)
+ { return static_cast<I>(off + static_cast<size_t>(be)); }
+ constexpr static size_t alloc_size = index_to_offset(en) - index_to_offset(be);
+};
+
+template<size_t n>
+using SimpleIndexing = ExclusiveIndexing<size_t, 0, n>;
+
+template<class I, I lo, I hi>
+struct InclusiveIndexing
+{
+ using index_type = I;
+ constexpr static size_t index_to_offset(index_type idx)
+ { return static_cast<size_t>(idx) - static_cast<size_t>(lo); }
+ constexpr static index_type offset_to_index(size_t off)
+ { return static_cast<I>(off + static_cast<size_t>(lo)); }
+ constexpr static size_t alloc_size = index_to_offset(hi) - index_to_offset(lo) + 1;
+};
+
+template<class E, E n=E::COUNT>
+struct EnumIndexing : ExclusiveIndexing<E, static_cast<E>(0), n>
+{
+};
+
+template<class I, size_t limit>
+struct InventoryIndexing
+{
+ using index_type = I;
+ constexpr static size_t index_to_offset(index_type idx)
+ { return idx.get0(); }
+ constexpr static index_type offset_to_index(size_t off)
+ { return I::from(off); }
+ constexpr static size_t alloc_size = limit;
+};
+
+template<class T, class I>
+struct GenericArray
+{
+ T data[I::alloc_size];
+public:
+ T *begin()
+ { return data + 0; }
+ T *end()
+ { return data + I::alloc_size; }
+ const T *begin() const
+ { return data + 0; }
+ const T *end() const
+ { return data + I::alloc_size; }
+ size_t size() const
+ { return I::alloc_size; }
+
+ T& operator [](typename I::index_type i_)
+ {
+ size_t i = I::index_to_offset(i_);
+ ALLEGE ("index in bounds", i < size());
+ return data[i];
+ }
+ const T& operator [](typename I::index_type i_) const
+ {
+ size_t i = I::index_to_offset(i_);
+ ALLEGE ("index in bounds", i < size());
+ return data[i];
+ }
+
+ friend bool operator == (GenericArray& lhs, GenericArray& rhs)
+ {
+ for (size_t i = 0; i < I::alloc_size; ++i)
+ {
+ if (lhs.data[i] != rhs.data[i])
+ return false;
+ }
+ return true;
+ }
+ friend bool operator != (GenericArray& lhs, GenericArray& rhs)
+ {
+ return !(lhs == rhs);
+ }
+};
+
+template<class T, size_t n>
+using Array = GenericArray<T, SimpleIndexing<n>>;
+} // namespace tmwa
diff --git a/src/generic/array_test.cpp b/src/generic/array_test.cpp
new file mode 100644
index 0000000..aff47ee
--- /dev/null
+++ b/src/generic/array_test.cpp
@@ -0,0 +1,162 @@
+#include "array.hpp"
+// array_test.cpp - Testsuite for a simple bounds-checked array.
+//
+// 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 <gtest/gtest.h>
+
+#include "../poison.hpp"
+
+
+namespace tmwa
+{
+TEST(Array, simple)
+{
+ GenericArray<int, SimpleIndexing<3>> a;
+ try
+ {
+ a[0];
+ a[1];
+ a[2];
+ SUCCEED();
+ }
+ catch (const AssertionError&)
+ {
+ FAIL();
+ }
+ try
+ {
+ a[3];
+ FAIL();
+ }
+ catch (const AssertionError&)
+ {
+ SUCCEED();
+ }
+}
+
+TEST(Array, inclusive1)
+{
+ GenericArray<int, InclusiveIndexing<int, 1, 3>> a;
+ try
+ {
+ a[0];
+ FAIL();
+ }
+ catch (const AssertionError&)
+ {
+ SUCCEED();
+ }
+ try
+ {
+ a[1];
+ a[2];
+ a[3];
+ SUCCEED();
+ }
+ catch (const AssertionError&)
+ {
+ FAIL();
+ }
+ try
+ {
+ a[4];
+ FAIL();
+ }
+ catch (const AssertionError&)
+ {
+ SUCCEED();
+ }
+}
+
+TEST(Array, negative)
+{
+ GenericArray<int, InclusiveIndexing<int, -1, 1>> a;
+ try
+ {
+ a[-2];
+ FAIL();
+ }
+ catch (const AssertionError&)
+ {
+ SUCCEED();
+ }
+ try
+ {
+ a[-1];
+ a[0];
+ a[1];
+ SUCCEED();
+ }
+ catch (const AssertionError&)
+ {
+ FAIL();
+ }
+ try
+ {
+ a[2];
+ FAIL();
+ }
+ catch (const AssertionError&)
+ {
+ SUCCEED();
+ }
+}
+
+TEST(Array, enum)
+{
+ enum class Blah
+ {
+ FOO,
+ BAR,
+ BAZ,
+ COUNT,
+ };
+
+ GenericArray<int, EnumIndexing<Blah>> a;
+ try
+ {
+ a[static_cast<Blah>(-1)];
+ FAIL();
+ }
+ catch (const AssertionError&)
+ {
+ SUCCEED();
+ }
+ try
+ {
+ a[Blah::FOO];
+ a[Blah::BAR];
+ a[Blah::BAZ];
+ SUCCEED();
+ }
+ catch (const AssertionError&)
+ {
+ FAIL();
+ }
+ try
+ {
+ a[Blah::COUNT];
+ FAIL();
+ }
+ catch (const AssertionError&)
+ {
+ SUCCEED();
+ }
+}
+} // namespace tmwa
diff --git a/src/generic/db.cpp b/src/generic/db.cpp
index b953ff0..458068c 100644
--- a/src/generic/db.cpp
+++ b/src/generic/db.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/generic/db.hpp b/src/generic/db.hpp
index 314c449..90c4f92 100644
--- a/src/generic/db.hpp
+++ b/src/generic/db.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_GENERIC_DB_HPP
-#define TMWA_GENERIC_DB_HPP
+#pragma once
// db.hpp - convenience wrappers over std::map<K, V>
//
// Copyright © 2013 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 <map>
-# include <memory>
+#include <map>
+#include <memory>
+
+namespace tmwa
+{
template<class K, class V>
class Map
{
@@ -176,5 +178,4 @@ public:
return impl.size();
}
};
-
-#endif // TMWA_GENERIC_DB_HPP
+} // namespace tmwa
diff --git a/src/generic/dumb_ptr.cpp b/src/generic/dumb_ptr.cpp
new file mode 100644
index 0000000..e690f7d
--- /dev/null
+++ b/src/generic/dumb_ptr.cpp
@@ -0,0 +1,26 @@
+#include "dumb_ptr.hpp"
+// dumb_ptr.cpp - dummy file to make Make dependencies work
+//
+// Copyright © 2013 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 "../poison.hpp"
+
+
+namespace tmwa
+{
+} // namespace tmwa
diff --git a/src/generic/dumb_ptr.hpp b/src/generic/dumb_ptr.hpp
new file mode 100644
index 0000000..da84171
--- /dev/null
+++ b/src/generic/dumb_ptr.hpp
@@ -0,0 +1,272 @@
+#pragma once
+// dumb_ptr.hpp - temporary new/delete wrappers
+//
+// Copyright © 2013 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 "fwd.hpp"
+
+#include <cstring>
+
+#include <algorithm>
+#include <utility>
+
+#include "../strings/astring.hpp"
+#include "../strings/zstring.hpp"
+#include "../strings/xstring.hpp"
+
+
+namespace tmwa
+{
+// unmanaged new/delete-able pointer
+// should be replaced by std::unique_ptr<T>
+template<class T>
+class dumb_ptr
+{
+ template<class U>
+ friend class dumb_ptr;
+ T *impl;
+public:
+ explicit
+ dumb_ptr(T *p=nullptr) noexcept
+ : impl(p)
+ {}
+ template<class U>
+ dumb_ptr(dumb_ptr<U> p)
+ : impl(p.impl)
+ {}
+ dumb_ptr(std::nullptr_t)
+ : impl(nullptr)
+ {}
+
+ void delete_()
+ {
+ delete impl;
+ *this = nullptr;
+ }
+ template<class... A>
+ void new_(A&&... a)
+ {
+ impl = new T(std::forward<A>(a)...);
+ }
+ template<class... A>
+ static
+ dumb_ptr<T> make(A&&... a)
+ {
+ return dumb_ptr<T>(new T(std::forward<A>(a)...));
+ }
+ dumb_ptr& operator = (std::nullptr_t)
+ {
+ impl = nullptr;
+ return *this;
+ }
+
+ T& operator *() const
+ {
+ return *impl;
+ }
+ T *operator->() const
+ {
+ return impl;
+ }
+
+ explicit
+ operator bool() const
+ {
+ return impl;
+ }
+ bool operator !() const
+ {
+ return !impl;
+ }
+
+ friend bool operator == (dumb_ptr l, dumb_ptr r)
+ {
+ return l.impl == r.impl;
+ }
+ friend bool operator != (dumb_ptr l, dumb_ptr r)
+ {
+ return !(l == r);
+ }
+};
+
+// unmanaged new/delete-able pointer
+// should be replaced by std::unique_ptr<T[]> or std::vector<T>
+template<class T>
+class dumb_ptr<T[]>
+{
+ T *impl;
+ size_t sz;
+public:
+ dumb_ptr() noexcept : impl(), sz() {}
+ dumb_ptr(std::nullptr_t)
+ : impl(nullptr), sz(0) {}
+ dumb_ptr(T *p, size_t z)
+ : impl(p)
+ , sz(z)
+ {}
+
+ void delete_()
+ {
+ delete[] impl;
+ *this = nullptr;
+ }
+ void new_(size_t z)
+ {
+ impl = new T[z]();
+ sz = z;
+ }
+ static
+ dumb_ptr<T[]> make(size_t z)
+ {
+ return dumb_ptr<T[]>(new T[z](), z);
+ }
+ dumb_ptr& operator = (std::nullptr_t)
+ {
+ impl = nullptr;
+ sz = 0;
+ return *this;
+ }
+
+ size_t size() const
+ {
+ return sz;
+ }
+ void resize(size_t z)
+ {
+ if (z == sz)
+ return;
+ T *np = new T[z]();
+ // not exception-safe, but we don't have a dtor anyway
+ size_t i = std::min(z, sz);
+ while (i-->0)
+ np[i] = std::move(impl[i]);
+ delete[] impl;
+ impl = np;
+ sz = z;
+ }
+
+ T& operator[](size_t i) const
+ {
+ return impl[i];
+ }
+
+ explicit
+ operator bool() const
+ {
+ return impl;
+ }
+ bool operator !() const
+ {
+ return !impl;
+ }
+
+ friend bool operator == (dumb_ptr l, dumb_ptr r)
+ {
+ return l.impl == r.impl;
+ }
+ friend bool operator != (dumb_ptr l, dumb_ptr r)
+ {
+ return !(l == r);
+ }
+};
+
+struct dumb_string
+{
+ dumb_ptr<char[]> impl;
+
+ dumb_string()
+ : impl()
+ {}
+ dumb_string(char *) = delete;
+ // copy ctor, copy assign, and dtor are all default
+
+ static dumb_string copy(const char *b, const char *e)
+ {
+ dumb_string rv;
+ rv.impl.new_((e - b) + 1);
+ std::copy(b, e, &rv.impl[0]);
+ return rv;
+ }
+ static dumb_string copys(XString s)
+ {
+ return dumb_string::copy(&*s.begin(), &*s.end());
+ }
+ static
+#ifndef __clang__
+ __attribute__((warning("shouldn't use this - slice instead")))
+#endif
+ dumb_string copyn(const char *sn, size_t n)
+ {
+ return dumb_string::copy(sn, sn + strnlen(sn, n));
+ }
+
+ static
+ dumb_string fake(ZString p)
+ {
+ dumb_string rv;
+ size_t len = p.size();
+ rv.impl = dumb_ptr<char[]>(const_cast<char *>(p.c_str()), len);
+ return rv;
+ }
+
+ dumb_string dup() const
+ {
+ return dumb_string::copy(&impl[0], &impl[0] + impl.size());
+ }
+ void delete_()
+ {
+ impl.delete_();
+ }
+
+ const char *c_str() const
+ {
+ return &impl[0];
+ }
+
+ operator ZString() const
+ {
+ return ZString(strings::really_construct_from_a_pointer, c_str(), nullptr);
+ }
+
+ AString str() const
+ {
+ return ZString(*this);
+ }
+
+ char& operator[](size_t i) const
+ {
+ return impl[i];
+ }
+
+ explicit
+ operator bool() const
+ {
+ return bool(impl);
+ }
+ bool operator !() const
+ {
+ return !impl;
+ }
+};
+
+inline
+const char *convert_for_printf(dumb_string ds)
+{
+ return ds.c_str();
+}
+} // namespace tmwa
diff --git a/src/generic/enum.cpp b/src/generic/enum.cpp
index 8c54aba..49402e9 100644
--- a/src/generic/enum.cpp
+++ b/src/generic/enum.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/generic/enum.hpp b/src/generic/enum.hpp
index 8b3509f..81c9b12 100644
--- a/src/generic/enum.hpp
+++ b/src/generic/enum.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_GENERIC_ENUM_HPP
-#define TMWA_GENERIC_ENUM_HPP
+#pragma once
// enum.hpp - Safe building blocks for enumerated types.
//
// Copyright © 2012-2014 Ben Longbons <b.r.longbons@gmail.com>
@@ -19,70 +18,23 @@
// 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 <cassert>
+#include <cassert>
+#include <cstddef>
-# include <type_traits>
+#include <algorithm>
+#include <type_traits>
-# include "../compat/iter.hpp"
+#include "../compat/iter.hpp"
-template<class T, class E, E max>
-struct earray
-{
- constexpr static
- size_t size()
- {
- return static_cast<size_t>(max);
- }
-
- // no ctor/dtor and one public member variable for easy initialization
- T _data[size()];
-
- T& operator[](E v)
- {
- auto i = static_cast<size_t>(v);
- assert (i < size());
- return _data[i];
- }
-
- const T& operator[](E v) const
- {
- auto i = static_cast<size_t>(v);
- assert (i < size());
- return _data[i];
- }
-
- T *begin()
- {
- return _data;
- }
+#include "array.hpp"
- T *end()
- {
- return _data + size();
- }
- const T *begin() const
- {
- return _data;
- }
-
- const T *end() const
- {
- return _data + size();
- }
-
- friend bool operator == (const earray& l, const earray& r)
- {
- return std::equal(l.begin(), l.end(), r.begin());
- }
-
- friend bool operator != (const earray& l, const earray& r)
- {
- return !(l == r);
- }
-};
+namespace tmwa
+{
+template<class T, class E, E max>
+using earray = GenericArray<T, EnumIndexing<E, max>>;
template<class T, class E, E max>
class eptr
@@ -100,7 +52,7 @@ public:
{}
eptr(earray<T, E, max>& arr)
- : _data(arr._data)
+ : _data(arr.data)
{}
T& operator [](E v) const
@@ -123,6 +75,7 @@ public:
// std::underlying_type isn't supported until gcc 4.7
// this is a poor man's emulation
+// TODO I'm depending on GCC 4.7 now, this can go away
template<class E>
struct underlying_type
{
@@ -148,7 +101,7 @@ struct remove_enum<E, true>
// This really should just go in a namespace
// that's how I use it anyway ...
-# define ENUM_BITWISE_OPERATORS(E) \
+#define ENUM_BITWISE_OPERATORS(E) \
inline \
E operator & (E l, E r) \
{ \
@@ -196,7 +149,7 @@ public:
static
E inced(E v)
{
- return E(U(v) + 1);
+ return static_cast<E>(static_cast<U>(v) + 1);
}
};
@@ -205,5 +158,4 @@ IteratorPair<ValueIterator<E, EnumMath<E>>> erange(E b, E e)
{
return {b, e};
}
-
-#endif // TMWA_GENERIC_ENUM_HPP
+} // namespace tmwa
diff --git a/src/generic/fwd.hpp b/src/generic/fwd.hpp
new file mode 100644
index 0000000..81c4e26
--- /dev/null
+++ b/src/generic/fwd.hpp
@@ -0,0 +1,30 @@
+#pragma once
+// generic/fwd.hpp - list of type names for generic lib
+//
+// 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
+template<class T>
+class dumb_ptr;
+class dumb_string;
+} // namespace tmwa
diff --git a/src/generic/intern-pool.cpp b/src/generic/intern-pool.cpp
index f6df5a6..f45b098 100644
--- a/src/generic/intern-pool.cpp
+++ b/src/generic/intern-pool.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/generic/intern-pool.hpp b/src/generic/intern-pool.hpp
index 0ffddd2..030aa38 100644
--- a/src/generic/intern-pool.hpp
+++ b/src/generic/intern-pool.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_GENERIC_INTERN_POOL_HPP
-#define TMWA_GENERIC_INTERN_POOL_HPP
+#pragma once
// intern-pool.hpp - Cached integer/string lookups.
//
// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
@@ -19,17 +18,21 @@
// 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 <cassert>
+#include <cassert>
+#include <cstddef>
-# include <map>
-# include <vector>
+#include <map>
+#include <vector>
-# include "../strings/rstring.hpp"
-# include "../strings/zstring.hpp"
-# include "../strings/xstring.hpp"
+#include "../strings/rstring.hpp"
+#include "../strings/zstring.hpp"
+#include "../strings/xstring.hpp"
+
+namespace tmwa
+{
class InternPool
{
std::map<RString, size_t> known;
@@ -58,5 +61,4 @@ public:
return known.size();
}
};
-
-#endif // TMWA_GENERIC_INTERN_POOL_HPP
+} // namespace tmwa
diff --git a/src/generic/intern-pool_test.cpp b/src/generic/intern-pool_test.cpp
index b72ab04..c91be91 100644
--- a/src/generic/intern-pool_test.cpp
+++ b/src/generic/intern-pool_test.cpp
@@ -1,5 +1,5 @@
#include "intern-pool.hpp"
-// intern-pool.hpp - Testsuite for cached integer/string lookups.
+// intern-pool_test.cpp - Testsuite for cached integer/string lookups.
//
// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
//
@@ -20,21 +20,25 @@
#include <gtest/gtest.h>
-#include "../strings/base.hpp"
+#include "../strings/literal.hpp"
#include "../poison.hpp"
+
+namespace tmwa
+{
TEST(InternPool, whydoesthisalwaysneedasecondname)
{
InternPool p;
EXPECT_EQ(0, p.size());
- EXPECT_EQ(0, p.intern("hello"));
- EXPECT_EQ(0, p.intern("hello"));
+ EXPECT_EQ(0, p.intern("hello"_s));
+ EXPECT_EQ(0, p.intern("hello"_s));
EXPECT_EQ(1, p.size());
- EXPECT_EQ(1, p.intern("world"));
- EXPECT_EQ(0, p.intern("hello"));
- EXPECT_EQ(1, p.intern("world"));
+ EXPECT_EQ(1, p.intern("world"_s));
+ EXPECT_EQ(0, p.intern("hello"_s));
+ EXPECT_EQ(1, p.intern("world"_s));
EXPECT_EQ(2, p.size());
- EXPECT_EQ("hello", p.outtern(0));
- EXPECT_EQ("world", p.outtern(1));
+ EXPECT_EQ("hello"_s, p.outtern(0));
+ EXPECT_EQ("world"_s, p.outtern(1));
}
+} // namespace tmwa
diff --git a/src/generic/matrix.cpp b/src/generic/matrix.cpp
index e1e1f5e..b14ab7d 100644
--- a/src/generic/matrix.cpp
+++ b/src/generic/matrix.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/generic/matrix.hpp b/src/generic/matrix.hpp
index 40ff9a8..86ce6c2 100644
--- a/src/generic/matrix.hpp
+++ b/src/generic/matrix.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_GENERIC_MATRIX_HPP
-#define TMWA_GENERIC_MATRIX_HPP
+#pragma once
// matrix.hpp - A 2D array.
//
// Copyright © 2013 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 "../sanity.hpp"
+#include "fwd.hpp"
-# include <cassert>
+#include <cassert>
-# include "../compat/memory.hpp"
+#include "../compat/memory.hpp"
+
+namespace tmwa
+{
template<class T>
class Matrix
{
@@ -74,5 +76,4 @@ public:
return _ys;
}
};
-
-#endif // TMWA_GENERIC_MATRIX_HPP
+} // namespace tmwa
diff --git a/src/generic/md5.cpp b/src/generic/md5.cpp
index b49d36f..771ad0f 100644
--- a/src/generic/md5.cpp
+++ b/src/generic/md5.cpp
@@ -18,17 +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 <cstring>
-
#include "../compat/rawmem.hpp"
#include "../strings/xstring.hpp"
-#include "../strings/vstring.hpp"
-
-#include "random.hpp"
#include "../poison.hpp"
+
+namespace tmwa
+{
// auxilary data
/*
sin() constant table
@@ -250,3 +248,4 @@ MD5_state MD5_from_string(XString msg)
}
return state;
}
+} // namespace tmwa
diff --git a/src/generic/md5.hpp b/src/generic/md5.hpp
index 8b1c6ad..50bc987 100644
--- a/src/generic/md5.hpp
+++ b/src/generic/md5.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_GENERIC_MD5CALC_HPP
-#define TMWA_GENERIC_MD5CALC_HPP
+#pragma once
// md5.hpp - Fundamental MD5 operations.
//
// Copyright © 2011-2014 Ben Longbons <b.r.longbons@gmail.com>
@@ -19,19 +18,18 @@
// 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 <netinet/in.h>
+#include <cstdint>
-# include <cstdint>
-# include <cstddef>
-# include <cstdio>
+#include <array>
-# include <array>
+#include "../strings/fwd.hpp"
+#include "../strings/vstring.hpp"
-# include "../strings/fwd.hpp"
-# include "../strings/vstring.hpp"
+namespace tmwa
+{
/// The digest state - becomes the output
struct MD5_state
{
@@ -58,5 +56,4 @@ void MD5_to_str(MD5_state state, md5_string& out);
// Convenience
MD5_state MD5_from_string(XString msg);
-
-#endif // TMWA_GENERIC_MD5CALC_HPP
+} // namespace tmwa
diff --git a/src/generic/md5_test.cpp b/src/generic/md5_test.cpp
index f6a2324..86cbd53 100644
--- a/src/generic/md5_test.cpp
+++ b/src/generic/md5_test.cpp
@@ -25,6 +25,9 @@
#include "../poison.hpp"
+
+namespace tmwa
+{
// This should be made part of the main API,
// but is not yet to keep the diff small.
// Edit: hack to fix the new strict comparison.
@@ -38,11 +41,12 @@ VString<32> MD5(XString in)
TEST(md5calc, rfc1321)
{
- EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", MD5(""));
- EXPECT_EQ("0cc175b9c0f1b6a831c399e269772661", MD5("a"));
- EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72", MD5("abc"));
- EXPECT_EQ("f96b697d7cb7938d525a2f31aaf161d0", MD5("message digest"));
- EXPECT_EQ("c3fcd3d76192e4007dfb496cca67e13b", MD5("abcdefghijklmnopqrstuvwxyz"));
- EXPECT_EQ("d174ab98d277d9f5a5611c2c9f419d9f", MD5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));
- EXPECT_EQ("57edf4a22be3c955ac49da2e2107b67a", MD5("12345678901234567890123456789012345678901234567890123456789012345678901234567890"));
+ EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e"_s, MD5(""_s));
+ EXPECT_EQ("0cc175b9c0f1b6a831c399e269772661"_s, MD5("a"_s));
+ EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72"_s, MD5("abc"_s));
+ EXPECT_EQ("f96b697d7cb7938d525a2f31aaf161d0"_s, MD5("message digest"_s));
+ EXPECT_EQ("c3fcd3d76192e4007dfb496cca67e13b"_s, MD5("abcdefghijklmnopqrstuvwxyz"_s));
+ EXPECT_EQ("d174ab98d277d9f5a5611c2c9f419d9f"_s, MD5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"_s));
+ EXPECT_EQ("57edf4a22be3c955ac49da2e2107b67a"_s, MD5("12345678901234567890123456789012345678901234567890123456789012345678901234567890"_s));
}
+} // namespace tmwa
diff --git a/src/generic/oops.cpp b/src/generic/oops.cpp
new file mode 100644
index 0000000..601ab37
--- /dev/null
+++ b/src/generic/oops.cpp
@@ -0,0 +1,48 @@
+#include "oops.hpp"
+// oops.cpp - Stuff that shouldn't happen.
+//
+// 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 <cstdlib>
+#include <cstring>
+#include <cstdio>
+
+//#include "../poison.hpp"
+
+
+namespace tmwa
+{
+static
+std::string do_asprintf(const char *desc, const char *expr,
+ const char *file, size_t line, const char *function)
+{
+ char *what = nullptr;
+ int len = asprintf(&what, "%s:%zu: error: in '%s', incorrectly alleged that '%s' (%s)",
+ file, line, function, desc, expr);
+ if (len == -1)
+ abort();
+ std::string out = what;
+ free(what);
+ return out;
+}
+
+AssertionError::AssertionError(const char *desc, const char *expr,
+ const char *file, size_t line, const char *function)
+: std::runtime_error(do_asprintf(desc, expr, file, line, function))
+{}
+} // namespace tmwa
diff --git a/src/generic/oops.hpp b/src/generic/oops.hpp
new file mode 100644
index 0000000..f5cf54a
--- /dev/null
+++ b/src/generic/oops.hpp
@@ -0,0 +1,40 @@
+#pragma once
+// oops.hpp - Stuff that shouldn't happen.
+//
+// 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 "fwd.hpp"
+
+#include <cstddef>
+
+#include <stdexcept>
+
+
+namespace tmwa
+{
+class AssertionError : public std::runtime_error
+{
+public:
+ AssertionError(const char *desc, const char *expr,
+ const char *file, size_t line, const char *function);
+};
+
+#define ALLEGE(desc, expr) \
+ if (expr) {} \
+ else throw AssertionError(desc, #expr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
+} // namespace tmwa
diff --git a/src/generic/oops_test.cpp b/src/generic/oops_test.cpp
new file mode 100644
index 0000000..11c87e7
--- /dev/null
+++ b/src/generic/oops_test.cpp
@@ -0,0 +1,54 @@
+#include "oops.hpp"
+// oops_test.cpp - Testsuite for stuff that shouldn't happen.
+//
+// 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 <gtest/gtest.h>
+
+#include "../poison.hpp"
+
+
+namespace tmwa
+{
+TEST(oops, okay)
+{
+ try
+ {
+ ALLEGE ("the sky is gray", true);
+ SUCCEED();
+ }
+ catch (const AssertionError& e)
+ {
+ FAIL();
+ }
+}
+
+TEST(oops, uhoh)
+{
+ try
+ {
+ ALLEGE ("the sky is falling", 1 == 0);
+ FAIL();
+ }
+ catch (const AssertionError& e)
+ {
+ ASSERT_STREQ(strstr(e.what(), "src/generic/"),
+ "src/generic/oops_test.cpp:45: error: in 'virtual void tmwa::oops_uhoh_Test::TestBody()', incorrectly alleged that 'the sky is falling' (1 == 0)");
+ }
+}
+} // namespace tmwa
diff --git a/src/generic/operators.cpp b/src/generic/operators.cpp
index 8d79e1b..614ae51 100644
--- a/src/generic/operators.cpp
+++ b/src/generic/operators.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/generic/operators.hpp b/src/generic/operators.hpp
index 2a71c46..bb05765 100644
--- a/src/generic/operators.hpp
+++ b/src/generic/operators.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_GENERIC_OPERATORS_HPP
-#define TMWA_GENERIC_OPERATORS_HPP
+#pragma once
// operators.hpp - ADL helper for value wrappers.
//
// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
@@ -19,8 +18,11 @@
// 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"
+
+namespace tmwa
+{
namespace _operators
{
class Comparable {};
@@ -63,5 +65,4 @@ namespace _operators
}
using _operators::Comparable;
-
-#endif // TMWA_GENERIC_OPERATORS_HPP
+} // namespace tmwa
diff --git a/src/generic/random.cpp b/src/generic/random.cpp
index 8a06571..e37a3d1 100644
--- a/src/generic/random.cpp
+++ b/src/generic/random.cpp
@@ -20,7 +20,11 @@
#include "../poison.hpp"
+
+namespace tmwa
+{
namespace random_
{
std::mt19937 generate{std::random_device()()};
} // namespace random_
+} // namespace tmwa
diff --git a/src/generic/random.hpp b/src/generic/random.hpp
index 45b5371..5d67236 100644
--- a/src/generic/random.hpp
+++ b/src/generic/random.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_GENERIC_RANDOM_HPP
-#define TMWA_GENERIC_RANDOM_HPP
+#pragma once
// random.hpp - Random number generation.
//
// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
@@ -19,14 +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 "../sanity.hpp"
+#include "fwd.hpp"
-# include "random.t.hpp"
+#include "random.t.hpp"
-# include "../sanity.hpp"
+#include <random>
-# include <random>
+namespace tmwa
+{
// This is not namespace random since that collides with a C function,
// but this can be revisited when everything goes into namespace tmwa.
namespace random_
@@ -85,5 +85,4 @@ namespace random_
return random_::choice(il);
}
} // namespace random_
-
-#endif // TMWA_GENERIC_RANDOM_HPP
+} // namespace tmwa
diff --git a/src/generic/random.t.hpp b/src/generic/random.t.hpp
index d26bf56..b4c4764 100644
--- a/src/generic/random.t.hpp
+++ b/src/generic/random.t.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_GENERIC_RANDOM_T_HPP
-#define TMWA_GENERIC_RANDOM_T_HPP
+#pragma once
// random.t.hpp - Random number generation.
//
// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
@@ -19,8 +18,11 @@
// 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"
+
+namespace tmwa
+{
namespace random_
{
struct Fraction
@@ -39,5 +41,4 @@ namespace random_
}
};
} // namespace random_
-
-#endif // TMWA_GENERIC_RANDOM_T_HPP
+} // namespace tmwa
diff --git a/src/generic/random2.hpp b/src/generic/random2.hpp
index 40fcbcf..23d165c 100644
--- a/src/generic/random2.hpp
+++ b/src/generic/random2.hpp
@@ -1,5 +1,4 @@
-#ifndef TMWA_GENERIC_RANDOM2_HPP
-#define TMWA_GENERIC_RANDOM2_HPP
+#pragma once
// random2.hpp - Random number generation.
//
// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
@@ -19,14 +18,17 @@
// 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 "random.hpp"
+#include "random.hpp"
-# include <algorithm>
+#include <algorithm>
-# include "../compat/iter.hpp"
+#include "../compat/iter.hpp"
+
+namespace tmwa
+{
namespace random_
{
namespace detail
@@ -91,5 +93,4 @@ namespace random_
std::random_shuffle(c.begin(), c.end(), random_::to);
}
} // namespace random_
-
-#endif // TMWA_GENERIC_RANDOM2_HPP
+} // namespace tmwa