CbmRoot
Loading...
Searching...
No Matches
CaObjectInitController.h
Go to the documentation of this file.
1/* Copyright (C) 2022 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergey Gorbunov, Sergei Zharko [committer] */
4
5#pragma once // include this header only once per compilation unit
6
10
12
13#include <bitset>
14#include <cassert>
15#include <iomanip>
16#include <iostream>
17#include <sstream>
18#include <string>
19
20namespace cbm::algo::ca
21{
28 // TODO: Possible improvement: introduce another template parameter, which represents a local enum class...
29 template<int NumberOfFlags, class InitKeyEnum>
31 public:
34 bool GetFlag(InitKeyEnum bitKey) const;
36 bool IsFinalized() const { return fInitFlags.size() == fInitFlags.count(); }
40 void SetFlag(InitKeyEnum bitKey, bool newStatus = true);
43 std::string ToString(int indentLevel = 0) const;
44
45 private:
46 std::bitset<NumberOfFlags> fInitFlags{};
47 };
48
49 //
50 //------------------------------------------------------------------------------------------------------------------------
51 //
52 template<int NumberOfFlags, class InitKeyEnum>
54 {
55 int bitIndex = static_cast<int>(bitKey);
56 if (bitIndex >= NumberOfFlags || bitIndex < 0) {
57 LOG(fatal) << "L1OnjectInitController::GetFlagStatus: attempt of flag access with index = " << bitIndex
58 << " outside the range [0, " << NumberOfFlags << ']';
59 //assert((!(bitIndex >= NumberOfFlags || bitIndex < 0)));
60 }
61 return fInitFlags[static_cast<int>(bitKey)];
62 }
63 //
64 //------------------------------------------------------------------------------------------------------------------------
65 //
66 template<int NumberOfFlags, class InitKeyEnum>
67 void ObjectInitController<NumberOfFlags, InitKeyEnum>::SetFlag(InitKeyEnum bitKey, bool newStatus)
68 {
69 int bitIndex = static_cast<int>(bitKey);
70 if (bitIndex >= NumberOfFlags || bitIndex < 0) {
71 LOG(fatal) << "L1OnjectInitController::GetFlagStatus: attempt of flag access with index = " << bitIndex
72 << " outside the range [0, " << NumberOfFlags << ']';
73 //assert((!(bitIndex >= NumberOfFlags || bitIndex < 0)));
74 }
75 fInitFlags[static_cast<int>(bitKey)] = newStatus;
76 }
77 //
78 //------------------------------------------------------------------------------------------------------------------------
79 //
80 template<int NumberOfFlags, class InitKeyEnum>
82 {
83 std::stringstream aStream{};
84 constexpr char indentChar = '\t';
85 std::string indent(indentLevel, indentChar);
86 aStream << indent << "CaObjectInitController: flag values";
87 aStream << '\n' << indent << "index: ";
88 for (int idx = 0; idx < NumberOfFlags; ++idx) {
89 aStream << std::setw(3) << std::setfill(' ') << idx << ' ';
90 }
91 aStream << '\n' << indent << "value: ";
92 for (int idx = 0; idx < NumberOfFlags; ++idx) {
93 aStream << " " << static_cast<int>(fInitFlags[idx]) << ' ';
94 }
95 return aStream.str();
96 }
97
98} // namespace cbm::algo::ca
bool GetFlag(InitKeyEnum bitKey) const
void SetFlag(InitKeyEnum bitKey, bool newStatus=true)
bool IsFinalized() const
Checks, if the object is finalized, i.e. all its fields were set up.
std::bitset< NumberOfFlags > fInitFlags
object of flags sets
std::string ToString(int indentLevel=0) const
TODO: SZh 8.11.2022: add selection of parameterisation.
Definition CaBranch.h:14