File indexing completed on 2025-01-18 09:57:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef GAUDIALG_TUPLEPUT_H
0012 #define GAUDIALG_TUPLEPUT_H 1
0013 #include "GaudiAlg/TupleObj.h"
0014 #include "GaudiKernel/System.h"
0015 #include "TClass.h"
0016 #include <memory>
0017
0018
0019
0020
0021
0022
0023
0024 namespace Tuples {
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 template <class VALUE>
0035 class ItemStore final {
0036 friend class TupleObj;
0037
0038 public:
0039
0040 ItemStore() = default;
0041
0042 private:
0043
0044 NTuple::Item<VALUE>* getItem( std::string_view key, Tuples::TupleObj* tuple ) {
0045
0046 auto ifound = m_map.find( key );
0047
0048 if ( m_map.end() != ifound ) return &ifound->second.first;
0049
0050
0051 if ( !tuple ) return nullptr;
0052
0053 if ( !tuple->goodItem( key ) ) {
0054 tuple->Error( fmt::format( "ItemStore::getItem('{}') item name is not unique", key ) ).ignore();
0055 return nullptr;
0056 }
0057
0058 NTuple::Tuple* tup = tuple->tuple();
0059 if ( !tup ) {
0060 tuple->Error( fmt::format( "ItemStore::getItem('{}') invalid NTuple::Tuple*", key ) ).ignore();
0061 return nullptr;
0062 }
0063
0064
0065 auto [iter, ok] = m_map.try_emplace( key, NTuple::Item<VALUE>{}, std::string{ key } );
0066 if ( ok ) {
0067 auto nh = m_map.extract( iter );
0068 nh.key() = nh.mapped().second;
0069
0070 auto r = m_map.insert( std::move( nh ) );
0071 iter = r.position;
0072 ok = r.inserted;
0073 }
0074 if ( !ok ) {
0075 tuple->Warning( fmt::format( "ItemStore::getItem('{}') item already exists, new one not inserted!", key ) )
0076 .ignore();
0077 return nullptr;
0078 }
0079 auto& item = iter->second.first;
0080
0081 StatusCode sc = tup->addItem( iter->second.second, item );
0082 if ( sc.isFailure() ) {
0083 tuple->Error( fmt::format( "ItemStore::getItem('{}') cannot addItem", key ), sc ).ignore();
0084 m_map.erase( iter );
0085 return nullptr;
0086 }
0087
0088 if ( !tuple->addItem( iter->second.second, System::typeinfoName( typeid( VALUE ) ) ) ) {
0089 tuple->Warning( fmt::format( "ItemStore::getItem('{}') the item not unique ", key ) ).ignore();
0090 m_map.erase( iter );
0091 return nullptr;
0092 }
0093
0094 return &item;
0095 }
0096
0097
0098 ItemStore( const ItemStore& ) = delete;
0099 ItemStore& operator=( const ItemStore& ) = delete;
0100
0101 private:
0102 std::unordered_map<std::string_view, std::pair<NTuple::Item<VALUE>, std::string>> m_map;
0103 };
0104 }
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116 template <class TYPE>
0117 StatusCode Tuples::TupleObj::put( std::string_view name, const TYPE* obj ) {
0118 if ( invalid() ) { return ErrorCodes::InvalidTuple; }
0119 if ( !evtColType() ) { return ErrorCodes::InvalidOperation; }
0120
0121
0122 static bool s_fail = false;
0123 static TClass* s_type = nullptr;
0124
0125 if ( s_fail ) {
0126 return ErrorCodes::InvalidItem;
0127 }
0128 else if ( !s_type ) {
0129 s_type = TClass::GetClass( typeid( TYPE ) );
0130 if ( !s_type ) {
0131 s_fail = true;
0132 return Error( fmt::format( " put('{}',{}) :Invalid ROOT Type", name, System::typeinfoName( typeid( TYPE ) ) ),
0133 ErrorCodes::InvalidItem );
0134 }
0135 }
0136
0137 static Tuples::ItemStore<TYPE*> s_map;
0138
0139 auto item = s_map.getItem( name, this );
0140 if ( !item ) { return Error( fmt::format( " put('{}'): invalid item detected", name ), ErrorCodes::InvalidItem ); }
0141
0142 *item = const_cast<TYPE*>( obj );
0143
0144 return StatusCode::SUCCESS;
0145 }
0146
0147
0148
0149
0150
0151 #endif