Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:56

0001 //! \file Auxiliary.h
0002 
0003 #ifndef VGDMLAuxiliary_h
0004 #define VGDMLAuxiliary_h
0005 
0006 #include <string>
0007 #include <memory>
0008 #include <vector>
0009 
0010 namespace vgdml {
0011 
0012 /**
0013  * \brief Class representing a GDML auxiliary tag
0014  *
0015  * The GDML schema provides the `<auxiliary/>` tag to allow attachment of additional,
0016  * user specified data to . It has the general form in XML
0017  *
0018  * ```xml
0019  * <auxiliary auxtype="foo" auxvalue="bar" auxunit="optional">
0020  *   <!-- Zero to N child auxiliaries -->
0021  *   <auxiliary ...>
0022  *   ...
0023  * </auxiliary>
0024  * ```
0025  *
0026  * Auxiliary tags may be added either as child nodes of the `<volume>` or `<userinfo>` tags.
0027  *
0028  * In this class, the attributes of the auxiliary tags are stored as `std::string` and it is left to
0029  * the client to interpret their values as appropriate. The list of child auxiliary tags, if any, are
0030  * exposed as a vector and again left to the client interpret this subtree and the leaf auxiliary tags
0031  * and attributes.
0032  */
0033 class Auxiliary {
0034 public:
0035   /// Convenience type alias for collection of child Auxiliary tags
0036   using AuxiliaryList = std::vector<Auxiliary>;
0037 
0038   /// Default constructor
0039   /// Initializes all attributes to the empty string, with zero child nodes
0040   Auxiliary() = default;
0041 
0042   /// Destructor
0043   ~Auxiliary() = default;
0044 
0045   /// Copy constructor
0046   Auxiliary(const Auxiliary &rhs)
0047       : type(rhs.type), value(rhs.value), unit(rhs.unit), children(new AuxiliaryList{*(rhs.children)})
0048   {
0049   }
0050 
0051   /// Move Constructor
0052   Auxiliary(Auxiliary &&rhs) : Auxiliary() { swap(*this, rhs); }
0053 
0054   /// Copy assignment operator
0055   Auxiliary &operator=(const Auxiliary &rhs)
0056   {
0057     Auxiliary tmp(rhs);
0058     swap(*this, tmp);
0059     return *this;
0060   }
0061 
0062   /// Move assignment operator
0063   Auxiliary &operator=(Auxiliary &&rhs)
0064   {
0065     swap(*this, rhs);
0066     return *this;
0067   }
0068 
0069   /// Return the value of the auxiliary `type` attribute
0070   const std::string &GetType() const { return type; }
0071 
0072   /// Return the value of the auxiliary `value` attribute
0073   const std::string &GetValue() const { return value; }
0074 
0075   /// Return the value of the auxiliary `unit` attribute
0076   const std::string &GetUnit() const { return unit; }
0077 
0078   /// Return the collection of child auxiliary tags
0079   const AuxiliaryList &GetChildren() const { return *children; }
0080 
0081   /// Set the value for the `type` attribute
0082   void SetType(const std::string &s) { type = s; }
0083 
0084   /// Set the value for the `value` attribute
0085   void SetValue(const std::string &s) { value = s; }
0086 
0087   /// Set the value for the `unit` attribute
0088   void SetUnit(const std::string &s) { unit = s; }
0089 
0090   /// Add an auxiliary object as a child of this one
0091   void AddChild(const Auxiliary &a) { children->push_back(a); }
0092 
0093   /// Implementation of swap to enable copy-and-swap
0094   /// Copy-and-swap used as simplest way to copy/move the held unique_ptr
0095   /// whilst ensuring moved-from object is left with a new, empty auxiliary
0096   /// list. This is held in a unique_ptr to avoid an incomplete type.
0097   friend void swap(Auxiliary &lhs, Auxiliary &rhs)
0098   {
0099     using std::swap;
0100     swap(lhs.type, rhs.type);
0101     swap(lhs.value, rhs.value);
0102     swap(lhs.unit, rhs.unit);
0103     swap(lhs.children, rhs.children);
0104   }
0105 
0106 private:
0107   std::string type  = "";                                     ///< value of `type` attribute
0108   std::string value = "";                                     ///< value of `value` attribute
0109   std::string unit  = "";                                     ///< value of `unit` attribute
0110   std::unique_ptr<AuxiliaryList> children{new AuxiliaryList}; ///< collection of child Auxiliary tags
0111 };
0112 
0113 /// Return true if input Auxiliary instances are equal
0114 /// Equality means that all attributes are equal, and that both instances
0115 /// have the same child nodes all the way down the tree.
0116 inline bool operator==(const Auxiliary &lhs, const Auxiliary &rhs)
0117 {
0118   if (lhs.GetType() != rhs.GetType()) return false;
0119   if (lhs.GetValue() != rhs.GetValue()) return false;
0120   if (lhs.GetUnit() != rhs.GetUnit()) return false;
0121   if (lhs.GetChildren() != rhs.GetChildren()) return false;
0122 
0123   return true;
0124 }
0125 
0126 /// Return true if input Auxiliary instances are not equal
0127 inline bool operator!=(const Auxiliary &lhs, const Auxiliary &rhs)
0128 {
0129   return !(lhs == rhs);
0130 }
0131 
0132 } // namespace vgdml
0133 
0134 #endif