473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with STL

I'm trying a double indirect look-up table for some function pointer in my app (yep, I have good reason).
I'm trying to use std::map for the table but (because of the double indirection) I need to use a "std::map*"
Which I don't know how to map[index] (with a pointer)

Now perhaps I'm misusing it, I'm not sure (I know nothing of STL and I have some problem understanding its header)

Anyway here is my code (problem explained in red), if you could give me some tips.....
==============
static std::map<const char*, SELmap_sels;
static std::map<OClass, std::map<SEL, IMP>*map_imps;

void id::GetImpAlways(const char* name, SEL& sel, IMP& imp)
{
if( !_handle )
throw gcnew ObjectDisposedException(Class()->Name);

if(map_sels.find(name) == map_sels.end())
{
sel = sel_register_name(name);
map_sels[name] = sel;
}
else
{
sel = map_sels[name];
}

std::map<SEL, IMP* class_imps; // here I get the problematic "map *"
if(map_imps.find(_handle->class_pointer) == map_imps.end())
{
class_imps = new std::map<SEL, IMP>();
map_imps[_handle->class_pointer] = class_imps;
}
else
{
class_imps = map_imps[_handle->class_pointer];
}

if(class_imps->find(sel) == class_imps->end())
{
imp = get_imp(_handle->class_pointer, sel);
if( !imp )
throw gcnew ObjectiveCException("No such method");
class_imps[sel] = imp; // can't do that with 'map*', what should I so?
}
else
{
imp = class_imps[sel]; // can't do that with 'map*', what should I so?
}
}
==============
Jul 12 '06 #1
13 1177
BTW, never mind my problem.
The program crash at the first line: if(map_sels.find(name) == map_sels.end())
How do I do?
I have a home made C hashtable possibly I should use it instead :-/
"Lloyd Dupont" <net.galador@ldwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
I'm trying a double indirect look-up table for some function pointer in my app (yep, I have good reason).
I'm trying to use std::map for the table but (because of the double indirection) I need to use a "std::map*"
Which I don't know how to map[index] (with a pointer)

Now perhaps I'm misusing it, I'm not sure (I know nothing of STL and I have some problem understanding its header)

Anyway here is my code (problem explained in red), if you could give me some tips.....
==============
static std::map<const char*, SELmap_sels;
static std::map<OClass, std::map<SEL, IMP>*map_imps;

void id::GetImpAlways(const char* name, SEL& sel, IMP& imp)
{
if( !_handle )
throw gcnew ObjectDisposedException(Class()->Name);

if(map_sels.find(name) == map_sels.end())
{
sel = sel_register_name(name);
map_sels[name] = sel;
}
else
{
sel = map_sels[name];
}

std::map<SEL, IMP* class_imps; // here I get the problematic "map *"
if(map_imps.find(_handle->class_pointer) == map_imps.end())
{
class_imps = new std::map<SEL, IMP>();
map_imps[_handle->class_pointer] = class_imps;
}
else
{
class_imps = map_imps[_handle->class_pointer];
}

if(class_imps->find(sel) == class_imps->end())
{
imp = get_imp(_handle->class_pointer, sel);
if( !imp )
throw gcnew ObjectiveCException("No such method");
class_imps[sel] = imp; // can't do that with 'map*', what should I so?
}
else
{
imp = class_imps[sel]; // can't do that with 'map*', what should I so?
}
}
==============
Jul 12 '06 #2
never mind, I wil use my own C Hashtable...
"Lloyd Dupont" <net.galador@ldwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
I'm trying a double indirect look-up table for some function pointer in my app (yep, I have good reason).
I'm trying to use std::map for the table but (because of the double indirection) I need to use a "std::map*"
Which I don't know how to map[index] (with a pointer)

Now perhaps I'm misusing it, I'm not sure (I know nothing of STL and I have some problem understanding its header)

Anyway here is my code (problem explained in red), if you could give me some tips.....
==============
static std::map<const char*, SELmap_sels;
static std::map<OClass, std::map<SEL, IMP>*map_imps;

