SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
ModelEntityItem.h
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 // .NAME ModelEntityItem.h - Hold a model entity as an attribute value.
11 // .SECTION Description
12 // An attribute whose value is a model entity (such as an edge or face).
13 //
14 // While the actual value stored is a EntityRef, only the UUID of the model entity
15 // is saved when the attribute is serialized.
16 // Then, when deserialized, the attribute resource's refModelManager()
17 // is used to recreate the entityref.
18 // (Thus the UUID is assumed to be present in the model manager referenced by
19 // the attribute resource. See smtk::attrib::Resource::refModelManager().)
20 // If the entity is not present, the returned smtk::model::EntityRef
21 // instances will be invalid and no type checking of attribute values
22 // can be performed.
23 //
24 // .SECTION See Also
25 
26 #ifndef smtk_attribute_ModelEntityItem_h
27 #define smtk_attribute_ModelEntityItem_h
28 
29 #include "smtk/CoreExports.h"
30 #include "smtk/attribute/ComponentItem.h"
31 #include "smtk/model/EntityRef.h"
32 
33 namespace smtk
34 {
35 namespace common
36 {
37 class UUID;
38 }
39 namespace attribute
40 {
41 
42 class Attribute;
43 class ModelEntityItemDefinition;
44 class SMTKCORE_EXPORT ModelEntityItem : public ComponentItem
45 {
46  friend class ModelEntityItemDefinition;
47 
48 public:
50  ~ModelEntityItem() override;
51 
54 
55  Item::Type type() const override;
56 
57  smtk::model::EntityRef value(std::size_t element = 0) const;
58  bool setValue(const smtk::model::EntityRef& val);
59  bool setValue(std::size_t element, const smtk::model::EntityRef& val);
60 
61  template<typename I>
62  bool setValues(I vbegin, I vend, std::size_t offset = 0);
63  template<typename I>
64  bool appendValues(I vbegin, I vend);
65 
66  bool appendValue(const smtk::model::EntityRef& val);
67 
68  bool contains(const smtk::model::EntityRef& entity) const;
69 
70  std::ptrdiff_t find(const smtk::model::EntityRef& entity) const;
71 
72  using Superclass::valueAsString;
73 
74 protected:
75  friend class Definition;
76 
77  ModelEntityItem(Attribute* owningAttribute, int itemPosition);
78  ModelEntityItem(Item* owningItem, int myPosition, int mySubGroupPosition);
79 };
80 
81 template<typename I>
82 bool ModelEntityItem::setValues(I vbegin, I vend, std::size_t offset)
83 {
84  bool ok = false;
85  std::size_t num = vend - vbegin + offset;
86  if (this->setNumberOfValues(num))
87  {
88  ok = true;
89  std::size_t i = 0;
90  for (I it = vbegin; it != vend; ++it, ++i)
91  {
92  if (!this->setValue(offset + i, *it))
93  {
94  ok = false;
95  break;
96  }
97  }
98  }
99  // Enable or disable the item if it is optional.
100  if (ok)
101  {
102  this->setIsEnabled(num > 0);
103  }
104  return ok;
105 }
106 
107 template<typename I>
108 bool ModelEntityItem::appendValues(I vbegin, I vend)
109 {
110  return this->setValues(vbegin, vend, this->numberOfValues());
111 }
112 
113 } // namespace attribute
114 } // namespace smtk
115 
116 #endif /* smtk_attribute_ModelEntityItem_h */
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::attribute::Item::Type
Type
Definition: Item.h:52
smtk::attribute::Definition
Definition: Definition.h:45
smtk::attribute::ModelEntityItemDefinition
Definition: ModelEntityItemDefinition.h:26
smtk::attribute::Item::setIsEnabled
void setIsEnabled(bool isEnabledValue)
Set the instance's local enabled state.
Definition: Item.h:181
smtk::attribute::ComponentItem::setValue
bool setValue(ComponentPtr value)
Set the i-th value as a component.
Definition: ComponentItem.h:64
smtk::common::UUID
Definition: UUID.h:38
EntityRef.h
smtk::attribute::ReferenceItem::setNumberOfValues
bool setNumberOfValues(std::size_t newSize)
Set the number of entities to be associated with this item (returns true if permitted).
Definition: ReferenceItem.cxx:317
smtk::attribute::Item
Definition: Item.h:43
smtk::attribute::ComponentItem::appendValue
bool appendValue(ComponentPtr value, bool allowDuplicates=true)
Append a value to the item if possible.
Definition: ComponentItem.h:72
smtkTypeMacro
#define smtkTypeMacro(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:51
smtk::attribute::ModelEntityItem::setValue
bool setValue(const smtk::model::EntityRef &val)
Set the entity stored with this item.
Definition: ModelEntityItem.cxx:54
smtk::attribute::ComponentItem
Hold resource components as an attribute value.
Definition: ComponentItem.h:43
smtk::attribute::ModelEntityItem
Definition: ModelEntityItem.h:44
smtk::attribute::ReferenceItem::numberOfValues
std::size_t numberOfValues() const
Return the size of the item (number of entities associated with the item).
Definition: ReferenceItem.cxx:312
smtk::attribute::Attribute
Represent a (possibly composite) value according to a definition.
Definition: Attribute.h:49
smtk::model::EntityRef
A lightweight entityref pointing to a model entity's resource.
Definition: EntityRef.h:112