MySQL 9.1.0
Source Code Documentation
template_utils.h File Reference
#include <assert.h>
#include <ctype.h>
#include <stddef.h>
#include <algorithm>
#include <iterator>
#include <optional>
#include <type_traits>

Go to the source code of this file.

Classes

struct  ReturnValueOrError< VALUE_TYPE >
 Utility to allow returning values from functions which can fail (until we have std::optional). More...
 

Namespaces

namespace  myu
 

Functions

template<typename Container_type >
void delete_container_pointers (Container_type &container)
 Clears a container, but deletes all objects that the elements point to first. More...
 
template<typename Container_type >
void my_free_container_pointers (Container_type &container)
 Clears a container, but frees all objects that the elements point to first. More...
 
template<typename T >
constexpr T pointer_cast (void *p)
 Casts from one pointer type, to another, without using reinterpret_cast or C-style cast: foo f; bar *b= pointer_cast<bar>(f); This avoids having to do: foo f; bar *b= static_cast<bar>(static_cast<void*>(f));. More...
 
template<typename T >
constexpr T pointer_cast (const void *p)
 
template<typename Target , typename Source >
Target down_cast (Source *arg)
 Casts from one pointer type to another in a type hierarchy. More...
 
template<typename Target , typename Source >
Target down_cast (Source &arg)
 Casts from one reference type to another in a type hierarchy. More...
 
template<typename To >
To implicit_cast (To x)
 Sometimes the compiler insists that types be the same and does not do any implicit conversion. More...
 
template<class T , size_t N>
constexpr size_t array_elements (T(&)[N]) noexcept
 Number of elements in a constant C array. More...
 
template<class InputIt , class Pred , class Action >
void myu::Split (InputIt first, InputIt last, Pred &&pred, Action &&action)
 Split a range into sub ranges delimited by elements satisfying a predicate. More...
 
template<class InputIt , class Pred >
InputIt myu::FindTrimmedEnd (InputIt first, InputIt last, Pred &&pred)
 Search backwards for the first occurrence of an element which does not satisfy the trimming predicate, and return an InputIt to the element after it. More...
 
template<class InputIt , class Pred >
std::pair< InputIt, InputIt > myu::FindTrimmedRange (InputIt first, InputIt last, Pred &&pred)
 Searches for a sub range such that no elements before or after fail to satisfy the trimming predicate. More...
 

Variables

const auto myu::IsSpace = [](char c) { return isspace(c); }
 Convenience lambdas for common predicates. More...
 
const auto myu::IsComma = [](char c) { return c == ','; }
 

Function Documentation

◆ array_elements()

template<class T , size_t N>
constexpr size_t array_elements ( T(&)  [N])
constexprnoexcept

Number of elements in a constant C array.

◆ delete_container_pointers()

template<typename Container_type >
void delete_container_pointers ( Container_type &  container)

Clears a container, but deletes all objects that the elements point to first.

Template Parameters
Container_typeContainer of pointers.

◆ down_cast() [1/2]

template<typename Target , typename Source >
Target down_cast ( Source arg)
inline

Casts from one reference type to another in a type hierarchy.

In debug mode, we verify the cast is indeed legal.

Template Parameters
TargetThe descendent type, must be a reference type.
SourceThe parent type.
Parameters
argThe reference to be down-cast.
Returns
A reference of type Target.

◆ down_cast() [2/2]

template<typename Target , typename Source >
Target down_cast ( Source arg)
inline

Casts from one pointer type to another in a type hierarchy.

In debug mode, we verify the cast is indeed legal.

Template Parameters
TargetThe descendent type, must be a pointer type.
SourceThe parent type.
Parameters
argThe pointer to be down-cast.
Returns
A pointer of type Target.

◆ implicit_cast()

template<typename To >
To implicit_cast ( To  x)
inline

Sometimes the compiler insists that types be the same and does not do any implicit conversion.

For example: Derived1 *a; Derived2 *b; // Derived1 and 2 are children classes of Base Base *x= cond ? a : b; // Error, need to force a cast.

Use: Base x= cond ? implicit_cast<Base>(a) : implicit_cast<Base*>(b); static_cast would work too, but would be less safe (allows any pointer-to-pointer conversion, not only up-casts).

◆ my_free_container_pointers()

template<typename Container_type >
void my_free_container_pointers ( Container_type &  container)

Clears a container, but frees all objects that the elements point to first.

Template Parameters
Container_typeContainer of pointers.

◆ pointer_cast() [1/2]

template<typename T >
constexpr T pointer_cast ( const void *  p)
constexpr

◆ pointer_cast() [2/2]

template<typename T >
constexpr T pointer_cast ( void *  p)
constexpr

Casts from one pointer type, to another, without using reinterpret_cast or C-style cast: foo f; bar *b= pointer_cast<bar>(f); This avoids having to do: foo f; bar *b= static_cast<bar>(static_cast<void*>(f));.