summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2021-02-17 19:51:28 +0000
committerGitHub <noreply@github.com>2021-02-17 19:51:28 +0000
commitf85e9ab9254e2ae4ac13170f9edea00fb8d931a2 (patch)
tree43fa19520c7f744f798de0ed8bcbb8e01c82e327 /src/util
parenta8f6befd398cb8f962f3bb1fab092d6355bfe015 (diff)
downloadminetest-f85e9ab9254e2ae4ac13170f9edea00fb8d931a2.tar.gz
minetest-f85e9ab9254e2ae4ac13170f9edea00fb8d931a2.tar.bz2
minetest-f85e9ab9254e2ae4ac13170f9edea00fb8d931a2.zip
Add nametag background setting and object property (#10937)
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Optional.h77
-rw-r--r--src/util/serialize.h2
2 files changed, 78 insertions, 1 deletions
diff --git a/src/util/Optional.h b/src/util/Optional.h
new file mode 100644
index 000000000..9c2842b43
--- /dev/null
+++ b/src/util/Optional.h
@@ -0,0 +1,77 @@
+/*
+Minetest
+Copyright (C) 2021 rubenwardy
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#pragma once
+
+#include "debug.h"
+
+struct nullopt_t
+{
+};
+constexpr nullopt_t nullopt{};
+
+/**
+ * An implementation of optional for C++11, which aims to be
+ * compatible with a subset of std::optional features.
+ *
+ * Unfortunately, Minetest doesn't use C++17 yet.
+ *
+ * @tparam T The type to be stored
+ */
+template <typename T>
+class Optional
+{
+ bool m_has_value = false;
+ T m_value;
+
+public:
+ Optional() noexcept {}
+ Optional(nullopt_t) noexcept {}
+ Optional(const T &value) noexcept : m_has_value(true), m_value(value) {}
+ Optional(const Optional<T> &other) noexcept :
+ m_has_value(other.m_has_value), m_value(other.m_value)
+ {
+ }
+
+ void operator=(nullopt_t) noexcept { m_has_value = false; }
+
+ void operator=(const Optional<T> &other) noexcept
+ {
+ m_has_value = other.m_has_value;
+ m_value = other.m_value;
+ }
+
+ T &value()
+ {
+ FATAL_ERROR_IF(!m_has_value, "optional doesn't have value");
+ return m_value;
+ }
+
+ const T &value() const
+ {
+ FATAL_ERROR_IF(!m_has_value, "optional doesn't have value");
+ return m_value;
+ }
+
+ const T &value_or(const T &def) const { return m_has_value ? m_value : def; }
+
+ bool has_value() const noexcept { return m_has_value; }
+
+ explicit operator bool() const { return m_has_value; }
+};
diff --git a/src/util/serialize.h b/src/util/serialize.h
index b3ec28eab..15bdd050d 100644
--- a/src/util/serialize.h
+++ b/src/util/serialize.h
@@ -290,7 +290,7 @@ inline void writeS8(u8 *data, s8 i)
inline void writeS16(u8 *data, s16 i)
{
- writeU16(data, (u16)i);
+ writeU16(data, (u16)i);
}
inline void writeS32(u8 *data, s32 i)