XdmfAttribute.cpp 8.26 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 : XdmfAttribute.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 27
#include "XdmfAttributeCenter.hpp"
#include "XdmfAttributeType.hpp"
28
#include "XdmfError.hpp"
29

30
shared_ptr<XdmfAttribute>
31
XdmfAttribute::New()
32
{
33
  shared_ptr<XdmfAttribute> p(new XdmfAttribute());
34
  return p;
35 36
}

37
XdmfAttribute::XdmfAttribute() :
38 39 40
  mCenter(XdmfAttributeCenter::Grid()),
  mName(""),
  mType(XdmfAttributeType::NoAttributeType())
41 42 43 44 45 46 47
{
}

XdmfAttribute::~XdmfAttribute()
{
}

48
const std::string XdmfAttribute::ItemTag = "Attribute";
49

50
shared_ptr<const XdmfAttributeCenter>
51
XdmfAttribute::getCenter() const
52
{
53
  return mCenter;
54 55
}

56 57
std::map<std::string, std::string>
XdmfAttribute::getItemProperties() const
58
{
59
  std::map<std::string, std::string> attributeProperties;
60
  attributeProperties.insert(std::make_pair("Name", mName));
61 62 63
  mType->getProperties(attributeProperties);
  mCenter->getProperties(attributeProperties);
  return attributeProperties;
64 65
}

66 67
std::string
XdmfAttribute::getItemTag() const
68
{
69
  return ItemTag;
70 71
}

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

78
shared_ptr<const XdmfAttributeType>
79
XdmfAttribute::getType() const
80
{
81
  return mType;
82 83
}

84 85
void
XdmfAttribute::populateItem(const std::map<std::string, std::string> & itemProperties,
86
                            const std::vector<shared_ptr<XdmfItem> > & childItems,
87
                            const XdmfCoreReader * const reader)
88
{
89 90 91 92 93 94 95 96
  XdmfItem::populateItem(itemProperties, childItems, reader);

  std::map<std::string, std::string>::const_iterator name =
    itemProperties.find("Name");
  if(name != itemProperties.end()) {
    mName = name->second;
  }
  else {
97 98 99
    XdmfError::message(XdmfError::FATAL,
                       "'Name' not found in itemProperties in "
                       "XdmfAttribute::populateItem");
100 101 102 103
  }

  mCenter = XdmfAttributeCenter::New(itemProperties);
  mType = XdmfAttributeType::New(itemProperties);
104
  for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter = 
105 106 107
        childItems.begin();
      iter != childItems.end();
      ++iter) {
108
    if(shared_ptr<XdmfArray> array = shared_dynamic_cast<XdmfArray>(*iter)) {
109 110 111 112 113
      this->swap(array);
      if (array->getReference()) {
        this->setReference(array->getReference());
        this->setReadMode(XdmfArray::Reference);
      }
114
      break;
115 116
    }
  }
117 118
}

119
void
120
XdmfAttribute::setCenter(const shared_ptr<const XdmfAttributeCenter> center)
121
{
122
  mCenter = center;
123
  this->setIsChanged(true);
124 125
}

126 127
void
XdmfAttribute::setName(const std::string & name)
128
{
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
129
  mName = name;
130
  this->setIsChanged(true);
131 132
}

133
void
134
XdmfAttribute::setType(const shared_ptr<const XdmfAttributeType> type)
135
{
136
  mType = type;
137 138 139 140 141 142 143
  this->setIsChanged(true);
}

// C Wrappers

XDMFATTRIBUTE * XdmfAttributeNew()
{
144 145
  shared_ptr<XdmfAttribute> * p = new shared_ptr<XdmfAttribute>(XdmfAttribute::New());
  return (XDMFATTRIBUTE *) p;
146 147 148 149
}

int XdmfAttributeGetCenter(XDMFATTRIBUTE * attribute)
{
150 151 152
  shared_ptr<XdmfAttribute> & refAttribute = *(shared_ptr<XdmfAttribute> *)(attribute);
  const shared_ptr<const XdmfAttributeCenter> attributeCenter = refAttribute->getCenter();
  if (attributeCenter == XdmfAttributeCenter::Grid()) {
153 154
    return XDMF_ATTRIBUTE_CENTER_GRID;
  }
155
  else if (attributeCenter == XdmfAttributeCenter::Cell()) {
156 157
    return XDMF_ATTRIBUTE_CENTER_CELL;
  }
158
  else if (attributeCenter == XdmfAttributeCenter::Face()) {
159 160
    return XDMF_ATTRIBUTE_CENTER_FACE;
  }
161
  else if (attributeCenter == XdmfAttributeCenter::Edge()) {
162 163
    return XDMF_ATTRIBUTE_CENTER_EDGE;
  }
164
  else if (attributeCenter == XdmfAttributeCenter::Node()) {
165 166 167 168 169 170 171 172 173
    return XDMF_ATTRIBUTE_CENTER_NODE;
  }
  else {
    return -1;
  }
}

