replace CMapStringToOb by standard C
-
I have inherited some old code. Our sadistic project management has decided to forbid use of MFC in the hope of being more portable. I replaced all the CStrings with string, now I have got some tricky parts left. The guy who wrote this used CMapStringToOb in what seems to be a very clever way and I think it is a fine piece of code. But I have to replace it - but with what? I can't use CObjects any more. I have things like this: CString csMsgID; pCommand SACommand; //(which is a CObject) CMapStringToOb m_knownCommands; m_knownCommands.Lookup((LPCTSTR)csMsgID, (CObject*&) pCommand) m_knownCommands.SetAt((LPCTSTR)csMsgID, apFba[i]); I suppose I could plod through and dream up something complicated and slow, but does anyone have an idea how to do this in an elegant manner? Is this what the type Object is for?
------------- Bibo ergo sum
-
I have inherited some old code. Our sadistic project management has decided to forbid use of MFC in the hope of being more portable. I replaced all the CStrings with string, now I have got some tricky parts left. The guy who wrote this used CMapStringToOb in what seems to be a very clever way and I think it is a fine piece of code. But I have to replace it - but with what? I can't use CObjects any more. I have things like this: CString csMsgID; pCommand SACommand; //(which is a CObject) CMapStringToOb m_knownCommands; m_knownCommands.Lookup((LPCTSTR)csMsgID, (CObject*&) pCommand) m_knownCommands.SetAt((LPCTSTR)csMsgID, apFba[i]); I suppose I could plod through and dream up something complicated and slow, but does anyone have an idea how to do this in an elegant manner? Is this what the type Object is for?
------------- Bibo ergo sum
RedSonja wrote:
I have inherited some old code. Our sadistic project management has decided to forbid use of MFC in the hope of being more portable.
I'm definitely not an
MFC
fan, anyway I think your management deserves a kick in the ass. Must you be stuck withC
language or may you useC++
? WithC++
, you may useSTL
containers. If you need to use onlyC
, then I suppose you should search for a library offering hashing functions. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
RedSonja wrote:
I have inherited some old code. Our sadistic project management has decided to forbid use of MFC in the hope of being more portable.
I'm definitely not an
MFC
fan, anyway I think your management deserves a kick in the ass. Must you be stuck withC
language or may you useC++
? WithC++
, you may useSTL
containers. If you need to use onlyC
, then I suppose you should search for a library offering hashing functions. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Oh, yes, we can use STL containers. Which do you recommend? (I am not really lazy, but I do not usually program this high up in the food chain, and my boss wanted it done in December.)
------------- Bibo ergo sum
I guess using a std::map would do the trick. The problem is that you won't be able to use CObject as a base class, but anyway it is quite ugly anyway. If all the objects in your map have the same base class, then it is fine. If your map needs to store different kind of objects, then probably you need to think the design a bit better...
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Oh, yes, we can use STL containers. Which do you recommend? (I am not really lazy, but I do not usually program this high up in the food chain, and my boss wanted it done in December.)
------------- Bibo ergo sum
std::map< std::string, YourRootClass * >
However, as already suggested by Cedric Moonen, maybe better to re-think a bit about the project design (I know it would require experience, time and effort) than mimic the
MFC
behaviour. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
std::map< std::string, YourRootClass * >
However, as already suggested by Cedric Moonen, maybe better to re-think a bit about the project design (I know it would require experience, time and effort) than mimic the
MFC
behaviour. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Hmm, you are quite right. Looking at the code a bit harder (I was scared off by CMapStringToOb looking so complicated) I can see he has nothing more than a list of objects derived from the same class, nothing more than that. So I can try with std::list and do the clever bits in a simpler way. I was hoping to avoid changing every single reference to the things (this is a very large amount of code), but it has to be done, and this week no-one is there to ask what I'm doing. I have redesigned this project quite a lot since it landed on my desk, and this is just about the last untouched corner.
------------- Bibo ergo sum
-
Hmm, you are quite right. Looking at the code a bit harder (I was scared off by CMapStringToOb looking so complicated) I can see he has nothing more than a list of objects derived from the same class, nothing more than that. So I can try with std::list and do the clever bits in a simpler way. I was hoping to avoid changing every single reference to the things (this is a very large amount of code), but it has to be done, and this week no-one is there to ask what I'm doing. I have redesigned this project quite a lot since it landed on my desk, and this is just about the last untouched corner.
------------- Bibo ergo sum
RedSonja wrote:
I can see he has nothing more than a list of objects derived from the same class, nothing more than that. So I can try with std::list and do the clever bits in a simpler way.
It is a bit more complex than just a list. It is a map which associate each object to a key (being a string). No duplicate key can be found in the map. To begin working with a std::map, you can look at these tutorials: here[^] or here[^]. You can probably find hundreds of tutorials on the net if you google a bit.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
RedSonja wrote:
I can see he has nothing more than a list of objects derived from the same class, nothing more than that. So I can try with std::list and do the clever bits in a simpler way.
It is a bit more complex than just a list. It is a map which associate each object to a key (being a string). No duplicate key can be found in the map. To begin working with a std::map, you can look at these tutorials: here[^] or here[^]. You can probably find hundreds of tutorials on the net if you google a bit.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++