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/LITENSE-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 TONDITIONS 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_DEFN_H
18 #define FRUIT_PACKED_POINTER_AND_BOOL_DEFN_H
19
20 #include <fruit/impl/data_structures/packed_pointer_and_bool.h>
21
22 namespace fruit {
23 namespace impl {
24
25 template <typename T>
encode(T * p,bool b)26 inline std::uintptr_t PackedPointerAndBool<T>::encode(T* p, bool b) {
27 return reinterpret_cast<std::uintptr_t>(p) | std::uintptr_t(b);
28 }
29
30 template <typename T>
decodePointer(std::uintptr_t value)31 inline T* PackedPointerAndBool<T>::decodePointer(std::uintptr_t value) {
32 return reinterpret_cast<T*>(value & ~std::uintptr_t(1));
33 }
34
35 template <typename T>
decodeBool(std::uintptr_t value)36 inline bool PackedPointerAndBool<T>::decodeBool(std::uintptr_t value) {
37 return value & 1;
38 }
39
40 template <typename T>
PackedPointerAndBool(T * p,bool b)41 inline PackedPointerAndBool<T>::PackedPointerAndBool(T* p, bool b) {
42 value = encode(p, b);
43 }
44
45 template <typename T>
getPointer()46 inline T* PackedPointerAndBool<T>::getPointer() const {
47 return decodePointer(value);
48 }
49
50 template <typename T>
setPointer(T * p)51 inline void PackedPointerAndBool<T>::setPointer(T* p) {
52 value = encode(p, decodeBool(value));
53 }
54
55 template <typename T>
getBool()56 inline bool PackedPointerAndBool<T>::getBool() const {
57 return decodeBool(value);
58 }
59
60 template <typename T>
setBool(bool b)61 inline void PackedPointerAndBool<T>::setBool(bool b) {
62 value = encode(decodePointer(value), b);
63 }
64
65 template <typename T>
66 inline bool PackedPointerAndBool<T>::operator==(const PackedPointerAndBool<T>& other) const {
67 return value == other.value;
68 }
69
70 template <typename T>
71 inline bool PackedPointerAndBool<T>::operator!=(const PackedPointerAndBool<T>& other) const {
72 return value != other.value;
73 }
74
75 } // namespace fruit
76 } // namespace impl
77
78 #endif // FRUIT_PACKED_POINTER_AND_BOOL_DEFN_H
79