Skip to content

File bitfield.hpp

File List > include > nebula_common > util > bitfield.hpp

Go to the documentation of this file

// Copyright 2025 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <cstdint>
namespace nebula::util
{

#define BITFIELD_ACCESSOR(type, name, low_bit, high_bit, storage)        \
  [[nodiscard]] type name() const                                        \
  {                                                                      \
    return nebula::util::get_bitfield<type, low_bit, high_bit>(storage); \
  }

template <typename OutT, uint8_t LowBit, uint8_t HighBit, typename InT>
constexpr OutT get_bitfield(const InT & storage)
{
  constexpr uint8_t n_bits = HighBit - LowBit + 1;
  constexpr InT all_ones = ~static_cast<InT>(0);
  constexpr InT mask = ~(all_ones << n_bits);

  InT raw_value = (storage >> LowBit) & mask;
  return static_cast<OutT>(raw_value);
}

}  // namespace nebula::util