-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Helper structure any_ref for holding a generic reference
* Added a recursive modifier function to vref which allows the modification of an underlying object/value where it is important to retain the original type and therefore using a tree is not viable.
- Loading branch information
Showing
9 changed files
with
404 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
#include <typeinfo> | ||
#include <stdexcept> | ||
|
||
|
||
namespace ent | ||
{ | ||
// Helper structure to hold a generic reference to an object/value along | ||
// with the type_info so that a function can selectively modify | ||
// that value based on the typeid. Potentially very dangerous! | ||
// * Dangling references - unsafe to use beyond the lifetime of the object it is referencing | ||
// * Type safety - casting to the wrong type would be bad, the cast() function is provided to | ||
// check the type and throw an exception if there is a mismatch. | ||
struct any_ref | ||
{ | ||
void *value = nullptr; | ||
const std::type_info *type = nullptr; | ||
|
||
|
||
template <typename T> any_ref(T &value) | ||
: value(&value), type(&typeid(value)) {} | ||
|
||
|
||
template <typename T> bool is() | ||
{ | ||
return type && *type == typeid(T); | ||
} | ||
|
||
|
||
template <typename T> T &cast() | ||
{ | ||
if (this->is<T>()) | ||
{ | ||
return *static_cast<T*>(value); | ||
} | ||
|
||
throw std::runtime_error( | ||
std::string("bad conversion of any_ref from ") + type->name() + " to " + typeid(T).name() | ||
); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.