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 
11 #ifndef smtk_attribute_Item_h
12 #define smtk_attribute_Item_h
13 
14 #include "smtk/CoreExports.h"
15 #include "smtk/PublicPointerDefs.h"
16 #include "smtk/SharedFromThis.h"
17 #include "smtk/attribute/Categories.h"
18 #include "smtk/attribute/CopyAssignmentOptions.h"
19 #include "smtk/attribute/SearchStyle.h"
20 #include "smtk/common/Status.h"
21 #include <algorithm>
22 #include <map>
23 #include <queue>
24 #include <string>
25 #include <vector>
26 
27 namespace smtk
28 {
29 
30 namespace io
31 {
32 class Logger;
33 };
34 
35 namespace attribute
36 {
37 class ItemDefinition;
38 class GroupItem;
39 class Attribute;
40 
47 class SMTKCORE_EXPORT Item : public smtk::enable_shared_from_this<Item>
48 {
49  friend class Definition;
50  friend class GroupItemDefinition;
51  friend class ReferenceItemDefinition;
52  friend class ValueItemDefinition;
53 
54 public:
56  enum Type
57  {
59  DoubleType,
60  GroupType,
61  IntType,
62  StringType,
63  VoidType,
64  FileType,
65  DirectoryType,
66  ColorType,
67  ModelEntityType,
69  DateTimeType,
70  ReferenceType,
71  ResourceType,
72  ComponentType,
73  NUMBER_OF_TYPES
74  };
75 
76  virtual ~Item();
77  std::string name() const;
78  std::string label() const;
79  virtual Item::Type type() const = 0;
80 
94  bool isValid(bool useActiveCategories = true) const;
95  bool isValid(const std::set<std::string>& categories) const
96  {
97  return (!this->isEnabled()) || this->isIgnored() || this->isValidInternal(true, categories);
98  }
100 
107  virtual bool isRelevant(
108  bool includeCatagories = true,
109  bool includeReadAccess = false,
110  unsigned int readAccessLevel = 0) const;
111 
114  smtk::attribute::ItemPtr find(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE);
115  smtk::attribute::ConstItemPtr find(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE)
116  const;
117 
118  template<typename T>
119  typename T::Ptr findAs(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE);
120 
121  template<typename T>
122  typename T::ConstPtr findAs(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE) const;
124 
132  virtual void visitChildren(
133  std::function<void(smtk::attribute::ItemPtr, bool)> visitor,
134  bool activeChildren = true)
135  {
136  (void)visitor;
137  (void)activeChildren;
138  }
139  const smtk::attribute::ConstItemDefinitionPtr& definition() const { return m_definition; }
140 
141  template<typename DefType>
142  std::shared_ptr<const DefType> definitionAs() const
143  {
144  return std::dynamic_pointer_cast<const DefType>(m_definition);
145  }
146 
148  smtk::attribute::AttributePtr attribute() const;
149  smtk::attribute::ItemPtr owningItem() const
150  {
151  return (m_owningItem ? m_owningItem->shared_from_this() : smtk::attribute::ItemPtr());
152  }
156  int position() const { return m_position; }
157 
160  int subGroupPosition() const;
161 
165  std::string path(const std::string& sep = "/") const;
167 
169  bool isOptional() const;
170 
176  bool isEnabled() const;
177 
179  bool localEnabledState() const { return m_isEnabled; }
181  void setIsEnabled(bool isEnabledValue) { m_isEnabled = isEnabledValue; }
182 
193  void setForceRequired(bool val) { m_forceRequired = val; }
194  bool forceRequired() const { return m_forceRequired; }
196 
198  const smtk::attribute::Categories& categories() const;
199 
210  unsigned int advanceLevel(int mode = 0) const;
211  void setLocalAdvanceLevel(int mode, unsigned int level);
212  unsigned int localAdvanceLevel(int mode = 0) const
213  {
214  return (mode == 1 ? m_localAdvanceLevel[1] : m_localAdvanceLevel[0]);
215  }
216  // unsetAdvanceLevel causes the item to return its
217  // definition advance level information for the specified mode when calling
218  // the advanceLevel(mode) method
219  void unsetLocalAdvanceLevel(int mode = 0);
220  // Returns true if the item is returning its local
221  // advance level information
222  bool hasLocalAdvanceLevelInfo(int mode = 0) const
223  {
224  return (mode == 1 ? m_hasLocalAdvanceLevelInfo[1] : m_hasLocalAdvanceLevelInfo[0]);
225  }
226 
227  void setUserData(const std::string& key, smtk::simulation::UserDataPtr value)
228  {
229  m_userData[key] = value;
230  }
231  smtk::simulation::UserDataPtr userData(const std::string& key) const;
232  void clearUserData(const std::string& key) { m_userData.erase(key); }
233  void clearAllUserData() { m_userData.clear(); }
234 
235  virtual void reset();
236 
239  virtual bool rotate(std::size_t fromPosition, std::size_t toPosition);
240 
242  virtual void detachOwningResource() {}
243 
245  void detachOwningAttribute() { m_attribute = nullptr; }
248  void detachOwningItem() { m_owningItem = nullptr; }
249 
250  using Status = smtk::common::Status;
251 
259  virtual Status assign(
260  const smtk::attribute::ConstItemPtr& sourceItem,
261  const CopyAssignmentOptions& options = CopyAssignmentOptions());
262 
263  virtual Status assign(
264  const smtk::attribute::ConstItemPtr& sourceItem,
265  const CopyAssignmentOptions& options,
266  smtk::io::Logger& logger);
268 
276  void setIsIgnored(bool val) { m_isIgnored = val; }
277 
278  bool isIgnored() const { return m_isIgnored; }
280 
281  static std::string type2String(Item::Type t);
282  static Item::Type string2Type(const std::string& s);
283 
284 protected:
285  Item(Attribute* owningAttribute, int itemPosition);
286  Item(Item* owningItem, int myPosition, int mySubGroupPosition);
287  virtual bool setDefinition(smtk::attribute::ConstItemDefinitionPtr def);
289  virtual smtk::attribute::ItemPtr findInternal(const std::string& name, SearchStyle style);
290  virtual smtk::attribute::ConstItemPtr findInternal(const std::string& name, SearchStyle style)
291  const;
292  // Internal method for rotate()
293  template<typename T>
294  bool rotateVector(std::vector<T>& v, std::size_t fromPosition, std::size_t toPosition);
297  virtual bool isValidInternal(bool useCategories, const std::set<std::string>& categories)
298  const = 0;
299  Attribute* m_attribute;
300  Item* m_owningItem;
301  int m_position;
302  int m_subGroupPosition;
303  bool m_isEnabled;
304  bool m_isIgnored;
306  std::map<std::string, smtk::simulation::UserDataPtr> m_userData;
307 
308 private:
309  bool m_hasLocalAdvanceLevelInfo[2];
310  unsigned int m_localAdvanceLevel[2];
311  bool m_forceRequired;
312 };
313 
314 inline smtk::simulation::UserDataPtr Item::userData(const std::string& key) const
315 {
316  std::map<std::string, smtk::simulation::UserDataPtr>::const_iterator it = m_userData.find(key);
317  return ((it == m_userData.end()) ? smtk::simulation::UserDataPtr() : it->second);
318 }
319 
320 template<typename T>
321 typename T::Ptr Item::findAs(const std::string& iname, SearchStyle style)
322 {
323  return smtk::dynamic_pointer_cast<T>(this->find(iname, style));
324 }
325 
326 template<typename T>
327 typename T::ConstPtr Item::findAs(const std::string& iname, SearchStyle style) const
328 {
329  return smtk::dynamic_pointer_cast<const T>(this->find(iname, style));
330 }
331 
332 template<typename T>
333 bool Item::rotateVector(std::vector<T>& v, std::size_t fromPosition, std::size_t toPosition)
334 {
335  if (fromPosition == toPosition) // no-op
336  {
337  return true;
338  }
339 
340  std::size_t lastPosition = v.size() - 1;
341  if ((fromPosition > lastPosition) || (toPosition > lastPosition))
342  {
343  return false;
344  }
345 
346  auto first = v.begin();
347  auto middle = v.begin();
348  auto last = v.begin();
349  if (fromPosition < toPosition)
350  {
351  first += fromPosition;
352  last += toPosition + 1;
353  middle += fromPosition + 1;
354  }
355  else
356  {
357  first += toPosition;
358  last += fromPosition + 1;
359  middle += fromPosition;
360  }
361  std::rotate(first, middle, last);
362  return true;
363 }
364 } // namespace attribute
365 } // namespace smtk
366 
367 #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:56
PublicPointerDefs.h
smtk::attribute::Definition
Stores the definition of an attribute.
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:245
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:132
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:193
smtk::attribute::Item::setIsEnabled
void setIsEnabled(bool isEnabledValue)
Set the instance's local enabled state.
Definition: Item.h:181
smtk::attribute::Item::detachOwningResource
virtual void detachOwningResource()
Release the item's dependency on its parent attribute's Resource.
Definition: Item.h:242
smtk::attribute::Item::MeshEntityType
@ MeshEntityType
Needed for backward compatibility w/r XML/JSON formats < 4.0.
Definition: Item.h:68
smtk::attribute::ConstItemDefinitionPtr
smtk::shared_ptr< const smtk::attribute::ItemDefinition > ConstItemDefinitionPtr
Definition: PublicPointerDefs.h:475
smtk::io::Logger
Log messages for later presentation to a user or a file.
Definition: Logger.h:94
smtk::attribute::Item::localEnabledState
bool localEnabledState() const
Return the state of the instance's isEnabled state.
Definition: Item.h:179
smtk::attribute::Item::AttributeRefType
@ AttributeRefType
Needed for backward compatibility w/r XML/JSON formats < 4.0.
Definition: Item.h:58
smtk::attribute::Item::setIsIgnored
void setIsIgnored(bool val)
Controls if an item should be ignored.
Definition: Item.h:276
smtk::attribute::Item::isIgnored
bool isIgnored() const
Controls if an item should be ignored.
Definition: Item.h:278
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:248
smtk::attribute::Item
The base class for items that hold information inside an attribute.
Definition: Item.h:47
smtk::common::Status
A return value for methods that need to indicate both success/failure and modification/stasis.
Definition: Status.h:30
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:321
smtk::attribute::Attribute
Represent a (possibly composite) value according to a definition.
Definition: Attribute.h:50
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:95
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:131
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:156
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:194