Program Listing for File base_manager.hpp

Return to documentation for file (rcppsw/metrics/base_manager.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <map>
#include <string>
#include <utility>

#include "rcppsw/math/vector2.hpp"
#include "rcppsw/math/vector3.hpp"
#include "rcppsw/metrics/collector_group.hpp"
#include "rcppsw/metrics/output_mode.hpp"

/*******************************************************************************
 * Namespaces
 ******************************************************************************/
namespace rcppsw::metrics {

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
class base_manager {
 public:
  base_manager(void) = default;
  virtual ~base_manager(void) = default;

  virtual void initialize(void) = 0;

  virtual bool flush(const rmetrics::output_mode& mode,
                     const rtypes::timestep& t)  = 0;

  virtual void interval_reset(const rtypes::timestep& t) = 0;

  virtual void finalize(void) = 0;

  virtual void collector_preregister(const std::string& scoped_name,
                                     const rmetrics::output_mode& mode) = 0;

  template <typename TCollector, typename... Args>
  bool collector_register(const std::string& scoped_name,
                          Args&&... args) {
    return m_collector_map[scoped_name]->collector_register<TCollector>(
        scoped_name,
        std::forward<Args>(args)...);
  }


  template <typename T>
  void collect(const std::string& scoped_name, const T& collectee) {
    auto it = m_collector_map.find(scoped_name);
    if (it != m_collector_map.end()) {
      it->second->collect(scoped_name, collectee);
    }
  } /* collect() */

  template <typename T>
  void
  collect_if(const std::string& scoped_name,
             const T& collectee,
             const std::function<bool(const rmetrics::base_metrics&)>& pred) {
    auto it = m_collector_map.find(scoped_name);
    if (it != m_collector_map.end()) {
      it->second->collect_if(scoped_name, collectee, pred);
    }
  } /* collect() */

  bool collector_unregister(const std::string& scoped_name) {
    auto it = m_collector_map.find(scoped_name);
    if (it != m_collector_map.end()) {
      return it->second->collector_unregister(scoped_name);
    }
    return false;
  }

  template <typename T = base_collector>
  T* get(const std::string& key) {
    return m_collector_map[key]->get<T>(key);
  }

 protected:
  using collector_map_type = std::map<std::string, rmetrics::collector_group*>;

  collector_map_type* collector_map(void) { return &m_collector_map; }


 private:
  /* clang-format off */
  collector_map_type m_collector_map{};
  /* clang-format on */
};

} /* namespace rcppsw::metrics */