Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
GUPI.hpp
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#ifndef GUPI_hpp_included
11#define GUPI_hpp_included 1
12
13#include <list>
14#include <map>
15
16#include <LUPI_dataBuffer.hpp>
17#include <LUPI.hpp>
18#include <HAPI.hpp>
19
20namespace GUPI {
21
22class Entry;
23class Suite;
24
25typedef Entry *(*GUPI_parseSuite)( Suite *a_parent, HAPI::Node const &a_node );
26
27#define GUPI_documentationChars "documentation"
28#define GUPI_titleChars "title"
29#define GUPI_abstractChars "abstract"
30#define GUPI_bodyChars "body"
31#define GUPI_endfCompatibleChars "endfCompatible"
32#define GUPI_doiChars "doi"
33#define GUPI_publicationDateChars "publicationDate"
34#define GUPI_versionChars "version"
35
36/*
37============================================================
38======================== WriteInfo =========================
39============================================================
40*/
41
42class WriteInfo {
43
44 public:
45 std::list<std::string> m_lines;
48 std::string m_sep;
49
50 WriteInfo( std::string const &a_incrementalIndent = " ", int a_valuesPerLine = 100, std::string const &a_sep = " " );
51
52 std::string incrementalIndent( std::string const &indent ) { return( indent + m_incrementalIndent ); }
53 void push_back( std::string const &a_line ) { m_lines.push_back( a_line ); }
54
55 void addNodeStarter( std::string const &indent, std::string const &a_moniker, std::string const &a_attributes = "" ) {
56 m_lines.push_back( indent + "<" + a_moniker + a_attributes + ">" ); }
57 void addNodeStarterEnder( std::string const &indent, std::string const &a_moniker, std::string const &a_attributes = "" ) {
58 m_lines.push_back( indent + "<" + a_moniker + a_attributes + "/>" ); }
59 void addNodeEnder( std::string const &a_moniker ) { m_lines.back( ) += "</" + a_moniker + ">"; }
60 std::string addAttribute( std::string const &a_name, std::string const &a_value ) const { return( " " + a_name + "=\"" + a_value + "\"" ); }
61
62 std::string nodeStarter( std::string const &indent, std::string const &a_moniker, std::string const &a_attributes = "" )
63 { return( indent + "<" + a_moniker + a_attributes + ">" ); }
64 std::string nodeEnder( std::string const &a_moniker ) { return( "</" + a_moniker + ">" ); }
65
66 void print( );
67 void clear( ) { m_lines.clear( ); } /**< Clears the contents of *m_lines*. */
68};
69
70/*
71============================================================
72========================= Ancestry =========================
73============================================================
74*/
75class Ancestry {
76
77 public:
78 /* *********************************************************************************************************//**
79 * Constructs and returns the key name/value for the *this* node.
80 *
81 * @return The constructed key name/value.
82 ***********************************************************************************************************/
83 static std::string buildXLinkItemKey( std::string const &a_name, std::string const &a_key ) {
84
85 if( a_key.size( ) == 0 ) return( "" );
86 return( "[@" + a_name + "='" + a_key + "']" );
87 }
88
89 private:
90 std::string m_moniker; /**< The node's name (i.e., moniker). */
91 Ancestry *m_ancestor; /**< The parent node of *this*. */
92 std::string m_attribute; /**< The name of the attribute in the node that uniquely identifies the node when the parent node containing other child nodes with the same moniker. */
93
94 Ancestry *findInAncestry2( std::size_t a_index, std::vector<std::string> const &a_segments );
95 Ancestry const *findInAncestry2( std::size_t a_index, std::vector<std::string> const &a_segments ) const ;
96
97 public:
98 Ancestry( std::string const &a_moniker, std::string const &a_attribute = "" );
99 virtual ~Ancestry( );
100 Ancestry &operator=( Ancestry const &a_ancestry );
101
102 std::string const &moniker( ) const { return( m_moniker ); } /**< Returns the value of the *m_moniker* member. */
103 void setMoniker( std::string const &a_moniker ) { m_moniker = a_moniker; } /**< Set the value of the *m_moniker* member to *a_moniker*. */
104 Ancestry *ancestor( ) { return( m_ancestor ); } /**< Returns the value of the *m_ancestor* member. */
105 Ancestry const *ancestor( ) const { return( m_ancestor ); } /**< Returns the value of the *m_ancestor* member. */
106 void setAncestor( Ancestry *a_ancestor ) { m_ancestor = a_ancestor; } /**< Sets the *m_ancestor* member to *a_ancestor*. */
107 std::string attribute( ) const { return( m_attribute ); } /**< Returns the value of the *m_attribute* member. */
108
109 Ancestry *root( );
110 Ancestry const *root( ) const ;
111 bool isChild( Ancestry *a_instance ) { return( this == a_instance->m_ancestor ); } /**< Returns true if *a_instance* is a child of *this*. */
112 bool isParent( Ancestry *a_parent ) { return( this->m_ancestor == a_parent ); } /**< Returns true if *a_instance* is the parent of *this*. */
113 bool isRoot( ) const { return( this->m_ancestor == nullptr ); } /**< Returns true if *this* is the root ancestor. */
114
115 Ancestry *findInAncestry( std::string const &a_href );
116 Ancestry const *findInAncestry( std::string const &a_href ) const ;
117
118 /* *********************************************************************************************************//**
119 * Used to tranverse **GNDS** nodes. This method returns a pointer to a derived class' *a_item* member or nullptr if none exists.
120 *
121 * @param a_item [in] The name of the class member whose pointer is to be return.
122 * @return The pointer to the class member or nullptr if class does not have a member named a_item.
123 ***********************************************************************************************************/
124 virtual Ancestry *findInAncestry3( std::string const &a_item ) = 0;
125 virtual Ancestry const *findInAncestry3( std::string const &a_item ) const = 0;
126
127 virtual LUPI_HOST void serialize( LUPI::DataBuffer &a_buffer, LUPI::DataBuffer::Mode a_mode );
128 virtual std::string xlinkItemKey( ) const { return( "" ); } /**< Returns the value of *this*'s key. */
129 std::string toXLink( ) const ;
130
131 virtual void toXMLList( WriteInfo &a_writeInfo, std::string const &a_indent = "" ) const ;
132 void printXML( ) const ;
133};
134
135/*
136============================================================
137========================== Entry ===========================
138============================================================
139*/
140class Entry : public Ancestry {
141
142 private:
143 std::string m_keyName; /**< The name of the key used by the parent suite to reference *this* entry. */
144 std::string m_keyValue; /**< The key used by the parent suite to reference *this* entry. */
145
146 public:
147 Entry( std::string const &a_moniker, std::string const &a_keyName, std::string const &a_keyValue );
148 Entry( HAPI::Node const &a_node, std::string const &a_keyName );
149 ~Entry( );
150
151 std::string const &keyName( ) const { return( m_keyName ); } /**< Returns a const reference to the *m_keyName* member. */
152 std::string const &keyValue( ) const { return( m_keyValue ); } /**< Returns a const reference to the *m_keyValue* member. */
153
154 Ancestry *findInAncestry3( LUPI_maybeUnused std::string const &a_item ) { return( nullptr ); }
155 Ancestry const *findInAncestry3( LUPI_maybeUnused std::string const &a_item ) const { return( nullptr ); }
157 std::string xlinkItemKey( ) const {
158
159 if( m_keyValue == "" ) return( "" );
160 return( buildXLinkItemKey( m_keyName, m_keyValue ) );
161 }
162};
163
164/*
165============================================================
166========================== Text ============================
167============================================================
168*/
169
170class Text : public Ancestry {
171
172 public:
173 enum class Encoding {
176 };
177
178 enum class Markup {
183 };
184
185 private:
186 std::string m_body;
187 Encoding m_encoding;
188 Markup m_markup;
189 std::string m_label;
190
191 public:
192 Text( HAPI::Node const &a_node );
193 ~Text( );
194
195 std::string const &body( ) const { return m_body; }
196 Encoding encoding( ) const { return m_encoding; }
197 Markup markup( ) const { return m_markup; }
198 std::string const &label( ) const { return m_label; }
199
200 Ancestry *findInAncestry3( LUPI_maybeUnused std::string const &a_item ) { return( nullptr ); }
201 Ancestry const *findInAncestry3( LUPI_maybeUnused std::string const &a_item ) const { return( nullptr ); }
202
203};
204
205/*
206============================================================
207===================== Documentation ========================
208============================================================
209*/
210class Documentation : public Ancestry {
211
212 private:
213 std::string m_doi; /**< The name of the key used by the parent suite to reference *this* entry. */
214 std::string m_publicationDate; /**< The key used by the parent suite to reference *this* entry. */
215 std::string m_version;
216
217 Text m_title;
218 Text m_abstract;
219 Text m_body;
220
221 public:
222 // Documentation(std::string const &a_moniker, Text const &a_doi, std::string const &a_publicationDate, Text const &a_version);
223 Documentation(HAPI::Node const &a_node);
225
226 std::string const &doi( ) const { return m_doi; }
227 std::string const &publicationDate( ) const { return m_publicationDate; }
228 std::string const &version( ) const { return m_version; }
229
230 Text const &title( ) const { return m_title; }
231 Text const &abstract( ) const { return m_abstract; }
232 Text const &body( ) const { return m_body; }
233
234 Ancestry *findInAncestry3( LUPI_maybeUnused std::string const &a_item ) { return( nullptr ); }
235 Ancestry const *findInAncestry3( LUPI_maybeUnused std::string const &a_item ) const { return( nullptr ); }
236
237};
238
239/*
240============================================================
241=========================== Suite ==========================
242============================================================
243*/
244class Suite : public Ancestry {
245
246 public:
247 typedef std::vector<Entry *> Entries; /**< The typedef the the *m_entries* member. */
248
249 private:
250 std::string m_keyName; /**< The name of the key used to look up items in the suite. */
251 mutable Entries m_entries; /**< The list of nodes stored within *this*. */
252 std::map<std::string,std::size_t> m_map; /**< A map of *this* node labels to their index in *m_entries*. */
253
254 Suite( Suite const *a_suite ); // FIXME, should we make public or private copy constructor? Making private for now.
255
256 public:
257 Suite( std::string const &a_keyName );
258 Suite( std::string const &a_moniker, std::string const &a_keyName );
259 Suite( HAPI::Node const &a_node, std::string const &a_keyName, GUPI_parseSuite a_parseSuite );
260 ~Suite( );
261
262 std::string const &keyName( ) const { return( m_keyName ); } /**< Returns a const reference to the *m_keyName* member. */
263 std::size_t size( ) const { return( m_entries.size( ) ); } /**< Returns the number of node contained by *this*. */
264
265 std::size_t operator[]( std::string const &a_label ) const ;
266 typedef Entries::iterator iterator;
267 typedef Entries::const_iterator const_iterator;
268 iterator begin( ) { return m_entries.begin( ); } /**< The C++ begin iterator for *this*. */
269 const_iterator begin( ) const { return m_entries.begin( ); } /**< The C++ const begin iterator for *this*. */
270 iterator end( ) { return m_entries.end( ); } /**< The C++ end iterator for *this*. */
271 const_iterator end( ) const { return m_entries.end( ); } /**< The C++ const end iterator for *this*. */
272
273 template<typename T> T *get( std::size_t a_Index );
274 template<typename T> T const *get( std::size_t a_Index ) const ;
275 template<typename T> T *get( std::string const &a_label );
276 template<typename T> T const *get( std::string const &a_label ) const ;
277
278 void parse( HAPI::Node const &a_node, GUPI_parseSuite a_parseSuite );
279 void add( Entry *a_entry );
280 iterator find( std::string const &a_label );
281 const_iterator find( std::string const &a_label ) const ;
282 bool has( std::string const &a_label ) const { return( find( a_label ) != m_entries.end( ) ); }
283
284 Ancestry *findInAncestry3( std::string const &a_item );
285 Ancestry const *findInAncestry3( std::string const &a_item ) const ;
286 std::vector<iterator> findAllOfMoniker( std::string const &a_moniker ) ;
287 std::vector<const_iterator> findAllOfMoniker( std::string const &a_moniker ) const ;
288
289 void toXMLList( WriteInfo &a_writeInfo, std::string const &a_indent = "" ) const ;
290 void printEntryLabels( std::string const &a_header ) const ;
291};
292
293/* *********************************************************************************************************//**
294 * Returns the node at index *a_index*.
295 *
296 * @param a_index [in] The index of the node to return.
297 *
298 * @return The node at index *a_index*.
299 ***********************************************************************************************************/
300
301template<typename T> T *Suite::get( std::size_t a_index ) {
302
303 Entry *entry = m_entries[a_index];
304 T *object = dynamic_cast<T *>( entry );
305
306 if( object == nullptr ) throw LUPI::Exception( "GIDI::Suite::get( std::size_t ): invalid cast" );
307
308 return( object );
309}
310
311/* *********************************************************************************************************//**
312 * Returns the node at index *a_index*.
313 *
314 * @param a_index [in] The index of the node to return.
315 *
316 * @return The node at index *a_index*.
317 ***********************************************************************************************************/
318
319template<typename T> T const *Suite::get( std::size_t a_index ) const {
320
321 Entry *entry = m_entries[a_index];
322 T *object = dynamic_cast<T *>( entry );
323
324 if( object == nullptr ) throw LUPI::Exception( "GIDI::Suite::get( std::size_t ): invalid cast" );
325
326 return( object );
327}
328
329/* *********************************************************************************************************//**
330 * Returns the node with label *a_label*.
331 *
332 * @param a_label [in] The label of the node to return.
333 *
334 * @return The node with label *a_label*.
335 ***********************************************************************************************************/
336
337template<typename T> T *Suite::get( std::string const &a_label ) {
338
339 auto index = (*this)[a_label];
340 Entry *entry = m_entries[index];
341 T *object = dynamic_cast<T *>( entry );
342
343 if( object == nullptr ) throw LUPI::Exception( "GIDI::Suite::get( std::string const & ): invalid cast" );
344
345 return( object );
346}
347
348/* *********************************************************************************************************//**
349 * Returns the node with label *a_label*.
350 *
351 * @param a_label [in] The label of the node to return.
352 *
353 * @return The node with label *a_label*.
354 ***********************************************************************************************************/
355
356template<typename T> T const *Suite::get( std::string const &a_label ) const {
357
358 auto index = (*this)[a_label];
359 Entry *entry = m_entries[index];
360 T *object = dynamic_cast<T *>( entry );
361
362 if( object == nullptr ) throw LUPI::Exception( "GIDI::Suite::get( std::string const & ): invalid cast" );
363
364 return( object );
365}
366
367} // End of namespace GUPI.
368
369#endif // GUPI_hpp_included
#define LUPI_HOST
#define LUPI_maybeUnused
void setMoniker(std::string const &a_moniker)
Definition GUPI.hpp:103
virtual Ancestry * findInAncestry3(std::string const &a_item)=0
Ancestry * findInAncestry(std::string const &a_href)
Ancestry & operator=(Ancestry const &a_ancestry)
virtual Ancestry const * findInAncestry3(std::string const &a_item) const =0
static std::string buildXLinkItemKey(std::string const &a_name, std::string const &a_key)
Definition GUPI.hpp:83
void setAncestor(Ancestry *a_ancestor)
Definition GUPI.hpp:106
Ancestry * ancestor()
Definition GUPI.hpp:104
std::string const & moniker() const
Definition GUPI.hpp:102
virtual LUPI_HOST void serialize(LUPI::DataBuffer &a_buffer, LUPI::DataBuffer::Mode a_mode)
void printXML() const
Ancestry(std::string const &a_moniker, std::string const &a_attribute="")
bool isRoot() const
Definition GUPI.hpp:113
Ancestry * root()
std::string toXLink() const
Ancestry const * ancestor() const
Definition GUPI.hpp:105
bool isParent(Ancestry *a_parent)
Definition GUPI.hpp:112
bool isChild(Ancestry *a_instance)
Definition GUPI.hpp:111
virtual void toXMLList(WriteInfo &a_writeInfo, std::string const &a_indent="") const
std::string attribute() const
Definition GUPI.hpp:107
virtual ~Ancestry()
virtual std::string xlinkItemKey() const
Definition GUPI.hpp:128
std::string const & doi() const
Definition GUPI.hpp:226
Text const & body() const
Definition GUPI.hpp:232
Ancestry const * findInAncestry3(LUPI_maybeUnused std::string const &a_item) const
Definition GUPI.hpp:235
std::string const & publicationDate() const
Definition GUPI.hpp:227
Documentation(HAPI::Node const &a_node)
Text const & abstract() const
Definition GUPI.hpp:231
std::string const & version() const
Definition GUPI.hpp:228
Text const & title() const
Definition GUPI.hpp:230
Ancestry * findInAncestry3(LUPI_maybeUnused std::string const &a_item)
Definition GUPI.hpp:234
LUPI_HOST void serialize(LUPI::DataBuffer &a_buffer, LUPI::DataBuffer::Mode a_mode)
Definition GUPI_entry.cc:59
Ancestry * findInAncestry3(LUPI_maybeUnused std::string const &a_item)
Definition GUPI.hpp:154
std::string xlinkItemKey() const
Definition GUPI.hpp:157
std::string const & keyValue() const
Definition GUPI.hpp:152
Ancestry const * findInAncestry3(LUPI_maybeUnused std::string const &a_item) const
Definition GUPI.hpp:155
std::string const & keyName() const
Definition GUPI.hpp:151
Entry(std::string const &a_moniker, std::string const &a_keyName, std::string const &a_keyValue)
Definition GUPI_entry.cc:25
T * get(std::size_t a_Index)
Definition GUPI.hpp:301
iterator end()
Definition GUPI.hpp:270
Entries::iterator iterator
Definition GUPI.hpp:266
T const * get(std::string const &a_label) const
std::vector< Entry * > Entries
Definition GUPI.hpp:247
std::string const & keyName() const
Definition GUPI.hpp:262
iterator begin()
Definition GUPI.hpp:268
const_iterator begin() const
Definition GUPI.hpp:269
T const * get(std::size_t a_Index) const
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
const_iterator end() const
Definition GUPI.hpp:271
iterator find(std::string const &a_label)
std::vector< iterator > findAllOfMoniker(std::string const &a_moniker)
bool has(std::string const &a_label) const
Definition GUPI.hpp:282
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
std::string const & body() const
Definition GUPI.hpp:195
Ancestry * findInAncestry3(LUPI_maybeUnused std::string const &a_item)
Definition GUPI.hpp:200
std::string const & label() const
Definition GUPI.hpp:198
Encoding encoding() const
Definition GUPI.hpp:196
Text(HAPI::Node const &a_node)
Definition GUPI_text.cc:18
Ancestry const * findInAncestry3(LUPI_maybeUnused std::string const &a_item) const
Definition GUPI.hpp:201
Markup markup() const
Definition GUPI.hpp:197
std::string m_incrementalIndent
Definition GUPI.hpp:46
void addNodeStarterEnder(std::string const &indent, std::string const &a_moniker, std::string const &a_attributes="")
Definition GUPI.hpp:57
void push_back(std::string const &a_line)
Definition GUPI.hpp:53
std::list< std::string > m_lines
Definition GUPI.hpp:45
void clear()
Definition GUPI.hpp:67
void addNodeEnder(std::string const &a_moniker)
Definition GUPI.hpp:59
int m_valuesPerLine
Definition GUPI.hpp:47
std::string incrementalIndent(std::string const &indent)
Definition GUPI.hpp:52
WriteInfo(std::string const &a_incrementalIndent=" ", int a_valuesPerLine=100, std::string const &a_sep=" ")
void addNodeStarter(std::string const &indent, std::string const &a_moniker, std::string const &a_attributes="")
Definition GUPI.hpp:55
std::string nodeEnder(std::string const &a_moniker)
Definition GUPI.hpp:64
std::string nodeStarter(std::string const &indent, std::string const &a_moniker, std::string const &a_attributes="")
Definition GUPI.hpp:62
std::string m_sep
Definition GUPI.hpp:48
std::string addAttribute(std::string const &a_name, std::string const &a_value) const
Definition GUPI.hpp:60
Definition GUPI.hpp:20
Entry *(* GUPI_parseSuite)(Suite *a_parent, HAPI::Node const &a_node)
Definition GUPI.hpp:25