Skip to content

iceoryx_utils/internal/concurrent/smart_lock.hpp🔗

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex AI Inc. 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_UTILS_CONCURRENT_SMART_LOCK_HPP
#define IOX_UTILS_CONCURRENT_SMART_LOCK_HPP

#include <mutex>

namespace iox
{
namespace concurrent
{
template <typename T, typename MutexType = ::std::mutex>
class smart_lock
{
  private:
    class Proxy
    {
      public:
        Proxy(T* base, MutexType* lock) noexcept;
        ~Proxy() noexcept;

        T* operator->() noexcept;
        T* operator->() const noexcept;

      private:
        T* base;
        MutexType* lock;
    };

  public:
    smart_lock() noexcept;

    smart_lock(const T& t) noexcept;

    template <typename... ArgTypes>
    smart_lock(ArgTypes&&... args) noexcept;

    smart_lock(const smart_lock& rhs) noexcept;
    smart_lock(smart_lock&& rhs) noexcept;
    smart_lock& operator=(const smart_lock& rhs) noexcept;
    smart_lock& operator=(smart_lock&& rhs) noexcept;
    ~smart_lock() noexcept = default;

    Proxy operator->() noexcept;

    Proxy operator->() const noexcept;

    Proxy GetScopeGuard() noexcept;

    Proxy GetScopeGuard() const noexcept;

    T GetCopy() const noexcept;

  private:
    T base;
    mutable MutexType lock;
};
} // namespace concurrent
} // namespace iox

#include "iceoryx_utils/internal/concurrent/smart_lock.inl"

#endif // IOX_UTILS_CONCURRENT_SMART_LOCK_HPP

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