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 
58  typedef std::function<
59  bool(const Item*, bool includeCatagories, bool includeReadAccess, unsigned int readAccessLevel)>
61 
62  enum Type
63  {
65  DoubleType,
66  GroupType,
67  IntType,
68  StringType,
69  VoidType,
70  FileType,
71  DirectoryType,
72  ColorType,
73  ModelEntityType,
75  DateTimeType,
76  ReferenceType,
77  ResourceType,
78  ComponentType,
79  NUMBER_OF_TYPES
80  };
81 
82  virtual ~Item();
83  std::string name() const;
84  std::string label() const;
85  virtual Item::Type type() const = 0;
86 
100  bool isValid(bool useActiveCategories = true) const;
101  bool isValid(const std::set<std::string>& categories) const
102  {
103  return (!this->isEnabled()) || this->isIgnored() || this->isValidInternal(true, categories);
104  }
106 
109  void setCustomIsRelevant(RelevanceFunc func) { m_customIsRelevant = func; }
110  RelevanceFunc customIsRelevant() const { return m_customIsRelevant; }
112 
118  bool isRelevant(
119  bool includeCatagories = true,
120  bool includeReadAccess = false,
121  unsigned int readAccessLevel = 0) const;
122 
129  virtual bool defaultIsRelevant(
130  bool includeCatagories,
131  bool includeReadAccess,
132  unsigned int readAccessLevel) const;
133 
136  smtk::attribute::ItemPtr find(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE);
137  smtk::attribute::ConstItemPtr find(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE)
138  const;
139 
140  template<typename T>
141  typename T::Ptr findAs(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE);
142 
143  template<typename T>
144  typename T::ConstPtr findAs(const std::string& name, SearchStyle style = RECURSIVE_ACTIVE) const;
146 
154  virtual void visitChildren(
155  std::function<void(smtk::attribute::ItemPtr, bool)> visitor,
156  bool activeChildren = true)
157  {
158  (void)visitor;
159  (void)activeChildren;
160  }
161  const smtk::attribute::ConstItemDefinitionPtr& definition() const { return m_definition; }
162 
163  template<typename DefType>
164  std::shared_ptr<const DefType> definitionAs() const
165  {
166  return std::dynamic_pointer_cast<const DefType>(m_definition);
167  }
168 
170  smtk::attribute::AttributePtr attribute() const;
171  smtk::attribute::ItemPtr owningItem() const
172  {
173  return (m_owningItem ? m_owningItem->shared_from_this() : smtk::attribute::ItemPtr());
174  }
178  int position() const { return m_position; }
179 
182  int subGroupPosition() const;
183 
187  std::string path(const std::string& sep = "/") const;
189 
191  bool isOptional() const;
192 
198  bool isEnabled() const;
199 
201  bool localEnabledState() const { return m_isEnabled; }
203  void setIsEnabled(bool isEnabledValue) { m_isEnabled = isEnabledValue; }
204 
215  void setForceRequired(bool val) { m_forceRequired = val; }
216  bool forceRequired() const { return m_forceRequired; }
218 
220  const smtk::attribute::Categories& categories() const;
221 
232  unsigned int advanceLevel(int mode = 0) const;
233  void setLocalAdvanceLevel(int mode, unsigned int level);
234  unsigned int localAdvanceLevel(int mode = 0) const
235  {
236  return (mode == 1 ? m_localAdvanceLevel[1] : m_localAdvanceLevel[0]);
237  }
238  // unsetAdvanceLevel causes the item to return its
239  // definition advance level information for the specified mode when calling
240  // the advanceLevel(mode) method
241  void unsetLocalAdvanceLevel(int mode = 0);
242  // Returns true if the item is returning its local
243  // advance level information
244  bool hasLocalAdvanceLevelInfo(int mode = 0) const
245  {
246  return (mode == 1 ? m_hasLocalAdvanceLevelInfo[1] : m_hasLocalAdvanceLevelInfo[0]);
247  }
248 
249  void setUserData(const std::string& key, smtk::simulation::UserDataPtr value)
250  {
251  m_userData[key] = value;
252  }
253  smtk::simulation::UserDataPtr userData(const std::string& key) const;
254  void clearUserData(const std::string& key) { m_userData.erase(key); }
255  void clearAllUserData() { m_userData.clear(); }
256 
257  virtual void reset();
258 
261  virtual bool rotate(std::size_t fromPosition, std::size_t toPosition);
262 
264  virtual void detachOwningResource() {}
265 
267  void detachOwningAttribute() { m_attribute = nullptr; }
270  void detachOwningItem() { m_owningItem = nullptr; }
271 
272  using Status = smtk::common::Status;
273 
281  virtual Status assign(
282  const smtk::attribute::ConstItemPtr& sourceItem,
283  const CopyAssignmentOptions& options = CopyAssignmentOptions());
284 
285  virtual Status assign(
286  const smtk::attribute::ConstItemPtr& sourceItem,
287  const CopyAssignmentOptions& options,
288  smtk::io::Logger& logger);
290 
298  void setIsIgnored(bool val) { m_isIgnored = val; }
299 
300  bool isIgnored() const { return m_isIgnored; }
302 
303  static std::string type2String(Item::Type t);
304  static Item::Type string2Type(const std::string& s);
305 
306 protected:
307  Item(Attribute* owningAttribute, int itemPosition);
308  Item(Item* owningItem, int myPosition, int mySubGroupPosition);
309  virtual bool setDefinition(smtk::attribute::ConstItemDefinitionPtr def);
311  virtual smtk::attribute::ItemPtr findInternal(const std::string& name, SearchStyle style);
312  virtual smtk::attribute::ConstItemPtr findInternal(const std::string& name, SearchStyle style)
313  const;
314  // Internal method for rotate()
315  template<typename T>
316  bool rotateVector(std::vector<T>& v, std::size_t fromPosition, std::size_t toPosition);
319  virtual bool isValidInternal(bool useCategories, const std::set<std::string>& categories)
320  const = 0;
321  Attribute* m_attribute;
322  Item* m_owningItem;
323  int m_position;
324  int m_subGroupPosition;
325  bool m_isEnabled;
326  bool m_isIgnored;
328  std::map<std::string, smtk::simulation::UserDataPtr> m_userData;
329  RelevanceFunc m_customIsRelevant = nullptr;
330 
331 private:
332  bool m_hasLocalAdvanceLevelInfo[2];
333  unsigned int m_localAdvanceLevel[2];
334  bool m_forceRequired;
335 };
336 
337 inline smtk::simulation::UserDataPtr Item::userData(const std::string& key) const
338 {
339  std::map<std::string, smtk::simulation::UserDataPtr>::const_iterator it = m_userData.find(key);
340  return ((it == m_userData.end()) ? smtk::simulation::UserDataPtr() : it->second);
341 }
342 
343 template<typename T>
344 typename T::Ptr Item::findAs(const std::string& iname, SearchStyle style)
345 {
346  return smtk::dynamic_pointer_cast<T>(this->find(iname, style));
347 }
348 
349 template<typename T>
350 typename T::ConstPtr Item::findAs(const std::string& iname, SearchStyle style) const
351 {
352  return smtk::dynamic_pointer_cast<const T>(this->find(iname, style));
353 }
354 
355 template<typename T>
356 bool Item::rotateVector(std::vector<T>& v, std::size_t fromPosition, std::size_t toPosition)
357 {
358  if (fromPosition == toPosition) // no-op
359  {
360  return true;
361  }
362 
363  std::size_t lastPosition = v.size() - 1;
364  if ((fromPosition > lastPosition) || (toPosition > lastPosition))
365  {
366  return false;
367  }
368 
369  auto first = v.begin();
370  auto middle = v.begin();
371  auto last = v.begin();
372  if (fromPosition < toPosition)
373  {
374  first += fromPosition;
375  last += toPosition + 1;
376  middle += fromPosition + 1;
377  }
378  else
379  {
380  first += toPosition;
381  last += fromPosition + 1;
382  middle += fromPosition;
383  }
384  std::rotate(first, middle, last);
385  return true;
386 }
387 } // namespace attribute
388 } // namespace smtk
389 
390 #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:62
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:272
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:267
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:154
smtk::simulation::UserDataPtr
smtk::shared_ptr< smtk::simulation::UserData > UserDataPtr
Definition: PublicPointerDefs.h:678
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:215
smtk::attribute::Item::setIsEnabled
void setIsEnabled(bool isEnabledValue)
Set the instance's local enabled state.
Definition: Item.h:203
smtk::attribute::Item::detachOwningResource
virtual void detachOwningResource()
Release the item's dependency on its parent attribute's Resource.
Definition: Item.h:264
smtk::attribute::Item::MeshEntityType
@ MeshEntityType
Needed for backward compatibility w/r XML/JSON formats < 4.0.
Definition: Item.h:74
smtk::attribute::Item::setCustomIsRelevant
void setCustomIsRelevant(RelevanceFunc func)
Set and Get Methods for specifying a custom isRelevant function.
Definition: Item.h:109
smtk::attribute::ConstItemDefinitionPtr
smtk::shared_ptr< const smtk::attribute::ItemDefinition > ConstItemDefinitionPtr
Definition: PublicPointerDefs.h:482
smtk::io::Logger
Log messages for later presentation to a user or a file.
Definition: Logger.h:94
smtk::attribute::Item::RelevanceFunc
std::function< bool(const Item *, bool includeCatagories, bool includeReadAccess, unsigned int readAccessLevel)> RelevanceFunc
Typedef for custom relevance functions.
Definition: Item.h:60
smtk::attribute::Item::localEnabledState
bool localEnabledState() const
Return the state of the instance's isEnabled state.
Definition: Item.h:201
smtk::attribute::Item::AttributeRefType
@ AttributeRefType
Needed for backward compatibility w/r XML/JSON formats < 4.0.
Definition: Item.h:64
smtk::attribute::Item::setIsIgnored
void setIsIgnored(bool val)
Controls if an item should be ignored.
Definition: Item.h:298
smtk::attribute::Item::isIgnored
bool isIgnored() const
Controls if an item should be ignored.
Definition: Item.h:300
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:270
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:463
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:476
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:344
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:33
smtk::attribute::Item::isValid
bool isValid(const std::set< std::string > &categories) const
tests the validity of an item
Definition: Item.h:101
smtk::attribute::Item::customIsRelevant
RelevanceFunc customIsRelevant() const
Set and Get Methods for specifying a custom isRelevant function.
Definition: Item.h:110
smtk::attribute::ItemPtr
smtk::shared_ptr< smtk::attribute::Item > ItemPtr
Definition: PublicPointerDefs.h:474
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:178
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:149
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:216