Program Listing for File collector_group.hpp

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

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <utility>

#include "rcppsw/metrics/base_collector.hpp"
#include "rcppsw/rcppsw.hpp"
#include "rcppsw/metrics/write_status.hpp"

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

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/

class collector_group : public rer::client<collector_group> {
 public:
  using key_type = std::string;
  using mapped_type = std::unique_ptr<base_collector>;

  collector_group(void);
  virtual ~collector_group(void) = default;

  size_t size(void) const { return m_collectors.size(); }

  template <typename T, typename... Args>
  bool collector_register(const key_type& name, Args&&... args) {
    auto it = m_collectors.find(name);
    if (it == m_collectors.end()) {
      m_collectors[name] = std::make_unique<T>(std::forward<Args>(args)...);
      return true;
    }
    return false;
  }

  bool collector_unregister(const key_type& name);

  bool collect(const key_type& name, const base_metrics& metrics);

  bool collect_if(const key_type& name,
                  const base_metrics& metrics,
                  const std::function<bool(const base_metrics&)>& predicate);
  template <typename T = base_collector>
  T* get(const key_type& key) {
    return static_cast<T*>(m_collectors[key].get());
  }

  void initialize(void);

  void interval_reset(const rtypes::timestep& t);

  void finalize(void);

  bool flush(bool fail_ok, const rtypes::timestep& t);

 private:
  /* clang-format off */
  std::map<key_type, mapped_type> m_collectors{};
  /* clang-format on */
};

} /* namespace rcppsw::metrics */