Singletons

Started by
39 comments, last by Way Walker 15 years, 3 months ago
Quote:Original post by M2tM
Quote:Original post by Way Walker
For the SDL_mixer example, what's the problem with free functions? Why not just place all of the function and variables in a namespace? Or, if you want to have private data, why not a class with only static members? (Which was actually the first suggestion in the linked thread.)


Having looked into this issue a bit more, it is not possible with mem_func to actually do what I need to do. That said, the singleton is indeed the most reasonable method for doing this, I will outline the concerns:

- SDL_Mixer is a c library based on a single state, changing its namespace wouldn't help things, namespaces are used to avoid name clashes, not to encapsulate data.


I didn't mean something like
namespace MyMixer {#include "SDL_mixer.h"}
I was more thinking that a namespace is very much like a class that only contains static members, public functions, and public or private variables. Perhaps it'd be clearer if I asked: Why do you need a class instance in the first place?

Quote:
- Namespaces do not exist in c.


Neither do singletons. What's your point?

Quote:
- When writing a wrapper to cover the functionality in an interface which abstracts somewhat low-level concepts we have to keep in mind we are still calling functions that execute on a single state. Just because we create multiple instances of the class does not mean we actually have multiple audio players. Therefore global state is a reality when wrapping c libraries.


Well, I've used C libraries that don't execute on a single state, so global state isn't necessarily a reality when wrapping C libraries. Assuming you do need a class instance, why did you decide on a singleton instead of a monostate or only creating one instance of the class? If you'd made a monostate (which "execute &#111;n a single state" and "creat[ing] multiple instances of the class does not mean we actually have multiple audio players") you would then write code that didn't know whether it was interacting with a single state or multiple states. So, if you switched to a library with multiple states, the transition would be clean.<br><br><!–QUOTE–><BLOCKQUOTE><span class="smallfont">Quote:</span><table border=0 cellpadding=4 cellspacing=0 width="95%"><tr><td class=quote><!–/QUOTE–><!–STARTQUOTE–><br>- The best method of learning that a song or sound has finished playing is via a c function pointer callback passed to SDL_Mixer. The other way is to query the state of each channel every frame which isn't ideal.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>Ok, you need a free function or a static member function to do this. Why does that require a singleton?<br><br><!–QUOTE–><BLOCKQUOTE><span class="smallfont">Quote:</span><table border=0 cellpadding=4 cellspacing=0 width="95%"><tr><td class=quote><!–/QUOTE–><!–STARTQUOTE–><br>- There is no way of converting a c++ member function into a standard c function pointer. In order to allow for either a c function pointer or a c++ member function you would have to have designed the callback in such a way that it could accept it. Similar to the STL's algorithms.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>I agree, <br>void Mix_HookMusicFinished(void (*music_finished)(void *), void *data);<br>would've been a better idea. It's unfortunate they didn't design it like that.<br>

This topic is closed to new replies.

Advertisement