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