XdmfItem.hpp 13 KB
Newer Older
1 2 3
#ifndef XDMFITEM_HPP_
#define XDMFITEM_HPP_

4
// Forward Declarations
5
class XdmfCoreReader;
6
class XdmfInformation;
7 8 9
class XdmfVisitor;

// Includes
10
#include <boost/shared_ptr.hpp>
11
#include <loki/Visitor.h>
12
#include <map>
13
#include <string>
14
#include <vector>
15
#include "XdmfCore.hpp"
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
// Macro that allows children XdmfItems to be attached to a parent XdmfItem.
// -- For Header File
#define XDMF_CHILDREN(ChildClass, ChildName, SearchName)                      \
                                                                              \
public:                                                                       \
                                                                              \
  /** Get a ChildClass attached to this item by index.
      @param index of the ChildClass to retrieve.
      @return requested ChildClass. If no ChildClass##s exist at the index,
      a NULL pointer is returned.
  */                                                                          \
  virtual boost::shared_ptr<ChildClass>                                       \
  get##ChildName(const unsigned int index);                                   \
                                                                              \
  /** Get a ChildClass attached to this item by index (const version).
      @param index of the ChildClass to retrieve.
      @return requested ChildClass. If no ChildClass##s exist at the index, a
      NULL pointer is returned.
  */                                                                          \
  virtual boost::shared_ptr<const ChildClass>                                 \
  get##ChildName(const unsigned int index) const;                             \
                                                                              \
  /** Get a ChildClass attached to this item by SearchName.
      @param SearchName of the ChildClass to retrieve.
      @return requested ChildClass. If no ChildClass##s are found with the
      correct SearchName, a NULL pointer is returned.
  */                                                                          \
  virtual boost::shared_ptr<ChildClass>                                       \
  get##ChildName(const std::string & SearchName);                             \
                                                                              \
  /** Get a ChildClass attached to this item by SearchName (const version).
      @param SearchName of the ChildClass to retrieve.
      @return requested ChildClass  If no ChildClass##s are found with the
      correct SearchName, a NULL pointer is returned.
  */                                                                          \
  virtual boost::shared_ptr<const ChildClass>                                 \
  get##ChildName(const std::string & SearchName) const;                       \
                                                                              \
  /** Get the number of ChildClass##s attached to this item.
      @return number of ChildClass##s attached to this item.
  */                                                                          \
  virtual unsigned int getNumber##ChildName##s() const;                       \
                                                                              \
  /** Insert a ChildClass into to this item.
      @param ChildName to attach to this item.
  */                                                                          \
  virtual void insert(const boost::shared_ptr<ChildClass> ChildName);         \
                                                                              \
  /** Remove a ChildClass from this item by index. If no ChildClass##s exist
      at the index, nothing is removed.
      @param index of the ChildClass to remove.
  */                                                                          \
  virtual void remove##ChildName(const unsigned int index);                   \
                                                                              \
  /** Remove a ChildClass from this item by SearchName. If no ChildClass##s
      have the correct SearchName, nothing is removed.
      @param SearchName of the ChildClass to remove.
  */                                                                          \
  virtual void remove##ChildName(const std::string & SearchName);             \
                                                                              \
protected :                                                                   \
                                                                              \
  std::vector<boost::shared_ptr<ChildClass> > m##ChildName##s;                \
                                                                              \
public :

