Skip to content

iceoryx_posh/internal/popo/building_blocks/typed_unique_id.hpp🔗

Namespaces🔗

Name
iox
iox::popo
iox::popo::internal

Classes🔗

Name
struct iox::popo::InvalidId_t
Struct to signal the constructor to create an invalid id.
class iox::popo::TypedUniqueId
Unique ID depending on a type. If you would like to assign different types consistent unique ids use this class. Every types gets its own distinct set of ids starting with 0. If the types are the same the ids are the same.

Source code🔗

// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_POSH_POPO_BUILDING_BLOCKS_TYPED_UNIQUE_ID_HPP
#define IOX_POSH_POPO_BUILDING_BLOCKS_TYPED_UNIQUE_ID_HPP

#include "iceoryx_utils/cxx/newtype.hpp"
#include "iceoryx_utils/error_handling/error_handling.hpp"

#include <atomic>
#include <cstdint>
#include <limits>

namespace iox
{
namespace popo
{
namespace internal
{
void setUniqueRouDiId(const uint16_t id) noexcept;

void unsetUniqueRouDiId() noexcept;

uint16_t getUniqueRouDiId() noexcept;
} // namespace internal

struct InvalidId_t
{
};
constexpr InvalidId_t InvalidId = InvalidId_t();

//
template <typename T>
class TypedUniqueId : public cxx::NewType<uint64_t,
                                          cxx::newtype::ProtectedConstructByValueCopy,
                                          cxx::newtype::Comparable,
                                          cxx::newtype::Sortable,
                                          cxx::newtype::Convertable,
                                          cxx::newtype::CopyConstructable,
                                          cxx::newtype::MoveConstructable,
                                          cxx::newtype::CopyAssignable,
                                          cxx::newtype::MoveAssignable>
{
  public:
    using ThisType::ThisType;

    TypedUniqueId() noexcept;

    TypedUniqueId(InvalidId_t) noexcept;

    bool isValid() const noexcept;

  private:
    static constexpr uint64_t INVALID_UNIQUE_ID = 0u;
    static constexpr uint64_t ROUDI_ID_BIT_LENGTH = 16u;
    static constexpr uint64_t UNIQUE_ID_BIT_LENGTH = 48u;
    static std::atomic<uint64_t> globalIDCounter; // = 0u
};

} // namespace popo
} // namespace iox

#include "iceoryx_posh/internal/popo/building_blocks/typed_unique_id.inl"

#endif

Updated on 31 May 2022 at 15:29:16 CEST