void id::GetImpAlways(const char* name, SEL& sel, IMP& imp)
{
if( !_handle )
throw gcnew ObjectDisposedException(Class()->Name);

if(map_sels.find(name) == map_sels.end())
{
sel = sel_register_name(name);
map_sels[name] = sel;
}
else
{
sel = map_sels[name];
}

std::map<SEL, IMP* class_imps; // here I get the problematic "map *"
if(map_imps.find(_handle->class_pointer) == map_imps.end())
{
class_imps = new std::map<SEL, IMP>();
map_imps[_handle->class_pointer] = class_imps;
}
else
{
class_imps = map_imps[_handle->class_pointer];
}

if(class_imps->find(sel) == class_imps->end())
{
imp = get_imp(_handle->class_pointer, sel);
if( !imp )
throw gcnew ObjectiveCException("No such method");
class_imps[sel] = imp; // can't do that with 'map*', what should I so?
}
else
{
imp = class_imps[sel]; // can't do that with 'map*', what should I so?
}
}
==============
Jul 12 '06 #3
Lloyd Dupont wrote:
I'm trying a double indirect look-up table for some function pointer in
my app (yep, I have good reason).
I'm trying to use std::map for the table but (because of the double
indirection) I need to use a "std::map*"
Which I don't know how to map[index] (with a pointer)

Now perhaps I'm misusing it, I'm not sure (I know nothing of STL and I
have some problem understanding its header)

Anyway here is my code (problem explained in red), if you could give me
some tips.....
==============

static std::map<const char*, SELmap_sels;
That's your first problem. That will base look up on pointer value, not
string value. Instead, you want:

#include <cstring>
struct CharStringLess
{
bool operator()(char const* lhs, char const* rhs) const
{
return std::strcmp(lhs, rhs) < 0;
}
};

static std::map<const char*, SEL, CharStringLessmap_sels;

[snip]
std::map<SEL, IMP* class_imps; // here I get the problematic "map *"
[snip]
class_imps[sel] = imp; // can't do that with 'map*', what
should I so?
You just need to dereference the pointer:
(*class_imps)[sel] = imp;
Or you could use a reference:
std::map<SEL, IMP>& class_imps_ref = *class_imps;
class_imps_ref[sel] = imp;
}
else
{
imp = class_imps[sel]; // can't do that with 'map*', what should
I so?
Same as above.

Tom
Jul 12 '06 #4
Lloyd Dupont wrote:
never mind, I wil use my own C Hashtable...
This isn't a chatroom, it can take a while for anyone to read your post
(particularly if you post at the time you did, when neither Americans
nor Europeans tend to be working).

Tom
Jul 12 '06 #5
Tom' already pointed out how to deal with your "map *" issue. From what
you've shown here, there's absolutely no need for you to store a pointer to
a map in the first place. Why complicate matters by storing a pointer to a
map? Also, when using the C++ standard library, liberal use of typedefs
will greatly increase the maintainability and understandability of your
code - not to mention saving you typing.

-cd

// uncompiled code - typos may be lurking

// borrowing from Tom's reply...
struct string_less_t
{
bool operator()(char const* lhs, char const* rhs) const
{
return std::strcmp(lhs, rhs) < 0;
}
};

typedef std::map<const char*, SEL, string_less_tsel_map_t;
typedef std::map<SEL, IMPimp_map_t;
typedef imp_map_t* imp_map_ptr_t;
typedef std::map<OClass, imp_map_tclass_map_t;

static sel_map_t map_sels;
static class_map_t map_imps;

void id::GetImpAlways(char char* name, SEL& sel, IMP& imp)
{
if( !_handle )
throw gcnew ObjectDisposedException(Class()->Name);

if(map_sels.count(name) == 0)
{
sel = sel_register_name(name);
map_sels[name] = sel;
}
else
{
sel = map_sels[name];
}

// not optimal - map_imps is searched twice, but the optimal
// code is very ugly and harder to understand

if(map_imps.count(_handle->class_pointer) == 0)
map_imps[_handle->class_pointer] = imp_map_t();

imp_map_ptr_t class_imps = &map_imps[_handle->class_pointer];

if(class_imps->count(sel) == 0)
{
imp = get_imp(_handle->class_pointer, sel);
if( !imp )
throw gcnew ObjectiveCException("No such method");
(*class_imps)[sel] = imp;
}
else
{
imp = (*class_imps)[sel];
}
}

Jul 12 '06 #6
???

"Tom Widmer [VC++ MVP]" <to********@hotmail.comwrote in message
news:ey**************@TK2MSFTNGP04.phx.gbl...
Lloyd Dupont wrote:
>never mind, I wil use my own C Hashtable...

This isn't a chatroom, it can take a while for anyone to read your post
(particularly if you post at the time you did, when neither Americans nor
Europeans tend to be working).

Tom

Jul 13 '06 #7
As I said, as you read, it was not necessary to take time to answer.
Anyway as you take the time I will politely reply ;-)

