Skip to content
Snippets Groups Projects
ElementData.h 5.15 KiB
Newer Older
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file ElementData.h */

#ifndef AMDIS_ELEMENTDATA_H
#define AMDIS_ELEMENTDATA_H

#include "Serializable.h"
#include "CreatorMap.h"

namespace AMDiS {

  const int ESTIMATABLE = 1;
  const int COARSENABLE = 2;
  const int PERIODIC = 3;
  const int ELEMENT_REGION = 4;
  const int SURFACE_REGION = 5;

  // ============================================================================
  // ===== class ElementData ====================================================
  // ============================================================================

  /** \brief
   * Base class for element data. To allow to assign arbitrary data to arbitrary
   * elements at run time, the decorator pattern in combination with the
   * chain-of-responsibility pattern is applied. So only data have to be managed 
   * at each element which are used for this element at this time.
   */
  class ElementData : public Serializable
  {
  public:
    /** \brief
     * constructor
     */
    ElementData(ElementData *decorated = NULL) 
      : decorated_(decorated)
    virtual ~ElementData() {}

    /** \brief
     * Refinement of parent to child1 and child2.
     */
    virtual bool refineElementData(Element* parent, 
				   Element* child1,
				   Element* child2,
				   int elType)
    {
      if (decorated_) {
	bool remove = 
	  decorated_->refineElementData(parent, child1, child2, elType);

	if (remove) {
	  ElementData *tmp = decorated_->decorated_;
	  delete decorated_;
	  decorated_ = tmp;
	}
      }
      return false;

    /** \brief
     *
     */
    virtual void coarsenElementData(Element* parent, 
				    Element* thisChild,
				    Element* otherChild,
				    int elTypeParent)
    {
      if(decorated_) {
	decorated_->coarsenElementData(parent, 
				       thisChild, 
				       otherChild, 
				       elTypeParent);
	delete decorated_;
	decorated_ = NULL;
      }

    /** \brief
     * Returns a copy of this ElementData object including all decorated data.
     */
    virtual ElementData *clone() const {
      if(decorated_) {
	return decorated_->clone();
      }
      return NULL;

    /** \brief
     * Returns the name of element data type.
     */
    virtual const int getTypeID() const = 0;

    /** \brief
     * Returns whether the ElemnetData object is of the type specified by 
     * typeName. Must return true, even if typeName describes a base class.
     */
    virtual bool isOfType(int typeID) const = 0;

    /** \brief
     * Implements Serializable::serialize().
     */
    virtual void serialize(std::ostream& out);

    /** \brief
     * Implements Serializable::deserialize().
     */
    virtual void deserialize(std::istream& in);

    /** \brief
     * Returns first element data in the chain which is of the spcified type.
     */
    inline ElementData *getElementData(int typeID) {
      if (this->isOfType(typeID)) {
	return this;
      } else {
	if (decorated_) {
	  return decorated_->getElementData(typeID);
	}
      }
      return NULL;

    inline ElementData *getDecorated(int typeID) { 
      if (decorated_) {
	return decorated_->getElementData(typeID);
      }
      return NULL;

    inline bool deleteDecorated(int typeID) {
      if(decorated_) {
	if(decorated_->isOfType(typeID)) {
	  ElementData *tmp = decorated_;
	  decorated_ = decorated_->decorated_;
	  delete tmp;
	  return true;
	} else {
	  return decorated_->deleteDecorated(typeID);
	}
      } 
      return false;
    }

    inline ElementData *getDecorated() { 
      return decorated_; 
    }
    inline void setDecorated(ElementData *d) {
      decorated_ = d;
    }

  protected:
    /** \brief
     * Pointer to next ElementData object in the chain of responsibility.
     */
    ElementData *decorated_;
  };

}

#endif