WPILibC++  2019.3.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
PointerLikeTypeTraits.h
1 //===- llvm/Support/PointerLikeTypeTraits.h - Pointer Traits ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PointerLikeTypeTraits class. This allows data
11 // structures to reason about pointers and other things that are pointer sized.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef WPIUTIL_WPI_POINTERLIKETYPETRAITS_H
16 #define WPIUTIL_WPI_POINTERLIKETYPETRAITS_H
17 
18 #include <cstdint>
19 #include <cstdlib>
20 #include <type_traits>
21 
22 namespace wpi {
23 
26 template <typename T> struct PointerLikeTypeTraits;
27 
28 namespace detail {
30 template <size_t N>
32  : std::integral_constant<size_t, ConstantLog2<N / 2>::value + 1> {};
33 template <> struct ConstantLog2<1> : std::integral_constant<size_t, 0> {};
34 
35 // Provide a trait to check if T is pointer-like.
36 template <typename T, typename U = void> struct HasPointerLikeTypeTraits {
37  static const bool value = false;
38 };
39 
40 // sizeof(T) is valid only for a complete T.
41 template <typename T> struct HasPointerLikeTypeTraits<
42  T, decltype((sizeof(PointerLikeTypeTraits<T>) + sizeof(T)), void())> {
43  static const bool value = true;
44 };
45 
46 template <typename T> struct IsPointerLike {
47  static const bool value = HasPointerLikeTypeTraits<T>::value;
48 };
49 
50 template <typename T> struct IsPointerLike<T *> {
51  static const bool value = true;
52 };
53 } // namespace detail
54 
55 // Provide PointerLikeTypeTraits for non-cvr pointers.
56 template <typename T> struct PointerLikeTypeTraits<T *> {
57  static inline void *getAsVoidPointer(T *P) { return P; }
58  static inline T *getFromVoidPointer(void *P) { return static_cast<T *>(P); }
59 
60  enum { NumLowBitsAvailable = detail::ConstantLog2<alignof(T)>::value };
61 };
62 
63 template <> struct PointerLikeTypeTraits<void *> {
64  static inline void *getAsVoidPointer(void *P) { return P; }
65  static inline void *getFromVoidPointer(void *P) { return P; }
66 
74  enum { NumLowBitsAvailable = 2 };
75 };
76 
77 // Provide PointerLikeTypeTraits for const things.
78 template <typename T> struct PointerLikeTypeTraits<const T> {
80 
81  static inline const void *getAsVoidPointer(const T P) {
82  return NonConst::getAsVoidPointer(P);
83  }
84  static inline const T getFromVoidPointer(const void *P) {
85  return NonConst::getFromVoidPointer(const_cast<void *>(P));
86  }
87  enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable };
88 };
89 
90 // Provide PointerLikeTypeTraits for const pointers.
91 template <typename T> struct PointerLikeTypeTraits<const T *> {
93 
94  static inline const void *getAsVoidPointer(const T *P) {
95  return NonConst::getAsVoidPointer(const_cast<T *>(P));
96  }
97  static inline const T *getFromVoidPointer(const void *P) {
98  return NonConst::getFromVoidPointer(const_cast<void *>(P));
99  }
100  enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable };
101 };
102 
103 // Provide PointerLikeTypeTraits for uintptr_t.
104 template <> struct PointerLikeTypeTraits<uintptr_t> {
105  static inline void *getAsVoidPointer(uintptr_t P) {
106  return reinterpret_cast<void *>(P);
107  }
108  static inline uintptr_t getFromVoidPointer(void *P) {
109  return reinterpret_cast<uintptr_t>(P);
110  }
111  // No bits are available!
112  enum { NumLowBitsAvailable = 0 };
113 };
114 
115 } // end namespace wpi
116 
117 #endif
Definition: PointerLikeTypeTraits.h:56
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...
Definition: PointerLikeTypeTraits.h:26
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Definition: PointerLikeTypeTraits.h:36
Definition: PointerLikeTypeTraits.h:46
A tiny meta function to compute the log2 of a compile time constant.
Definition: PointerLikeTypeTraits.h:31