XdmfItem.hpp 27.7 KB
Newer Older
Kenneth Leiter's avatar
Kenneth Leiter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfItem.hpp                                                        */
/*                                                                           */
/*  Author:                                                                  */
/*     Kenneth Leiter                                                        */
/*     kenneth.leiter@arl.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2011 US Army Research Laboratory                          */
/*     All Rights Reserved                                                   */
/*     See Copyright.txt for details                                         */
/*                                                                           */
/*     This software is distributed WITHOUT ANY WARRANTY; without            */
/*     even the implied warranty of MERCHANTABILITY or FITNESS               */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice             */
/*     for more information.                                                 */
/*                                                                           */
/*****************************************************************************/

24 25 26
#ifndef XDMFITEM_HPP_
#define XDMFITEM_HPP_

27 28 29 30 31 32
// C Compatible Includes
#include "XdmfCore.hpp"
#include "XdmfVisitor.hpp"

#ifdef __cplusplus

33
// Forward Declarations
34
class XdmfCoreReader;
35
class XdmfInformation;
36 37 38
class XdmfVisitor;

// Includes
39
#include <loki/Visitor.h>
40 41
#include <libxml/xmlexports.h>
#include <libxml/tree.h>
42
#include <map>
43
#include <set>
44
#include <string>
45
#include <vector>
46
#include "XdmfSharedPtr.hpp"
47

48 49
// Macro that allows children XdmfItems to be attached to a parent XdmfItem.
// -- For Header File
50
#define XDMF_CHILDREN(ParentClass, ChildClass, ChildName, SearchName)         \
51 52 53
                                                                              \
public:                                                                       \
                                                                              \
54 55 56 57 58 59 60 61 62 63 64 65 66 67
  /** Get a ChildClass attached to this item by index.<br>
      Example of use:<br>
      C++<br>
      //Assume that exampleItem is a shared pointer to the ParentClass object<br>
      //Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      unsigned int getIndex = 0;<br>
      shared_ptr<XdmfInformation> exampleChild = exampleItem->getInformation(getIndex);<br>
      Python<br>
      '''<br>
      Assume that exampleItem is a shared pointer to the ParentClass object<br>
      Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      '''<br>
      getIndex = 0<br>
      exampleChild = exampleItem.getInformation(getIndex)<br>
68 69 70
      @return requested ChildClass. If no ChildClass##s exist at the index,
      a NULL pointer is returned.
  */                                                                          \
71
  virtual shared_ptr<ChildClass>                                              \
72 73
  get##ChildName(const unsigned int index);                                   \
                                                                              \
74 75 76 77 78 79 80
  /** Get a ChildClass attached to this item by index (const version).<br>
      Example of use:<br>
      C++<br>
      //Assume that exampleItem is a shared pointer to the ParentClass object<br>
      //Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      unsigned int getIndex = 0;<br>
      shared_ptr<const XdmfInformation> exampleChildConst = exampleItem->getInformation(getIndex);<br>
81
      Python: does not support a constant version of this function
82 83 84 85
      @param index of the ChildClass to retrieve.
      @return requested ChildClass. If no ChildClass##s exist at the index, a
      NULL pointer is returned.
  */                                                                          \
86
  virtual shared_ptr<const ChildClass>                                        \
87 88
  get##ChildName(const unsigned int index) const;                             \
                                                                              \
89 90 91 92 93 94 95 96 97 98 99 100 101 102
  /** Get a ChildClass attached to this item by SearchName.<br>
      Example of use:<br>
      C++<br>
      //Assume that exampleItem is a shared pointer to the ParentClass object<br>
      //Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      std::string findingInfo = "Find this";<br>
      shared_ptr<XdmfInformation> exampleStringChild = exampleItem->getInformation(findingInfo);<br>
      Python<br>
      '''<br>
      Assume that exampleItem is a shared pointer to the ParentClass object<br>
      Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      '''<br>
      findingInfo = "Find this"<br>
      exampleStringChild = exampleItem.getInformation(findingInfo)<br>
103 104 105 106
      @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.
  */                                                                          \
107
  virtual shared_ptr<ChildClass>                                              \
