Botcraft 1.21.4
Loading...
Searching...
No Matches
StdAnyUtilities.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <any>
4#include <string>
5#include <typeindex>
6#include <typeinfo>
7#include <functional>
8#include <unordered_map>
9
10namespace Botcraft::Utilities
11{
13 {
14 public:
15 /// @brief Give a string representation of a std::any value
16 /// @param value A std::any object to convert to string
17 /// @return If the type of the std::any is in registered_types, the string version of value, type name only otherwise.
18 static std::string ToString(const std::any& value);
19
20 /// @brief Default function to convert all unregistered
21 /// @param value Value to convert
22 /// @return String v
23 static std::string DefaultToString(const std::any& value);
24
25 static void RegisterType(const std::type_index& index, const std::function<std::string(const std::any&)>& f);
26
27 template<class T>
28 static void RegisterType(const std::function<std::string(const std::any&)>& f)
29 {
30 RegisterType(std::type_index(typeid(T)), f);
31 }
32
33 private:
34 /// @brief Maps std::type_index of a std::any to a function to convert it to std::string
35 static std::unordered_map<std::type_index, std::function<std::string(const std::any&)>> registered_types;
36 };
37}
static std::unordered_map< std::type_index, std::function< std::string(const std::any &)> > registered_types
Maps std::type_index of a std::any to a function to convert it to std::string.
static void RegisterType(const std::function< std::string(const std::any &)> &f)
static std::string ToString(const std::any &value)
Give a string representation of a std::any value.
static std::string DefaultToString(const std::any &value)
Default function to convert all unregistered.
static void RegisterType(const std::type_index &index, const std::function< std::string(const std::any &)> &f)