Re: Map problem
In article <6e90926f-18bc-42ac-bac6-16e590038a75
@x41g2000hsb.googlegroups.com>, naumansulaiman@googlemail.com says...
Hi, I want to create a map of named events from a const array.
eg std::map<std::string, HANDLE> this needs to be intialised so that
initially it will be contain the names of the all the events with a
NULL handle then
i can do the following
std::map<std::string, HANDLE> Events;
std::map<std::string, HANDLE>::iterator it;
const std::string[] { "EventName1", "EventName2", "EventName3"};
it = Events.begin();
while (it != Events.end())
{
Events[it->first] = CreateEvent(NULL,FALSE,FALSE, it->first.c_str())
++it;
}
So i f i ever need a new event all i need to do is add it to the array
and the event will be created
managed, destroyed just using the map.
However do i use the string array to initalise the map keys however?
I'd start by writing a wrapper class for the events:
// warning: untested code.
class event {
HANDLE ev;
public:
// You usually don't want to name events unless you're sharing them
// across process boundaries.
event() : ev(CreateEvent(NULL, FALSE, FALSE, NULL) {}
// haven't thought a lot about this -- might cause problem when copying
// an event. They're really more "identity" than "value" objects.
~event() { CloseHandle(ev); }
DWORD wait(DWORD time = INFINITE) {
return WaitForSingleObject(ev, time);
}
};
Then you can put events into your map a bit more directly:
std::map<std::string, event> events;
char const *names[] = {"Event1", "Event2", "Event3"};
#define elements(x) (sizeof(x)/sizeof(x[0]))
for (int i=0; i<elements(names); ++i)
events.insert(std::make_pair(names[i], event()));
--
Later,
Jerry.
The universe is a figment of its own imagination.