int XdmfAttributeGetType(XDMFATTRIBUTE * attribute)
{
174 175 176
  shared_ptr<XdmfAttribute> & refAttribute = *(shared_ptr<XdmfAttribute> *)(attribute);
  const shared_ptr<const XdmfAttributeType> attributeType = refAttribute->getType();
  if (attributeType == XdmfAttributeType::Scalar()) {
177 178
    return XDMF_ATTRIBUTE_TYPE_SCALAR;
  }
179
  else if (attributeType == XdmfAttributeType::Vector()) {
180 181
    return XDMF_ATTRIBUTE_TYPE_VECTOR;
  }
182
  else if (attributeType == XdmfAttributeType::Tensor()) {
183 184
    return XDMF_ATTRIBUTE_TYPE_TENSOR;
  }
185
  else if (attributeType == XdmfAttributeType::Matrix()) {
186 187
    return XDMF_ATTRIBUTE_TYPE_MATRIX;
  }
188
  else if (attributeType == XdmfAttributeType::Tensor6()) {
189 190
    return XDMF_ATTRIBUTE_TYPE_TENSOR6;
  }
191
  else if (attributeType == XdmfAttributeType::GlobalId()) {
192 193
    return XDMF_ATTRIBUTE_TYPE_GLOBALID;
  }
194
  else if (attributeType == XdmfAttributeType::NoAttributeType()) {
195 196 197 198 199 200 201 202 203 204
    return XDMF_ATTRIBUTE_TYPE_NOTYPE;
  }
  else {
    return -1;
  }
}

void XdmfAttributeSetCenter(XDMFATTRIBUTE * attribute, int center, int * status)
{
  XDMF_ERROR_WRAP_START(status)
205
  shared_ptr<XdmfAttribute> & refAttribute = *(shared_ptr<XdmfAttribute> *)(attribute);
206
  switch(center) {
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  case XDMF_ATTRIBUTE_CENTER_GRID:
    refAttribute->setCenter(XdmfAttributeCenter::Grid());
    break;
  case XDMF_ATTRIBUTE_CENTER_CELL:
    refAttribute->setCenter(XdmfAttributeCenter::Cell());
    break;
  case XDMF_ATTRIBUTE_CENTER_FACE:
    refAttribute->setCenter(XdmfAttributeCenter::Face());
    break;
  case XDMF_ATTRIBUTE_CENTER_EDGE:
    refAttribute->setCenter(XdmfAttributeCenter::Edge());
    break;
  case XDMF_ATTRIBUTE_CENTER_NODE:
    refAttribute->setCenter(XdmfAttributeCenter::Node());
    break;
  default:
    XdmfError::message(XdmfError::FATAL,
		       "Error: Invalid Attribute Center: Code " + center);
    break;
226 227
  }
  XDMF_ERROR_WRAP_END(status)
228
}
229 230 231 232

void XdmfAttributeSetType(XDMFATTRIBUTE * attribute, int type, int * status)
{
  XDMF_ERROR_WRAP_START(status)
233
    shared_ptr<XdmfAttribute> & refAttribute = *(shared_ptr<XdmfAttribute> *)(attribute);
234
  switch(type) {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  case XDMF_ATTRIBUTE_TYPE_SCALAR:
    refAttribute->setType(XdmfAttributeType::Scalar());
    break;
  case XDMF_ATTRIBUTE_TYPE_VECTOR:
    refAttribute->setType(XdmfAttributeType::Vector());
    break;
  case XDMF_ATTRIBUTE_TYPE_TENSOR:
    refAttribute->setType(XdmfAttributeType::Tensor());
    break;
  case XDMF_ATTRIBUTE_TYPE_MATRIX:
    refAttribute->setType(XdmfAttributeType::Matrix());
    break;
  case XDMF_ATTRIBUTE_TYPE_TENSOR6:
    refAttribute->setType(XdmfAttributeType::Tensor6());
    break;
  case XDMF_ATTRIBUTE_TYPE_GLOBALID:
    refAttribute->setType(XdmfAttributeType::GlobalId());
    break;
  case XDMF_ATTRIBUTE_TYPE_NOTYPE:
    refAttribute->setType(XdmfAttributeType::NoAttributeType());
    break;
  default:
    XdmfError::message(XdmfError::FATAL,
		       "Error: Invalid Attribute Type: Code " + type);
    break;
260 261 262 263 264 265
  }
  XDMF_ERROR_WRAP_END(status)
}

XDMF_ITEM_C_CHILD_WRAPPER(XdmfAttribute, XDMFATTRIBUTE)
XDMF_ARRAY_C_CHILD_WRAPPER(XdmfAttribute, XDMFATTRIBUTE)