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

Function write

examples/source/usertype_bitfields.cpp:60–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

58 template <typename Base, std::size_t bit_target = 0x0,
59 std::size_t size = 0x1>
60 void write(Base& b, bit_type_t<size> bits) {
61 typedef bit_type_t<sizeof(Base) * CHAR_BIT>
62 aligned_type;
63 static const std::size_t aligned_type_bit_size
64 = sizeof(aligned_type) * CHAR_BIT;
65 static_assert(
66 sizeof(Base) * CHAR_BIT >= (bit_target + size),
67 "bit offset and size are too large for the "
68 "desired structure.");
69 static_assert((bit_target % aligned_type_bit_size)
70 <= ((bit_target + size)
71 % aligned_type_bit_size),
72 "bit offset and size cross beyond largest "
73 "integral constant boundary.");
74
75 const std::size_t aligned_target
76 = (bit_target + size) / aligned_type_bit_size;
77 const aligned_type bits_left
78 = static_cast<aligned_type>(
79 bit_target - aligned_target);
80 const aligned_type shifted_mask
81 = ((static_cast<aligned_type>(1) << size) - 1)
82 << bits_left;
83 const aligned_type compl_shifted_mask = ~shifted_mask;
84 // Jump by native size of a pointer to target
85 // then OR the bits
86 aligned_type* jumper = static_cast<aligned_type*>(
87 static_cast<void*>(&b));
88 jumper += aligned_target;
89 aligned_type& aligned = *jumper;
90 aligned &= compl_shifted_mask;
91 aligned
92 |= (static_cast<aligned_type>(bits) << bits_left);
93 }
94
95 template <typename Base, std::size_t bit_target = 0x0,
96 std::size_t size = 0x1>

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected