| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef RUNTIME_VM_BOOLFIELD_H_ | 5 #ifndef RUNTIME_VM_BOOLFIELD_H_ |
| 6 #define RUNTIME_VM_BOOLFIELD_H_ | 6 #define RUNTIME_VM_BOOLFIELD_H_ |
| 7 | 7 |
| 8 #include "vm/globals.h" | 8 #include "vm/globals.h" |
| 9 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| 11 | 11 |
| 12 // BoolField is a template for encoding and decoding a bit inside an | 12 // BoolField is a template for encoding and decoding a bit inside an |
| 13 // unsigned machine word. | 13 // unsigned machine word. |
| 14 template<int position> | 14 template <int position> |
| 15 class BoolField { | 15 class BoolField { |
| 16 public: | 16 public: |
| 17 // Returns a uword with the bool value encoded. | 17 // Returns a uword with the bool value encoded. |
| 18 static uword encode(bool value) { | 18 static uword encode(bool value) { |
| 19 ASSERT(position < sizeof(uword)); | 19 ASSERT(position < sizeof(uword)); |
| 20 return static_cast<uword>((value ? 1U : 0) << position); | 20 return static_cast<uword>((value ? 1U : 0) << position); |
| 21 } | 21 } |
| 22 | 22 |
| 23 // Extracts the bool from the value. | 23 // Extracts the bool from the value. |
| 24 static bool decode(uword value) { | 24 static bool decode(uword value) { |
| 25 ASSERT(position < sizeof(uword)); | 25 ASSERT(position < sizeof(uword)); |
| 26 return (value & (1U << position)) != 0; | 26 return (value & (1U << position)) != 0; |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Returns a uword with the bool field value encoded based on the | 29 // Returns a uword with the bool field value encoded based on the |
| 30 // original value. Only the single bit corresponding to this bool | 30 // original value. Only the single bit corresponding to this bool |
| 31 // field will be changed. | 31 // field will be changed. |
| 32 static uword update(bool value, uword original) { | 32 static uword update(bool value, uword original) { |
| 33 ASSERT(position < sizeof(uword)); | 33 ASSERT(position < sizeof(uword)); |
| 34 const uword mask = 1U << position; | 34 const uword mask = 1U << position; |
| 35 return value ? original | mask : original & ~mask; | 35 return value ? original | mask : original & ~mask; |
| 36 } | 36 } |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 } // namespace dart | 39 } // namespace dart |
| 40 | 40 |
| 41 #endif // RUNTIME_VM_BOOLFIELD_H_ | 41 #endif // RUNTIME_VM_BOOLFIELD_H_ |
| OLD | NEW |