Re: Problem with STL

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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@xxxxxxxxxxxxxxx>
wrote in message news:OYtVcmbpGHA.1796@xxxxxxxxxxxxxxxxxxxxxxx
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_t> sel_map_t;
typedef std::map<SEL, IMP> imp_map_t;
typedef imp_map_t* imp_map_ptr_t;
typedef std::map<OClass, imp_map_t> class_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];
}
}





.



Relevant Pages

  • Re: HPGCC: exit() and return
    ... The switch statements ... static char const *string2basen{ ... Do not declare char const * string; as a const value (use just ... pointer since it is 'const', ...
    (comp.sys.hp48)
  • Re: Q about passing data as a const array
    ... The const on the len parameter is superfluous; ... exactly equivalent to "const char *data", ... void func(const struct mydata foo); ... Applying const to a pointer parameter can be quite useful, ...
    (comp.lang.c)
  • Re: Whats the deal with const?
    ... specify the const part. ... The definition of a function parameter as "const char *x" defines that ... argument as a pointer to one or more constant characters. ... int some_func ...
    (comp.lang.c)
  • Re: Q about passing data as a const array
    ... The const on the len parameter is superfluous; ... exactly equivalent to "const char *data", ... void func(const struct mydata foo); ... Applying const to a pointer parameter can be quite useful, ...
    (comp.lang.c)
  • Re: Implicit addition of const qualifiers
    ... > A lot of functions use const pointer arguments. ... function that takes a const char* parameter (points to chars that cannot ... All that happens is that the function cannot easily modify ...
    (comp.lang.c)