0

I need help in implementing a design:

I have MessageID's(integer Macros) declared in project. Each MID is associated one or more sources(enum 0 -19). By checking each source of MID, I want to call different functions. My followed following approach:

   typedef std::pair<int,unsigned int *> MIDPair; 
            - this binds MID(int) with sources(array of int)

   typedef std::map<MIDPair,fpPtr> mapRSE; 
            - carries MIDpair with funtion pointer

Initially I am creating different pairs(mid's and array having applicable sources) and pushing them in map with applicable function pointers. I when i receive any MID i will check the current source and call corresponding function.

please let me know if my approach is correct on the cotainers selected/ or suggest me any other approach

Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48

1 Answers1

0

Your approach is workable:

  • you'll need to use lower_bound or upper_bound to find a key with that MID value in the map, but you won't necessarily have the source enum value you want at that location: you'll have to increment over all keys with that MID, checking for the source value

    • you can use a binary search through the array of source integers (if you keep them sorted)

That's likely not too bad efficiency wise, but does involve quite a lot of fiddly coding.

You'd probably find it simpler to use a container like:

std::map<int, std::map<unsigned, fpPtr>> mapRSE;

Then you could call mapRSE[mid][source]() (or use .at or .find if you don't want to crash on an unexpected key).

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252