108 109
  get##ChildName(const std::string & SearchName);                             \
                                                                              \
110 111 112 113 114 115 116
  /** Get a ChildClass attached to this item by SearchName (const version).<br>
      Example of use:<br>
      C++<br>
      //Assume that exampleItem is a shared pointer to the ParentClass object<br>
      //Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      std::string findingInfo = "Find this";<br>
      shared_ptr<const XdmfInformation> exampleStringChildConst = exampleItem->getInformation(findingInfo);<br>
117
      Python: does not support a constant version of this function
118 119 120 121
      @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.
  */                                                                          \
122
  virtual shared_ptr<const ChildClass>                                        \
123 124
  get##ChildName(const std::string & SearchName) const;                       \
                                                                              \
125 126 127 128 129 130 131 132 133 134 135 136
  /** Get the number of ChildClass##s attached to this item.<br>
      Example of use:<br>
      C++<br>
      //Assume that exampleItem is a shared pointer to the ParentClass object<br>
      //Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      unsigned int exampleSize = exampleItem->getNumberInformations();<br>
      Python<br>
      '''<br>
      Assume that exampleItem is a shared pointer to the ParentClass object<br>
      Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      '''<br>
      exampleSize = exampleItem.getNumberInformations()<br>
137 138 139 140
      @return number of ChildClass##s attached to this item.
  */                                                                          \
  virtual unsigned int getNumber##ChildName##s() const;                       \
                                                                              \
141 142 143 144 145 146 147 148 149 150
  /** Insert a ChildClass into to this item.<br>
      Example of use:<br>
      C++<br>
      shared_ptr<XdmfInformation> exampleItem = XdmfInformation::New("Parent", "This is a parent information");<br>
      shared_ptr<XdmfInformation> addChild = XdmfInformation::New("Child", "This is a child information");<br>
      exampleItem->insert(addChild);<br>
      Python<br>
      exampleItem = XdmfInformation.New("Parent", "This is a parent information")<br>
      addChild = XdmfInformation.New("Child", "This is a child information")<br>
      exampleItem.insert(addChild)<br>
151 152
      @param ChildName to attach to this item.
  */                                                                          \
153
  virtual void insert(const shared_ptr<ChildClass> ChildName);                \
154
                                                                              \
155
  /** Remove a ChildClass from this item by index. If no object exists
156 157 158 159 160 161 162 163 164 165 166 167
      at the index, nothing is removed.<br>
      Example of use:<br>
      C++<br>
      //Assume that exampleItem is a shared pointer to the ParentClass object<br>
      //Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      unsigned int removeIndex = 0;<br>
      exampleItem->removeInformation(removeIndex);<br>
      Python<br>
      '''<br>
      Assume that exampleItem is a shared pointer to the ParentClass object<br>
      Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      '''<br>
168
      removeIndex = 0<br>
169
      exampleItem.removeInformation(removeIndex)
170 171 172 173 174
      @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
175 176 177 178 179 180 181 182 183 184 185 186 187 188
      have the correct SearchName, nothing is removed.<br>
      Example of use:<br>
      C++<br>
      //Assume that exampleItem is a shared pointer to the ParentClass object<br>
      //Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      std::string removeInfo = "Remove this";<br>
      exampleItem->removeInformation(removeInfo);<br>
      Python<br>
      '''<br>
      Assume that exampleItem is a shared pointer to the ParentClass object<br>
      Using an XdmfInformation as an example because all XdmfItems have XdmfInformation as a child class<br>
      '''<br>
      removeInfo = "Remove this"<br>
      exampleItem.removeInformation(removeInfo)<br>
189 190 191 192 193 194
      @param SearchName of the ChildClass to remove.
  */                                                                          \
  virtual void remove##ChildName(const std::string & SearchName);             \
                                                                              \
protected :                                                                   \
                                                                              \
195
  std::vector<shared_ptr<ChildClass> > m##ChildName##s;                       \
196 197 198 199 200 201
                                                                              \
public :

