llmaterialid.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @file llmaterialid.cpp
  3. * @brief Implementation of llmaterialid
  4. * @author [email protected]
  5. *
  6. * $LicenseInfo:firstyear=2012&license=viewergpl$
  7. *
  8. * Copyright (c) 2012, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "linden_common.h"
  34. #include "llmaterialid.h"
  35. const LLMaterialID LLMaterialID::null;
  36. LLMaterialID::LLMaterialID()
  37. {
  38. clear();
  39. }
  40. LLMaterialID::LLMaterialID(const LLSD& matidp)
  41. {
  42. if (matidp.isBinary())
  43. {
  44. parseFromBinary(matidp.asBinary());
  45. }
  46. else if (matidp.isUUID())
  47. {
  48. set(matidp.asUUID().mData);
  49. }
  50. else
  51. {
  52. llwarns << "Non-binary and non-UUID material LLSD: "
  53. << matidp << llendl;
  54. llassert(false);
  55. clear();
  56. }
  57. }
  58. LLMaterialID::LLMaterialID(const LLSD::Binary& matidp)
  59. {
  60. parseFromBinary(matidp);
  61. }
  62. LLMaterialID::LLMaterialID(const void* memoryp)
  63. {
  64. set(memoryp);
  65. }
  66. LLMaterialID::LLMaterialID(const LLMaterialID& other_mat_id)
  67. {
  68. copyFromOtherMaterialID(other_mat_id);
  69. }
  70. LLMaterialID::LLMaterialID(const LLUUID& uuid)
  71. {
  72. set(uuid.mData);
  73. }
  74. void LLMaterialID::set(const void* memoryp)
  75. {
  76. // Assumes that the required size of memory is available
  77. if (memoryp)
  78. {
  79. memcpy(mID, memoryp, UUID_BYTES * sizeof(U8));
  80. }
  81. else
  82. {
  83. llwarns << "NULL memory pointer passed !" << llendl;
  84. llassert(false);
  85. clear();
  86. }
  87. }
  88. void LLMaterialID::clear()
  89. {
  90. memset(mID, 0, UUID_BYTES * sizeof(U8));
  91. }
  92. LLUUID LLMaterialID::asUUID() const
  93. {
  94. LLUUID ret;
  95. memcpy(ret.mData, mID, UUID_BYTES * sizeof(U8));
  96. return ret;
  97. }
  98. LLSD LLMaterialID::asLLSD() const
  99. {
  100. LLSD::Binary mat_id_binary;
  101. mat_id_binary.resize(UUID_BYTES * sizeof(U8));
  102. memcpy(mat_id_binary.data(), mID, UUID_BYTES * sizeof(U8));
  103. return LLSD(mat_id_binary);
  104. }
  105. std::string LLMaterialID::asString() const
  106. {
  107. std::string mat_id_str;
  108. for (size_t i = 0; i < UUID_BYTES / sizeof(U32); ++i)
  109. {
  110. if (i != 0)
  111. {
  112. mat_id_str += "-";
  113. }
  114. const U32* value =
  115. reinterpret_cast<const U32*>(&get()[i * sizeof(U32)]);
  116. mat_id_str += llformat("%08x", *value);
  117. }
  118. return mat_id_str;
  119. }
  120. std::ostream& operator<<(std::ostream& s, const LLMaterialID &material_id)
  121. {
  122. s << material_id.asString();
  123. return s;
  124. }
  125. void LLMaterialID::parseFromBinary(const LLSD::Binary& matidp)
  126. {
  127. llassert(matidp.size() == (UUID_BYTES * sizeof(U8)));
  128. memcpy(mID, &matidp[0], UUID_BYTES * sizeof(U8));
  129. }
  130. void LLMaterialID::copyFromOtherMaterialID(const LLMaterialID& other_mat_id)
  131. {
  132. memcpy(mID, other_mat_id.get(), UUID_BYTES * sizeof(U8));
  133. }
  134. S32 LLMaterialID::compareToOtherMaterialID(const LLMaterialID& other_mat_id) const
  135. {
  136. S32 retval = 0;
  137. for (size_t i = 0; retval == 0 && i < UUID_BYTES / sizeof(U32); ++i)
  138. {
  139. const U32* this_val =
  140. reinterpret_cast<const U32*>(&get()[i * sizeof(U32)]);
  141. const U32* other_val =
  142. reinterpret_cast<const U32*>(&other_mat_id.get()[i * sizeof(U32)]);
  143. retval = *this_val < *other_val ? -1
  144. : (*this_val > *other_val ? 1 : 0);
  145. }
  146. return retval;
  147. }