Choreonoid  1.8
DeviceOverwriteItem.h
Go to the documentation of this file.
1 #ifndef CNOID_BODY_PLUGIN_DEVICE_OVERWRITE_ITEM_H
2 #define CNOID_BODY_PLUGIN_DEVICE_OVERWRITE_ITEM_H
3 
5 #include <cnoid/LocatableItem>
6 #include <cnoid/RenderableItem>
7 #include "exportdecl.h"
8 
9 namespace cnoid {
10 
11 class BodyItem;
12 class Body;
13 class Link;
14 class Device;
15 class DeviceOverwriteItem;
16 
17 class CNOID_EXPORT DeviceOverwriteMediator : public Referenced
18 {
19 public:
20  static void registerMediator(const std::string& deviceTypeName, DeviceOverwriteMediator* mediator);
21  static DeviceOverwriteMediator* findMediator(const std::string& id, const std::string& deviceTypeName);
22 
23  const std::string& id() { return id_; }
24  virtual Device* findDevice(Body* body, const Mapping* info);
25  virtual Device* restoreDevice(Body* body, Device* device = nullptr, const Mapping* info = nullptr) = 0;
26  virtual bool storeInformation(DeviceOverwriteItem* item, Mapping* info) = 0;
27 
28  class DeviceInfo {
29  public:
30  DeviceInfo(Device* device, DeviceOverwriteItem* item, bool isNewDevice, bool isNewItem)
31  : device_(device), item_(item), isNewDevice_(isNewDevice), isNewItem_(isNewItem) { }
32  explicit operator bool() const { return device_ != nullptr; }
33  Device* device() { return device_; }
34  template<class DeviceType> DeviceType* device() { return dynamic_cast<DeviceType*>(device_); }
35  DeviceOverwriteItem* item() { return item_; }
36  bool isNewDevice() const { return isNewDevice_; }
37  bool isNewItem() const { return isNewItem_; }
38 
39  private:
40  Device* device_;
41  DeviceOverwriteItem* item_;
42  bool isNewDevice_;
43  bool isNewItem_;
44  };
45 
46  DeviceInfo findOrCreateDevice(BodyItem* bodyItem, bool doCreateOverwriteItem = false);
47 
48 protected:
49  DeviceOverwriteMediator(const std::string& id);
50  static bool restoreDeviceName(Device* device, const Mapping* info);
51  static bool restoreDeviceLink(Device* device, const Mapping* info, Body* body);
52 
53 private:
54  std::string id_;
55 };
56 
58 
59 
60 class CNOID_EXPORT DeviceOverwriteItem : public BodyElementOverwriteItem,
61  public LocatableItem, public RenderableItem
62 {
63 public:
64  static void initializeClass(ExtensionManager* ext);
65 
68  virtual ~DeviceOverwriteItem();
69 
70  virtual bool setName(const std::string& name) override;
71 
72  bool setDevice(BodyItem* bodyItem, Device* device, bool isAdditionalDevice);
73  Device* device();
74  template<class DeviceType> DeviceType* device() {
75  return dynamic_cast<DeviceType*>(device());
76  }
77  bool isAdditionalDevice() const;
78  bool isPreExistingDevice() const;
79 
80  // This function returns the original device before overwriting if it is
81  // originally existing one.
82  Device* originalDevice();
83 
84  bool restoreOriginalDevice();
85 
86  void setMediatorId(const std::string& id);
87 
88  // LocatableItem function
89  virtual LocationProxyPtr getLocationProxy() override;
90 
91  // RenderableItem function. This returns the coordinate marker.
92  virtual SgNode* getScene() override;
93 
94  class Impl;
95 
96 protected:
97  virtual Item* doDuplicate(Item* duplicatedParentItem) const override;
98  virtual void onDisconnectedFromBodyItem() override;
99  virtual void doPutProperties(PutPropertyFunction& putProperty) override;
100  virtual bool store(Archive& archive) override;
101  virtual bool restore(const Archive& archive) override;
102 
103 private:
104  Impl* impl;
105 };
106 
108 
109 }
110 
111 #endif
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::DeviceOverwriteMediator::DeviceInfo::isNewDevice
bool isNewDevice() const
Definition: DeviceOverwriteItem.h:36
cnoid::DeviceOverwriteMediator::DeviceInfo
Definition: DeviceOverwriteItem.h:28
cnoid::ExtensionManager
Definition: ExtensionManager.h:23
cnoid::PutPropertyFunction
Definition: PutPropertyFunction.h:51
BodyElementOverwriteItem.h
cnoid::DeviceOverwriteMediatorPtr
ref_ptr< DeviceOverwriteMediator > DeviceOverwriteMediatorPtr
Definition: DeviceOverwriteItem.h:57
cnoid::Archive
Definition: Archive.h:22
cnoid::DeviceOverwriteItem
Definition: DeviceOverwriteItem.h:60
cnoid::DeviceOverwriteMediator::id
const std::string & id()
Definition: DeviceOverwriteItem.h:23
cnoid::DeviceOverwriteMediator::DeviceInfo::device
DeviceType * device()
Definition: DeviceOverwriteItem.h:34
cnoid::ref_ptr
Definition: Referenced.h:103
cnoid::BodyItem
Definition: BodyItem.h:28
cnoid::DeviceOverwriteMediator::DeviceInfo::isNewItem
bool isNewItem() const
Definition: DeviceOverwriteItem.h:37
cnoid::DeviceOverwriteMediator::DeviceInfo::DeviceInfo
DeviceInfo(Device *device, DeviceOverwriteItem *item, bool isNewDevice, bool isNewItem)
Definition: DeviceOverwriteItem.h:30
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::RenderableItem
Definition: RenderableItem.h:11
cnoid::DeviceOverwriteItemPtr
ref_ptr< DeviceOverwriteItem > DeviceOverwriteItemPtr
Definition: DeviceOverwriteItem.h:107
cnoid::DeviceOverwriteMediator::DeviceInfo::device
Device * device()
Definition: DeviceOverwriteItem.h:33
cnoid::Referenced
Definition: Referenced.h:54
cnoid::DeviceOverwriteMediator
Definition: DeviceOverwriteItem.h:17
cnoid::Body
Definition: Body.h:28
cnoid::BodyElementOverwriteItem
Definition: BodyElementOverwriteItem.h:15
cnoid::Device
Definition: Device.h:53
cnoid::SgNode
Definition: SceneGraph.h:157
cnoid::LocatableItem
Definition: LocatableItem.h:62
cnoid::DeviceOverwriteItem::device
DeviceType * device()
Definition: DeviceOverwriteItem.h:74
cnoid::DeviceOverwriteMediator::DeviceInfo::item
DeviceOverwriteItem * item()
Definition: DeviceOverwriteItem.h:35