diff --git a/.idea/discord.xml b/.idea/discord.xml index 414ea3fc..677ad08a 100644 --- a/.idea/discord.xml +++ b/.idea/discord.xml @@ -1,4 +1,10 @@ + + + + \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index d73b8189..23c456bf 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -19,9 +19,9 @@ int main(int argc, char* argv[]) { // return StartGame(argc, argv); - Any a(new std::string("whee")); - Any b(new int(125)); + auto a = Any::from({"wee"}); + auto b = Any(a); + b.set(57); - std::cout << *a.get() << std::endl; - std::cout << *b.get() << std::endl; + std::cout << a.get() << ", " << b.get_or("fuck that") << std::endl; } \ No newline at end of file diff --git a/src/util/Any.h b/src/util/Any.h index 91e2bb02..b2f5b226 100644 --- a/src/util/Any.h +++ b/src/util/Any.h @@ -9,34 +9,43 @@ class Any { public: Any() = default; - template Any(T* val) { - set(val); + + template static Any from(const T& val) { + Any a; + a.set(val); + return a; } - template void set(T* val) { + template void set(const T& val) noexcept { + data = std::make_shared(std::move(val)); type = typeid(T).hash_code(); - hasData = true; - data = val; } - template T* get() { - if (!hasData || type != typeid(T).hash_code()) return nullptr; - return static_cast(data); + template const T& get() const { + if (empty()) throw std::logic_error("Tried to get empty Any."); + else if (type != typeid(T).hash_code()) throw std::logic_error("Any is not of type specified."); + return *std::static_pointer_cast(data); } - bool empty() { - return !hasData; + template const T& get_or(const T& other) const noexcept { + try { return get(); } + catch (...) { return other; } } - template void erase() { - if (!hasData) return; - hasData = false; - delete static_cast(data); + template const bool is() const noexcept { + return typeid(T).hash_code() == type; + } + + bool const empty() const noexcept { + return !data; + } + + void reset() noexcept { + data = nullptr; type = 0; } private: std::size_t type = 0; - bool hasData = false; - void* data = nullptr; + std::shared_ptr data = nullptr; }; \ No newline at end of file diff --git a/src/util/Util.h b/src/util/Util.h index 94c96cfb..52e3deb4 100644 --- a/src/util/Util.h +++ b/src/util/Util.h @@ -31,9 +31,15 @@ namespace Util { return floatToString((float)val); } + static std::string vecToString(glm::ivec3 vec) { + std::ostringstream out; + out << vec.x << ", " << vec.y << ", " << vec.z; + return out.str(); + } + static std::string vecToString(glm::vec3 vec) { std::ostringstream out; - out << (int)vec.x << ", " << (int)vec.y << ", " << (int)vec.z; + out << vec.x << ", " << vec.y << ", " << vec.z; return out.str(); }