Belle II Software development
Mapping.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <tracking/trackFindingCDC/utilities/StringManipulation.h>
11
12#include <cassert>
13
14namespace Belle2 {
19 namespace TrackFindingCDC {
20
22 template <class T>
23 class Mapping {
24
25 public:
27 virtual ~Mapping() = default;
28
30 virtual std::string map(int index __attribute__((unused)),
31 T& t __attribute__((unused)))
32 {
33 return "";
34 }
35
37 virtual std::string info()
38 {
39 return "(no info available)\n";
40 }
41 };
42
44 template <class T>
45 class ConstantMapping : public Mapping<T> {
46
47 public:
49 explicit ConstantMapping(const std::string& value)
50 : m_value(value)
51 {
52 }
53
55 std::string map(int index __attribute__((unused)), T& t __attribute__((unused))) override
56 {
57 return m_value;
58 }
59
61 std::string info() override
62 {
63 return "always " + m_value + "\n";
64 }
65
66 private:
68 std::string m_value;
69 };
70
72 template <class T>
73 class CycleMapping : public Mapping<T> {
74
75 public:
77 explicit CycleMapping(const std::vector<std::string>& values)
78 : m_values(values)
79 {
80 }
81
83 std::string map(int index, T& t __attribute__((unused))) override
84 {
85 assert(index >= 0);
86 return m_values[index % m_values.size()];
87 }
88
90 std::string info() override
91 {
92 return "Cycle through " + bracketed(join(", ", m_values)) + "\n";
93 }
94
95 private:
97 std::vector<std::string> m_values;
98 };
99 }
101}
Realizing a mapping returning a constant attribute value regardless of object or position.
Definition: Mapping.h:45
ConstantMapping(const std::string &value)
Constructor receiving the constant value.
Definition: Mapping.h:49
std::string map(int index, T &t) override
Return the constant value.
Definition: Mapping.h:55
std::string m_value
Memory for the returned attribute value.
Definition: Mapping.h:68
std::string info() override
Informal string summarizing the translation from the object to the attribute value.
Definition: Mapping.h:61
Realizing a mapping from a pool of values to be cycled from the index.
Definition: Mapping.h:73
std::vector< std::string > m_values
Memory for the attribute values to be cycled.
Definition: Mapping.h:97
CycleMapping(const std::vector< std::string > &values)
Constructor receiving the values to be cycled.
Definition: Mapping.h:77
std::string map(int index, T &t) override
Return a value from the cycle value according to the given index.
Definition: Mapping.h:83
std::string info() override
Informal string summarizing the translation from the object to the attribute value.
Definition: Mapping.h:90
Interface defining a mapping of objects to attribute values e.g. a color.
Definition: Mapping.h:23
virtual ~Mapping()=default
Destructor of interfaces should be virtual.
virtual std::string map(int index, T &t)
Main function returning an attribute value for an object at the given index.
Definition: Mapping.h:30
virtual std::string info()
Informal string summarizing the translation from the object to the attribute value.
Definition: Mapping.h:37
Abstract base class for different kinds of events.