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