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?
}
}
============== | | | | re: Problem with STL
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:%23T72JOXpGHA.4760@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?
}
}
============== | | | | re: Problem with STL
never mind, I wil use my own C Hashtable...
"Lloyd Dupont" <net.galador@ldwrote in message news:%23T72JOXpGHA.4760@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?
}
}
============== | | | | re: Problem with STL
Lloyd Dupont wrote: Quote:
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] Quote:
std::map<SEL, IMP* class_imps; // here I get the problematic "map *"
[snip] Quote:
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; Quote:
}
else
{
imp = class_imps[sel]; // can't do that with 'map*', what should
I so?
Same as above.
Tom | | | | re: Problem with STL
Lloyd Dupont wrote: Quote:
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 | | | | re: Problem with STL
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];
}
} | | | | re: Problem with STL
???
"Tom Widmer [VC++ MVP]" <tom_usenet@hotmail.comwrote in message
news:eyhqBLZpGHA.1548@TK2MSFTNGP04.phx.gbl... Quote:
Lloyd Dupont wrote: Quote:
>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
| | | | re: Problem with STL
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]" <tom_usenet@hotmail.comwrote in message
news:udWv9HZpGHA.756@TK2MSFTNGP05.phx.gbl... Quote:
Lloyd Dupont wrote: Quote:
>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] Quote:
> std::map<SEL, IMP* class_imps; // here I get the problematic "map
>*"
[snip] Quote:
> 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;
> Quote:
> }
> else
> {
> imp = class_imps[sel]; // can't do that with 'map*', what should
>I so?
>
Same as above.
>
Tom
| | | | re: Problem with STL
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]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam >
wrote in message news:OYtVcmbpGHA.1796@TK2MSFTNGP03.phx.gbl... Quote:
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];
}
}
>
>
>
| | | | re: Problem with STL
Lloyd Dupont <net.galador@ldwrote: Quote:
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
-- SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org
"The sarcasm is mightier than the sword."
Eric Jarvis | | | | re: Problem with STL
>3rd: For the present, as I indicate in a previous post to save you the Quote: Quote:
>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? | | | | re: Problem with STL
>3rd: For the present, as I indicate in a previous post to save you the Quote: Quote:
>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...... | | | | re: Problem with STL
Lloyd Dupont <net.galador@ldwrote: Quote: Quote: Quote:
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
-- SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org
"The sarcasm is mightier than the sword."
Eric Jarvis | | | | re: Problem with STL
You asked a question here and didn't give the world a Quote:
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... |  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,392 network members.
|