473,466 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Class for static maps

I have a program where several classes each have a single static
std::map to keep track of all the instances of that class. For each
of these classes, I want a static lookupByID(int id) method which
returns a pointer to the instance that the id references... e.g...

Item * Item::lookupByID(Uint32 id);
Level * Level::lookupByID(Uint32 id);
Lifeform * Lifeform::lookupByID(Uint32 id);

.... etc. Instead of rewriting the same code for each of these
classes, I instead wrote an Indexable class...

template <class Child> class Indexable {
private:
static std::map<Uint32,Child*> mapByID;
static Uint32 _nextID;
Uint32 _id;
public:
static Child * lookupByID(Uint32 id) { return mapByID[id]; }
Indexable() { _id=_nextID++; mapByID[_id]=(Child*)this; }
~Indexable() { mapByID[_id]=NULL; }
Uint32 id() const { return _id; }
};

and made each of my other classes subclass Indexable...

class Item : public Indexable<Item> { ... };
class Level : public Indexable<Level> { ... };
class Lifeform : public Indexable<Lifeform> { ... };

This achieves what I've wanted, but it still seems a bit weird to me.
How would you guys handle this situation?
Jul 22 '05 #1
4 1603

"Grey Plastic" <gr*********@hotmail.com> wrote in message
news:1d**************************@posting.google.c om...
I have a program where several classes each have a single static
std::map to keep track of all the instances of that class. For each
of these classes, I want a static lookupByID(int id) method which
returns a pointer to the instance that the id references... e.g...

Item * Item::lookupByID(Uint32 id);
Level * Level::lookupByID(Uint32 id);
Lifeform * Lifeform::lookupByID(Uint32 id);

... etc. Instead of rewriting the same code for each of these
classes, I instead wrote an Indexable class...

template <class Child> class Indexable {
private:
static std::map<Uint32,Child*> mapByID;
static Uint32 _nextID;
Uint32 _id;
public:
static Child * lookupByID(Uint32 id) { return mapByID[id]; }
Indexable() { _id=_nextID++; mapByID[_id]=(Child*)this; }
~Indexable() { mapByID[_id]=NULL; }
Uint32 id() const { return _id; }
};

and made each of my other classes subclass Indexable...

class Item : public Indexable<Item> { ... };
class Level : public Indexable<Level> { ... };
class Lifeform : public Indexable<Lifeform> { ... };

This achieves what I've wanted, but it still seems a bit weird to me.
How would you guys handle this situation?


It's a well known technique. Make a base class aware of a derived class by
passing the derived class as a template parameter to the base class.
Congratulations on discovering it yourself I'd say.

john
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c0*************@ID-196037.news.uni-berlin.de...

"Grey Plastic" <gr*********@hotmail.com> wrote in message
news:1d**************************@posting.google.c om...
I have a program where several classes each have a single static
std::map to keep track of all the instances of that class. For each
of these classes, I want a static lookupByID(int id) method which
returns a pointer to the instance that the id references... e.g...

Item * Item::lookupByID(Uint32 id);
Level * Level::lookupByID(Uint32 id);
Lifeform * Lifeform::lookupByID(Uint32 id);

... etc. Instead of rewriting the same code for each of these
classes, I instead wrote an Indexable class...

template <class Child> class Indexable {
private:
static std::map<Uint32,Child*> mapByID;
static Uint32 _nextID;
Uint32 _id;
public:
static Child * lookupByID(Uint32 id) { return mapByID[id]; }
Indexable() { _id=_nextID++; mapByID[_id]=(Child*)this; }
~Indexable() { mapByID[_id]=NULL; }
Uint32 id() const { return _id; }
};

and made each of my other classes subclass Indexable...

class Item : public Indexable<Item> { ... };
class Level : public Indexable<Level> { ... };
class Lifeform : public Indexable<Lifeform> { ... };

This achieves what I've wanted, but it still seems a bit weird to me.
How would you guys handle this situation?


It's a well known technique. Make a base class aware of a derived class by
passing the derived class as a template parameter to the base class.
Congratulations on discovering it yourself I'd say.


And it's called the "Curiously recurring template pattern (CRTP)".
Also as you have correctly noted that even though the base class depends
on the derived class, it cannot do so in a way that requires the complete type
of derived to be known. That means you can refer to Child*/Child& but not Child
in the base class.

