// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma // de Barcelona (UAB). // // This work is licensed under the terms of the MIT license. // For a copy, see . #pragma once #include "carla/AtomicSharedPtr.h" #include "carla/NonCopyable.h" #include #include #include namespace carla { namespace client { namespace detail { /// Holds an atomic pointer to a list. /// /// @warning Only Load method is atomic, modifications to the list are locked /// with a mutex. template class AtomicList : private NonCopyable { using ListT = std::vector; public: AtomicList() : _list(std::make_shared()) {} template void Push(ValueT &&value) { std::lock_guard lock(_mutex); auto new_list = std::make_shared(*Load()); new_list->emplace_back(std::forward(value)); _list = new_list; } void DeleteByIndex(size_t index) { std::lock_guard lock(_mutex); auto new_list = std::make_shared(*Load()); auto begin = new_list->begin(); std::advance(begin, index); new_list->erase(begin); _list = new_list; } template void DeleteByValue(const ValueT &value) { std::lock_guard lock(_mutex); auto new_list = std::make_shared(*Load()); new_list->erase(std::remove(new_list->begin(), new_list->end(), value), new_list->end()); _list = new_list; } void Clear() { std::lock_guard lock(_mutex); _list = std::make_shared(); } /// Returns a pointer to the list. std::shared_ptr Load() const { return _list.load(); } private: std::mutex _mutex; AtomicSharedPtr _list; }; } // namespace detail } // namespace client } // namespace carla