// Macro that allows children XdmfItems to be attached to a parent XdmfItem.
// -- For Implementation File
#define XDMF_CHILDREN_IMPLEMENTATION(ParentClass,                             \
202 203 204
                                     ChildClass,                              \
                                     ChildName,                               \
                                     SearchName)                              \
205
                                                                              \
206
  shared_ptr<ChildClass>                                                      \
207 208 209 210 211 212
  ParentClass::get##ChildName(const unsigned int index)                       \
  {                                                                           \
    return boost::const_pointer_cast<ChildClass>                              \
      (static_cast<const ParentClass &>(*this).get##ChildName(index));        \
  }                                                                           \
                                                                              \
213
  shared_ptr<const ChildClass>                                                \
214 215 216 217 218
  ParentClass::get##ChildName(const unsigned int index) const                 \
  {                                                                           \
    if(index < m##ChildName##s.size()) {                                      \
      return m##ChildName##s[index];                                          \
    }                                                                         \
219
    return shared_ptr<ChildClass>();                                          \
220 221
  }                                                                           \
                                                                              \
222
  shared_ptr<ChildClass>                                                      \
223 224 225 226 227 228
  ParentClass::get##ChildName(const std::string & SearchName)                 \
  {                                                                           \
    return boost::const_pointer_cast<ChildClass>                              \
      (static_cast<const ParentClass &>(*this).get##ChildName(SearchName));   \
  }                                                                           \
                                                                              \
229
  shared_ptr<const ChildClass>                                                \
230 231
  ParentClass::get##ChildName(const std::string & SearchName) const           \
  {                                                                           \
232
    for(std::vector<shared_ptr<ChildClass> >::const_iterator iter =           \
233 234 235 236 237 238 239
          m##ChildName##s.begin();                                            \
        iter != m##ChildName##s.end();                                        \
        ++iter) {                                                             \
      if((*iter)->get##SearchName().compare(SearchName) == 0) {               \
        return *iter;                                                         \
      }                                                                       \
    }                                                                         \
240
    return shared_ptr<ChildClass>();                                          \
241 242 243 244 245 246 247 248 249
  }                                                                           \
                                                                              \
  unsigned int                                                                \
  ParentClass::getNumber##ChildName##s() const                                \
  {                                                                           \
    return m##ChildName##s.size();                                            \
  }                                                                           \
                                                                              \
  void                                                                        \
250
  ParentClass::insert(const shared_ptr<ChildClass> ChildName)                 \
251 252
  {                                                                           \
    m##ChildName##s.push_back(ChildName);                                     \
253
    this->setIsChanged(true);                                                 \
254 255 256 257 258 259 260 261
  }                                                                           \
                                                                              \
  void                                                                        \
  ParentClass::remove##ChildName(const unsigned int index)                    \
  {                                                                           \
    if(index < m##ChildName##s.size()) {                                      \
      m##ChildName##s.erase(m##ChildName##s.begin() + index);                 \
    }                                                                         \
262
    this->setIsChanged(true);                                                 \
263 264 265 266 267
  }                                                                           \
                                                                              \
  void                                                                        \
  ParentClass::remove##ChildName(const std::string & SearchName)              \
  {                                                                           \
268
    for(std::vector<shared_ptr<ChildClass> >::iterator iter =                 \
269 270 271 272 273 274 275 276
          m##ChildName##s.begin();                                            \
        iter != m##ChildName##s.end();                                        \
        ++iter) {                                                             \
        if((*iter)->get##SearchName().compare(SearchName) == 0) {             \
          m##ChildName##s.erase(iter);                                        \
          return;                                                             \
        }                                                                     \
    }                                                                         \
277
    this->setIsChanged(true);                                                 \
278
  }
279

280
/**
281 282
 * @brief Base class of any object that is able to be added to an Xdmf
 * structure.
283
 *
284 285 286
 * 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.
287
 */
288
class XDMFCORE_EXPORT XdmfItem : public Loki::BaseVisitable<void> {
289

290
public:
291

292
  virtual ~XdmfItem() = 0;
293

294 295
  LOKI_DEFINE_VISITABLE_BASE()
  XDMF_CHILDREN(XdmfItem, XdmfInformation, Information, Key)
296
  friend class XdmfCoreReader;
297 298 299
  friend class XdmfWriter;
  friend class XdmfHeavyDataWriter;
  friend class XdmfHDF5Writer;
300

301
  /**
302 303
   * Get the tag for this item.  This is equivalent to tags in XML
   * parlance.
304
   *
305 306 307 308
   * Example of use:
   *
   * C++
   *
309
   * @dontinclude ExampleXdmfItem.cpp
310 311 312 313
   * @skipline //#initialization
   * @until //#initialization
   * @skipline //#getItemTag
   * @until //#getItemTag
314 315 316
   *
   * Python
   *
317
   * @dontinclude XdmfExampleItem.py
318 319 320 321
   * @skipline #//initialization
   * @until #//initialization
   * @skipline #//getItemTag
   * @until #//getItemTag
322
   *
323
   * @return    The tag for this XdmfItem.
324 325
   */
  virtual std::string getItemTag() const = 0;
326

327
  /**
328 329
   * Get the key/value property pairs for this item. These are
   * equivalent to attributes in XML parlance.
330
   *
331 332 333 334
   * Example of use:
   *
   * C++
   *
335
   * @dontinclude ExampleXdmfItem.cpp
336 337 338 339
   * @skipline //#initialization
   * @until //#initialization
   * @skipline //#getItemProperties
   * @until //#getItemProperties
340 341 342
   *
   * Python
   *
343
   * @dontinclude XdmfExampleItem.py
344 345 346 347
   * @skipline #//initialization
   * @until #//initialization
   * @skipline #//getItemProperties
   * @until #//getItemProperties
348
   *
349
   * @return    A map of key/value properties associated with this XdmfItem.
350 351
   */
  virtual std::map<std::string, std::string> getItemProperties() const = 0;
352

353 354 355
  /**
   * Traverse this item by passing the visitor to child items.
   *
356 357 358 359
   * Example of use:
   *
   * C++
   *
360
   * @dontinclude ExampleXdmfItem.cpp
361 362 363 364
   * @skipline //#initialization
   * @until //#initialization
   * @skipline //#traverse
   * @until //#traverse
365 366 367
   *
   * Python
   *
368
   * @dontinclude XdmfExampleItem.py
369 370 371 372
   * @skipline #//initialization
   * @until #//initialization
   * @skipline #//traverse
   * @until #//traverse
373
   *
374
   * @param     visitor         The visitor to pass to child items.
375
   */
376
  virtual void traverse(const shared_ptr<XdmfBaseVisitor> visitor);
377

378 379
  XdmfItem(XdmfItem &);

380
protected:
381

382
  XdmfItem();
383

384
  /**
385 386 387
   * 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.
388 389 390 391 392 393 394 395 396
   *
   * @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,
397
               const std::vector<shared_ptr<XdmfItem > > & childItems,
398
               const XdmfCoreReader * const reader);
399

400 401 402 403 404 405 406 407 408 409
  bool
  getIsChanged();

  void
  setIsChanged(bool status);

  std::set<XdmfItem *> mParents;

  bool mIsChanged;

410
private:
411

412 413
  XdmfItem(const XdmfItem &);  // Not implemented.
  void operator=(const XdmfItem &);  // Not implemented.
414

415 416
};

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
#endif

#ifdef __cplusplus
extern "C" {
#endif

// C wrappers go here

struct XDMFITEM; // Simply as a typedef to ensure correct typing
typedef struct XDMFITEM XDMFITEM;

#ifndef XDMFINFORMATIONCDEFINE
#define XDMFINFORMATIONCDEFINE
struct XDMFINFORMATION; // Simply as a typedef to ensure correct typing
typedef struct XDMFINFORMATION XDMFINFORMATION;
#endif

XDMFCORE_EXPORT void XdmfItemAccept(XDMFITEM * item, XDMFVISITOR * visitor, int * status);

XDMFCORE_EXPORT void XdmfItemFree(void * item);

XDMFCORE_EXPORT XDMFINFORMATION * XdmfItemGetInformation(XDMFITEM * item, unsigned int index);

XDMFCORE_EXPORT XDMFINFORMATION * XdmfItemGetInformationByKey(XDMFITEM * item, char * key);

XDMFCORE_EXPORT unsigned int XdmfItemGetNumberInformations(XDMFITEM * item);

XDMFCORE_EXPORT void XdmfItemInsertInformation(XDMFITEM * item, XDMFINFORMATION * information, int passControl);

XDMFCORE_EXPORT void XdmfItemRemoveInformation(XDMFITEM * item, unsigned int index);

XDMFCORE_EXPORT void XdmfItemRemoveInformationByKey(XDMFITEM * item, char * key);

XDMFCORE_EXPORT char * XdmfItemGetItemTag(XDMFITEM * item);

#define XDMF_ITEM_C_CHILD_DECLARE(ClassName, CClassName, Level)                                                       \
                                                                                                                      \
Level##_EXPORT void ClassName##Accept ( CClassName * item, XDMFVISITOR * visitor, int * status);                      \
XDMFCORE_EXPORT void ClassName##Free(void * item);                                                                    \
Level##_EXPORT XDMFINFORMATION * ClassName##GetInformation( CClassName * item, unsigned int index);                   \
Level##_EXPORT XDMFINFORMATION * ClassName##GetInformationByKey( CClassName * item, char * key);                      \
Level##_EXPORT unsigned int ClassName##GetNumberInformations( CClassName * item);                                     \
Level##_EXPORT void ClassName##InsertInformation( CClassName * item, XDMFINFORMATION * information, int passControl); \
Level##_EXPORT void ClassName##RemoveInformation( CClassName * item, unsigned int index);                             \
Level##_EXPORT void ClassName##RemoveInformationByKey( CClassName * item, char * key);                                \
Level##_EXPORT char * ClassName##GetItemTag( CClassName * item);


#define XDMF_ITEM_C_CHILD_WRAPPER(ClassName, CClassName)                                                    \
void ClassName##Accept( CClassName * item, XDMFVISITOR * visitor, int * status)                             \
{                                                                                                           \
  XdmfItemAccept(((XDMFITEM *)item), visitor, status);                                                      \
}                                                                                                           \
                                                                                                            \
void ClassName##Free(void * item)                                                                           \
{                                                                                                           \
  XdmfItemFree(item);                                                                                       \
}                                                                                                           \
                                                                                                            \
XDMFINFORMATION * ClassName##GetInformation( CClassName * item, unsigned int index)                         \
{                                                                                                           \
  return XdmfItemGetInformation(((XDMFITEM *)item), index);                                                 \
}                                                                                                           \
                                                                                                            \
XDMFINFORMATION * ClassName##GetInformationByKey( CClassName * item, char * key)                            \
{                                                                                                           \
  return XdmfItemGetInformationByKey(((XDMFITEM *)item), key);                                              \
}                                                                                                           \
                                                                                                            \
unsigned int ClassName##GetNumberInformations( CClassName * item)                                           \
{                                                                                                           \
  return XdmfItemGetNumberInformations(((XDMFITEM *)item));                                                 \
}                                                                                                           \
                                                                                                            \
void ClassName##InsertInformation( CClassName * item, XDMFINFORMATION * information, int passControl)       \
{                                                                                                           \
  XdmfItemInsertInformation(((XDMFITEM *)item), information, passControl);                                  \
}                                                                                                           \
                                                                                                            \
void ClassName##RemoveInformation( CClassName * item, unsigned int index)                                   \
{                                                                                                           \
  XdmfItemRemoveInformation(((XDMFITEM *)item), index);                                                     \
}                                                                                                           \
                                                                                                            \
void ClassName##RemoveInformationByKey( CClassName * item, char * key)                                      \
{                                                                                                           \
  XdmfItemRemoveInformationByKey(((XDMFITEM *)item), key);                                                  \
}                                                                                                           \
                                                                                                            \
char * ClassName##GetItemTag( CClassName * item)                                                            \
{                                                                                                           \
  return XdmfItemGetItemTag(((XDMFITEM *)item));                                                            \
} 

#ifdef __cplusplus
}
#endif

515
#endif /* XDMFITEM_HPP_ */