-Sharad

Jul 22 '05 #3

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c0*************@ID-221354.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c0*************@ID-196037.news.uni-berlin.de...

"Grey Plastic" <gr*********@hotmail.com> wrote in message
news:1d**************************@posting.google.c om...
I have a program where several classes each have a single static
std::map to keep track of all the instances of that class. For each
of these classes, I want a static lookupByID(int id) method which
returns a pointer to the instance that the id references... e.g...

Item * Item::lookupByID(Uint32 id);
Level * Level::lookupByID(Uint32 id);
Lifeform * Lifeform::lookupByID(Uint32 id);

... etc. Instead of rewriting the same code for each of these
classes, I instead wrote an Indexable class...

template <class Child> class Indexable {
private:
static std::map<Uint32,Child*> mapByID;
static Uint32 _nextID;
Uint32 _id;
public:
static Child * lookupByID(Uint32 id) { return mapByID[id]; }
Indexable() { _id=_nextID++; mapByID[_id]=(Child*)this; }
~Indexable() { mapByID[_id]=NULL; }
Uint32 id() const { return _id; }
};

and made each of my other classes subclass Indexable...

class Item : public Indexable<Item> { ... };
class Level : public Indexable<Level> { ... };
class Lifeform : public Indexable<Lifeform> { ... };

This achieves what I've wanted, but it still seems a bit weird to me.
How would you guys handle this situation?
It's a well known technique. Make a base class aware of a derived class by passing the derived class as a template parameter to the base class.
Congratulations on discovering it yourself I'd say.


And it's called the "Curiously recurring template pattern (CRTP)".
Also as you have correctly noted that even though the base class depends
on the derived class, it cannot do so in a way that requires the complete

type of derived to be known. That means you can refer to Child*/Child& but not Child in the base class.

-Sharad


I don't think that's right. How about this?

template <class Rep>
class RCObject
{
friend class RCPtr<Rep>;
public:
RCObject() : _ref(0) {}
RCObject(const RCObject<Rep>&) : _ref(0) {}
RCObject<Rep>& operator=(const RCObject<Rep>&) {}
~RCObject() { assert(_ref == 0); }
Rep* clone() const { return new Rep(*static_cast<const Rep*>(this)); }
private:
size_t _ref;
};

RCObject is a base class for intrusively reference counted objects. It
defines a clone method that calls the derived class copy ctor and therefore
needs to have the complete type.

I think the point is that Rep needs to be known when RCObject is
instantiated not when it is compiled.

john
Jul 22 '05 #4

I think the point is that Rep needs to be known when RCObject is
instantiated not when it is compiled.

True, I should I have been clearer.
The error will come during the instantiation of the base class.

In this code -

template<typename T>
struct B {
T p;
};

struct D: B<D> { // CRTP
};
int main(){
}

When B tries to get instatntiated with T=D, compiler would throw hands saying
B<T>::p has incomplete type.

Best wishes,
Sharad
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ben | last post by:
Hi all, I'm not yet good at thinking the right way in c++ so although I could solve this problem, I'm not sure if they way I'm thinking of is the best way to do it. I need a data type or class...
0
by: Simon Elliott | last post by:
I have a class factory which maps various parameters onto static creator functions: typedef TbarAutoPtr (*TcreateBar)(const std::string& barName); typedef std::map <std::string, TcreateBar>...
9
by: HL | last post by:
Hi, What is the difference between declaring a class as static and delaring methods as static (class not static)? Also, when do we use a static constructor?
18
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age"...
3
by: rsforster | last post by:
Does anyone know where I can get info on the rules for creating static libraries in C++ (I am using Visual Studio 6.0)? Or can anyone answer the following questions: 1. I have globals that are...
0
by: Alex Brown | last post by:
Is it a problem to attach Non-static site map providers under one that inherits from StaticSiteMapProvider ? We are implementing a custom site map provider for a website that is being converted...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
8
by: jayaramganapathy | last post by:
Hello friends, I have a map like std::map< std::string , std::map<std::string, std::string>* EpPropCache::propertyCache ; (This is a static instance and taken from *.cpp file) As you can see...
4
gpraghuram
by: gpraghuram | last post by:
I wrote the following code #include <map> #include<iostream> using namespace std; //typedef map<int,int> Maps; class A {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.