XdmfSet.cpp 6.93 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
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfSet.cpp                                                         */
/*                                                                           */
/*  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.                                                 */
/*                                                                           */
/*****************************************************************************/
23

24
#include <utility>
25
#include "XdmfAttribute.hpp"
26
#include "XdmfHDF5Controller.hpp"
27
#include "XdmfSet.hpp"
28
#include "XdmfSetType.hpp"
29
#include "XdmfError.hpp"
30

31 32
XDMF_CHILDREN_IMPLEMENTATION(XdmfSet, XdmfAttribute, Attribute, Name)

33
  shared_ptr<XdmfSet>
34
XdmfSet::New()
35
{
36
  shared_ptr<XdmfSet> p(new XdmfSet());
37
  return p;
38 39
}

40
XdmfSet::XdmfSet() :
41 42
  mName(""),
  mType(XdmfSetType::NoSetType())
43 44 45
{
}

46 47 48 49 50 51 52
XdmfSet::XdmfSet(XdmfSet & refSet) :
  XdmfArray(refSet),
  mName(refSet.mName),
  mType(refSet.mType)
{
}

53 54 55 56
XdmfSet::~XdmfSet()
{
}

57
const std::string XdmfSet::ItemTag = "Set";
58

59 60
std::map<std::string, std::string>
XdmfSet::getItemProperties() const
61
{
62
  std::map<std::string, std::string> setProperties;
63
  setProperties.insert(std::make_pair("Name", mName));
64 65
  mType->getProperties(setProperties);
  return setProperties;
66 67
}

68 69
std::string
XdmfSet::getItemTag() const
70
{
71
  return ItemTag;
72 73
}

74 75
std::string
XdmfSet::getName() const
76
{
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
77
  return mName;
78 79
}

80
shared_ptr<const XdmfSetType>
81
XdmfSet::getType() const
82
{
83
  return mType;
84 85
}

86 87
void
XdmfSet::populateItem(const std::map<std::string, std::string> & itemProperties,
88
                      const std::vector<shared_ptr<XdmfItem> > & childItems,
89
                      const XdmfCoreReader * const reader)
90
{
91 92 93 94 95 96 97
  XdmfItem::populateItem(itemProperties, childItems, reader);
  std::map<std::string, std::string>::const_iterator name =
    itemProperties.find("Name");
  if(name != itemProperties.end()) {
    mName = name->second;
  }
  mType = XdmfSetType::New(itemProperties);
98
  bool filled = false;
99
  for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
100 101 102
        childItems.begin();
      iter != childItems.end();
      ++iter) {
103 104
    if(shared_ptr<XdmfAttribute> attribute =
       shared_dynamic_cast<XdmfAttribute>(*iter)) {
105 106
      this->insert(attribute);
    }
107 108
    else if(shared_ptr<XdmfArray> array = 
            shared_dynamic_cast<XdmfArray>(*iter)) {
109
      if (!filled) {
110
        this->swap(array);
111 112
        filled = true;
      }
113 114 115 116
      if (array->getReference()) {
        this->setReference(array->getReference());
        this->setReadMode(XdmfArray::Reference);
      }
117 118 119
      // TODO: If multiple dataitems.
    }
  }
120 121
}

122 123
void
XdmfSet::setName(const std::string & name)
124
{
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
125
  mName = name;
126
  this->setIsChanged(true);
127 128
}

129
void
130
XdmfSet::setType(const shared_ptr<const XdmfSetType> type)
131
{
132
  mType = type;
133
  this->setIsChanged(true);
134
}
135

136
void
137
XdmfSet::traverse(const shared_ptr<XdmfBaseVisitor> visitor)
138
{
139
  XdmfItem::traverse(visitor);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  for (unsigned int i = 0; i < mAttributes.size(); ++i)
  {
    mAttributes[i]->accept(visitor);
  }
}

// C Wrappers

XDMFSET * XdmfSetNew()
{
  try
  {
    shared_ptr<XdmfSet> generatedSet = XdmfSet::New();
    return (XDMFSET*)((void *)(new XdmfSet(*generatedSet.get())));
  }
  catch (...)
  {
    shared_ptr<XdmfSet> generatedSet = XdmfSet::New();
    return (XDMFSET*)((void *)(new XdmfSet(*generatedSet.get())));
159
  }
160
}
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

XDMFATTRIBUTE * XdmfSetGetAttribute(XDMFSET * set, unsigned int index)
{
  return (XDMFATTRIBUTE *)((void *)(((XdmfSet *)(set))->getAttribute(index).get()));
}

XDMFATTRIBUTE * XdmfSetGetAttributeByName(XDMFSET * set, char * Name)
{
  return (XDMFATTRIBUTE *)((void *)(((XdmfSet *)(set))->getAttribute(Name).get()));
}

unsigned int XdmfSetGetNumberAttributes(XDMFSET * set)
{
  return ((XdmfSet *)(set))->getNumberAttributes();
}

int XdmfSetGetType(XDMFSET * set)
{
  shared_ptr<const XdmfSetType> checkType = ((XdmfSet *)set)->getType();

  if (checkType == XdmfSetType::NoSetType()) {
    return XDMF_SET_TYPE_NO_SET_TYPE;
  }
  else if (checkType == XdmfSetType::Node()) {
    return XDMF_SET_TYPE_NODE;
  }
  else if (checkType == XdmfSetType::Cell()) {
    return XDMF_SET_TYPE_CELL;
  }
  else if (checkType == XdmfSetType::Face()) {
    return XDMF_SET_TYPE_FACE;
  }
  else if (checkType == XdmfSetType::Edge()) {
    return XDMF_SET_TYPE_EDGE;
  }
  else {
    return -1;
  }
}

void XdmfSetInsertAttribute(XDMFSET * set, XDMFATTRIBUTE * Attribute, int passControl)
{
  if (passControl) {
    ((XdmfSet *)(set))->insert(shared_ptr<XdmfAttribute>((XdmfAttribute *)Attribute));
  }
  else {
    ((XdmfSet *)(set))->insert(shared_ptr<XdmfAttribute>((XdmfAttribute *)Attribute, XdmfNullDeleter()));
  }
}

void XdmfSetRemoveAttribute(XDMFSET * set, unsigned int index)
{
  ((XdmfSet *)(set))->removeAttribute(index);
}

void XdmfSetRemoveAttributeByName(XDMFSET * set, char * Name)
{
  ((XdmfSet *)(set))->removeAttribute(Name);
}

void XdmfSetSetType(XDMFSET * set, int type, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  shared_ptr<const XdmfSetType> newType = shared_ptr<const XdmfSetType>();
  switch (type) {
    case XDMF_SET_TYPE_NO_SET_TYPE:
      newType = XdmfSetType::NoSetType();
      break;
    case XDMF_SET_TYPE_NODE:
      newType = XdmfSetType::Node();
      break;
    case XDMF_SET_TYPE_CELL:
      newType = XdmfSetType::Cell();
      break;
    case XDMF_SET_TYPE_FACE:
      newType = XdmfSetType::Face();
      break;
    case XDMF_SET_TYPE_EDGE:
      newType = XdmfSetType::Edge();
      break;
    default:
      XdmfError::message(XdmfError::FATAL,
                         "Error: Invalid Set Type: Code " + type);
      break;
  }
  ((XdmfSet *)set)->setType(newType);
  XDMF_ERROR_WRAP_END(status)
}

XDMF_ITEM_C_CHILD_WRAPPER(XdmfSet, XDMFSET)
XDMF_ARRAY_C_CHILD_WRAPPER(XdmfSet, XDMFSET)