SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
FileSystemItem.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 FileSystemItem.h -
11 // .SECTION Description
12 // .SECTION See Also
13 
14 #ifndef smtk_attribute_FileSystemItem_h
15 #define smtk_attribute_FileSystemItem_h
16 
17 #include "smtk/CoreExports.h"
18 #include "smtk/PublicPointerDefs.h"
19 #include "smtk/attribute/Item.h"
20 #include <cassert>
21 #include <string>
22 #include <vector>
23 
24 namespace smtk
25 {
26 namespace attribute
27 {
28 class FileSystemItemDefinition;
29 class SMTKCORE_EXPORT FileSystemItem : public Item
30 {
31  friend class FileItemDefinition;
32 
33 public:
34  typedef std::vector<std::string>::const_iterator const_iterator;
35 
37  ~FileSystemItem() override;
38  Item::Type type() const override = 0;
39 
40  bool shouldBeRelative() const;
41  bool shouldExist() const;
42  std::size_t numberOfValues() const { return m_values.size(); }
43  bool setNumberOfValues(std::size_t newSize);
44  std::size_t numberOfRequiredValues() const;
45  bool isExtensible() const;
46  std::size_t maxNumberOfValues() const;
47  std::string value(std::size_t element = 0) const { return m_values[element]; }
48  bool setValue(const std::string& val) { return this->setValue(0, val); }
49  bool setValue(std::size_t element, const std::string& val);
50  bool appendValue(const std::string& val);
51  bool removeValue(std::size_t element);
52  void reset() override;
53  virtual bool setToDefault(std::size_t elementIndex = 0);
54  // Returns true if there is a default defined and the item is curently set to it
55  virtual bool isUsingDefault(std::size_t elementIndex) const;
56  // This method tests all of the values of the items w/r the default value
57  virtual bool isUsingDefault() const;
58  // Does this item have a default value?
59  bool hasDefault() const;
60  std::string defaultValue() const;
61  virtual std::string valueAsString(const std::string& format = "") const
62  {
63  return this->valueAsString(0, format);
64  }
65  virtual std::string valueAsString(std::size_t element, const std::string& format = "") const;
66  virtual bool isSet(std::size_t element = 0) const
67  {
68  return m_isSet.size() > element ? m_isSet[element] : false;
69  }
70 
71  virtual void unset(std::size_t element = 0)
72  {
73  assert(m_isSet.size() > element);
74  m_isSet[element] = false;
75  }
76 
77  // Iterator-style access to values:
78  const_iterator begin() const;
79  const_iterator end() const;
80  template<typename I>
81  bool setValues(I vbegin, I vend, std::size_t offset = 0);
82  template<typename I>
83  bool appendValues(I vbegin, I vend);
84 
85  // Assigns this item to be equivalent to another. Options are processed by derived item classes
86  // Returns true if success and false if a problem occurred. Does not currently support any options directly.
87  using Item::assign;
88  bool assign(
89  const smtk::attribute::ConstItemPtr& sourceItem,
90  const CopyAssignmentOptions& options,
91  smtk::io::Logger& logger) override;
92 
93 protected:
94  FileSystemItem(Attribute* owningAttribute, int itemPosition);
95  FileSystemItem(Item* owningItem, int position, int subGroupPosition);
96  bool setDefinition(smtk::attribute::ConstItemDefinitionPtr vdef) override;
97  bool isValidInternal(bool useCategories, const std::set<std::string>& categories) const override;
98  std::vector<std::string> m_values;
99  std::vector<bool> m_isSet;
100 
101 private:
102 };
103 
104 template<typename I>
105 bool FileSystemItem::setValues(I vbegin, I vend, std::size_t offset)
106 {
107  bool ok = false;
108  std::size_t num = vend - vbegin + offset;
109  if (this->setNumberOfValues(num))
110  {
111  ok = true;
112  std::size_t i = 0;
113  for (I it = vbegin; it != vend; ++it, ++i)
114  {
115  if (!this->setValue(offset + i, *it))
116  {
117  ok = false;
118  break;
119  }
120  }
121  }
122  // Enable or disable the item if it is optional.
123  if (ok)
124  this->setIsEnabled(num > 0);
125  return ok;
126 }
127 
128 template<typename I>
129 bool FileSystemItem::appendValues(I vbegin, I vend)
130 {
131  return this->setValues(vbegin, vend, this->numberOfValues());
132 }
133 
134 } // namespace attribute
135 } // namespace smtk
136 
137 #endif /* smtk_attribute_FileSystemItem_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
PublicPointerDefs.h
smtk::attribute::CopyAssignmentOptions
Class used to specify copy and assignment options.
Definition: CopyAssignmentOptions.h:211
smtk::attribute::Item::setIsEnabled
void setIsEnabled(bool isEnabledValue)
Set the instance's local enabled state.
Definition: Item.h:181
smtk::attribute::Item::assign
virtual bool assign(const smtk::attribute::ConstItemPtr &sourceItem, unsigned int options)
Assigns this item to be equivalent to another.
Definition: Item.cxx:292
smtk::attribute::FileSystemItem
Definition: FileSystemItem.h:29
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:95
smtk::attribute::FileItemDefinition
Definition: FileItemDefinition.h:27
smtk::attribute::Item
Definition: Item.h:43
smtkTypeMacro
#define smtkTypeMacro(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:51
smtk::attribute::ConstItemPtr
smtk::shared_ptr< const smtk::attribute::Item > ConstItemPtr
Definition: PublicPointerDefs.h:469
smtk::attribute::Attribute
Represent a (possibly composite) value according to a definition.
Definition: Attribute.h:49