From 2fb605b7250f2a801c752044880b8ab43ed76e57 Mon Sep 17 00:00:00 2001 From: jingkaimori Date: Mon, 15 Apr 2024 12:55:08 +0800 Subject: [PATCH] improve code of resources to use them inside namespace. --- Kernel/Abstractions/resource.hpp | 52 +++++++++++++------------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/Kernel/Abstractions/resource.hpp b/Kernel/Abstractions/resource.hpp index 87e24a364..229aed9e3 100644 --- a/Kernel/Abstractions/resource.hpp +++ b/Kernel/Abstractions/resource.hpp @@ -1,12 +1,14 @@ -/****************************************************************************** - * MODULE : resource.hpp - * COPYRIGHT : (C) 1999 Joris van der Hoeven - ******************************************************************************* - * This software falls under the GNU general public license version 3 or later. - * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE - * in the root directory or . - ******************************************************************************/ +/** \file resource.hpp + * \copyright GPLv3 + * \details Utils to define a class that holding its instances globally. + * Instances is called "Resources", and can be looked up by a string. + * Weak referrence is provided to access member of resources. + * \author Joris van der Hoeven + * \date 1999 + * \author jingkaimori + * \date 2024 + */ #ifndef RESOURCE_H #define RESOURCE_H @@ -14,6 +16,10 @@ #include "hashmap.hpp" #include "string.hpp" +/** + * \brief base class of resources + * \note no referrence counting is applied on this structure + */ template struct rep { string res_name; inline rep (string res_name2) : res_name (res_name2) { @@ -27,12 +33,15 @@ template class resource_ptr { ~resource_ptr (){}; public: - R* rep; - static hashmap instances; - inline R* operator->() { return rep; } + R* rep; + inline static hashmap instances= + hashmap (NULL); + /* C++17 feature, use inline keyword here to pack definition along with + declaration, instead of defining static member inside expansion + of RESOURCE macro.*/ + inline R* operator->() { return rep; } }; -#if (defined(_WIN32) || defined(_WIN64)) #define RESOURCE(PTR) \ struct PTR##_rep; \ struct PTR : public resource_ptr { \ @@ -40,25 +49,6 @@ template class resource_ptr { inline PTR (string s) { rep= (PTR##_rep*) instances[s]; } \ inline ~PTR () {} \ } -#else -#define RESOURCE(PTR) \ - struct PTR##_rep; \ - template <> hashmap resource_ptr::instances; \ - struct PTR : public resource_ptr { \ - inline PTR (PTR##_rep* rep2= NULL) { rep= rep2; } \ - inline PTR (string s) { rep= (PTR##_rep*) instances[s]; } \ - inline ~PTR () {} \ - } -#endif - -#if (defined(_WIN32) || defined(_WIN64)) -#define RESOURCE_CODE(PTR) \ - hashmap resource_ptr::instances (NULL); -#else -#define RESOURCE_CODE(PTR) \ - template <> \ - hashmap resource_ptr::instances (NULL); -#endif template inline bool