This is not a problem: I will fully compare pointer as these string are hard
coded constant and the lookup need to be as fast aspossible.
a pointer comparison is most appropriate.

Anyway it's all woking now with C Hashtable, whu should I bother?

"Tom Widmer [VC++ MVP]" <to********@hotmail.comwrote in message
news:ud*************@TK2MSFTNGP05.phx.gbl...
Lloyd Dupont wrote:
>I'm trying a double indirect look-up table for some function pointer in
my app (yep, I have good reason).
I'm trying to use std::map for the table but (because of the double
indirection) I need to use a "std::map*"
Which I don't know how to map[index] (with a pointer)
Now perhaps I'm misusing it, I'm not sure (I know nothing of STL and I
have some problem understanding its header)
Anyway here is my code (problem explained in red), if you could give me
some tips.....
==============

static std::map<const char*, SELmap_sels;

That's your first problem. That will base look up on pointer value, not
string value. Instead, you want:

#include <cstring>
struct CharStringLess
{
bool operator()(char const* lhs, char const* rhs) const
{
return std::strcmp(lhs, rhs) < 0;
}
};

static std::map<const char*, SEL, CharStringLessmap_sels;

[snip]
> std::map<SEL, IMP* class_imps; // here I get the problematic "map
*"
[snip]
> class_imps[sel] = imp; // can't do that with 'map*', what should
I so?