// Macro that allows children XdmfItems to be attached to a parent XdmfItem.
// -- For Implementation File
#define XDMF_CHILDREN_IMPLEMENTATION(ParentClass,                             \
86 87 88
                                     ChildClass,                              \
                                     ChildName,                               \
                                     SearchName)                              \
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
                                                                              \
  boost::shared_ptr<ChildClass>                                               \
  ParentClass::get##ChildName(const unsigned int index)                       \
  {                                                                           \
    return boost::const_pointer_cast<ChildClass>                              \
      (static_cast<const ParentClass &>(*this).get##ChildName(index));        \
  }                                                                           \
                                                                              \
  boost::shared_ptr<const ChildClass>                                         \
  ParentClass::get##ChildName(const unsigned int index) const                 \
  {                                                                           \
    if(index < m##ChildName##s.size()) {                                      \
      return m##ChildName##s[index];                                          \
    }                                                                         \
    return boost::shared_ptr<ChildClass>();                                   \
  }                                                                           \
                                                                              \
  boost::shared_ptr<ChildClass>                                               \
  ParentClass::get##ChildName(const std::string & SearchName)                 \
  {                                                                           \
    return boost::const_pointer_cast<ChildClass>                              \
      (static_cast<const ParentClass &>(*this).get##ChildName(SearchName));   \
  }                                                                           \
                                                                              \
  boost::shared_ptr<const ChildClass>                                         \
  ParentClass::get##ChildName(const std::string & SearchName) const           \
  {                                                                           \
    for(std::vector<boost::shared_ptr<ChildClass> >::const_iterator iter =    \
          m##ChildName##s.begin();                                            \
        iter != m##ChildName##s.end();                                        \
        ++iter) {                                                             \
      if((*iter)->get##SearchName().compare(SearchName) == 0) {               \
        return *iter;                                                         \
      }                                                                       \
    }                                                                         \
    return boost::shared_ptr<ChildClass>();                                   \
  }                                                                           \
                                                                              \
  unsigned int                                                                \
  ParentClass::getNumber##ChildName##s() const                                \
  {                                                                           \
    return m##ChildName##s.size();                                            \
  }                                                                           \
                                                                              \
  void                                                                        \
  ParentClass::insert(const boost::shared_ptr<ChildClass> ChildName)          \
  {                                                                           \
    m##ChildName##s.push_back(ChildName);                                     \
  }                                                                           \
                                                                              \
  void                                                                        \
  ParentClass::remove##ChildName(const unsigned int index)                    \
  {                                                                           \
    if(index < m##ChildName##s.size()) {                                      \
      m##ChildName##s.erase(m##ChildName##s.begin() + index);                 \
    }                                                                         \
  }                                                                           \
                                                                              \
  void                                                                        \
  ParentClass::remove##ChildName(const std::string & SearchName)              \
  {                                                                           \
    for(std::vector<boost::shared_ptr<ChildClass> >::iterator iter =          \
          m##ChildName##s.begin();                                            \
        iter != m##ChildName##s.end();                                        \
        ++iter) {                                                             \
        if((*iter)->get##SearchName().compare(SearchName) == 0) {             \
          m##ChildName##s.erase(iter);                                        \
          return;                                                             \
        }                                                                     \
    }                                                                         \
  }
160

161
/**
162 163
 * @brief Base class of any object that is able to be added to an Xdmf
 * structure.
164
 *
165 166 167
 * XdmfItem is an abstract base class. An XdmfItem is a structure that
 * can be visited and traversed by an XdmfVisitor and have its
 * contents written to an Xdmf file.
168
 */
169
class XDMFCORE_EXPORT XdmfItem : public Loki::BaseVisitable<void> {
170

171 172 173 174 175 176 177 178 179
 public:

  virtual ~XdmfItem() = 0;

  LOKI_DEFINE_VISITABLE_BASE();
  XDMF_CHILDREN(XdmfInformation, Information, Key);
  friend class XdmfCoreReader;

  /**
180 181
   * Get the tag for this item.  This is equivalent to tags in XML
   * parlance.
182 183 184 185 186 187
   *
   * @return the tag for this XdmfItem.
   */
  virtual std::string getItemTag() const = 0;

  /**
188 189
   * Get the key/value property pairs for this item. These are
   * equivalent to attributes in XML parlance.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
   *
   * @return a map of key/value properties associated with this XdmfItem.
   */
  virtual std::map<std::string, std::string> getItemProperties() const = 0;

  /**
   * Traverse this item by passing the visitor to child items.
   *
   * @param visitor the visitor to pass to child items.
   */
  virtual void traverse(const boost::shared_ptr<XdmfBaseVisitor> visitor);

 protected:

  XdmfItem();

  /**
207 208 209
   * Populates an item using a map of key/value property pairs and a
   * vector of its child items. This is used to support generic
   * reading of XdmfItems from disk.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
   *
   * @param itemProperties a map of key/value properties associated with
   * this item.
   * @param childItems a vector of child items to be added to this item.
   * @param reader the current XdmfCoreReader being used to populate Xdmf
   * structures.
   */
  virtual void
  populateItem(const std::map<std::string, std::string> & itemProperties,
               std::vector<boost::shared_ptr<XdmfItem > > & childItems,
               const XdmfCoreReader * const reader);

 private:

  XdmfItem(const XdmfItem &);  // Not implemented.
  void operator=(const XdmfItem &);  // Not implemented.
226

227 228
};

229
#ifdef _WIN32
230 231 232 233 234 235 236 237 238
XDMFCORE_TEMPLATE template class XDMFCORE_EXPORT
std::allocator<boost::shared_ptr<XdmfInformation> >;
XDMFCORE_TEMPLATE template class XDMFCORE_EXPORT
std::vector<boost::shared_ptr<XdmfInformation>,
            std::allocator<boost::shared_ptr<XdmfInformation> > >;
XDMFCORE_TEMPLATE template class XDMFCORE_EXPORT
boost::shared_ptr<Loki::BaseVisitor>;
XDMFCORE_TEMPLATE template class XDMFCORE_EXPORT
Loki::BaseVisitable<void, false>;
239 240
#endif

241
#endif /* XDMFITEM_HPP_ */