1
0

mp4array.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is MPEG4IP.
  13. *
  14. * The Initial Developer of the Original Code is Cisco Systems Inc.
  15. * Portions created by Cisco Systems Inc. are
  16. * Copyright (C) Cisco Systems Inc. 2001. All Rights Reserved.
  17. *
  18. * Contributor(s):
  19. * Dave Mackie [email protected]
  20. */
  21. #ifndef __MP4_ARRAY_INCLUDED__
  22. #define __MP4_ARRAY_INCLUDED__
  23. typedef u_int32_t MP4ArrayIndex;
  24. class MP4Array {
  25. public:
  26. MP4Array() {
  27. m_numElements = 0;
  28. m_maxNumElements = 0;
  29. }
  30. inline bool ValidIndex(MP4ArrayIndex index) {
  31. if (m_numElements == 0 || index > m_numElements - 1) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. inline MP4ArrayIndex Size(void) {
  37. return m_numElements;
  38. }
  39. inline MP4ArrayIndex MaxSize(void) {
  40. return m_maxNumElements;
  41. }
  42. protected:
  43. MP4ArrayIndex m_numElements;
  44. MP4ArrayIndex m_maxNumElements;
  45. };
  46. // macro to generate subclasses
  47. // we use this as an alternative to templates
  48. // due to the excessive compile time price of extensive template usage
  49. template <class type>
  50. class MP4TArray : public MP4Array
  51. {
  52. public:
  53. MP4TArray() {
  54. m_elements = NULL;
  55. }
  56. ~MP4TArray() {
  57. MP4Free(m_elements);
  58. }
  59. inline void Add(type newElement) {
  60. Insert(newElement, m_numElements);
  61. }
  62. void Insert(type newElement, MP4ArrayIndex newIndex) {
  63. if (newIndex > m_numElements) {
  64. throw new MP4Error(ERANGE, "MP4Array::Insert");
  65. }
  66. if (m_numElements == m_maxNumElements) {
  67. m_maxNumElements = MAX(m_maxNumElements, 1) * 2;
  68. m_elements = (type*)MP4ReallocArray(m_elements,
  69. m_maxNumElements, sizeof(type));
  70. }
  71. memmove(&m_elements[newIndex + 1], &m_elements[newIndex],
  72. (m_numElements - newIndex) * sizeof(type));
  73. m_elements[newIndex] = newElement;
  74. m_numElements++;
  75. }
  76. void Delete(MP4ArrayIndex index) {
  77. if (!ValidIndex(index)) {
  78. throw new MP4Error(ERANGE, "MP4Array::Delete");
  79. }
  80. m_numElements--;
  81. if (index < m_numElements) {
  82. memmove(&m_elements[index], &m_elements[index + 1],
  83. (m_numElements - index) * sizeof(type));
  84. }
  85. }
  86. void Resize(MP4ArrayIndex newSize) {
  87. m_numElements = newSize;
  88. m_maxNumElements = newSize;
  89. m_elements = (type*)MP4ReallocArray(m_elements,
  90. m_maxNumElements, sizeof(type));
  91. }
  92. type& operator[](MP4ArrayIndex index) {
  93. if (!ValidIndex(index)) {
  94. throw new MP4Error(ERANGE, "index %u of %u", "MP4Array::[]", index, m_numElements);
  95. }
  96. return m_elements[index];
  97. }
  98. protected:
  99. type* m_elements;
  100. };
  101. #define MP4ARRAY_DECL(name, type) typedef MP4TArray<type> name##Array;
  102. MP4ARRAY_DECL(MP4Integer8, u_int8_t)
  103. MP4ARRAY_DECL(MP4Integer16, u_int16_t)
  104. MP4ARRAY_DECL(MP4Integer32, u_int32_t)
  105. MP4ARRAY_DECL(MP4Integer64, u_int64_t)
  106. MP4ARRAY_DECL(MP4Float32, float)
  107. MP4ARRAY_DECL(MP4Float64, double)
  108. MP4ARRAY_DECL(MP4String, char*)
  109. MP4ARRAY_DECL(MP4Bytes, u_int8_t*)
  110. #endif /* __MP4_ARRAY_INCLUDED__ */