Program Listing for File map-storage.h

Return to documentation for file (backend/map-manager/include/map-manager/map-storage.h)

#ifndef MAP_MANAGER_MAP_STORAGE_H_
#define MAP_MANAGER_MAP_STORAGE_H_

#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <aslam/common/memory.h>
#include <aslam/common/reader-writer-lock.h>
#include <maplab-common/macros.h>
#include <maplab-common/monitor.h>

namespace backend {

template <typename MapType>
class MapManager;

template <typename MapType>
class MapStorage {
 private:
  friend class MapManager<MapType>;

  struct MapAndMutex {
   private:
    MAPLAB_POINTER_TYPEDEFS(MapAndMutex);
    friend class MapStorage<MapType>;
    friend class MapManager<MapType>;

    AlignedUniquePtr<MapType> map;

    mutable aslam::ReaderWriterMutex map_mutex;

    explicit MapAndMutex(AlignedUniquePtr<MapType>& map_)  // NOLINT
        : map(std::move(map_)) {}
  };

  static MapStorage<MapType>* getInstance() {
    static MapStorage<MapType> map_storage;
    return &map_storage;
  }

 public:
  MapType* getMapMutable(const std::string& key);
  const MapType& getMap(const std::string& key) const;

  typename common::Monitor<MapType>::WriteAccess getMapWriteAccess(
      const std::string& key);

  typename common::Monitor<MapType>::ReadAccess getMapReadAccess(
      const std::string& key) const;

  bool hasMap(const std::string& key) const;

  bool isKeyValid(const std::string& key) const;

  std::string removeProhibitedCharactersFromKey(const std::string& key) const;

  size_t numberOfMaps() const {
    return map_storage_container_.size();
  }

  void getAllMapKeys(std::unordered_set<std::string>* all_map_keys_list) const;
  void getAllMapKeys(std::vector<std::string>* all_map_keys_list) const;

  void addMap(const std::string& key, AlignedUniquePtr<MapType>& map);

  std::unique_ptr<MapAndMutex> releaseMapAndMutex(const std::string& key);

  void renameMap(const std::string& old_key, const std::string& new_key);

  aslam::ReaderWriterMutex* getContainerMutex() const;

 private:
  typedef typename std::unordered_map<std::string, std::unique_ptr<MapAndMutex>>
      MapStorageContainer;
  typedef typename MapStorageContainer::value_type MapStorageContainerValueType;
  typedef typename MapStorageContainer::iterator MapStorageContainerIterator;
  typedef typename MapStorageContainer::const_iterator
      MapStorageContainerConstIterator;

  MapStorage() {}

  MapStorage(const MapStorage&) = delete;
  MapStorage& operator=(const MapStorage&) = delete;

  bool isCharacterProhibited(const char character) const;

  void addMap(const std::string& key, std::unique_ptr<MapAndMutex>& map_handle);

  MapStorageContainer map_storage_container_;

  mutable aslam::ReaderWriterMutex container_mutex_;
};

}  // namespace backend

#include "map-manager/map-storage-inl.h"

#endif  // MAP_MANAGER_MAP_STORAGE_H_