Program Listing for File caching_arena_map.hpp

Return to documentation for file (cosm/arena/caching_arena_map.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <memory>
#include <string>
#include <vector>

#include "cosm/arena/base_arena_map.hpp"
#include "cosm/arena/ds/cache_vector.hpp"
#include "cosm/arena/repr/arena_cache.hpp"

/*******************************************************************************
 * Namespaces
 ******************************************************************************/
namespace cosm::arena {

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
class caching_arena_map final : public rer::client<caching_arena_map>,
                                public base_arena_map {
 public:
  caching_arena_map(const caconfig::arena_map_config* config, rmath::rng* rng);
  ~caching_arena_map(void) override;

  cads::acache_vectorno& caches(void) { return m_cachesno; }
  const cads::acache_vectorno& caches(void) const { return m_cachesno; }

  size_t n_caches(void) const { return m_cacheso.size(); }

  void caches_add(const cads::acache_vectoro& caches,
                  cpargos::swarm_manager_adaptor* sm);

  void cache_remove(repr::arena_cache* victim,
                    cpargos::swarm_manager_adaptor* sm);

  const cads::loctree* cloctree(void) const { return m_cloctree.get(); }

  rtypes::type_uuid robot_on_cache(const rmath::vector2d& pos) const;

  rtypes::type_uuid
  robot_on_block(const rmath::vector2d& pos,
                 const rtypes::type_uuid& ent_id) const override;

  bool initialize(cpargos::swarm_manager_adaptor* sm,
                  const crepr::config::nests_config* nests) override;

  cds::block3D_vectorno free_blocks(bool oos_ok) const override;

  bool placement_conflict(const crepr::sim_block3D* block,
                          const rmath::vector2d& loc) const override;

  void bloctree_update(const crepr::sim_block3D* block,
                       const locking& locking) override;
  void cloctree_update(const carepr::arena_cache* cache);
  std::shared_mutex* cache_mtx(void) { return &m_cache_mtx; }
  std::shared_mutex* cache_mtx(void) const { return &m_cache_mtx; }

  void zombie_caches_clear(void) { m_zombie_caches.clear(); }
  const cads::acache_vectoro& zombie_caches(void) const {
    return m_zombie_caches;
  }
  void created_caches(const cads::acache_vectorro& created_caches) {
    m_created_caches = created_caches;
  }
  void created_caches_clear(void) { m_created_caches.clear(); }

  cds::const_spatial_entity_vector
  initial_dist_precalc(const crepr::sim_block3D* block) const override;

  void ordered_lock(const locking& locking) override;
  void ordered_unlock(const locking& locking) override;

 private:
  void pre_block_dist_lock(const locking& locking) override {
    ordered_lock(locking);
  }
  void post_block_dist_unlock(const locking& locking) override {
    ordered_unlock(locking);
  }
  bool bloctree_verify(void) const override;
  bool cloctree_verify(void) const;
  bool initialize_private(void);

  /* clang-format off */
  mutable std::shared_mutex              m_cache_mtx{};

  cads::acache_vectorro                  m_created_caches{};
  cads::acache_vectoro                   m_cacheso{};
  cads::acache_vectorno                  m_cachesno{};

  cads::acache_vectoro                   m_zombie_caches{};
  std::unique_ptr<cads::loctree>         m_cloctree;

  /* clang-format on */
};

} /* namespace cosm::arena */