1 /* 2 * Copyright 2014 Google Inc. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef FRUIT_PACKED_POINTER_AND_BOOL_H 18 #define FRUIT_PACKED_POINTER_AND_BOOL_H 19 20 namespace fruit { 21 namespace impl { 22 23 /** 24 * This class stores a T* and a bool in the space of a pointer. 25 * alignof(T) must be at least 2. 26 */ 27 template <typename T> 28 class PackedPointerAndBool { 29 private: 30 static_assert(alignof(T) >= 2, "alignof(T) must be at least 2 for the packing to be possible"); 31 32 std::uintptr_t value; 33 34 static std::uintptr_t encode(T* p, bool b); 35 static T* decodePointer(std::uintptr_t value); 36 static bool decodeBool(std::uintptr_t value); 37 38 public: 39 PackedPointerAndBool(T* p, bool b); 40 41 PackedPointerAndBool() = default; 42 43 PackedPointerAndBool(const PackedPointerAndBool&) = default; 44 PackedPointerAndBool(PackedPointerAndBool&&) = default; 45 46 PackedPointerAndBool& operator=(const PackedPointerAndBool&) = default; 47 PackedPointerAndBool& operator=(PackedPointerAndBool&&) = default; 48 49 T* getPointer() const; 50 void setPointer(T* p); 51 52 bool getBool() const; 53 void setBool(bool b); 54 55 bool operator==(const PackedPointerAndBool<T>& other) const; 56 bool operator!=(const PackedPointerAndBool<T>& other) const; 57 }; 58 59 } // namespace impl 60 } // namespace fruit 61 62 #include <fruit/impl/data_structures/packed_pointer_and_bool.defn.h> 63 64 #endif // FRUIT_PACKED_POINTER_AND_BOOL_H 65