LLVM API Documentation

PointerLikeTypeTraits.h
Go to the documentation of this file.
00001 //===- llvm/Support/PointerLikeTypeTraits.h - Pointer Traits ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the PointerLikeTypeTraits class.  This allows data
00011 // structures to reason about pointers and other things that are pointer sized.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
00016 #define LLVM_SUPPORT_POINTERLIKETYPETRAITS_H
00017 
00018 #include "llvm/Support/DataTypes.h"
00019 
00020 namespace llvm {
00021   
00022 /// PointerLikeTypeTraits - This is a traits object that is used to handle
00023 /// pointer types and things that are just wrappers for pointers as a uniform
00024 /// entity.
00025 template <typename T>
00026 class PointerLikeTypeTraits {
00027   // getAsVoidPointer
00028   // getFromVoidPointer
00029   // getNumLowBitsAvailable
00030 };
00031 
00032 // Provide PointerLikeTypeTraits for non-cvr pointers.
00033 template<typename T>
00034 class PointerLikeTypeTraits<T*> {
00035 public:
00036   static inline void *getAsVoidPointer(T* P) { return P; }
00037   static inline T *getFromVoidPointer(void *P) {
00038     return static_cast<T*>(P);
00039   }
00040   
00041   /// Note, we assume here that malloc returns objects at least 4-byte aligned.
00042   /// However, this may be wrong, or pointers may be from something other than
00043   /// malloc.  In this case, you should specialize this template to reduce this.
00044   ///
00045   /// All clients should use assertions to do a run-time check to ensure that
00046   /// this is actually true.
00047   enum { NumLowBitsAvailable = 2 };
00048 };
00049   
00050 // Provide PointerLikeTypeTraits for const pointers.
00051 template<typename T>
00052 class PointerLikeTypeTraits<const T*> {
00053   typedef PointerLikeTypeTraits<T*> NonConst;
00054 
00055 public:
00056   static inline const void *getAsVoidPointer(const T* P) {
00057     return NonConst::getAsVoidPointer(const_cast<T*>(P));
00058   }
00059   static inline const T *getFromVoidPointer(const void *P) {
00060     return NonConst::getFromVoidPointer(const_cast<void*>(P));
00061   }
00062   enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable };
00063 };
00064 
00065 // Provide PointerLikeTypeTraits for uintptr_t.
00066 template<>
00067 class PointerLikeTypeTraits<uintptr_t> {
00068 public:
00069   static inline void *getAsVoidPointer(uintptr_t P) {
00070     return reinterpret_cast<void*>(P);
00071   }
00072   static inline uintptr_t getFromVoidPointer(void *P) {
00073     return reinterpret_cast<uintptr_t>(P);
00074   }
00075   // No bits are available!
00076   enum { NumLowBitsAvailable = 0 };
00077 };
00078   
00079 } // end namespace llvm
00080 
00081 #endif