Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.9 KiB

  1. ///////////////////////////////////////////////////////
  2. // StringMap.H
  3. //
  4. // STL - based template which throws COM-compatible exceptions
  5. // for making maps
  6. /////////////////////////////////////////////////////////
  7. #pragma once
  8. #pragma warning (disable: 4786) // exceeds 255 chars in browser info
  9. #include <comdef.h>
  10. #include <string>
  11. #include <map>
  12. using namespace std;
  13. class throwing_allocator : public allocator<wchar_t>
  14. {
  15. public:
  16. // overloading the allocation routines to throw
  17. // exceptions rather than fail silently
  18. pointer allocate(size_type _N, const void *)
  19. {
  20. pointer p = (_Allocate((difference_type)_N, (pointer)0));
  21. if (p == NULL)
  22. _com_raise_error (E_OUTOFMEMORY);
  23. return p;
  24. }
  25. char _FARQ *_Charalloc(size_type _N)
  26. {
  27. char _FARQ * p = _Allocate((difference_type)_N, (char _FARQ *)0);
  28. if (p == NULL)
  29. _com_raise_error (E_OUTOFMEMORY);
  30. return p;
  31. }
  32. };
  33. typedef basic_string<wchar_t, char_traits<wchar_t>,
  34. throwing_allocator > throwing_wstring;
  35. class StringToStringMap
  36. {
  37. typedef std::map<throwing_wstring, throwing_wstring> throwing_map;
  38. throwing_map m_map;
  39. public:
  40. HRESULT Add(LPCWSTR Key, LPCWSTR Value)
  41. {
  42. try
  43. {
  44. m_map[Key] = Value;
  45. // throwing_map::iterator p = m_map.find(Key);
  46. // ASSERT (m_map.find(Key) != m_map.end());
  47. // _bstr_t b = (*p).second.c_str();
  48. // TRACE ("%s -> %s\n", (LPCTSTR) _bstr_t(Key), (LPCTSTR) b);
  49. }
  50. catch (_com_error e)
  51. {
  52. return e.Error(); // out of memory
  53. }
  54. return S_OK;
  55. }
  56. throwing_map::size_type GetSize()
  57. {
  58. return m_map.size();
  59. }
  60. HRESULT Find(LPCWSTR Key, _bstr_t& Value, bool &bFound)
  61. {
  62. try
  63. {
  64. throwing_map::iterator p = m_map.find(Key);
  65. if (p == m_map.end())
  66. {
  67. bFound = false;
  68. return S_FALSE;
  69. }
  70. else
  71. {
  72. Value = (*p).second.c_str();
  73. bFound = true;
  74. }
  75. }
  76. catch (_com_error e)
  77. {
  78. return e.Error(); // out of memory
  79. }
  80. return S_OK;
  81. }
  82. };