llmaterialid.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @file llmaterialid.h
  3. * @brief Header file for llmaterialid
  4. * @author [email protected]
  5. * $LicenseInfo:firstyear=2012&license=viewergpl$
  6. *
  7. * Copyright (c) 2012, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLMATERIALID_H
  33. #define LL_LLMATERIALID_H
  34. #include <string>
  35. #include "llsd.h"
  36. #include "lluuid.h"
  37. class LLMaterialID final
  38. {
  39. public:
  40. LLMaterialID();
  41. LLMaterialID(const LLSD& matidp);
  42. LLMaterialID(const LLSD::Binary& matidp);
  43. LLMaterialID(const void* memoryp);
  44. LLMaterialID(const LLMaterialID& other_mat_id);
  45. LLMaterialID(const LLUUID& uuid);
  46. // Allow the use of the C++11 default move constructor
  47. LLMaterialID(LLMaterialID&& other) noexcept = default;
  48. LL_INLINE bool operator==(const LLMaterialID& other_mat_id) const
  49. {
  50. return compareToOtherMaterialID(other_mat_id) == 0;
  51. }
  52. LL_INLINE bool operator!=(const LLMaterialID& other_mat_id) const
  53. {
  54. return compareToOtherMaterialID(other_mat_id) != 0;
  55. }
  56. LL_INLINE bool operator<(const LLMaterialID& other_mat_id) const
  57. {
  58. return compareToOtherMaterialID(other_mat_id) < 0;
  59. }
  60. LL_INLINE bool operator<=(const LLMaterialID& other_mat_id) const
  61. {
  62. return compareToOtherMaterialID(other_mat_id) <= 0;
  63. }
  64. LL_INLINE bool operator>(const LLMaterialID& other_mat_id) const
  65. {
  66. return compareToOtherMaterialID(other_mat_id) > 0;
  67. }
  68. LL_INLINE bool operator>=(const LLMaterialID& other_mat_id) const
  69. {
  70. return compareToOtherMaterialID(other_mat_id) >= 0;
  71. }
  72. LL_INLINE bool isNull() const
  73. {
  74. return compareToOtherMaterialID(LLMaterialID::null) == 0;
  75. }
  76. LL_INLINE bool notNull() const
  77. {
  78. return compareToOtherMaterialID(LLMaterialID::null) != 0;
  79. }
  80. LLMaterialID& operator=(const LLMaterialID& other_mat_id)
  81. {
  82. copyFromOtherMaterialID(other_mat_id);
  83. return *this;
  84. }
  85. LL_INLINE const U8* get() const { return mID; }
  86. void set(const void* memoryp);
  87. void clear();
  88. LLUUID asUUID() const;
  89. LLSD asLLSD() const;
  90. std::string asString() const;
  91. friend std::ostream& operator<<(std::ostream& s,
  92. const LLMaterialID& material_id);
  93. // Returns a 64 bits digest of the material Id, by XORing its two 64 bits
  94. // long words. HB
  95. LL_INLINE U64 getDigest64() const
  96. {
  97. U64* tmp = (U64*)mID;
  98. return tmp[0] ^ tmp[1];
  99. }
  100. public:
  101. static const LLMaterialID null;
  102. private:
  103. void parseFromBinary(const LLSD::Binary& matidp);
  104. void copyFromOtherMaterialID(const LLMaterialID& other_mat_id);
  105. S32 compareToOtherMaterialID(const LLMaterialID& other_mat_id) const;
  106. public:
  107. U8 mID[UUID_BYTES];
  108. };
  109. // std::hash implementation for LLMaterialID
  110. namespace std
  111. {
  112. template<> struct hash<LLMaterialID>
  113. {
  114. LL_INLINE size_t operator()(const LLMaterialID& id) const noexcept
  115. {
  116. return id.getDigest64();
  117. }
  118. };
  119. }
  120. // For use with boost::unordered_map and boost::unordered_set
  121. LL_INLINE size_t hash_value(const LLMaterialID& id) noexcept
  122. {
  123. return id.getDigest64();
  124. }
  125. #endif // LL_LLMATERIALID_H