summaryrefslogtreecommitdiff
path: root/src/compat/borrow.hpp
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2014-09-30 16:20:07 -0700
committerBen Longbons <b.r.longbons@gmail.com>2014-10-04 16:17:30 -0700
commit2b7f56003f27e04868bba2052e5395f68a5ad817 (patch)
treef05ffd6e8d771c7abe85c0a9dadb3836854b6a2d /src/compat/borrow.hpp
parent77563ec532c8b51134c3020fb3c70a8bed8457fb (diff)
downloadtmwa-2b7f56003f27e04868bba2052e5395f68a5ad817.tar.gz
tmwa-2b7f56003f27e04868bba2052e5395f68a5ad817.tar.bz2
tmwa-2b7f56003f27e04868bba2052e5395f68a5ad817.tar.xz
tmwa-2b7f56003f27e04868bba2052e5395f68a5ad817.zip
Implement Option<T> and Borrowed<T>
Diffstat (limited to 'src/compat/borrow.hpp')
-rw-r--r--src/compat/borrow.hpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/compat/borrow.hpp b/src/compat/borrow.hpp
new file mode 100644
index 0000000..8b23d7f
--- /dev/null
+++ b/src/compat/borrow.hpp
@@ -0,0 +1,101 @@
+#pragma once
+// borrow.hpp - a non-null, unowned, pointer
+//
+// 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 <cstdlib>
+
+#include <iterator>
+
+#include "option.hpp"
+
+// unit tests currention in option_test.cpp
+
+namespace tmwa
+{
+ // TODO see if const-by-default is a thing
+ template<class T>
+ class Borrowed
+ {
+ T *stupid;
+ public:
+ explicit
+ Borrowed(T *p) : stupid(p)
+ {
+ if (!p) abort();
+ }
+
+ T& operator *() const
+ {
+ return *stupid;
+ }
+
+ T *operator ->() const
+ {
+ return stupid;
+ }
+
+ template<class U>
+ Borrowed<U> downcast_to() const
+ {
+ static_assert(std::is_base_of<T, U>::value, "base check");
+ static_assert(!std::is_same<T, U>::value, "same check");
+ return Borrowed<U>(static_cast<U *>(stupid));
+ }
+
+ template<class U>
+ Borrowed<U> upcast_to() const
+ {
+ static_assert(std::is_base_of<U, T>::value, "base check");
+ static_assert(!std::is_same<T, U>::value, "same check");
+ return Borrowed<U>(stupid);
+ }
+ };
+
+ namespace option
+ {
+ template<class T>
+ class OptionRepr<Borrowed<T>>
+ {
+ T *stupider;
+ public:
+ void set_none() { stupider = nullptr; }
+ void post_set_some() {}
+ bool is_some() const { return stupider != nullptr; }
+ Borrowed<T> *ptr() { return reinterpret_cast<Borrowed<T> *>(&stupider); }
+ const Borrowed<T> *ptr() const { return reinterpret_cast<const Borrowed<T> *>(&stupider); }
+ };
+ }
+
+ template<class T>
+ using P = Borrowed<T>;
+
+ template<class T>
+ Borrowed<T> borrow(T& ref)
+ {
+ return Borrowed<T>(&ref);
+ }
+
+ template<class T>
+ T *as_raw_pointer(Option<Borrowed<T>> ptr)
+ {
+ return &*TRY_UNWRAP(ptr, return nullptr);
+ }
+} // namespace tmwa