Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
GUPI_suite.cc
Go to the documentation of this file.
1/*
2# <<BEGIN-copyright>>
3# Copyright 2019, Lawrence Livermore National Security, LLC.
4# This file is part of the gidiplus package (https://github.com/LLNL/gidiplus).
5# gidiplus is licensed under the MIT license (see https://opensource.org/licenses/MIT).
6# SPDX-License-Identifier: MIT
7# <<END-copyright>>
8*/
9
10#include <GUPI.hpp>
11
12namespace GUPI {
13
14/*! \class Suite
15 * This class is used to store a list (i.e., suite) of similar type **GNDS** nodes.
16*/
17
18/* *********************************************************************************************************//**
19 ***********************************************************************************************************/
20
21Suite::Suite( std::string const &a_keyName ) :
22 Ancestry( "" ),
23 m_keyName( a_keyName ) {
24
25}
26
27/* *********************************************************************************************************//**
28 * @param a_moniker [in] The **GNDS** moniker for the Suite instance.
29 * @param a_keyName [in] The name of the key for elements of *this*.
30 ***********************************************************************************************************/
31
32Suite::Suite( std::string const &a_moniker, std::string const &a_keyName ) :
33 Ancestry( a_moniker ),
34 m_keyName( a_keyName ) {
35
36}
37
38/* *********************************************************************************************************//**
39 * @param a_node [in] The HAPI::Node to be parsed and used to construct the Product.
40 * @param a_keyName [in] The name of the key for referencing up child nodes.
41 * @param a_parseSuite [in] This function to call to parse each sub-node.
42 ***********************************************************************************************************/
43
44Suite::Suite( HAPI::Node const &a_node, std::string const &a_keyName, GUPI_parseSuite a_parseSuite ) :
45 Ancestry( a_node.name( ) ),
46 m_keyName( a_keyName ) {
47
48 parse( a_node, a_parseSuite );
49}
50
51/* *********************************************************************************************************//**
52 ***********************************************************************************************************/
53
55
56 for( std::vector<Entry *>::const_iterator iter = m_entries.begin( ); iter < m_entries.end( ); ++iter ) delete *iter;
57}
58
59/* *********************************************************************************************************//**
60 * This methods parses all the child nodes of *a_node*.
61 *
62 * @param a_node [in] The HAPI::Node to be parsed and used to construct the Product.
63 * @param a_parseSuite [in] This function to call to parse each sub-node.
64 ***********************************************************************************************************/
65
66void Suite::parse( HAPI::Node const &a_node, GUPI_parseSuite a_parseSuite ) {
67
68 for( HAPI::Node child = a_node.first_child( ); !child.empty( ); child.to_next_sibling( ) ) {
69 Entry *form = a_parseSuite( this, child );
70 if( form != nullptr ) add( form );
71 }
72}
73
74/* *********************************************************************************************************//**
75 * Returns the index of the node in *this* that has keyValue *a_keyValue*.
76
77 * @return [in] The index of the node with keyValue *a_keyValue* in *this*.
78 ***********************************************************************************************************/
79
80std::size_t Suite::operator[]( std::string const &a_keyValue ) const {
81
82 auto const iter = m_map.find( a_keyValue );
83 if( iter == m_map.end( ) ) {
84 throw LUPI::Exception( "form '" + a_keyValue + "' not in database." );
85 }
86
87 return( iter->second );
88}
89
90/* *********************************************************************************************************//**
91 * Adds the node *a_form* to *this*.
92 *
93 * @param a_form [in] The form to add.
94 ***********************************************************************************************************/
95
96void Suite::add( Entry *a_form ) {
97
98 std::size_t i1 = 0;
99
100 for( Suite::iterator iter = m_entries.begin( ); iter != m_entries.end( ); ++iter, ++i1 ) {
101 if( (*iter)->keyValue( ) == a_form->keyValue( ) ) {
102 m_entries[i1] = a_form;
103 a_form->setAncestor( this );
104 return;
105 }
106 }
107 m_map[a_form->keyValue( )] = m_entries.size( );
108 m_entries.push_back( a_form );
109 a_form->setAncestor( this );
110}
111
112/* *********************************************************************************************************//**
113 * Returns the iterator to the node with keyValue *a_keyValue*.
114 *
115 * @param a_keyValue [in] The keyValue of the node to find.
116 *
117 * @return The iterator to the node with keyValue *a_keyValue*.
118 ***********************************************************************************************************/
119
120Suite::iterator Suite::find( std::string const &a_keyValue ) {
121
122 for( Suite::iterator iter = m_entries.begin( ); iter != m_entries.end( ); ++iter ) {
123 if( (*iter)->keyName( ) == a_keyValue ) return( iter );
124 }
125
126 return( m_entries.end( ) );
127}
128
129/* *********************************************************************************************************//**
130 * Returns the iterator to the node with keyValue *a_keyValue*.
131 *
132 * @param a_keyValue [in] The keyValue of the node to find.
133 *
134 * @return The iterator to the node with keyValue *a_keyValue*.
135 ***********************************************************************************************************/
136
137Suite::const_iterator Suite::find( std::string const &a_keyValue ) const {
138
139 for( Suite::const_iterator iter = m_entries.begin( ); iter != m_entries.end( ); ++iter ) {
140 if( (*iter)->keyValue( ) == a_keyValue ) return( iter );
141 }
142
143 return( m_entries.end( ) );
144}
145
146/* *********************************************************************************************************//**
147 * Returns a list of iterators to the nodes in *this* that have **GNDS** moniker *a_moniker*.
148 *
149 * @param a_moniker [in] The moniker to search for.
150 *
151 * @return List of iterators to the nodes in *this* that have moniker *a_moniker*.
152 ***********************************************************************************************************/
153
154std::vector<Suite::iterator> Suite::findAllOfMoniker( std::string const &a_moniker ) {
155
156 std::vector<Suite::iterator> iters;
157
158 for( Suite::iterator iter = m_entries.begin( ); iter != m_entries.end( ); ++iter ) {
159 if( (*iter)->moniker( ) == a_moniker ) iters.push_back( iter );
160 }
161
162 return( iters );
163}
164
165/* *********************************************************************************************************//**
166 * Returns a list of iterators to the nodes in *this* that have **GNDS** moniker *a_moniker*.
167 *
168 * @param a_moniker [in] The moniker to search for.
169 *
170 * @return List of iterators to the nodes in *this* that have moniker *a_moniker*.
171 ***********************************************************************************************************/
172
173std::vector<Suite::const_iterator> Suite::findAllOfMoniker( std::string const &a_moniker ) const {
174
175 std::vector<Suite::const_iterator> iters;
176
177 for( Suite::const_iterator iter = m_entries.begin( ); iter != m_entries.end( ); ++iter ) {
178 if( (*iter)->moniker( ) == a_moniker ) iters.push_back( iter );
179 }
180
181 return( iters );
182}
183
184/* *********************************************************************************************************//**
185 * Used by Ancestry to tranverse GNDS nodes. This method returns a pointer to a derived class' a_item member or nullptr if none exists.
186 *
187 * @param a_item [in] The name of the class member whose pointer is to be return.
188 * @return The pointer to the class member or nullptr if class does not have a member named a_item.
189 ***********************************************************************************************************/
190
191Ancestry *Suite::findInAncestry3( std::string const &a_item ) {
192
193 std::size_t index( a_item.find( '=' ) ), lastQuote = a_item.size( ) - 2;
194
195 if( index == std::string::npos ) return( nullptr );
196 ++index;
197 if( index > lastQuote ) throw LUPI::Exception( "Suite::findInAncestry3: invalide xlink" );
198 if( a_item[index] != '\'' ) throw LUPI::Exception( "Suite::findInAncestry3: invalid xlink, missing '." );
199 ++index;
200 if( a_item[lastQuote] != '\'' ) throw LUPI::Exception( "Suite::findInAncestry3: invalid xlink, missing endl '." );
201
202 std::string keyValue( a_item.substr( index, lastQuote - index ) );
203
204 return( get<Ancestry>( keyValue ) );
205}
206
207/* *********************************************************************************************************//**
208 * Used by Ancestry to tranverse GNDS nodes. This method returns a pointer to a derived class' a_item member or nullptr if none exists.
209 *
210 * @param a_item [in] The name of the class member whose pointer is to be return.
211 * @return The pointer to the class member or nullptr if class does not have a member named a_item.
212 ***********************************************************************************************************/
213
214Ancestry const *Suite::findInAncestry3( std::string const &a_item ) const {
215
216 std::size_t index( a_item.find( '=' ) ), lastQuote = a_item.size( ) - 2;
217
218 if( index == std::string::npos ) return( nullptr );
219 ++index;
220 if( index > lastQuote ) throw LUPI::Exception( "Suite::findInAncestry3: invalide xlink" );
221 if( a_item[index] != '\'' ) throw LUPI::Exception( "Suite::findInAncestry3: invalid xlink, missing '." );
222 ++index;
223 if( a_item[lastQuote] != '\'' ) throw LUPI::Exception( "Suite::findInAncestry3: invalid xlink, missing endl '." );
224
225 std::string keyValue( a_item.substr( index, lastQuote - index ) );
226
227 return( get<Ancestry>( keyValue ) );
228}
229
230/* *********************************************************************************************************//**
231 * Fills the argument *a_writeInfo* with the XML lines that represent *this*. Recursively enters each sub-node.
232 *
233 * @param a_writeInfo [in/out] Instance containing incremental indentation and other information and stores the appended lines.
234 * @param a_indent [in] The amount to indent *this* node.
235 ***********************************************************************************************************/
236
237void Suite::toXMLList( WriteInfo &a_writeInfo, std::string const &a_indent ) const {
238
239 std::string indent2 = a_writeInfo.incrementalIndent( a_indent );
240
241 if( size( ) == 0 ) return;
242
243 std::string XMLLine( a_indent + "<" + moniker( ) + ">" );
244 a_writeInfo.push_back( XMLLine );
245
246 for( Suite::const_iterator iter = m_entries.begin( ); iter != m_entries.end( ); ++iter ) (*iter)->toXMLList( a_writeInfo, indent2 );
247
248 a_writeInfo.addNodeEnder( moniker( ) );
249}
250
251/* *********************************************************************************************************//**
252 * Prints the list of node keyValues to std::cout.
253 *
254 * @param a_header [in] A string printed before the list of keyValues is printed.
255 ***********************************************************************************************************/
256
257void Suite::printEntryLabels( std::string const &a_header ) const {
258
259 std::cout << a_header << ": size = " << size( ) << std::endl;
260
261 for( Suite::const_iterator iter = m_entries.begin( ); iter != m_entries.end( ); ++iter )
262 std::cout << " " << (*iter)->keyValue( ) << std::endl;
263}
264
265}
void setAncestor(Ancestry *a_ancestor)
Definition GUPI.hpp:106
std::string const & moniker() const
Definition GUPI.hpp:102
Ancestry(std::string const &a_moniker, std::string const &a_attribute="")
std::string const & keyValue() const
Definition GUPI.hpp:152
T * get(std::size_t a_Index)
Definition GUPI.hpp:301
Entries::iterator iterator
Definition GUPI.hpp:266
void printEntryLabels(std::string const &a_header) const
void toXMLList(WriteInfo &a_writeInfo, std::string const &a_indent="") const
void add(Entry *a_entry)
Definition GUPI_suite.cc:96
iterator find(std::string const &a_label)
std::vector< iterator > findAllOfMoniker(std::string const &a_moniker)
std::size_t size() const
Definition GUPI.hpp:263
Entries::const_iterator const_iterator
Definition GUPI.hpp:267
void parse(HAPI::Node const &a_node, GUPI_parseSuite a_parseSuite)
Definition GUPI_suite.cc:66
Ancestry * findInAncestry3(std::string const &a_item)
std::size_t operator[](std::string const &a_label) const
Definition GUPI_suite.cc:80
void push_back(std::string const &a_line)
Definition GUPI.hpp:53
void addNodeEnder(std::string const &a_moniker)
Definition GUPI.hpp:59
std::string incrementalIndent(std::string const &indent)
Definition GUPI.hpp:52
bool empty() const
Definition HAPI_Node.cc:150
Node first_child() const
Definition HAPI_Node.cc:82
Definition GUPI.hpp:20
Entry *(* GUPI_parseSuite)(Suite *a_parent, HAPI::Node const &a_node)
Definition GUPI.hpp:25