NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
ResultGroup.h
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#pragma once
7
8#include "NAGASH/Global.h"
9#include "NAGASH/Result.h"
10#include "NAGASH/Toolkit.h"
11#include "NAGASH/MapTool.h"
12#include "NAGASH/SystTool.h"
13
14namespace NAGASH
15{
20 {
21 public:
25 ResultGroup(std::shared_ptr<MSGTool> MSG, std::shared_ptr<ConfigTool> c) : config(c), maptool(MSG), msg(MSG) {}
26 ResultGroup() = delete;
27 ~ResultGroup() = default;
28
31 void AddResult(std::shared_ptr<Result> result);
34 void Merge(std::shared_ptr<ResultGroup> subGroup);
36 void Write();
37
38 template <typename R, typename... Args>
39 std::shared_ptr<R> BookResult(const TString &name, const TString &fname = "", Args &&...args);
40 template <typename R>
41 std::shared_ptr<R> Get(const TString &name);
42
43 private:
44 std::map<TString, std::shared_ptr<Result>> ResultMap;
45 std::shared_ptr<ConfigTool> config;
47 std::shared_ptr<MSGTool> msg;
48 };
49
57 template <typename R, typename... Args>
58 inline std::shared_ptr<R> ResultGroup::BookResult(const TString &rname, const TString &fname, Args &&...args)
59 {
60 static_assert(!std::is_pointer<R>::value, "class ResultGroup: Can not book result in pointer type");
61 static_assert(std::is_base_of<Result, R>::value, "class ResulGroup: Input type must be or be inherited from Result class");
62
63 auto result = std::make_shared<R>(msg, config, rname, fname, std::forward<Args>(args)...);
64
65 if (ResultMap.find(rname) != ResultMap.end())
66 {
67 msg->StartTitle("ResultGroup::BookResult");
68 msg->MSG(MSGLevel::WARNING, "Result ", rname, " already exists, this book will be ignored and return null");
69 msg->EndTitle();
70 return nullptr;
71 }
72 else
73 {
74 ResultMap.emplace(std::pair<TString, std::shared_ptr<Result>>(rname, std::dynamic_pointer_cast<R>(result)));
75 return result;
76 }
77 }
78
84 template <typename R>
85 inline std::shared_ptr<R> ResultGroup::Get(const TString &name)
86 {
87 static_assert(!std::is_pointer<R>::value, "class ResultGroup: Can not book result in pointer type");
88 static_assert(std::is_base_of<Result, R>::value, "class ResultGroup: Input type must be or be inherited from Result class");
89
90 auto titleguard = msg->StartTitleWithGuard("ResultGroup::Get");
91 std::shared_ptr<R> result = nullptr;
92 auto findresult = maptool.FindMostSimilar(ResultMap, name);
93 if (findresult != ResultMap.end())
94 result = std::dynamic_pointer_cast<R>(findresult->second);
95 if (result == nullptr)
96 msg->MSG(MSGLevel::ERROR, "the input key ", name, " not found, returning nullptr");
97
98 return result;
99 }
100
101} // namespace NAGASH
Some global definitions.
Helper functions for std::map.
Definition MapTool.h:17
std::map< TString, T >::iterator FindMostSimilar(std::map< TString, T > &mymap, const TString &key)
Find value to the most similar key in a map.
Definition MapTool.h:43
Used to manipulate a group of Result objects., especially inside NAGASH::Job.
Definition ResultGroup.h:20
void Merge(std::shared_ptr< ResultGroup > subGroup)
Merge two ResultGroup into one.
std::map< TString, std::shared_ptr< Result > > ResultMap
Definition ResultGroup.h:44
void Write()
Write all the results inside, i.e. call Result::WriteToFile() for each result.
~ResultGroup()=default
std::shared_ptr< ConfigTool > config
Definition ResultGroup.h:45
std::shared_ptr< MSGTool > msg
Definition ResultGroup.h:47
ResultGroup(std::shared_ptr< MSGTool > MSG, std::shared_ptr< ConfigTool > c)
Constructor.
Definition ResultGroup.h:25
std::shared_ptr< R > Get(const TString &name)
Get the result with the given name. If the given name is not find, the search will turn to fuzzy sear...
Definition ResultGroup.h:85
std::shared_ptr< R > BookResult(const TString &name, const TString &fname="", Args &&...args)
Book a result in this ResultGroup.
Definition ResultGroup.h:58
void AddResult(std::shared_ptr< Result > result)
Add a result to the group.