nsID.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsID_h__
  6. #define nsID_h__
  7. #include <string.h>
  8. #include "nscore.h"
  9. #define NSID_LENGTH 39
  10. /**
  11. * A "unique identifier". This is modeled after OSF DCE UUIDs.
  12. */
  13. struct nsID
  14. {
  15. /**
  16. * @name Identifier values
  17. */
  18. //@{
  19. uint32_t m0;
  20. uint16_t m1;
  21. uint16_t m2;
  22. uint8_t m3[8];
  23. //@}
  24. /**
  25. * @name Methods
  26. */
  27. //@{
  28. /**
  29. * Ensures everything is zeroed out.
  30. */
  31. void Clear();
  32. /**
  33. * Equivalency method. Compares this nsID with another.
  34. * @return <b>true</b> if they are the same, <b>false</b> if not.
  35. */
  36. inline bool Equals(const nsID& aOther) const
  37. {
  38. // Unfortunately memcmp isn't faster than this.
  39. return
  40. (((uint32_t*)&m0)[0] == ((uint32_t*)&aOther.m0)[0]) &&
  41. (((uint32_t*)&m0)[1] == ((uint32_t*)&aOther.m0)[1]) &&
  42. (((uint32_t*)&m0)[2] == ((uint32_t*)&aOther.m0)[2]) &&
  43. (((uint32_t*)&m0)[3] == ((uint32_t*)&aOther.m0)[3]);
  44. }
  45. inline bool operator==(const nsID& aOther) const
  46. {
  47. return Equals(aOther);
  48. }
  49. /**
  50. * nsID Parsing method. Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
  51. * string into an nsID
  52. */
  53. bool Parse(const char* aIDStr);
  54. #ifndef XPCOM_GLUE_AVOID_NSPR
  55. /**
  56. * nsID string encoder. Returns an allocated string in
  57. * {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} format. Caller should free string.
  58. * YOU SHOULD ONLY USE THIS IF YOU CANNOT USE ToProvidedString() BELOW.
  59. */
  60. char* ToString() const;
  61. /**
  62. * nsID string encoder. Builds a string in
  63. * {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} format, into a char[NSID_LENGTH]
  64. * buffer provided by the caller (for instance, on the stack).
  65. */
  66. void ToProvidedString(char (&aDest)[NSID_LENGTH]) const;
  67. #endif // XPCOM_GLUE_AVOID_NSPR
  68. //@}
  69. };
  70. #ifndef XPCOM_GLUE_AVOID_NSPR
  71. /**
  72. * A stack helper class to convert a nsID to a string. Useful
  73. * for printing nsIDs. For example:
  74. * nsID aID = ...;
  75. * printf("%s", nsIDToCString(aID).get());
  76. */
  77. class nsIDToCString
  78. {
  79. public:
  80. explicit nsIDToCString(const nsID& aID)
  81. {
  82. aID.ToProvidedString(mStringBytes);
  83. }
  84. const char *get() const
  85. {
  86. return mStringBytes;
  87. }
  88. protected:
  89. char mStringBytes[NSID_LENGTH];
  90. };
  91. #endif
  92. /*
  93. * Class IDs
  94. */
  95. typedef nsID nsCID;
  96. // Define an CID
  97. #define NS_DEFINE_CID(_name, _cidspec) \
  98. const nsCID _name = _cidspec
  99. #define NS_DEFINE_NAMED_CID(_name) \
  100. static const nsCID k##_name = _name
  101. #define REFNSCID const nsCID&
  102. /**
  103. * An "interface id" which can be used to uniquely identify a given
  104. * interface.
  105. */
  106. typedef nsID nsIID;
  107. /**
  108. * A macro shorthand for <tt>const nsIID&<tt>
  109. */
  110. #define REFNSIID const nsIID&
  111. /**
  112. * Define an IID
  113. * obsolete - do not use this macro
  114. */
  115. #define NS_DEFINE_IID(_name, _iidspec) \
  116. const nsIID _name = _iidspec
  117. /**
  118. * A macro to build the static const IID accessor method. The Dummy
  119. * template parameter only exists so that the kIID symbol will be linked
  120. * properly (weak symbol on linux, gnu_linkonce on mac, multiple-definitions
  121. * merged on windows). Dummy should always be instantiated as "void".
  122. */
  123. #define NS_DECLARE_STATIC_IID_ACCESSOR(the_iid) \
  124. template<typename T, typename U> \
  125. struct COMTypeInfo;
  126. #define NS_DEFINE_STATIC_IID_ACCESSOR(the_interface, the_iid) \
  127. template<typename T> \
  128. struct the_interface::COMTypeInfo<the_interface, T> { \
  129. static const nsIID kIID NS_HIDDEN; \
  130. }; \
  131. template<typename T> \
  132. const nsIID the_interface::COMTypeInfo<the_interface, T>::kIID NS_HIDDEN = the_iid;
  133. /**
  134. * A macro to build the static const CID accessor method
  135. */
  136. #define NS_DEFINE_STATIC_CID_ACCESSOR(the_cid) \
  137. static const nsID& GetCID() {static const nsID cid = the_cid; return cid;}
  138. #define NS_GET_IID(T) (T::COMTypeInfo<T, void>::kIID)
  139. #define NS_GET_TEMPLATE_IID(T) (T::template COMTypeInfo<T, void>::kIID)
  140. #endif