Click to See Complete Forum and Search --> : Stop an object from being created


maverick786us
October 7th, 2005, 05:03 AM
How can we put a validation that an object of a class should'nt be created more than 10 times? can someone give me an example?

PadexArt
October 7th, 2005, 05:09 AM
You can use something like this ( a singleton like algorithm):


class CountedObj
{
private:
CountedObj() { ...}
CountedObj( const CountedObj&) { ...}
~CountedObj() { ...}

public:

static CountedObj* CreateObject()
{
if ( instances == 10)
return NULL;

++instances;
return new CountedObj();
}

static void ReleaseObject( CountedObj* obj)
{
--instances;
delete obj;
}

private:
static instances;
};



You can transfer all the args of the constructor to overloaded versions of the CreateFunction.

NOTE: if you plan to use something similar in a multithreaded environment, make sure you add the appropriate safety mechanisms.

maverick786us
October 7th, 2005, 05:16 AM
What kind of variable this instance is? is it of integer type?

exterminator
October 7th, 2005, 05:39 AM
What kind of variable this instance is? is it of integer type?
Probably, yes. You might also want to take a look at this FAQ - What is a Singleton class? (http://www.codeguru.com/forum/showthread.php?t=344782) In the codeguru C++ articles, there was an article by Darwen on template implementation of the Singleton base classes. You might want to search for it and have a look as well.if you plan to use something similar in a multithreaded environment, make sure you add the appropriate safety mechanisms.PadexArt, can we not use this in multithreaded environments? I had some time earlier read an article by Danny Kalev and he wrote that the usage of "new" operator makes it suitable for multi-threaded environments as well since the operator new is thread safe. This is his article that I am referring to - Implementing the Singleton Design Pattern (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp). Regards.

PadexArt
October 7th, 2005, 05:43 AM
PadexArt, can we not use this in multithreaded environments?

Yes, we can but the ++instances and --instances should be made thread safe.

exterminator
October 7th, 2005, 06:05 AM
Yes, we can but the ++instances and --instances should be made thread safe.Ok..got you. Thanks. I have got one more query in this regard. What do people mean when they say atomic creation of objects? I don't understand this term, tried finding out but seems like its an informal name given to something that I dont know. Could you please expand on this? Regards.

PadexArt
October 7th, 2005, 06:55 AM
Ok..got you. Thanks. I have got one more query in this regard. What do people mean when they say atomic creation of objects? I don't understand this term, tried finding out but seems like its an informal name given to something that I dont know. Could you please expand on this? Regards.

I can only think to 2 things:
1. have the object created and initialized with the same call ( see the CFile exemple)
2. a "transaction" like behavior
- lock object for MT acces
- create object
- initialize object
- should errors occur at any point, throw exception/return code and clean up
- unlock object for MT acces

with 2, you can see the objects creation and initialization as a single atomic instruction even if more are actually invoked.

Marc G
October 7th, 2005, 08:01 AM
Yes, we can but the ++instances and --instances should be made thread safe.
Which can be done with InterlockedIncrement and InterlockedDecrement or the other InterlockedXX functions ;)

PadexArt
October 7th, 2005, 08:05 AM
Which can be done with InterlockedIncrement and InterlockedDecrement or the other InterlockedXX functions ;)

yes. but I was trying to leave the OP the pleasure of discovering those. :D

Marc G
October 7th, 2005, 08:16 AM
yes. but I was trying to leave the OP the pleasure of discovering those. :D
Ah, sorry to have screwed the fun then :p
Anyway, I replied it because it's not always obvious and when I needed to make ++ and -- threadsafe, I didn't immediately found those nifty functions :wave: