| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* This file is a part of NA64SW software. | ||
| 2 | * Copyright (C) 2015-2022 NA64 Collaboration, CERN | ||
| 3 | * | ||
| 4 | * NA64SW is free software: you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation, either version 3 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 16 | |||
| 17 | #pragma once | ||
| 18 | |||
| 19 | #include "na64event/mem-mapping.hh" | ||
| 20 | |||
| 21 | #include <type_traits> | ||
| 22 | #include <limits> | ||
| 23 | #include <variant> | ||
| 24 | |||
| 25 | namespace na64dp { | ||
| 26 | namespace util { | ||
| 27 | |||
| 28 | /// Sets the variable given by reference for arithmetic type to quiet NaN | ||
| 29 | template<typename T> | ||
| 30 | typename std::enable_if< std::numeric_limits<T>::has_quiet_NaN >::type | ||
| 31 | 35 | reset_arithmetic_value(T & v) { | |
| 32 | 35 | v = std::numeric_limits<T>::quiet_NaN(); | |
| 33 | 35 | } | |
| 34 | |||
| 35 | /// Sets the variable given by reference for arithmetic type to min value | ||
| 36 | template<typename T> | ||
| 37 | typename std::enable_if< ! std::numeric_limits<T>::has_quiet_NaN >::type | ||
| 38 | 140 | reset_arithmetic_value(T & v) { | |
| 39 | 140 | v = std::numeric_limits<T>::min(); | |
| 40 | } | ||
| 41 | |||
| 42 | /// Arithmetic type reset; dispatches call to `reset_arithmetic_value()` | ||
| 43 | template<typename T> | ||
| 44 | typename std::enable_if< std::is_arithmetic<T>::value >::type | ||
| 45 | 210 | reset(T & v) { | |
| 46 | 210 | reset_arithmetic_value(v); | |
| 47 | } | ||
| 48 | |||
| 49 | /// Array unwinding reset | ||
| 50 | template<typename T> | ||
| 51 | typename std::enable_if< std::is_array<T>::value >::type | ||
| 52 | ✗ | reset(T & v) { | |
| 53 | ✗ | for( unsigned i = 0; i < std::extent<T>::value; ++i ) { | |
| 54 | ✗ | reset(v[i]); | |
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | /// Template specialization for `mem::Ref` template instances | ||
| 59 | template<typename T> void | ||
| 60 | 35 | reset(mem::Ref<T> & ref) { | |
| 61 | 35 | ref.reset(); | |
| 62 | 35 | } | |
| 63 | |||
| 64 | /// Template specialization for any pointer | ||
| 65 | template<typename T> void | ||
| 66 | reset(T *& ptr) { | ||
| 67 | ptr = nullptr; | ||
| 68 | } | ||
| 69 | |||
| 70 | template<typename ... ArgsT> void | ||
| 71 | ✗ | reset(std::variant<std::monostate, ArgsT...> & v) { | |
| 72 | ✗ | v = std::monostate{}; | |
| 73 | } | ||
| 74 | |||
| 75 | } // namespace ::na64dp::util | ||
| 76 | |||
| 77 | namespace event { | ||
| 78 | |||
| 79 | /// Standard-precision floating number type to be used in events representation | ||
| 80 | typedef float StdFloat_t; | ||
| 81 | |||
| 82 | /**\brief Access traits for the event field/attribute subtype | ||
| 83 | * | ||
| 84 | * Default is not defined. Specialized traits must provide: | ||
| 85 | * - `Getter` type of the callback function receiving the structure instance | ||
| 86 | * and returning `double` value. | ||
| 87 | * - `getters` | ||
| 88 | * */ | ||
| 89 | template<typename T, typename=void> struct Traits; | ||
| 90 | |||
| 91 | #if 0 // TODO: not sure we need it, kept for possible future usage | ||
| 92 | template<typename ImplT> class Stream; | ||
| 93 | /// `std::variant` value traits | ||
| 94 | template<typename ... VariantsT> | ||
| 95 | struct Traits< std::variant<std::monostate, mem::Ref<VariantsT>... > | ||
| 96 | , void | ||
| 97 | > { | ||
| 98 | typedef std::variant<std::monostate, mem::Ref<VariantsT>... > ValueType; | ||
| 99 | typedef double (*Getter)(const ValueType &); | ||
| 100 | typedef std::unordered_map<std::string, std::pair<std::string, Getter>> Getters; | ||
| 101 | static Getters getters; | ||
| 102 | // no NaturalKey type | ||
| 103 | template<typename StreamImplT> | ||
| 104 | static void stream_op(Stream<StreamImplT> & ds, ValueType & obj); | ||
| 105 | }; | ||
| 106 | #endif | ||
| 107 | |||
| 108 | template<typename T> | ||
| 109 | struct CallableTraits { | ||
| 110 | ///\brief Called to increase inheritance level for the current object | ||
| 111 | /// | ||
| 112 | /// Default implementation retrurns reference to the original callable | ||
| 113 | /// instance. For some data serialization formats, can change callable | ||
| 114 | /// state to handle encompassed type. | ||
| 115 | template<typename ParentT> static T & | ||
| 116 | inherited(T & callable) { | ||
| 117 | return callable; | ||
| 118 | } | ||
| 119 | |||
| 120 | ///\brief Returns attribute ID useful for callable | ||
| 121 | /// | ||
| 122 | /// Accpets strign identifier and integer index of the attribute. | ||
| 123 | /// By default returns same string identifier. | ||
| 124 | static const char * | ||
| 125 | attr_id(int attrIndex, const char * attrName) { | ||
| 126 | return attrName; | ||
| 127 | } | ||
| 128 | }; | ||
| 129 | |||
| 130 | } // namesapce ::na64dp::event | ||
| 131 | } // namespace na64dp | ||
| 132 |