ATLAS Offline Software
Loading...
Searching...
No Matches
AtlasSemantics.py
Go to the documentation of this file.
1# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
4from GaudiKernel.DataHandle import DataHandle
5import re
6
7
8class VarHandleKeySemantics(GaudiConfig2.semantics.PropertySemantics):
9 '''
10 Semantics for all data handle keys (Read, Write, Decor, Cond).
11 '''
12 __handled_types__ = (re.compile(r"SG::.*HandleKey<.*>$"),)
13
14 def __init__(self, cpp_type):
15 super().__init__(cpp_type)
16 # Deduce actual handle type
17 self._type = next(GaudiConfig2.semantics.extract_template_args(cpp_type))
18 self._isCond = 'CondHandle' in cpp_type
19
20 if cpp_type.startswith("SG::Read"):
21 self._mode = "R"
22 elif cpp_type.startswith("SG::Write"):
23 self._mode = "W"
24 else:
25 raise TypeError(f"C++ type {cpp_type} not supported")
26
27 def store(self, value):
28 if isinstance(value, DataHandle):
29 v = value.Path
30 elif isinstance(value, str):
31 v = value
32 else:
33 raise TypeError(f"cannot assign {value!r} ({type(value)}) to {self.name}"
34 ", expected string or DataHandle")
35 return DataHandle(v, self._mode, self._type, self._isCond)
36
37
38class VarHandleArraySematics(GaudiConfig2.semantics.SequenceSemantics):
39 '''
40 Treat VarHandleKeyArrays like arrays of strings
41 '''
42 __handled_types__ = (re.compile(r"SG::HandleKeyArray<.*>$"),)
43
44 class _ItemSemantics(GaudiConfig2.semantics.StringSemantics):
45 """Semantics for an item (DataHandle) in a VarHandleKeyArray converting to string"""
46
47 def __init__(self):
48 super().__init__("std::string")
49
50 def store(self, value):
51 if isinstance(value, DataHandle):
52 return value.Path
53 elif isinstance(value, str):
54 return value
55 else:
56 raise TypeError(f"cannot assign {value!r} ({type(value)}) to {self.name}"
57 ", expected string or DataHandle")
58
59 def __init__(self, cpp_type):
60 super().__init__(cpp_type, valueSem = self._ItemSemantics())
61
62 # Example for cpp_type:
63 # SG::HandleKeyArray<SG::ReadHandle<HiveDataObj>, SG::ReadHandleKey<HiveDataObj>,
64 # (Gaudi::DataHandle::Mode)4>
65 handle_type = next(GaudiConfig2.semantics.extract_template_args(cpp_type))
66 self._type = next(GaudiConfig2.semantics.extract_template_args(handle_type))
67 self._isCond = 'CondHandle' in handle_type
68
69 if handle_type.startswith("SG::ReadHandle"):
70 self._mode = "R"
71 elif handle_type.startswith("SG::WriteHandle"):
72 self._mode = "W"
73 else:
74 raise TypeError(f"C++ type {cpp_type} not supported")
75
76 def merge(self,bb,aa):
77 for b in bb:
78 if b not in aa:
79 aa.append(b)
80 return aa
81
82
83from AthenaServices.ItemListSemantics import OutputStreamItemListSemantics
84
85GaudiConfig2.semantics.SEMANTICS.append(VarHandleKeySemantics)
86GaudiConfig2.semantics.SEMANTICS.append(VarHandleArraySematics)
87GaudiConfig2.semantics.SEMANTICS.append(OutputStreamItemListSemantics)
an iterator over instances of a given type in StoreGateSvc.
Definition DataHandle.h:43
Definition merge.py:1