You just need to dereference the pointer:
(*class_imps)[sel] = imp;
Or you could use a reference:
std::map<SEL, IMP>& class_imps_ref = *class_imps;
class_imps_ref[sel] = imp;
> }
else
{
imp = class_imps[sel]; // can't do that with 'map*', what should
I so?

Same as above.

Tom

Jul 13 '06 #8
1st: forget about char* comparison, pointer comparison is most appropriate!

2nd: thanks for your sample anyway, it taught me some bits about STL I might
find handy in the future.

3rd: For the present, as I indicate in a previous post to save you the time
to answer, I already solved the problem with a C Hastable implementation I
wrote.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:OY**************@TK2MSFTNGP03.phx.gbl...
Tom' already pointed out how to deal with your "map *" issue. From what
you've shown here, there's absolutely no need for you to store a pointer
to a map in the first place. Why complicate matters by storing a pointer
to a map? Also, when using the C++ standard library, liberal use of
typedefs will greatly increase the maintainability and understandability
of your code - not to mention saving you typing.

-cd

// uncompiled code - typos may be lurking

// borrowing from Tom's reply...
struct string_less_t
{
bool operator()(char const* lhs, char const* rhs) const
{
return std::strcmp(lhs, rhs) < 0;
}
};

typedef std::map<const char*, SEL, string_less_tsel_map_t;
typedef std::map<SEL, IMPimp_map_t;
typedef imp_map_t* imp_map_ptr_t;
typedef std::map<OClass, imp_map_tclass_map_t;

static sel_map_t map_sels;
static class_map_t map_imps;

void id::GetImpAlways(char char* name, SEL& sel, IMP& imp)
{
if( !_handle )
throw gcnew ObjectDisposedException(Class()->Name);

if(map_sels.count(name) == 0)
{
sel = sel_register_name(name);
map_sels[name] = sel;
}
else
{
sel = map_sels[name];
}

// not optimal - map_imps is searched twice, but the optimal
// code is very ugly and harder to understand

if(map_imps.count(_handle->class_pointer) == 0)
map_imps[_handle->class_pointer] = imp_map_t();

imp_map_ptr_t class_imps = &map_imps[_handle->class_pointer];

if(class_imps->count(sel) == 0)
{
imp = get_imp(_handle->class_pointer, sel);
if( !imp )
throw gcnew ObjectiveCException("No such method");
(*class_imps)[sel] = imp;
}
else
{
imp = (*class_imps)[sel];
}
}

Jul 13 '06 #9
Lloyd Dupont <net.galador@ldwrote:
1st: forget about char* comparison, pointer comparison is most appropriate!

2nd: thanks for your sample anyway, it taught me some bits about STL I might
find handy in the future.

3rd: For the present, as I indicate in a previous post to save you the time
to answer, I already solved the problem with a C Hastable implementation I
wrote. ^
|
What an appropriate typo! Oh wait, you forgot an 'e'-+

<g>
[...]
Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"The sarcasm is mightier than the sword."
Eric Jarvis
Jul 20 '06 #10
>3rd: For the present, as I indicate in a previous post to save you the
>time
to answer, I already solved the problem with a C Hastable implementation
I
wrote. ^
|
What an appropriate typo! Oh wait, you forgot an 'e'-+
?
I'm searching but I can't find....
Could you help me improve my english?
Jul 21 '06 #11
>3rd: For the present, as I indicate in a previous post to save you the
>time
to answer, I already solved the problem with a C Hastable implementation
I
wrote. ^
|
What an appropriate typo! Oh wait, you forgot an 'e'-+
I see... ASCII art, works better with notepad.
Yeah a 'h' was missing.

Anyway I still don't get your comment......
Jul 21 '06 #12
Lloyd Dupont <net.galador@ldwrote:
3rd: For the present, as I indicate in a previous post to save you the
time
to answer, I already solved the problem with a C Hastable implementation
I
wrote. ^
|
What an appropriate typo! Oh wait, you forgot an 'e'-+
I see... ASCII art, works better with notepad.
Yeah a 'h' was missing.

Anyway I still don't get your comment......
You asked a question here and didn't give the world a
chance to read it, think about and answer you, but
/hasted/ to use some other implementation, despite
the fact that several people showed you what was
wrong with your code.

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"The sarcasm is mightier than the sword."
Eric Jarvis
Jul 25 '06 #13
You asked a question here and didn't give the world a
chance to read it, think about and answer you, but
/hasted/ to use some other implementation, despite
the fact that several people showed you what was
wrong with your code.
I had a problem, I asked a question first (always do like that).
Then I thought about it and after a couple of hours found an other way of
solving the problem which was:
1. more appropriate
2. I had no syntaxic problem to implement.

Then to save useless thinking for would-be helper I posted on the newsgroup:
"Solved"!

Now some people on the newsgroup start answering 3 hours after I say the
problem was solved.

Well, while it's nice to know how to solve my previous syntaxic problem and
improve my C++ syntax knowledge, it's a bit irrelevant now.

I'm surprised by the reaction here, looks like a bunch biggot to me...
Jul 26 '06 #14

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

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
by: Peter Olcott | last post by:
www.halting-problem.com
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: =?Utf-8?B?am8uZWw=?= | last post by:
Hello All, I am developing an Input Methop (IM) for PocketPC / Windows Mobile (PPC/WM). On some devices the IM will not start. The IM appears in the IM-List but when it is selected from the...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.