how to count the number of objects created in c++
pls explain with a simple example
Create template class with a static counter.
Each object in your application would then extend this template class.
When constructor is called increment static count (static variable is per class - shared by all objects of that class).
For example see Object Counter using Curiously recurring template pattern:
template <typename T>
struct counter
{
counter()
{
objects_created++;
objects_alive++;
}
counter(const counter&)
{
objects_created++;
objects_alive++;
}
protected:
virtual ~counter()
{
--objects_alive;
}
static int objects_created;
static int objects_alive;
};
template <typename T> int counter<T>::objects_created( 0 );
template <typename T> int counter<T>::objects_alive( 0 );
class X : counter<X>
{
// ...
};
class Y : counter<Y>
{
// ...
};
Usage for completeness:
int main()
{
X x1;
{
X x2;
X x3;
X x4;
X x5;
Y y1;
Y y2;
} // objects gone
Y y3;
cout << "created: "
<< " X:" << counter<X>::objects_created
<< " Y:" << counter<Y>::objects_created //well done
<< endl;
cout << "alive: "
<< " X:" << counter<X>::objects_alive
<< " Y:" << counter<Y>::objects_alive
<< endl;
}
Output:
created: X:5 Y:3
alive: X:1 Y:1
template <class T>
class Counter
{
private:
static int count;
public:
Counter()
{
count++;
}
Counter(const Counter &c)
{
count++;
}
~Counter()
{
count--;
}
static int GetCount() {
return count;
}
}
template<class T>
int Counter<T>::count = 0;
class MyClass : private Counter<MyClass>
{
public:
using Counter<MyClass>::GetCount;
}
This technique is called CRTP
Number of objects for what? If you want to count the number of objects of a specific class, you can use a static counter. Something like below.. Increment counter on creation and decrement while destruction..
class A
{
public:
static int counter;
A()
{
counter ++;
}
virtual ~A()
{
counter --;
}
};
int A :: counter = 0;
You have to overload the new and delete operators
to count memory allocations.
void * operator new (size_t size)
{
void * p = malloc (size);
num_allocations++;
return p;
}
void operator delete (void * p)
{
num_deletions++;
free (p);
}
You could create a counter variable into the public: of your class (assuming here you're talking about counting objects from a class you created)
class Widget {
public:
Widget() { ++count; }
Widget(const Widget&) { ++count; }
~Widget() { --count; }
static size_t howMany()
{ return count; }
private:
static size_t count;
};
// obligatory definition of count. This
// goes in an implementation file
size_t Widget::count = 0;
Taken from ddj.com
update on code as opposed to "stefanB" solution, some variables are in protected access mode and will not be accessible.
template <typename T>
struct counter
{
counter()
{
std::cout << "object created"<< endl;
this->objects_created++;
this->objects_alive++;
}
counter(const counter& test)
{
std::cout << "object created:" << test << endl;
this->objects_created++;
this->objects_alive++;
}
static int getObjectsAlive(){
return objects_alive;
}
static int getObjectsCreated(){
return objects_created;
}
protected:
virtual ~counter()
{
std::cout << "object destroyed"<< endl;
--objects_alive;
}
static int objects_created;
static int objects_alive;
};
template <typename T> int counter<T>::objects_created( 0 );
template <typename T> int counter<T>::objects_alive( 0 );
class X : counter<X>
{
// ...
};
class Y : counter<Y>
{
// ...
};
Usage for completeness:
int main()
{
X x1;
{
X x2;
X x3;
X x4;
X x5;
Y y1;
Y y2;
} // objects gone
Y y3;
cout << "created: "
<< " X:" << counter<X>::getObjectsCreated()
<< " Y:" << counter<Y>::getObjectsCreated() //well done
<< endl;
cout << "alive: "
<< " X:" << counter<X>::getObjectsAlive()
<< " Y:" << counter<Y>::getObjectsAlive()
<< endl;
}
Related
I tried using a static func in class A.
class A {
private:
static int a, b;
static void init(void) {
a = 1, b=0;
}
};
class B {
private:
A::init();
}
It is giving non-friend class cannot have a qualified name.
The following works:
class A {
private:
static int a;
public:
static void initialize();
void addStatic();
void getA();
};
class B {
public:
void initializeClassA();
};
int A::a = 10;
void A::addStatic() {
++a;
}
void A::getA() {
return a;
}
void A::initialize() {
a = 0;
}
void B::initializeClassA() {
A::initialize();
}
int main() {
A a;
B b;
std::cout << "Before Adding - " << a.getA() << std::endl;
for(int i = 0; i < 3; ++i)
{
a.addStatiic();
}
std::cout << "After Adding - " << a.getA() << std::endl;
b.initializeClassA();
std::cout << "After Reinitializing - " << a.getA() >> std::endl;
return 0;
}
I've got a class that needs to become a singleton. All good and fine only that it has a non-default constructor, i.e. it takes three arguments.
The best I could come up with is, to set the constuctor private and then provide some kind of public "setup" function.
Are there any better solutions? My work around so far looks something like - any ideas to improve this are welcome!
#include <iostream>
class singltn {
private:
static singltn *instance;
int data;
// Private constructor so that no objects can be created.
singltn() {
data = 0;
}
int _AA;
int _BB;
int _CC;
public:
static singltn *getInstance() {
if (!instance)
instance = new singltn;
return instance;
}
void setup(int AA, int BB, int CC) {
_AA = AA;
_BB = BB;
_CC = CC;
}
int getData() {
return this->data;
}
void setData(int data) {
this -> data = data;
}
int getAA(){
return this->_AA;
}
};
//Initialize pointer to zero so that it can be initialized in first call to getInstance
singltn *singltn::instance = 0;
int main(){
singltn *a = a->getInstance();
a->setup(111,222,333);
std::cout << "dat " << a->getData() << " _AA " << a-> getAA() << std::endl;
a->setData(100);
std::cout << "dat " << a->getData() << " _AA " << a-> getAA() << std::endl;
singltn *b = b->getInstance();
std::cout << "dat " << b->getData() << " _AA " << a-> getAA() << std::endl;
return 0;
}
You can use a static factory/getter function that calls a private constructor. Something like this:
class Foo {
public:
static Foo& GetInstance() {
static Foo foo(param1, param2);
return foo;
}
private:
Foo(int a, int b) {
// ...
}
}
Of course, that requires your factory function to somehow know the parameters somehow.
Don't use real singleton (which handles creation + unique instance + global access), and adapt it:
class MyClass
{
private:
int data = 0;
int _AA;
int _BB;
int _CC;
static std::unique_ptr<MyClass> uniqueInstance;
MyClass(int AA, int BB, int CC) : _AA(AA), _BB(BB), _CC(CC) {}
MyClass(const MyClass&) = delete;
MyClass& operator =(const MyClass&) = delete;
public:
static void Create(int AA, int BB, int CC)
{
// if (uniqueInstance) throw std::runtime_error("Call it only once");
uniqueInstance = std::make_unique<MyClass>(AA, BB, CC);
}
static MyClass& GetInstance()
{
if (!uniqueInstance) throw std::runtime_error("Call Create before");
return *uniqueInstance;
}
int getData() const { return this->data; }
void setData(int data) { this->data = data; }
int getAA() const { return _AA; }
};
std::unique_ptr<MyClass> MyClass::uniqueInstance;
int main(){
MyClass::Create(111, 222, 333);
auto& a = MyClass::GetInstance();
std::cout << "dat " << a.getData() << " _AA " << a.getAA() << std::endl;
a.setData(100);
std::cout << "dat " << a.getData() << " _AA " << a.getAA() << std::endl;
}
Adapting Steve's Answer to provide a setup function for the singleton class:
class Foo {
public:
static Foo& GetInstance() { return SetupInstance(-1, -1); }
static Foo& SetupInstance(int a, int b) {
static Foo foo(a, b);
return foo;
}
private:
Foo(int a, int b) {
// ...
}
};
As the constructor of the static singleton get's only called once, multiple calls to SetupInstance will not re-create a new object of Foo, but always return the object which was created at the first call.
I have an Abstract Class operations that inherits from VAR Class , which then all the operations derived class(out,sleep,Add) inherit from the operations class. FSM Class inherits from Var also, so That I want one instance of VAR class inside my program.
I am trying to make vector < pair< string, int>> var as a shared data between the FSM class and the Operations class and its deviates . I initialized the var in the main through the FSM class .
Each time we call the exist function in VAR through Class operation , it returns it doesn't exits cause it is empty ! How can I overcome this?
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class VAR
{
public:
vector<pair<string, int>> var;
VAR()
{
cout << "created VAR" << endl;
}
~VAR(){ cout << "Destrioed VAR" << endl; }
void createVar(string x,int y)
{
pair<string, int>t;
t.first = x;
t.second = y;
var.push_back(t);
}
int getVarValue(string x)
{
for (int i = 0; i<var.size(); i++)
{
if (var[i].first == x)
{
return var[i].second;
}
}
}
void setVarValue(string& x, int y)
{
for (int i = 0; i<var.size(); i++)
{
if (var[i].first == x)
{
var[i].second = y;
i = var.size();
}
}
}
bool exits(string& name)
{
for (int i = 0; i<var.size(); i++)
{
if (var[i].first == name)
return true;
}
return false;
}
};
class operations : virtual public VAR
{
public:
operations()
{
cout << "operations created" << endl;
}
~operations()
{
cout << "operations Destroied" << endl;
}
void virtual excute() = 0;
};
class Out :public virtual operations
{
public:
string s;
Out(string xx = "") :s(xx)
{
cout << "Out created" << endl;
}
~Out()
{
cout << "Out Destroied" << endl;
}
void virtual excute()
{
cout << "out Class" << endl;
if (exits(s))
cout<<"it never reach here, WHY !"<<endl;
}
};
class Add :public virtual operations
{
public:
string s;
Add(string ss = "") :s(ss)
{
cout << "ADD created" << endl;
}
~Add()
{
cout << "Add Destroied" << endl;
}
void virtual excute()
{
string ex1 = s.substr(s.find('=') + 1, s.find('+')), ex2 = s.substr(s.find('+') + 1);
if (exits(ex1))
cout<<"it never reach here, WHY !"<<endl;
else
result = atoi(ex1.c_str());
if (exits(ex2))
cout<<"it never reach here, WHY !"<<endl;
}
};
class state
{
public:
vector<operations*> instructionList;
string name;
void exec_all()
{
for (int x = 0; x < instructionList.size(); x++)
instructionList[x]->excute();
}
};
class transition
{
public:
vector < pair<state, vector<pair<state, int>>>> trans;
static int currentState;
};
class FSM :public virtual VAR, public virtual transition
{
public:
FSM()
{
cout << "FSM" << endl;
}
void intialize()
{
createVar("X", 1);
createVar("Y", 5);
}
};
void main()
{
FSM x;
pair<state, vector<pair<state, int>>> p1;
pair<state, int>p2;
x.intialize();
p2.first.name = "b";
p2.second = 3;
p1.first.name = "a";
p1.second.push_back(p2);
x.trans.push_back(p1);
x.trans[0].first.instructionList.push_back(new Add("X=X+Y"));
x.trans[0].first.instructionList.push_back(new Out("X"));
x.trans[0].first.exec_all();//wrong output cause exist() returns false
}
A minimal complete example looks something like this:
#include <iostream>
using namespace std;
class VAR
{
public:
int var;
virtual ~VAR()
{}
void setVar(int n)
{var=n;}
};
class Out :public VAR
{};
class FSM :public VAR
{};
int main()
{
FSM x;
x.setVar(5);
Out OP;
if (x.var==OP.var)
cout<<"it reaches here now" << endl;
else
cout << "it fails" << endl;
return(0);
}
And one way to fix it is like this:
class VAR
{
public:
static int var;
int var;
virtual ~VAR()
{}
void setVar(int n)
{var=n;}
};
int VAR::var=0;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My first question is: I am having a lot of trouble figuring out why the Example class is being constructed greater than the others. Below is a short app using a Template counter to track how many times the constructor/destructor/copy constructor is called for each class. There are a total of three classes: Example, Deep, Child. Each has a copy constructor... ugh.
Also, my second question, is what would be the correct way to define the copy constructor for the Child class?
In the printStatus(), it displays:
COUNTERS::NEW_COUNTER = 60
COUNTERS::DELETE_COUNTER = 50
COUNTERS::CONSTRUCTOR_COUNTER = 90
COUNTERS::DESTRUCTOR_COUNTER = 80
Example count = 10
Deep count = 0
Child count = 0
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class COUNTERS
{
public:
static int NEW_COUNTER;
static int DELETE_COUNTER;
static int CONSTRUCTOR_COUNTER;
static int DESTRUCTOR_COUNTER;
};
int COUNTERS::NEW_COUNTER = 0;
int COUNTERS::DELETE_COUNTER = 0;
int COUNTERS::CONSTRUCTOR_COUNTER = 0;
int COUNTERS::DESTRUCTOR_COUNTER = 0;
/* template used for counting constructors/destructors to debug memory leaks */
template <typename T>
class Countable
{
static unsigned cs_count_;
public:
Countable() { ++cs_count_; }
Countable( Countable const& ) { ++cs_count_; }
virtual ~Countable() { --cs_count_;}
static unsigned count() { return cs_count_; }
};
template <typename T>
unsigned Countable<T>::cs_count_ = 0;
class Example : public Countable<Example>
{
public:
string a;
int b;
Example() {
COUNTERS::CONSTRUCTOR_COUNTER++;
a = "exampleString";
b = 5;
}
virtual ~Example() {
COUNTERS::DESTRUCTOR_COUNTER++;
}
// copy constructor
Example(const Example& e) {
COUNTERS::CONSTRUCTOR_COUNTER++;
this->a = e.a;
this->b = e.b;
}
};
class Deep : public Countable<Deep>
{
public:
int a;
string b;
Example* e;
Deep()
{
COUNTERS::CONSTRUCTOR_COUNTER++;
a = 3;
b = "deepString";
e = new Example();
COUNTERS::NEW_COUNTER++;
}
virtual ~Deep() {
if(e != NULL) {
delete e;
COUNTERS::DELETE_COUNTER++;
}
COUNTERS::DESTRUCTOR_COUNTER++;
}
// copy constructor
Deep(const Deep& x)
{
COUNTERS::CONSTRUCTOR_COUNTER++;
this->a = x.a;
this->b = x.b;
this->e = new Example();
COUNTERS::NEW_COUNTER++;
this->e->a = x.e->a;
this->e->b = x.e->b;
};
};
class Child : public Countable<Child>
{
public:
Deep d;
string name;
int age;
Example* e;
vector<Example> list;
vector<Deep> deep_list;
void init()
{
Deep* var = new Deep(); COUNTERS::NEW_COUNTER++;
deep_list.push_back(*var);
delete var; COUNTERS::DELETE_COUNTER++;
}
Child() {
COUNTERS::CONSTRUCTOR_COUNTER++;
name = "a";
age = 10;
d.a = 1;
d.b = "deep";
d.e = NULL;
e = new Example();
COUNTERS::NEW_COUNTER++;
list.push_back(*e);
init();
}
virtual ~Child() {
COUNTERS::DESTRUCTOR_COUNTER++;
if(e != NULL) {
delete e;
COUNTERS::DELETE_COUNTER++;
}
}
// copy constructor
Child(const Child& c)
{
}
};
void myChildFunction(){
Child* c = new Child();
COUNTERS::NEW_COUNTER++;
delete c;
COUNTERS::DELETE_COUNTER++;
}
void printStatus(){
cout << "COUNTERS::NEW_COUNTER = " << COUNTERS::NEW_COUNTER << endl;
cout << "COUNTERS::DELETE_COUNTER = " << COUNTERS::DELETE_COUNTER << endl;
cout << "COUNTERS::CONSTRUCTOR_COUNTER = " << COUNTERS::CONSTRUCTOR_COUNTER << endl;
cout << "COUNTERS::DESTRUCTOR_COUNTER = " << COUNTERS::DESTRUCTOR_COUNTER << endl;
cout << "Example count = " << Example::count() << endl;
cout << "Deep count = " << Deep::count() << endl;
cout << "Child count = " << Child::count() << endl;
}
int main()
{
for(unsigned int i=0 ; i < 10; i++)
myChildFunction();
printStatus();
return 0;
}
You are missing out on deleting some Example objects because of this line:
d.e = NULL;
in Child::Child().
You are allocating memory for e in the constructor of Deep. After executing the above line, that memory is leaked.
You can resolve that problem by:
Removing that line (or commenting it out),
Deleting d.e before making it NULL, or
Doing something else that prevents the memory leak.
Update, in response to comment
Copy constructor for Child:
Child(const Child& c) : d(c.d),
name(c.name),
age(c.age),
e(new Example(*c.e)),
list(c.list),
deep_list(c.deep_list)
{
COUNTERS::DESTRUCTOR_COUNTER++; // This is for Child
COUNTERS::NEW_COUNTER++; // This is for new Example
}
I removed all information that cluttered your code.
When using templates, constructors and copy constructors NEED the following: Example < eltType >(void);
in the class definition. All objects that inherit from Countables are known as derived classes. They also may call a derived class a child, and the class in which it is derived from is called the parent. I added the COPY_CONSTRUCTOR_COUNT to add clarification to the data which is being presented on the console/command prompt. Usually when trying to preform a task, large or small, doing it incrementally and by providing methods, for each task, saves you time and a headache. I removed the new_count and delete_count from the equation, because I felt that it was not needed.
You will notice that I added : Countable( * ((Countable < eltType > *)&e))
This is a requirement when designing a program that involves inheritance, which introduces the
topic of Polymorphism :D
What that bit of code does is that it gets a pointer of a Countable, which will point to the address of object e, which then allows access to all super classes of this class, but not including e's class.
NOTE: Since e is a derived class of Countable, this is valid statement.
For you second question, all of your data members are public, you can use an iterator to copy your data stored in you vectors.
As a concern from one programmer to another, I hope your code in practice is well documented, and all methods declared in your class are defined in a .cpp file.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class COUNTERS
{
public:
static int NEW_COUNTER;
static int DELETE_COUNTER;
static int CONSTRUCTOR_COUNTER;
static int DESTRUCTOR_COUNTER;
static int COPY_CONSTRUCTOR_COUNTER;
};
int COUNTERS::NEW_COUNTER = 0;
int COUNTERS::DELETE_COUNTER = 0;
int COUNTERS::CONSTRUCTOR_COUNTER = 0;
int COUNTERS::DESTRUCTOR_COUNTER = 0;
int COUNTERS::COPY_CONSTRUCTOR_COUNTER = 0;
/* template used for counting constructors/destructors to debug memory leaks */
template <typename T>
class Countable
{
public:
Countable<T>()
{
incrementObjectCount();
};
Countable<T>(Countable const&)
{
incrementObjectCount();
};
virtual ~Countable()
{
decrementObjectCount();
};
static unsigned count()
{
return cs_count_;
};
protected:
static unsigned cs_count_;
////////////////////////////////////ADDED////////////////////////////////////
protected:
void incrementObjectCount(void){ ++cs_count_; };
void decrementObjectCount(void){ --cs_count_; };
void incrementDeconstructorCounter(void){ ++COUNTERS::DESTRUCTOR_COUNTER; };
/////////////////////////////////////ADDED////////////////////////////////////
};
template <typename T>
unsigned Countable<T>::cs_count_ = 0;
class Example : public Countable<Example>
{
public:
Example() : Countable<Example>()
{
COUNTERS::CONSTRUCTOR_COUNTER++;
}
virtual ~Example()
{
incrementDeconstructorCounter();
}
// copy constructor
Example(const Example& e) : Countable<Example>(*((Countable<Example>*)&e))
{
// COUNTERS::CONSTRUCTOR_COUNTER++; This is copy constructor, you addmitted this from "Child" class CCstr
++COUNTERS::COPY_CONSTRUCTOR_COUNTER; // For even more information added this
}
};
class Deep : public Countable<Deep>
{
public:
Deep() : Countable<Deep>()
{
COUNTERS::CONSTRUCTOR_COUNTER++;
}
virtual ~Deep()
{
COUNTERS::DESTRUCTOR_COUNTER++;
}
// copy constructor
Deep(const Deep& x) : Countable<Deep>(*((Countable<Deep>*)&x))
{
//COUNTERS::CONSTRUCTOR_COUNTER++;
++COUNTERS::COPY_CONSTRUCTOR_COUNTER; // For even more information added this
};
};
class Child : public Countable<Child>
{
public:
vector<Example> list;
vector<Deep> deep_list;
void init()
{
deep_list.push_back(Deep());
list.push_back(Example());
}
Child() : Countable<Child>()
{
COUNTERS::CONSTRUCTOR_COUNTER++;
init();
}
virtual ~Child()
{
COUNTERS::DESTRUCTOR_COUNTER++;
}
// copy constructor
Child(const Child& c) : Countable<Child>(*((Countable<Child>*)&c))
{
++COUNTERS::COPY_CONSTRUCTOR_COUNTER; // For even more information added this
}
};
void myChildFunction(){
Child* c = new Child();
//COUNTERS::NEW_COUNTER++;not needed
delete c;
//COUNTERS::DELETE_COUNTER++; not need
}
void printStatus(){
cout << "COUNTERS::NEW_COUNTER = " << COUNTERS::NEW_COUNTER << endl;
cout << "COUNTERS::DELETE_COUNTER = " << COUNTERS::DELETE_COUNTER << endl;
cout << "COUNTERS::CONSTRUCTOR_COUNTER = " << COUNTERS::CONSTRUCTOR_COUNTER << endl;
cout << "COUNTERS::DESTRUCTOR_COUNTER = " << COUNTERS::DESTRUCTOR_COUNTER << endl;
cout << "COUNTERS::COPY_CONSTRUCTOR_COUNTER = " << COUNTERS::COPY_CONSTRUCTOR_COUNTER << endl;
cout << "Example count = " << Example::count() << endl;
cout << "Deep count = " << Deep::count() << endl;
cout << "Child count = " << Child::count() << endl;
}
int main()
{
for (unsigned int i = 0; i < 10; i++)
myChildFunction();
printStatus();
system("pause");
return 0;
}
I want to define a container in the base class, which contains function obj or anything that can make my purpose happen. These function obj can call derived classes' functions. they all take same parameters.
#include <vector>
#include <functional>
#include <iostream>
class Foo {
Foo() {}
virtual ~Foo(){}
virtual void init()
{ registerCallback(0, &Foo::print_ori ); }
void print_ori(int i) const { std::cout << i << '\n'; }
void registerCallback(int key, ??? cb ) // NOT SURE HOW TO DEFINE THIS
{
callbacks[key] = cb;
}
void runCallbacks(int key, int n)
{
auto i = callbacks.find(key);
if (i != callbacks.end()) {
(*i)(*this, n);
}
}
std::map<int, std::function<void(const Foo&, int) > > callbacks; // obviously, it's wrong. how to fix it?
};
struct Foo2 : public Foo {
Foo2(int num) : Foo(num) {}
virtual void init()
{
Foo::init();
registerCallback(11, &Foo2::print1 );
registerCallback(12, &Foo2::print2 );
}
void print1(int i) const { std::cout << " - Foo2.p1 - " << i << endl; }
void print2(int i) const { std::cout << " - Foo2.p2 - " << i << endl; }
};
int main()
{
Foo* obj = new Foo2();
obj->init();
obj->runCallbacks(12, 456);
}
Here's a way to achieve what your code looks like it's trying to do, without using function pointers:
class Foo {
Foo() {}
virtual ~Foo(){}
void print_ori(int i) const { std::cout << i << '\n'; }
virtual void do_runCallbacks(int v)
{
}
void runCallbacks()
{
print_ori(3)
do_runCallBacks(3);
}
};
struct Foo2 : public Foo {
Foo2(int num) : Foo(num) {}
void do_runcallbacks(int v)
{
print1(v);
print2(v);
}
void print1(int i) const { std::cout << " - Foo2.p1 - " << i << endl; }
void print2(int i) const { std::cout << " - Foo2.p2 - " << i << endl; }
};
int main()
{
Foo* obj = new Foo2();
obj->runCallbacks();
}
Now, there may well be reasons to do this completely differently, but I don't see why you should need both virtual functions and inheritance, AND function objects/function pointers. That seems quite wrong to me ("smells bad")
Edit:
Here's something I came up with, that solves the type of problem you describe after edits of the original question.
#include <iostream>
#include <map>
using namespace std;
class event_interface
{
public:
virtual void action(int n) = 0;
};
class event_manager
{
public:
event_manager(int n) : num(n) {}
void register_event(int key, event_interface *eh)
{
handlers[key] = eh;
}
void callback(int key)
{
auto h = handlers.find(key);
if (h != handlers.end())
{
h->second->action(num);
}
}
private:
map<int, event_interface *> handlers;
int num;
};
class handler1 : public event_interface
{
public:
void action(int n) { cout << "in handler1::action. n=" << n << endl; }
};
class handler2 : public event_interface
{
public:
handler2(int n) : data(n) {}
void action(int n)
{
cout << "in handler2::action. n=" << n
<< " data = " << data << endl;
}
private:
int data;
};
class multihandler
{
private:
class handler3: public event_interface
{
public:
void action(int n) { cout << "in handler3::action. n=" << n << endl; }
};
class handler4: public event_interface
{
public:
handler4(multihandler *m) : mh(m) {}
void action(int n)
{
cout << "in handler4::action. n=" << n
<< " data = " << mh->data << endl;
}
private:
multihandler* mh;
};
public:
multihandler(event_manager& em) : h4(this)
{
em.register_event(62, &h3);
em.register_event(63, &h4);
data = 42;
}
private:
handler3 h3;
handler4 h4;
int data;
};
int main()
{
event_manager mgr(3);
handler1 h1;
handler2 h2(77);
multihandler mh(mgr);
mgr.register_event(12, &h1);
mgr.register_event(13, &h2);
int evts[] = { 12, 63, 62, 13, 18 };
for(auto i : evts)
{
cout << "Event: " << i << endl;
mgr.callback(i);
}
}