Opera 12.15 Source Code
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.

TypeDescriptor.h 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. **
  3. ** Copyright (C) 1995-2011 Opera Software ASA. All rights reserved.
  4. **
  5. ** This file is part of the Opera web browser.
  6. ** It may not be distributed under any circumstances.
  7. */
  8. #ifndef TYPEDESCRIPTOR_H
  9. #define TYPEDESCRIPTOR_H
  10. #include "modules/search_engine/SearchUtils.h"
  11. #if defined _DEBUG || defined SELFTEST
  12. #define ESTIMATE_MEMORY_USED_AVAILABLE
  13. #endif
  14. /**
  15. * @brief Description of the basic properties of a type.
  16. * @author Pavel Studeny <pavels@opera.com>
  17. *
  18. * Basic description of a type used by VectorBase or BTreeBase.
  19. */
  20. struct TypeDescriptor
  21. {
  22. typedef CHECK_RESULT(OP_STATUS (* AssignPtr)(void *dst, const void *src));
  23. typedef void (* DestructPtr)(void *item);
  24. typedef BOOL (* ComparePtr)(const void *left, const void *right);
  25. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  26. typedef size_t (* EstimateMemoryUsedPtr)(const void *item);
  27. #endif
  28. TypeDescriptor(size_t type_size, AssignPtr assign, ComparePtr compare, DestructPtr destruct
  29. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  30. , EstimateMemoryUsedPtr estimateMemoryUsed
  31. #endif
  32. )
  33. {
  34. size = type_size;
  35. size_align32 = (size_t)((type_size+3) & ~3);
  36. Assign = assign;
  37. Destruct = destruct;
  38. Compare = compare;
  39. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  40. EstimateMemoryUsed = estimateMemoryUsed;
  41. #endif
  42. }
  43. size_t size;
  44. size_t size_align32;
  45. AssignPtr Assign;
  46. DestructPtr Destruct;
  47. ComparePtr Compare;
  48. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  49. EstimateMemoryUsedPtr EstimateMemoryUsed;
  50. #endif
  51. };
  52. /**
  53. * @brief Default TypeDescriptor for non-pointer types. Used for integers etc.
  54. */
  55. template <typename T> struct DefDescriptor : public TypeDescriptor
  56. {
  57. DefDescriptor(void) : TypeDescriptor(sizeof(T), &Assign, &Compare, &Destruct
  58. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  59. , &EstimateMemoryUsed
  60. #endif
  61. ) {}
  62. CHECK_RESULT(static OP_STATUS Assign(void *dst, const void *src))
  63. {
  64. *(T *)dst = *(T *)src;
  65. return OpStatus::OK;
  66. }
  67. static void Destruct(void *item)
  68. {
  69. ((T *)item)->~T();
  70. }
  71. static BOOL Compare(const void *left, const void *right)
  72. {
  73. return (*(T *)left < *(T *)right);
  74. }
  75. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  76. static size_t EstimateMemoryUsed(const void *item)
  77. {
  78. return sizeof(T);
  79. }
  80. #endif
  81. };
  82. /**
  83. * @brief Default TypeDescriptor for pointers.
  84. * Warning: Do not use pointer to objects on instantiation of template,
  85. * that will give memory leaks. Do not use PtrDescriptor<int*>,
  86. * do use use PtrDescriptor<int>
  87. */
  88. template <typename T> struct PtrDescriptor : public TypeDescriptor
  89. {
  90. PtrDescriptor(void) : TypeDescriptor(sizeof(T *), &Assign, &Compare, &Destruct
  91. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  92. , &EstimateMemoryUsed
  93. #endif
  94. ) {}
  95. CHECK_RESULT(static OP_STATUS Assign(void *dst, const void *src))
  96. {
  97. *(T **)dst = *(T **)src;
  98. return OpStatus::OK;
  99. }
  100. static void Destruct(void *item)
  101. {
  102. OP_DELETE(*(T **)item);
  103. }
  104. static void DestructArray(void *item)
  105. {
  106. OP_DELETEA(*(T **)item);
  107. }
  108. static BOOL Compare(const void *left, const void *right)
  109. {
  110. return (*(T **)left < *(T **)right);
  111. }
  112. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  113. static size_t EstimateMemoryUsed(const void *item)
  114. {
  115. return sizeof(T*) + (*(const T **)item)->EstimateMemoryUsed() + 2*sizeof(size_t);
  116. }
  117. #endif
  118. };
  119. #endif // TYPEDESCRIPTOR_H