allow for pointer types in getters (was done for function pointers)

This commit is contained in:
Green Sky 2024-05-22 16:05:05 +02:00
parent 82cfb6d492
commit f7a519754d
No known key found for this signature in database
2 changed files with 25 additions and 6 deletions

View File

@ -87,12 +87,16 @@ static void plug_provideInstance(const SolanaAPI* solana_api, const char* id, co
// ---------- resolve optional ---------- // ---------- resolve optional ----------
template<typename T> template<typename T>
static T* plug_resolveInstanceOptional(const SolanaAPI* solana_api, const char* id, const char* version) { static auto plug_resolveInstanceOptional(const SolanaAPI* solana_api, const char* id, const char* version) {
return static_cast<T*>(solana_api->resolveInstance(id, version)); if constexpr (internal::is_pointer<T>::value) {
return reinterpret_cast<T>(solana_api->resolveInstance(id, version));
} else {
return static_cast<T*>(solana_api->resolveInstance(id, version));
}
} }
template<typename T> template<typename T>
static T* plug_resolveInstanceOptional(const SolanaAPI* solana_api, const char* id) { static auto plug_resolveInstanceOptional(const SolanaAPI* solana_api, const char* id) {
return plug_resolveInstanceOptional<T>(solana_api, id, internal::g_type_version<T>::version); return plug_resolveInstanceOptional<T>(solana_api, id, internal::g_type_version<T>::version);
} }
@ -104,15 +108,15 @@ struct ResolveException {
}; };
template<typename T> template<typename T>
static T* plug_resolveInstance(const SolanaAPI* solana_api, const char* id, const char* version) { static auto plug_resolveInstance(const SolanaAPI* solana_api, const char* id, const char* version) {
T* res = plug_resolveInstanceOptional<T>(solana_api, id, version); auto res = plug_resolveInstanceOptional<T>(solana_api, id, version);
if (res == nullptr) { if (res == nullptr) {
throw ResolveException{"missing " + std::string{id} + " " + version}; throw ResolveException{"missing " + std::string{id} + " " + version};
} }
return res; return res;
} }
template<typename T> template<typename T>
static T* plug_resolveInstance(const SolanaAPI* solana_api, const char* id) { static auto plug_resolveInstance(const SolanaAPI* solana_api, const char* id) {
return plug_resolveInstance<T>(solana_api, id, internal::g_type_version<T>::version); return plug_resolveInstance<T>(solana_api, id, internal::g_type_version<T>::version);
} }

View File

@ -21,5 +21,20 @@ namespace internal {
public: public:
static constexpr const char* version = get_version(); static constexpr const char* version = get_version();
}; };
// more type support
struct true_type {
const static bool value {true};
};
struct false_type {
const static bool value {false};
};
template<class T> struct is_pointer : false_type {};
template<class T> struct is_pointer<T*> : true_type {};
template<class T> struct is_pointer<T* const> : true_type {};
template<class T> struct is_pointer<T* volatile> : true_type {};
template<class T> struct is_pointer<T* const volatile> : true_type {};
} // internal } // internal