SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
CustomItem.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 #ifndef smtk_attribute_CustomItem_h
11 #define smtk_attribute_CustomItem_h
12 
13 #include "smtk/CoreExports.h"
14 #include "smtk/PublicPointerDefs.h"
15 
16 #include "smtk/attribute/CustomItemDefinition.h"
17 #include "smtk/attribute/Item.h"
18 
19 #include "smtk/common/CompilerInformation.h"
20 
21 SMTK_THIRDPARTY_PRE_INCLUDE
22 #include "nlohmann/json.hpp"
23 SMTK_THIRDPARTY_POST_INCLUDE
24 
25 namespace pugi
26 {
27 class xml_node;
28 }
29 
30 namespace smtk
31 {
32 namespace attribute
33 {
34 
37 class SMTKCORE_EXPORT CustomItemBase : public Item
38 {
39 public:
41 
42  CustomItemBase(smtk::attribute::Attribute* owningAttribute, int itemPosition)
43  : Item(owningAttribute, itemPosition)
44  {
45  }
46 
47  CustomItemBase(smtk::attribute::Item* owningItem, int myPosition, int mySubGroupPosition)
48  : Item(owningItem, myPosition, mySubGroupPosition)
49  {
50  }
51 
52  virtual const CustomItemBase& operator>>(nlohmann::json& json) const = 0;
53  virtual CustomItemBase& operator<<(const nlohmann::json& json) = 0;
54 
55  virtual const CustomItemBase& operator>>(pugi::xml_node& node) const = 0;
56  virtual CustomItemBase& operator<<(const pugi::xml_node& node) = 0;
57 };
58 
61 template<typename ItemType>
62 class CustomItem : public CustomItemBase
63 {
64 public:
65  typedef std::shared_ptr<ItemType> Ptr;
66 
67  static Ptr New(const std::string& myName) { return Ptr(new ItemType(myName)); }
68 
69  CustomItem(smtk::attribute::Attribute* owningAttribute, int itemPosition)
70  : CustomItemBase(owningAttribute, itemPosition)
71  {
72  }
73 
74  CustomItem(smtk::attribute::Item* owningItem, int myPosition, int mySubGroupPosition)
75  : CustomItemBase(owningItem, myPosition, mySubGroupPosition)
76  {
77  }
78 
79  bool setDefinition(smtk::attribute::ConstItemDefinitionPtr adef) override
80  {
82  dynamic_cast<const CustomItemDefinition<ItemType>*>(adef.get());
83  return !((def == nullptr) || (!Item::setDefinition(adef)));
84  }
85 
86  Item::Type type() const override { return this->definition()->type(); }
87 };
88 } // namespace attribute
89 } // namespace smtk
90 
91 #endif
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::ConstItemDefinitionPtr
smtk::shared_ptr< const smtk::attribute::ItemDefinition > ConstItemDefinitionPtr
Definition: PublicPointerDefs.h:475
smtk::attribute::CustomItemDefinition
Custom item definitions inherit from from a specialization of this template class,...
Definition: CustomItemDefinition.h:61
smtk::task::operator<<
std::ostream & operator<<(std::ostream &os, const State &s)
States may be appended to streams.
Definition: State.h:78
smtk::attribute::CustomItem
Custom items inherit from a specialization of this template class, using the corresponding custom ite...
Definition: CustomItem.h:62
smtk::attribute::CustomItemBase
A base class for custom (i.e.
Definition: CustomItem.h:37
smtk::attribute::Item
Definition: Item.h:43
smtkTypeMacro
#define smtkTypeMacro(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:51
smtk::attribute::Attribute
Represent a (possibly composite) value according to a definition.
Definition: Attribute.h:49