SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Item.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 Item.h -
11 // .SECTION Description
12 // .SECTION See Also
13 
14 #ifndef smtk_attribute_Item_h
15 #define smtk_attribute_Item_h
16 
17 #include "smtk/CoreExports.h"
18 #include "smtk/PublicPointerDefs.h"
19 #include "smtk/SharedFromThis.h"
20 #include "smtk/attribute/Categories.h"
21 #include "smtk/attribute/CopyAssignmentOptions.h"
22 #include "smtk/attribute/SearchStyle.h"
23 #include "smtk/common/Deprecation.h"
24 #include <algorithm>
25 #include <map>
26 #include <queue>
27 #include <string>
28 #include <vector>
29 
30 namespace smtk
31 {
32 
33 namespace io
34 {
35 class Logger;
36 };
37 
38 namespace attribute
39 {
40 class ItemDefinition;
41 class GroupItem;
42 class Attribute;
43 
44 class SMTKCORE_EXPORT Item : public smtk::enable_shared_from_this<Item>
45 {
46  friend class Definition;
47  friend class GroupItemDefinition;
48  friend class ReferenceItemDefinition;
49  friend class ValueItemDefinition;
50 
51 public:
53  enum Type
54  {
56  DoubleType,
57  GroupType,
58  IntType,
59  StringType,
60  VoidType,
61  FileType,
62  DirectoryType,
63  ColorType,
64  ModelEntityType,
66  DateTimeType,
67  ReferenceType,
68  ResourceType,
69  ComponentType,
70  NUMBER_OF_TYPES
71  };
72 
77  {
78  IGNORE_EXPRESSIONS = 0x001,
79  IGNORE_MODEL_ENTITIES = 0x002,
80  IGNORE_ATTRIBUTE_REF_ITEMS = 0x004,
81  IGNORE_RESOURCE_COMPONENTS = 0x008,
82  COPY_MODEL_ASSOCIATIONS = 0x010
83  };
84 
85  virtual ~Item();
86  std::string name() const;
87  std::string label() const;
88  virtual Item::Type type() const = 0;
89 
103  bool isValid(bool useActiveCategories = true) const;
104  bool isValid(const std::set<std::string>& categories) const
105  {
106  return (!this->isEnabled()) || this->isIgnored() || this->isValidInternal(true, categories);
107  }
109 
116  virtual bool isRelevant(
117  bool includeCatagories = true,
118  bool includeReadAccess = false,
119  unsigned int readAccessLevel = 0) const;
120 
123  smtk::attribute::ItemPtr find(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE);
124  smtk::attribute::ConstItemPtr find(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE)
125  const;
126 
127  template<typename T>
128  typename T::Ptr findAs(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE);
129 
130  template<typename T>
131  typename T::ConstPtr findAs(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE) const;
133 
141  virtual void visitChildren(
142  std::function<void(smtk::attribute::ItemPtr, bool)> visitor,
143  bool activeChildren = true)
144  {
145  (void)visitor;
146  (void)activeChildren;
147  }
148  const smtk::attribute::ConstItemDefinitionPtr& definition() const { return m_definition; }
149 
150  template<typename DefType>
151  std::shared_ptr<const DefType> definitionAs() const
152  {
153  return std::dynamic_pointer_cast<const DefType>(m_definition);
154  }
155 
157  smtk::attribute::AttributePtr attribute() const;
158  smtk::attribute::ItemPtr owningItem() const
159  {
160  return (m_owningItem ? m_owningItem->shared_from_this() : smtk::attribute::ItemPtr());
161  }
165  int position() const { return m_position; }
166 
167  int subGroupPosition() const { return m_subGroupPosition; }
168 
170  bool isOptional() const;
171 
177  bool isEnabled() const;
178 
180  bool localEnabledState() const { return m_isEnabled; }
182  void setIsEnabled(bool isEnabledValue) { m_isEnabled = isEnabledValue; }
183 
194  void setForceRequired(bool val) { m_forceRequired = val; }
195  bool forceRequired() const { return m_forceRequired; }
197 
199  const smtk::attribute::Categories& categories() const;
200 
211  unsigned int advanceLevel(int mode = 0) const;
212  void setLocalAdvanceLevel(int mode, unsigned int level);
213  unsigned int localAdvanceLevel(int mode = 0) const
214  {
215  return (mode == 1 ? m_localAdvanceLevel[1] : m_localAdvanceLevel[0]);
216  }
217  // unsetAdvanceLevel causes the item to return its
218  // definition advance level information for the specified mode when calling
219  // the advanceLevel(mode) method
220  void unsetLocalAdvanceLevel(int mode = 0);
221  // Returns true if the item is returning its local
222  // advance level information
223  bool hasLocalAdvanceLevelInfo(int mode = 0) const
224  {
225  return (mode == 1 ? m_hasLocalAdvanceLevelInfo[1] : m_hasLocalAdvanceLevelInfo[0]);
226  }
227 
228  void setUserData(const std::string& key, smtk::simulation::UserDataPtr value)
229  {
230  m_userData[key] = value;
231  }
232  smtk::simulation::UserDataPtr userData(const std::string& key) const;
233  void clearUserData(const std::string& key) { m_userData.erase(key); }
234  void clearAllUserData() { m_userData.clear(); }
235 
236  virtual void reset();
237 
240  virtual bool rotate(std::size_t fromPosition, std::size_t toPosition);
241 
243  virtual void detachOwningResource() {}
244 
246  void detachOwningAttribute() { m_attribute = nullptr; }
249  void detachOwningItem() { m_owningItem = nullptr; }
250 
253  SMTK_DEPRECATED_IN_22_11("Replaced by assign(ConstItemPtr&, const ItemAssignmentOptions&).")
254  virtual bool assign(const smtk::attribute::ConstItemPtr& sourceItem, unsigned int options);
255 
256  // Assigns this item to be equivalent to another. Options are processed by derived item classes.
257  // The options are defined in Item.h. Returns true if success and false if a problem occurred.
258  virtual bool assign(
259  const smtk::attribute::ConstItemPtr& sourceItem,
260  const CopyAssignmentOptions& options = CopyAssignmentOptions());
261 
262  virtual bool assign(
263  const smtk::attribute::ConstItemPtr& sourceItem,
264  const CopyAssignmentOptions& options,
265  smtk::io::Logger& logger);
266 
274  void setIsIgnored(bool val) { m_isIgnored = val; }
275 
276  bool isIgnored() const { return m_isIgnored; }
278 
279  static std::string type2String(Item::Type t);
280  static Item::Type string2Type(const std::string& s);
281  static void mapOldAssignmentOptions(CopyAssignmentOptions& options, unsigned int oldStyleOptions);
282 
283 protected:
284  Item(Attribute* owningAttribute, int itemPosition);
285  Item(Item* owningItem, int myPosition, int mySubGroupPOsition);
286  virtual bool setDefinition(smtk::attribute::ConstItemDefinitionPtr def);
288  virtual smtk::attribute::ItemPtr findInternal(const std::string& name, SearchStyle style);
289  virtual smtk::attribute::ConstItemPtr findInternal(const std::string& name, SearchStyle style)
290  const;
291  // Internal method for rotate()
292  template<typename T>
293  bool rotateVector(std::vector<T>& v, std::size_t fromPosition, std::size_t toPosition);
296  virtual bool isValidInternal(bool useCategories, const std::set<std::string>& categories)
297  const = 0;
298  Attribute* m_attribute;
299  Item* m_owningItem;
300  int m_position;
301  int m_subGroupPosition;
302  bool m_isEnabled;
303  bool m_isIgnored;
305  std::map<std::string, smtk::simulation::UserDataPtr> m_userData;
306 
307 private:
308  bool m_hasLocalAdvanceLevelInfo[2];
309  unsigned int m_localAdvanceLevel[2];
310  bool m_forceRequired;
311 };
312 
313 inline smtk::simulation::UserDataPtr Item::userData(const std::string& key) const
314 {
315  std::map<std::string, smtk::simulation::UserDataPtr>::const_iterator it = m_userData.find(key);
316  return ((it == m_userData.end()) ? smtk::simulation::UserDataPtr() : it->second);
317 }
318 
319 template<typename T>
320 typename T::Ptr Item::findAs(const std::string& iname, SearchStyle style)
321 {
322  return smtk::dynamic_pointer_cast<T>(this->find(iname, style));
323 }
324 
325 template<typename T>
326 typename T::ConstPtr Item::findAs(const std::string& iname, SearchStyle style) const
327 {
328  return smtk::dynamic_pointer_cast<const T>(this->find(iname, style));
329 }
330 
331 template<typename T>
332 bool Item::rotateVector(std::vector<T>& v, std::size_t fromPosition, std::size_t toPosition)
333 {
334  if (fromPosition == toPosition) // no-op
335  {
336  return true;
337  }
338 
339  std::size_t lastPosition = v.size() - 1;
340  if ((fromPosition > lastPosition) || (toPosition > lastPosition))
341  {
342  return false;
343  }
344 
345  auto first = v.begin();
346  auto middle = v.begin();
347  auto last = v.begin();
348  if (fromPosition < toPosition)
349  {
350  first += fromPosition;
351  last += toPosition + 1;
352  middle += fromPosition + 1;
353  }
354  else
355  {
356  first += toPosition;
357  last += fromPosition + 1;
358  middle += fromPosition;
359  }
360  std::rotate(first, middle, last);
361  return true;
362 }
363 } // namespace attribute
364 } // namespace smtk
365 
366 #endif /* smtk_attribute_Item_h */
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::attribute::Item::Type
Type
Definition: Item.h:53
PublicPointerDefs.h
smtk::attribute::Definition
Definition: Definition.h:45
smtk::attribute::CopyAssignmentOptions
Class used to specify copy and assignment options.
Definition: CopyAssignmentOptions.h:211
smtk::attribute::SearchStyle
SearchStyle
How should searches for items be conducted?
Definition: SearchStyle.h:24
smtk::attribute::Item::detachOwningAttribute
void detachOwningAttribute()
This should be used only by attributes.
Definition: Item.h:246
smtk::attribute::Item::visitChildren
virtual void visitChildren(std::function< void(smtk::attribute::ItemPtr, bool)> visitor, bool activeChildren=true)
visitChildren Invoke a function on each (or, if findInActiveChildren is true, each active) child item...
Definition: Item.h:141
smtk::simulation::UserDataPtr
smtk::shared_ptr< smtk::simulation::UserData > UserDataPtr
Definition: PublicPointerDefs.h:671
smtk::attribute::Item::setForceRequired
void setForceRequired(bool val)
Controls if an item should be forced to be required regardless of its local enable property.
Definition: Item.h:194
smtk::attribute::Item::setIsEnabled
void setIsEnabled(bool isEnabledValue)
Set the instance's local enabled state.
Definition: Item.h:182
smtk::attribute::Item::detachOwningResource
virtual void detachOwningResource()
Release the item's dependency on its parent attribute's Resource.
Definition: Item.h:243
smtk::attribute::Item::MeshEntityType
@ MeshEntityType
Needed for backward compatibility w/r XML/JSON formats < 4.0.
Definition: Item.h:65
smtk::attribute::ConstItemDefinitionPtr
smtk::shared_ptr< const smtk::attribute::ItemDefinition > ConstItemDefinitionPtr
Definition: PublicPointerDefs.h:475
smtk::attribute::Item::AssignmentOptions
AssignmentOptions
Deprecated Assignment Options.
Definition: Item.h:76
smtk::attribute::Item::localEnabledState
bool localEnabledState() const
Return the state of the instance's isEnabled state.
Definition: Item.h:180
smtk::attribute::Item::AttributeRefType
@ AttributeRefType
Needed for backward compatibility w/r XML/JSON formats < 4.0.
Definition: Item.h:55
smtk::attribute::Item::isIgnored
bool isIgnored() const
Controls if an item should be ignored.
Definition: Item.h:276
smtk::attribute::ReferenceItemDefinition
A definition for attribute items that store smtk::resource::PersistentObjectPtr as values.
Definition: ReferenceItemDefinition.h:40
smtk::attribute::Item::detachOwningItem
void detachOwningItem()
This should only be called by the item that owns this one.
Definition: Item.h:249
smtk::attribute::Item
Definition: Item.h:44
smtk::attribute::AttributePtr
smtk::shared_ptr< smtk::attribute::Attribute > AttributePtr
Definition: PublicPointerDefs.h:456
smtk::attribute::RECURSIVE_ACTIVE
@ RECURSIVE_ACTIVE
Recursively search for an active item.
Definition: SearchStyle.h:32
smtk::attribute::ConstItemPtr
smtk::shared_ptr< const smtk::attribute::Item > ConstItemPtr
Definition: PublicPointerDefs.h:469
smtk::attribute::ValueItemDefinition
Definition: ValueItemDefinition.h:41
smtk::attribute::Item::findAs
T::Ptr findAs(const std::string &name, SearchStyle style=RECURSIVE_ACTIVE)
return a child item that matches name and satisfies the SearchStyle
Definition: Item.h:320
smtk::attribute::Attribute
Represent a (possibly composite) value according to a definition.
Definition: Attribute.h:49
smtk::attribute::Categories
Represents the category constraints associated with an Attribute, Attribute Definition,...
Definition: Categories.h:32
smtk::attribute::Item::isValid
bool isValid(const std::set< std::string > &categories) const
tests the validity of an item
Definition: Item.h:104
smtk::attribute::ItemPtr
smtk::shared_ptr< smtk::attribute::Item > ItemPtr
Definition: PublicPointerDefs.h:467
SharedFromThis.h
Macros for dealing with shared-pointer classes.
smtk::attribute::GroupItemDefinition
A GroupItemDefinition represents a collection of Item Definitions.
Definition: GroupItemDefinition.h:32
smtkTypeMacroBase
#define smtkTypeMacroBase(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:55
smtk::attribute::Item::position
int position() const
Position is the item's location w/r to the owning item if not null or the owning attribute.
Definition: Item.h:165
smtk::attribute::Item::find
smtk::attribute::ItemPtr find(const std::string &name, SearchStyle style=RECURSIVE_ACTIVE)
return a child item that matches name and satisfies the SearchStyle
Definition: Item.cxx:136
smtk::attribute::Item::forceRequired
bool forceRequired() const
Controls if an item should be forced to be required regardless of its local enable property.
Definition: Item.h:195