-3

I'm a student who is studying game engine structure.

I've been managing shared resources through the Singleton Patterns Manager class while creating game engines. However, as the code got longer, the readability of the code became too poor. So, I changed the code thinking that the variable declaration should be made in a position where the variable is used.

First of all, there are two classes that need to be shared:

class Mesh {
public:
    Mesh() {}
    ~Mesh() {}
};
class Material {
public:
    Material() {}
    ~Material() {}
};

And the code below is the singleton pattern method used before.(singleton pattern)

class SomeManager {
public:
    static SomeManager& GetInst() {
        static SomeManager inst{};
        return inst;
    }

    Mesh* get_shared_msh() { return s_msh; }
    Material* get_shared_mtrl() { return s_mtrl; }

private:
    SomeManager()
        : s_msh()
        , s_mtrl()
    {
        s_msh = new Mesh;
        s_mtrl = new Material;

        //And another huge amount of init codes...
    }
    ~SomeManager() {
        delete s_msh;
        delete s_mtrl;

        //And another huge amount of release codes...
    }

    Mesh* s_msh;
    Material* s_mtrl;

    //... other variables
};

And the code below, it's the code I came up with.

It was hard to see the codes mixed up in one place, so I changed the shared resources used in each class to initialize in each class.

//Class that actually use these variables
class light {
public:
    light() {
        if (0 == s_instance_count) {
            init_static();
        }
        ++s_instance_count;
    }
    ~light() {
        --s_instance_count;
        if (0 == s_instance_count) {
            release_static();
        }
    }
private:
    static void init_static() {
        s_msh = new Mesh;
        s_mtrl = new Material;
    }
    static void release_static() {
        delete s_msh; s_msh = nullptr;
        delete s_mtrl; s_mtrl = nullptr;
    }

    static inline int s_instance_count = 0;
    static inline Mesh* s_msh = nullptr;
    static inline Material* s_mtrl = nullptr;
};

Question: I would like to ask programming masters if this idea is good or bad.

When I googled first before asking the question, I saw that most of the answers treated static variables as evils. However, old method is making it difficult to understand the program structure because it's so far away from where you actually use the resources and where you initialize them...

12
  • 1
    A singleton class with a public constructor is not a singleton. What stops the programmer from creating any amount of SomeManager_Singleton instances? See the Meyer's singleton as an example of a singleton. Commented Sep 20, 2024 at 12:30
  • 2
    std::shared_ptr is your friend and wants you to be happy. Commented Sep 20, 2024 at 12:30
  • 1
    @TeeMo I thought it would be too long to write the full code -- All you had to do is not make the constructor public. Look at the Meyer's singleton example at the link -- is the code at the link very long? Commented Sep 20, 2024 at 12:36
  • 2
    @TeeMo Ponder the simplicity of the following static method: Mesh* Light::sharedMesh() { static Mesh m{"light"}; return &m; } Commented Sep 20, 2024 at 12:47
  • 3
    @Botje: Or just Mesh& Light::sharedMesh() { static Mesh m{"light"}; return m; }. No chance of an inadvertent delete or other mistake like shared_ptr<Mesh> ptr{Light::sharedMesh()}; Commented Sep 20, 2024 at 12:52

1 Answer 1

0

Your approach is very good for singleton design pattern but there is an issue you should take care of concurrency also. Use appropriate Concurrency Handling technique to make sure that your shared resource are thread safe.

Resource Locking

  • Mutex
  • R/W Locking
  • Reference Counting etc.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.