Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[22_1] improve code of resources to use them inside namespace #321

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 21 additions & 31 deletions Kernel/Abstractions/resource.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@

/******************************************************************************
* 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 <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/
/** \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

#include "hashmap.hpp"
#include "string.hpp"

/**
* \brief base class of resources
* \note no referrence counting is applied on this structure
*/
template <class T> struct rep {
string res_name;
inline rep<T> (string res_name2) : res_name (res_name2) {
Expand All @@ -27,38 +33,22 @@ template <class R> class resource_ptr {
~resource_ptr (){};

public:
R* rep;
static hashmap<string, pointer> instances;
inline R* operator->() { return rep; }
R* rep;
inline static hashmap<string, pointer> instances=
hashmap<string, pointer> (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<PTR##_rep> { \
inline PTR (PTR##_rep* rep2= NULL) { rep= rep2; } \
inline PTR (string s) { rep= (PTR##_rep*) instances[s]; } \
inline ~PTR () {} \
}
#else
#define RESOURCE(PTR) \
struct PTR##_rep; \
template <> hashmap<string, pointer> resource_ptr<PTR##_rep>::instances; \
struct PTR : public resource_ptr<PTR##_rep> { \
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<string, pointer> resource_ptr<PTR##_rep>::instances (NULL);
#else
#define RESOURCE_CODE(PTR) \
template <> \
hashmap<string, pointer> resource_ptr<PTR##_rep>::instances (NULL);
#endif

template <class R>
inline bool
Expand Down
Loading