MCPcopy Create free account
hub / github.com/ThePhD/sol2 / read

Function read

examples/source/usertype_bitfields.cpp:97–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95 template <typename Base, std::size_t bit_target = 0x0,
96 std::size_t size = 0x1>
97 bit_type_t<size> read(Base& b) {
98 typedef bit_type_t<sizeof(Base) * CHAR_BIT>
99 aligned_type;
100 typedef bit_type_t<size> field_type;
101 static const std::size_t aligned_type_bit_size
102 = sizeof(aligned_type) * CHAR_BIT;
103 static_assert(
104 sizeof(Base) * CHAR_BIT >= (bit_target + size),
105 "bit offset and size are too large for the "
106 "desired structure.");
107 static_assert((bit_target % aligned_type_bit_size)
108 <= ((bit_target + size)
109 % aligned_type_bit_size),
110 "bit offset and size cross beyond largest "
111 "integral constant boundary.");
112
113 const std::size_t aligned_target
114 = (bit_target + size) / aligned_type_bit_size;
115 const aligned_type bits_left
116 = static_cast<aligned_type>(
117 bit_target - aligned_target);
118 const aligned_type mask
119 = (static_cast<aligned_type>(1) << size) - 1;
120 // Jump by native size of a pointer to target
121 // then OR the bits
122 aligned_type* jumper = static_cast<aligned_type*>(
123 static_cast<void*>(&b));
124 jumper += aligned_target;
125 const aligned_type& aligned = *jumper;
126 aligned_type field_bits
127 = (aligned >> bits_left) & mask;
128 field_type bits
129 = vcxx_warning_crap<field_type>(field_bits);
130 return bits;
131 }
132
133} // namespace itsy_bitsy
134

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected