Improved Any type.
parent
3ed1aad854
commit
9a46f2077d
|
@ -1,4 +1,10 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="DiscordIntegrationProjectSettings" description="Multiplayer Voxel Game" />
|
<component name="DiscordIntegrationProjectSettings" description="Multiplayer Voxel Game" />
|
||||||
|
<component name="DiscordProjectSettings">
|
||||||
|
<option name="show" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectNotificationSettings">
|
||||||
|
<option name="askShowProject" value="false" />
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -19,9 +19,9 @@
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
// return StartGame(argc, argv);
|
// return StartGame(argc, argv);
|
||||||
|
|
||||||
Any a(new std::string("whee"));
|
auto a = Any::from<std::string>({"wee"});
|
||||||
Any b(new int(125));
|
auto b = Any(a);
|
||||||
|
b.set<int>(57);
|
||||||
|
|
||||||
std::cout << *a.get<std::string>() << std::endl;
|
std::cout << a.get<std::string>() << ", " << b.get_or<std::string>("fuck that") << std::endl;
|
||||||
std::cout << *b.get<int>() << std::endl;
|
|
||||||
}
|
}
|
|
@ -9,34 +9,43 @@
|
||||||
class Any {
|
class Any {
|
||||||
public:
|
public:
|
||||||
Any() = default;
|
Any() = default;
|
||||||
template <typename T> Any(T* val) {
|
|
||||||
set<T>(val);
|
template <typename T> static Any from(const T& val) {
|
||||||
|
Any a;
|
||||||
|
a.set<T>(val);
|
||||||
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void set(T* val) {
|
template <typename T> void set(const T& val) noexcept {
|
||||||
|
data = std::make_shared<T>(std::move(val));
|
||||||
type = typeid(T).hash_code();
|
type = typeid(T).hash_code();
|
||||||
hasData = true;
|
|
||||||
data = val;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> T* get() {
|
template<typename T> const T& get() const {
|
||||||
if (!hasData || type != typeid(T).hash_code()) return nullptr;
|
if (empty()) throw std::logic_error("Tried to get empty Any.");
|
||||||
return static_cast<T*>(data);
|
else if (type != typeid(T).hash_code()) throw std::logic_error("Any is not of type specified.");
|
||||||
|
return *std::static_pointer_cast<T>(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool empty() {
|
template<typename T> const T& get_or(const T& other) const noexcept {
|
||||||
return !hasData;
|
try { return get<T>(); }
|
||||||
|
catch (...) { return other; }
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void erase() {
|
template <typename T> const bool is() const noexcept {
|
||||||
if (!hasData) return;
|
return typeid(T).hash_code() == type;
|
||||||
hasData = false;
|
}
|
||||||
delete static_cast<T*>(data);
|
|
||||||
|
bool const empty() const noexcept {
|
||||||
|
return !data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() noexcept {
|
||||||
|
data = nullptr;
|
||||||
type = 0;
|
type = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::size_t type = 0;
|
std::size_t type = 0;
|
||||||
bool hasData = false;
|
std::shared_ptr<void> data = nullptr;
|
||||||
void* data = nullptr;
|
|
||||||
};
|
};
|
|
@ -31,9 +31,15 @@ namespace Util {
|
||||||
return floatToString((float)val);
|
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) {
|
static std::string vecToString(glm::vec3 vec) {
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
out << (int)vec.x << ", " << (int)vec.y << ", " << (int)vec.z;
|
out << vec.x << ", " << vec.y << ", " << vec.z;
|
||||||
return out.str();
|
return out.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue