I have looked at this thread on singleton class implementation, but not clear on how to use it in practice. To make the context more concrete, say I have a input stream std::istream instance that many different class need to access, but instead of passing it in for each class constructor, I am thinking of using a singleton class Connection to wrap this info. So a client can just call:
Connection.getInstance().get_input_stream();
My questions are two fold: (1) is this a proper use of singleton class (2) on implementing this, I have tried something like this:
class Connection {
public:
static Connection& getInstance() {
static Connection instance; // Guaranteed to be destroyed
return instance;
}
std::istream& get_istream() {
return is;
}
void set_istream(std::istream & stream) {
is = stream;
}
private:
std::istream& is;
}
First this code doesn't compile for some reason. Second it feels awkward that you have to call set_istream() before it is usable. Is there a better way to get this done? Thanks.
EDIT: Apparently, trying to assign a reference is my silly doing, as many of you pointed out. The second part is how to pass in stream information into singleton - it seems not worth it, which suggests this might be a wrong case for using it. thanks you all for your answers.
A reference can't be modified after creation, so to "set" the istream, you'd have to pass it to the ctor:
class Connection {
/* ... */
Connection(std::istream & stream) : is(stream) {}
private:
std::istream& is;
}
That raises a problem, however, of how you pass the correct stream to the ctor when you're creating a static instance -- and the answer to that is that it's non-trivial (at best). You could use a pointer instead of a reference, so you can create an object and point it at an actual stream later, but at that point you're basically asking for trouble (requiring two-stage initialization).
Ultimately, it's just not worth it. My advice would be against using singleton in this case at all. It looks like it's adding quite a bit of work, and providing little (if anything) in return.
Quite a few people are starting to advise against using singletons at all, at least in C++, and quite frankly, I tend to agree most of the time. This kind of situation is why -- you usually end up with little in return for the work you do on it.
I've always been told to avoid singletons, so there's two parts to this:
class Connection {
public:
static std::istream& getInstance() {
assert(is);
return *is;
}
static void set_istream(std::istream & stream) {
is = &stream;
}
private:
static std::istream* is;
}
std::istream* Connection::is = NULL;
int main() {
Connection::set_istream(std::cin);
Connection::getInstance() << "moomoomoo\n";
}
Note I made the stream static, a pointer, and global (so it's actually a singleton. Your code.... wasn't. There's no reason to have that many references laying around, and if you have is = blah where is is a reference, that does not reseat the reference. It would copy the right to the left, which for streams, doesn't make sense and wouldn't even compile.
Of course, singletons are bad. Use globals, or non-singletons. (This is a global, which is only creatable once)
//params are ignored after first call
template<typename... Ts>
std::istream& Connection(Ts... Us) {
static std::ifstream c(std::forward<Ts>(Us)...);
return c;
}
int main() {
//construct and use
Connect("input.txt") << "thing" << std::endl;
Connect() << "stuff";
Connect("this is ignored") << "IM A COW STOP SAYING IM A DUCK";
}
It doesn't compile for two reasons. First you have a very minor problem with the calling syntax. Fix it like this:
Connection::getInstance().get_input_stream();
Second is that you can't assign to a reference, it must be initialized at construction time. Since your constructor is called from the middle of getInstance, that's impractical. Use a pointer instead.
Yes it's awkward to need to call set_istream before your singleton is usable, but that seems unavoidable.
Related
I'm new to C++ and OOP in general and have been trying to learn efficient or 'correct' ways to do things, but am still having trouble.
I'm creating a DataStore class that holds data for other classes/objects. There will only ever be one instance/object of this class; however, there doesn't really need to be an object/instance since it's global data, right. In this case I feel like it's just a way to provide scope. So, I want to directly change the class members instead of passing around the object. I have read about static and _extern, but I can't decide if either would be viable, or if anything else would be better.
Right now I'm passing the one created object around to change it's data, but I would rather the class be accessed as 'itself' instead of by 'an instance of itself' while still retaining the idea of it being an object.
Typically, this sort of problem (where you need one, but only ever one - and you are SURE you never ever need more), is solved by using a "singleton" pattern.
class Singleton
{
public:
static Singleton* getInstance()
{
if (!instance) instance = new Singleton();
return instance;
}
int getStuff() { return stuff; }
private:
Singleton() { stuff = 42; }
static Singleton *instance;
int stuff;
};
then in some suitiable .cpp file>
static Singleton *instance;
Or use a global variable directly:
class Something
{
public:
Something() { stuff = 42; }
int getStuff() { return stuff; }
private:
int stuff;
}
extern Something global_something; // Make sure everyone can find it.
In ONE .cpp file:
Something global_something;
Since BOTH of these are essentially a global variable solution, I expect someone disliking global variables will downvote it, but if you don't want to pass around your class object everywhere, a global variable is not a terrible idea. You just have to be aware that global variables are not necessarily a great idea as a solution in general. It can be hard to follow what is going on, and it certainly gets messy if you suddenly need more than one (because you decided to change the code to support two different storages, or whatever) - but this applies to a singleton too.
EDIT: In a comment OP explained the data store will be read by code running in multiple threads, and updated by code in one thread. My previous answer no longer applies. Here's a better answer.
Don't use a global variable to hold the store's instance. This will open the door for many subtle bugs that can haunt you for a long while. You should give your reading threads read-only access to the store. Your writing thread should get read-write access.
Make sure your read methods in the data store are properly marked as const. Then create a single instance of the data store, and put a pointer to it in a const global variable. Your writing thread should have another mechanism of getting a non-const pointer (add a GetInstance public static method, as suggested by #Mats).
My previous answer:
If you're certain there will always be just one data store instance, don't pass it around.
Global variables are frowned upon, and some languages (Java and C#) outlawed them altogether. So in C# and Java you use static class members instead, which are practically the same thing (with exactly the same problems).
If you can put your single instance in a a const global variable, you should be fine.
If you're doing any kind of multithreading, you'll need to make sure your store is thread-safe, or else really bad things will happen.
I do this for object that have 1 instance most of time during execution of program.
class Object {
private:
Object();
friend Object & GetObject();
public:
...
};
inline Object & GetObject() {
static Object O;
return O;
}
1) this is less verbose than singleton.
2) this avoid pitfall of global object, such as undefined initialization order.
you can use a controversial Singleton pattern or you can use one of PARAMETERISE FROM ABOVE approaches described in Mark Radford (Overload Journal #57 – Oct 2003) SINGLETON - the anti-pattern! article.
PARAMETERISE FROM ABOVE approach (in his opinion) strengthen encapsulation and ease initialisation difficulties.
The classic lazy evaluated and correctly destroyed singleton:
class S
{
public:
static S& getInstance()
{
static S instance; // Guaranteed to be destroyed.
// Instantiated on first use.
return instance;
}
private:
S() {}; // Constructor? (the {} brackets) are needed here.
// Dont forget to declare these two. You want to make sure they
// are unaccessable otherwise you may accidently get copies of
// your singleton appearing.
S(S const&); // Don't Implement
void operator=(S const&); // Don't implement
};
But note: this is not thread-safe.
see here for good StackOverflow post about Singletons
First off, sorry for the title. I couldn't really condense what I'm trying to ask into one phrase :(
I was reading this post, and it somehow got me thinking on function pointers. Specifically, I was wondering why it's "bad" (or, at least, rarely seen) to pass class member functions as function parameters, and then use that pointer on an existing object within that function.
Let's assume I have a template class "Container", which stores a single variable of type T and provides a method to get a const reference to this variable.
template<class T>
class Container {
public:
Container(T anObject) {
m_data = anObject;
}
const T& getData() const {
return m_data;
}
private:
T m_data;
};
Now, I would like to be able to execute member functions of T on m_data, but I don't want to make getData() non-const because that would enable all kinds of other mischief with the returned reference. My solution is to add a new public function, modifyData(...), to Container, which takes a function pointer to a member function of T as a parameter and executes it on m_data; like so:
// ...
void modifyData( void(typename T::*funcptr)(void) ) {
(m_data.*fptr)();
}
// ...
As-is, this will crash and burn if T is a pointer. For testing, I just created a specialized template for Container<T*> to address this, but I'm sure there would be a more elegant way.
A very construed example shows that this seems to work as intended:
// example class to be used with Container
class Data {
public:
Data() {m_count = 0; }
void incrementCount() { m_count++; }
int getCount() const { return m_count; }
private:
int m_count;
};
// ... in main.cpp:
Data dat;
Container<Data*> DCont(dat);
std::cout << cl.getData()->getCount() << std::endl; // outputs 0
DCont.modifyData<Data>(&Data::incrementCount);
std::cout << cl.getData()->getCount() << std::endl; // outputs 1
// compiler catches this:
// DCont.modifyData<SomeOtherClass>(&Data::incrementCount);
// this probably does something bad:
// DCont.modifyData<SomeOtherClass>(&SomeOtherClass::someFunc);
Now, instinctively this just seems like a horribly twisted way of doing things, and I've never seen code that works like this. But my question is, is there a performance/security reason why something like this is bad, or is it something that's just considered bad practice? If it's "just" bad practice, then why is that?
Obvious limitations that I could think of are that something like
// DCont.modifyData(&SomeOtherClass::someFunc);
will probably crash at runtime, but I think that could be addressed by checking the type of U against T in incrementData(). Also, as it is, modifyData only accepts void (*)() functions, but this could probably be addressed with variadic templates.
This example is obviously very construed and not implemented so well, but I think (hope?) it's good enough to explain what I'm talking about.
Thanks!
EDIT: There seems to be some confusion as to what the question is. Basically, this is the scenario I'm talking about: You have a bunch of classes from some library that you're trying to store in the container, and another function that generates certain containers; Now, you want the user to be able to call existing member functions on the objects within these containers, but not to modify the actual objects (like when returning a non-const reference with the getter). An actual implementation would probably use some sort of variadic template to be useful, but I need to think that through some more before posting example code.
In short, I'd like to limit a user's access to container members to only member functions of that member. Is there an easier way of doing this, or does this way not work in the way I was intending?
I don't have any problem with your architecture - I don't see it as bad practice. To me it seems quite a laborious way to protect data and doesn't really help you much in that the user can use any void function to modify the contained data which isn;t really a contract on what can and can't be changed.
I think the reason this construct is so rarely seen is that your requirement and goals of the container class are unusual.
I'm writing a game engine, and right now I was thinking about how I could ensure that ever state in the game (be it an entity state, a game state, etc) has only one instance. Singletons come to mind, but that seems like overkill. One other thing I thought about is nameless classes:
class EntityState
{
public:
virtual void foo() = 0;
};
class : public EntityState
{
public:
void foo() {}
} walkLeftState;
// Because it inherits from EntityState, a named class, I can also
// pass it as a parameter:
void Entity::changeState(EntityState* state) {}
But the problem is I want to add Python scripting, and I don't think Python has nameless classes. What other options are there?
Edit: Why I need only one of every state.
I need a away to identify what state an entity has. I could do it with magic values (i.e. an ID string), but that's a terribly ugly solution, imo. I'd much rather do it by comparing pointers, which I'm ensured to always be unique. But I can't identify by pointers unless there's a single instance of every state...
Edit 2: A solution...
I went for function objects in the end. Seeing as the State classes would only contain a function and nothing else, it didn't really make sense having the class in the first place, now that I think about it. In the end, I think I'll go for something like this:
typedef void (*EntityStateFunc)(Entity* entity, unsigned long currentTime);
namespace entitystate
{
void walkLeft(Entity* entity, unsigned long currentTime);
void stand(Entity* entity, unsigned long currentTime);
}
This should pretty much solve everything: no singletons, no complaints that it might make testing harder, it's simple... Only possible disadvantage is that I'll pretty much have my hands tied if a state ever needs to be more than a function; can anyone think of a scenario where a state needs to be something more complex than this?
Just create one of every state.
There is no reason to make your code so complicated by attempting to enforce this.
Do you write a function like this:
int foo() {
int x = 5;
x++;
return x;
}
And then go, oh my god, I only need one integer variable inside this function... I must enforce this? No.
You could avoid creating a hard constraint, and instead ensure that you're notified if and when you ever create more than you expected:
template <typename T>
struct expected_unique {
static int &getcount() {
static int count = 0;
return count;
}
static void object_created() {
int &lcount = getcount();
++lcount;
if (lcount > 1) {
std::err << "More than one " << typeid(T).name() << " " << lcount << "\n";
}
}
expected_unique() {
object_created();
}
expected_unique(const expected_unique&) {
object_created();
}
// optionally, if you only want to check no more than one at a time
// rather than no more than one ever.
~expected_unique() {
--getcount();
}
};
class WalkLeftState : public EntityState, private expected_unique<WalkLeftState> {
};
Obviously it's not thread-safe, you could make it so with a lock or with atomic int operations.
If there really is only one function, another alternative is:
class EntityState
{
void (*foo_func)();
public:
EntityState(void(*f)()) : foo_func(f) {}
bool operator==(const EntityState &rhs) {
return foo_func == rhs.foo_func;
}
bool operator!=(const EntityState &rhs) {
return !(*this == rhs);
}
void foo() { foo_func(); }
};
void walk_left_foo() {
}
EntityState walkLeftState(walk_left_foo);
Now, it doesn't matter whether or not there are multiple instances of EntityState using the same function, because comparison is performed according to whether the two states involved execute the same routine. So just switch your existing pointer comparisons to object comparisons.
However, if there's more than one virtual function in EntityState in real life, then this would be pretty unwieldy.
Singletons probably are overkill here. It's laudable to be defensive in
your programming, and to prevent misuse, but in this case, the simplest
solution is just to define each of the state classes in an unnamed
namespace in the source file. You not going to create multiple
instances in this one file, and no one else can even name them, much
less define an instance of one. (You can also leave them unnamed, but
that means no user defined destructor.)
I need a away to identify what state an entity has.
Why? To be able to switch on the state? That's exactly what the State pattern should prevent you from doing in the first place. It replaces "switching on states" with polymorphism. That is, instead of this:
switch (state.getState())
{
case WALK_LEFT:
--x;
break;
case WALK_RIGHT:
++x;
break;
case WALK_UP:
--y;
break;
case WALK_DOWN:
++y;
break;
}
you just say:
state.step();
and let the concrete step member functions do the right thing.
As I "hinted at" in the comments, enforcing a "only one instance may exist" constraint is nearly always the wrong thing to do. Actually, I'm willing to go out on a limb and say that it is always the wrong thing to do.
Instead, make it impossible to accidentally create new instances, but allow the programmer (you) to do so when you really want to.
In C++, there are two common ways in which instances might "accidentally" get created:
EntityState st = otherState;
EntityState st2;
Copy constructors are probably the #1 offender. It's easy to forget the &, so suddenly instead of creating a reference to the object, you create a copy. So prevent this by making the class noncopyable.
The default constructor is a less serious issue, but you might, for example, create a class member of type EntityState, and forget to initialize it. And voilá, the default constructor gives you a new instance of the class.
So prevent that too. Declare a constructor taking one or more parameters (which you don't accidentally call), ensure that no default constructor exists, and that the copy constructor is private.
Then you have to consciously think about it and want to create an instance before it happens. And so, as long as the programmer is sane, you'll only have one instance whenever you want just one instance to exist.
Are there any established patterns for checking class invariants in C++?
Ideally, the invariants would be automatically checked at the beginning and at the end of each public member function. As far as I know, C with classes provided special before and after member functions, but unfortunately, design by contract wasn't quite popular at the time and nobody except Bjarne used that feature, so he removed it.
Of course, manually inserting check_invariants() calls at the beginning and at the end of each public member function is tedious and error-prone. Since RAII is the weapon of choice to deal with exceptions, I came up with the following scheme of defining an invariance checker as the first local variable, and that invariance checker checks the invariants both at construction and destruction time:
template <typename T>
class invariants_checker
{
const T* p;
public:
invariants_checker(const T* p) : p(p)
{
p->check_invariants();
}
~invariants_checker()
{
p->check_invariants();
}
};
void Foo::bar()
{
// class invariants checked by construction of _
invariants_checker<Foo> _(this);
// ... mutate the object
// class invariants checked by destruction of _
}
Question #0: I suppose there is no way to declare an unnamed local variable? :)
We would still have to call check_invariants() manually at the end of the Foo constructor and at the beginning of the Foo destructor. However, many constructor bodies and destructor bodies are empty. In that case, could we use an invariants_checker as the last member?
#include <string>
#include <stdexcept>
class Foo
{
std::string str;
std::string::size_type cached_length;
invariants_checker<Foo> _;
public:
Foo(const std::string& str)
: str(str), cached_length(str.length()), _(this) {}
void check_invariants() const
{
if (str.length() != cached_length)
throw std::logic_error("wrong cached length");
}
// ...
};
Question #1: Is it valid to pass this to the invariants_checker constructor which immediately calls check_invariants via that pointer, even though the Foo object is still under construction?
Question #2: Do you see any other problems with this approach? Can you improve it?
Question #3: Is this approach new or well-known? Are there better solutions available?
Answer #0: You can have unnamed local variables, but you give up control over the life time of the object - and the whole point of the object is because you have a good idea when it goes out of scope. You can use
void Foo::bar()
{
invariants_checker<Foo>(this); // goes out of scope at the semicolon
new invariants_checker<Foo>(this); // the constructed object is never destructed
// ...
}
but neither is what you want.
Answer #1: No, I believe it's not valid. The object referenced by this is only fully constructed (and thus starts to exist) when the constructor finished. You're playing a dangerous game here.
Answer #2 & #3: This approach is not new, a simple google query for e.g. "check invariants C++ template" will yield a lot of hits on this topic. In particular, this solution can be improved further if you don't mind overloading the -> operator, like this:
template <typename T>
class invariants_checker {
public:
class ProxyObject {
public:
ProxyObject(T* x) : m(x) { m->check_invariants(); }
~ProxyObject() { m->check_invariants(); }
T* operator->() { return m; }
const T* operator->() const { return m; }
private:
T* m;
};
invariants_checker(T* x) : m(x) { }
ProxyObject operator->() { return m; }
const ProxyObject operator->() const { return m; }
private:
T* m;
};
The idea is that for the duration of a member function call, you create an anonymous proxy object which performs the check in its constructor and destructor. You can use the above template like this:
void f() {
Foo f;
invariants_checker<Foo> g( &f );
g->bar(); // this constructs and destructs the ProxyObject, which does the checking
}
Ideally, the invariants would be automatically checked at the beginning and at the end of each public member function
I think this is overkill; I instead check invariants judiciously. The data members of your class are private (right?), so only its member functions can change the data memebers and therefore invalidate invariants. So you can get away with checking an invariant just after a change to a data member that particiaptes in that invariant.
Question #0: I suppose there is no way to declare an unnamed local variable? :)
You can usually whip up something using macros and __LINE__, but if you just pick a strange enough name, it should already do, since you shouldn't have more than one (directly) in the same scope. This
class invariants_checker {};
template<class T>
class invariants_checker_impl : public invariants_checker {
public:
invariants_checker_impl(T* that) : that_(that) {that_->check_invariants();}
~invariants_checker_impl() {that_->check_invariants();}
private:
T* that_;
};
template<class T>
inline invariants_checker_impl<T> get_invariant_checker(T* that)
{return invariants_checker_impl<T>(that);}
#define CHECK_INVARIANTS const invariants_checker&
my_fancy_invariants_checker_object_ = get_invariant_checker(this)
works for me.
Question #1: Is it valid to pass this to the invariants_checker constructor which immediately calls check_invariants via that pointer, even though the Foo object is still under construction?
I'm not sure whether it invokes UB technical. In practice it would certainly be safe to do so - where it not for the fact that, in practice, a class member that has to be declared at a specific position in relation to other class members is going to be a problem sooner or later.
Question #2: Do you see any other problems with this approach? Can you improve it?
See #2. Take a moderately sized class, add half a decade of extending and bug-fixing by two dozen developers, and I consider the chances to mess this up at at least once at about 98%.
You can somewhat mitigate this by adding a shouting comment to the data member. Still.
Question #3: Is this approach new or well-known? Are there better solutions available?
I hadn't seen this approach, but given your description of before() and after() I immediately thought of the same solution.
I think Stroustrup had an article many (~15?) years ago, where he described a handle class overloading operator->() to return a proxy. This could then, in its ctor and dtor, perform before- and after-actions while being oblivious to the methods being invoked through it.
Edit: I see that Frerich has added an answer fleshing this out. Of course, unless your class already needs to be used through such a handle, this is a burden onto your class' users. (IOW: It won't work.)
#0: No, but things could be slightly better with a macro (if you're ok with that)
#1: No, but it depends. You cannot do anything that would cause this to be dereferenced in before the body (which yours would, but just before, so it could work). This means that you can store this, but not access fields or virtual functions. Calling check_invariants() is not ok if it's virtual. I think it would work for most implementations, but not guaranteed to work.
#2: I think it will be tedious, and not worth it. This have been my experience with invariant checking. I prefer unit tests.
#3: I've seen it. It seems like the right way to me if you're going to do it.
unit testing is better alternative that leads to smaller code with better performance
I clearly see the issue that your destructor is calling a function that will often throw, that's a no-no in C++ isn't it?
Suppose I have two instances of the same class. The class has a pointer to some data, and I want the instances to exchange the pointers as part of some private function's algorithm, but without compromising the data to everybody else by giving a direct access to it through a public function.
My first idea was to add a static variable of type bool called exchange, and two methods: a private one: void requestExchange() and a public one: Data** respond().
requestExchange would set exchange to true, and will be immediately followed with respond() from the instance of choice, which will do the following:
if(exchange==true){
exchange=false;
return data;
}
else{return...?!
This was when I realized that I have no way of simulating "NO OP" since data may in fact be NULL, so supposedly everything goes as a response. Any ideas as to what can be done?
UPDATE: I thought a bit about it, and since the request-respond combo will only be called in the context where NULL as a result will be meaningful (exchange is most certainly true) I suppose I can simply return NULL and simulate a NO OP this way. To an outsider NULL as a result will be useless... But I'm still interested in suggestions. There must be a more well structured way of doing this.
Objects of the same class can access each others' private data directly. You often see this in copy constructors, for example.
Your description of the problem is not very clear. Why can't you just make respond() a private function?
The idea with a static class member is fraught with peril. What if two pairs of such instances want to communicate simultaneously? What if one sets the exchange flag and then dies before it comes around to calling respond()? What about thread safety?
As for returning a NO-OP or error indicator, you can either use exceptions (that's what they are for, but if your project does not use exceptions it's not a good idea to introduce them suddenly), or go the route of boost::optional.
Might be best to separate your concerns with regards to returning the data and exchanging it.
class Foo
{
public:
Bar* data()
{
return pData;
}
private:
void exchangeData(Foo& Rhs)
{
if (this != &Rhs)
{
Bar* pTmp = pData;
pData = Rhs.pData;
Rhs.pData = pTmp;
}
}
Bar* pData;
}
Hopefully it's along the lines of what you want? The question isn't super clear....
I probably missed the point of your question. Why does this not do what you want?
class CMyClass
{
public:
void ExchangePointerWith( CMyClass& rhs );
private:
void* m_MyPtr;
};
and:
void CMyClass::ExchangePointerWith(CMyClass &rhs)
{
void* tmp= m_MyPtr;
m_MyPtr= rhs.m_MyPtr;
rhs.m_MyPtr= tmp;
}
Use std::swap() and build your class's own swap method then you know it should be exception safe. And swap() is a standard routing that most class should implement to make them efficient for the STL.
Remember that a class is automatically a friend of itself. So it can access the private member variables of another instance of the same class. See (Friend scope in C++)
#include <algorithm>
class myX
{
public:
void swap(myX& rhs) throw()
{
std::swap(data,rhs.data);
}
private:
void* data;
};