Skip to content

Commit 3bfbcf3

Browse files
YanzhaoWjose-luis-rs
authored andcommitted
Create R3BIOConnector.h
1 parent 92eef56 commit 3bfbcf3

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

r3bbase/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ set(HEADERS
6060
R3BException.h
6161
R3BFileSource.h
6262
R3BFileSource2.h
63+
R3BIOConnector.h
6364
R3BLogger.h
6465
R3BModule.h
6566
R3BShared.h

r3bbase/R3BIOConnector.h

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#pragma once
2+
/******************************************************************************
3+
* Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
4+
* Copyright (C) 2019-2023 Members of R3B Collaboration *
5+
* *
6+
* This software is distributed under the terms of the *
7+
* GNU General Public Licence (GPL) version 3, *
8+
* copied verbatim in the file "LICENSE". *
9+
* *
10+
* In applying this license GSI does not waive the privileges and immunities *
11+
* granted to it by virtue of its status as an Intergovernmental Organization *
12+
* or submit itself to any jurisdiction. *
13+
******************************************************************************/
14+
15+
// TODO: use C++20 std::source_location
16+
#include <boost/assert/source_location.hpp>
17+
18+
#include <FairRootManager.h>
19+
#include <R3BException.h>
20+
#include <fmt/format.h>
21+
#include <map>
22+
#include <string>
23+
#include <type_traits>
24+
#include <unordered_map>
25+
#include <vector>
26+
27+
// TODO: Use C++20 Concept to put more constains on the template parameters
28+
namespace R3B
29+
{
30+
template <typename InputType>
31+
class InputConnector
32+
{
33+
public:
34+
using RawDataType = std::remove_const_t<std::remove_cv_t<InputType>>;
35+
explicit InputConnector(std::string_view branchName)
36+
: branch_name_{ branchName }
37+
{
38+
}
39+
40+
// rule of 5
41+
~InputConnector() = default;
42+
InputConnector(const InputConnector&) = default;
43+
InputConnector(InputConnector&&) = delete;
44+
InputConnector& operator=(const InputConnector&) = default;
45+
InputConnector& operator=(InputConnector&&) = delete;
46+
47+
void init(const boost::source_location& loc = BOOST_CURRENT_LOCATION)
48+
{
49+
if (auto* ioman = FairRootManager::Instance(); ioman != nullptr)
50+
{
51+
data_ = ioman->InitObjectAs<const RawDataType*>(branch_name_.c_str());
52+
53+
if (data_ == nullptr)
54+
{
55+
throw R3B::runtime_error(
56+
fmt::format("Initialisation of the input data with the branch name \"{}\" failed!",
57+
branch_name_),
58+
loc);
59+
}
60+
}
61+
else
62+
{
63+
throw R3B::runtime_error(fmt::format("FairRootManager is nullptr during the initialisation of the "
64+
"input data with the branch name \"{}\"",
65+
branch_name_),
66+
loc);
67+
}
68+
}
69+
70+
[[nodiscard]] inline auto get(const boost::source_location& loc = BOOST_CURRENT_LOCATION) const
71+
-> const RawDataType&
72+
{
73+
check_init(loc);
74+
return *data_;
75+
}
76+
77+
auto size(const boost::source_location& loc = BOOST_CURRENT_LOCATION) const
78+
{
79+
check_init(loc);
80+
return data_->size();
81+
}
82+
83+
// implement range-based for loop:
84+
// TODO: is there a simpler way?
85+
auto begin(const boost::source_location& loc = BOOST_CURRENT_LOCATION)
86+
{
87+
check_init(loc);
88+
return data_->cbegin();
89+
}
90+
auto begin(const boost::source_location& loc = BOOST_CURRENT_LOCATION) const
91+
{
92+
check_init(loc);
93+
return data_->cbegin();
94+
}
95+
auto end(const boost::source_location& loc = BOOST_CURRENT_LOCATION)
96+
{
97+
check_init(loc);
98+
return data_->cend();
99+
}
100+
auto end(const boost::source_location& loc = BOOST_CURRENT_LOCATION) const
101+
{
102+
check_init(loc);
103+
return data_->cend();
104+
}
105+
106+
private:
107+
std::string branch_name_;
108+
const RawDataType* data_ = nullptr;
109+
void check_init(const boost::source_location& loc) const
110+
{
111+
if (data_ == nullptr)
112+
{
113+
throw R3B::runtime_error(
114+
fmt::format("Input data with the branch name \"{}\" cannot be queried without an initialisation!",
115+
branch_name_),
116+
loc);
117+
}
118+
}
119+
};
120+
121+
template <typename OutputType>
122+
class OutputConnector
123+
{
124+
public:
125+
using RawDataType = std::remove_const_t<std::remove_cv_t<OutputType>>;
126+
explicit OutputConnector(std::string_view branchName, bool persistance = true)
127+
: persistance_{ persistance }
128+
, branch_name_{ branchName }
129+
{
130+
}
131+
132+
// rule of 5
133+
~OutputConnector() = default;
134+
OutputConnector(const OutputConnector&) = delete;
135+
OutputConnector(OutputConnector&&) = delete;
136+
OutputConnector& operator=(const OutputConnector& other) = delete;
137+
OutputConnector& operator=(OutputConnector&&) = delete;
138+
139+
void init(const boost::source_location& loc = BOOST_CURRENT_LOCATION)
140+
{
141+
if (auto* ioman = FairRootManager::Instance(); ioman != nullptr)
142+
{
143+
ioman->RegisterAny(branch_name_.c_str(), data_ptr_, true);
144+
}
145+
else
146+
{
147+
throw R3B::runtime_error(fmt::format("FairRootManager is nullptr during the initialisation of the "
148+
"output data with the branch name \"{}\"",
149+
branch_name_),
150+
loc);
151+
}
152+
}
153+
154+
[[nodiscard]] inline auto get() -> RawDataType& { return data_; }
155+
156+
inline void clear() { data_.clear(); }
157+
158+
template <typename ResetOp>
159+
inline void clear(ResetOp&& opn)
160+
{
161+
opn(data_);
162+
}
163+
164+
private:
165+
bool persistance_ = true;
166+
std::string branch_name_;
167+
RawDataType data_;
168+
RawDataType* data_ptr_ = &data_;
169+
};
170+
171+
template <typename ElementType>
172+
using InputVectorConnector = InputConnector<std::vector<ElementType>>;
173+
174+
template <typename ElementType>
175+
using OutputVectorConnector = OutputConnector<std::vector<ElementType>>;
176+
177+
template <typename KeyType, typename ValueType>
178+
using InputMapConnector = InputConnector<std::map<KeyType, ValueType>>;
179+
180+
template <typename KeyType, typename ValueType>
181+
using OutputMapConnector = OutputConnector<std::map<KeyType, ValueType>>;
182+
183+
template <typename KeyType, typename ValueType>
184+
using InputHashConnector = InputConnector<std::unordered_map<KeyType, ValueType>>;
185+
186+
template <typename KeyType, typename ValueType>
187+
using OutputHashConnector = OutputConnector<std::unordered_map<KeyType, ValueType>>;
188+
} // namespace R3B

0 commit comments

Comments
 (0)