template_utils.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. #ifndef TEMPLATE_UTILS_INCLUDED
  19. #define TEMPLATE_UTILS_INCLUDED
  20. #include <stddef.h>
  21. #include "my_dbug.h"
  22. /**
  23. @file include/template_utils.h
  24. */
  25. /**
  26. Clears a container, but deletes all objects that the elements point to first.
  27. @tparam Container_type Container of pointers.
  28. */
  29. template <typename Container_type>
  30. void delete_container_pointers(Container_type &container) {
  31. typename Container_type::iterator it1 = container.begin();
  32. typename Container_type::iterator it2 = container.end();
  33. for (; it1 != it2; ++it1) {
  34. delete (*it1);
  35. }
  36. container.clear();
  37. }
  38. /**
  39. Clears a container, but frees all objects that the elements point to first.
  40. @tparam Container_type Container of pointers.
  41. */
  42. template <typename Container_type>
  43. void my_free_container_pointers(Container_type &container) {
  44. typename Container_type::iterator it1 = container.begin();
  45. typename Container_type::iterator it2 = container.end();
  46. for (; it1 != it2; ++it1) {
  47. my_free(*it1);
  48. }
  49. container.clear();
  50. }
  51. /**
  52. Casts from one pointer type, to another, without using
  53. reinterpret_cast or C-style cast:
  54. foo *f; bar *b= pointer_cast<bar*>(f);
  55. This avoids having to do:
  56. foo *f; bar *b= static_cast<bar*>(static_cast<void*>(f));
  57. */
  58. template <typename T>
  59. inline T pointer_cast(void *p) {
  60. return static_cast<T>(p);
  61. }
  62. template <typename T>
  63. inline const T pointer_cast(const void *p) {
  64. return static_cast<T>(p);
  65. }
  66. /**
  67. Casts from one pointer type to another in a type hierarchy.
  68. In debug mode, we verify the cast is indeed legal.
  69. @tparam Target The descendent type, must be a pointer type.
  70. @tparam Source The parent type.
  71. @param arg The pointer to be down-cast.
  72. @return A pointer of type Target.
  73. */
  74. template <typename Target, typename Source>
  75. inline Target down_cast(Source *arg) {
  76. DBUG_ASSERT(NULL != dynamic_cast<Target>(arg));
  77. return static_cast<Target>(arg);
  78. }
  79. /**
  80. Casts from one reference type to another in a type hierarchy.
  81. In debug mode, we verify the cast is indeed legal.
  82. @tparam Target The descendent type, must be a reference type.
  83. @tparam Source The parent type.
  84. @param arg The reference to be down-cast.
  85. @return A reference of type Target.
  86. */
  87. template <typename Target, typename Source>
  88. inline Target down_cast(Source &arg) {
  89. // We still use the pointer version of dynamic_cast, as the
  90. // reference-accepting version throws exceptions, and we don't want to deal
  91. // with that.
  92. DBUG_ASSERT(dynamic_cast<typename std::remove_reference<Target>::type *>(
  93. &arg) != nullptr);
  94. return static_cast<Target>(arg);
  95. }
  96. /**
  97. Sometimes the compiler insists that types be the same and does not do any
  98. implicit conversion. For example:
  99. Derived1 *a;
  100. Derived2 *b; // Derived1 and 2 are children classes of Base
  101. Base *x= cond ? a : b; // Error, need to force a cast.
  102. Use:
  103. Base *x= cond ? implicit_cast<Base*>(a) : implicit_cast<Base*>(b);
  104. static_cast would work too, but would be less safe (allows any
  105. pointer-to-pointer conversion, not only up-casts).
  106. */
  107. template <typename To>
  108. inline To implicit_cast(To x) {
  109. return x;
  110. }
  111. /**
  112. Utility to allow returning values from functions which can fail
  113. (until we have std::optional).
  114. */
  115. template <class VALUE_TYPE>
  116. struct ReturnValueOrError {
  117. /** Value returned from function in the normal case. */
  118. VALUE_TYPE value;
  119. /** True if an error occured. */
  120. bool error;
  121. };
  122. #endif // TEMPLATE_UTILS_INCLUDED