Choreonoid  1.5
Item.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_BASE_ITEM_H
6 #define CNOID_BASE_ITEM_H
7 
8 #include "PutPropertyFunction.h"
9 #include <cnoid/Referenced>
10 #include <cnoid/Signal>
11 #include <cnoid/NullOut>
12 #include <ctime>
13 #include <bitset>
14 #include <string>
15 #include <list>
16 #include "exportdecl.h"
17 
18 namespace cnoid {
19 
20 class Item;
21 typedef ref_ptr<Item> ItemPtr;
22 
23 class RootItem;
24 class ItemTreeArchiver;
25 class ExtensionManager;
27 class Archive;
28 
39 {
40  template<class ItemType>
41  class ItemCallback
42  {
43  boost::function<bool(ItemType* item)> function;
44  public:
45  ItemCallback(boost::function<bool(ItemType* item)> f) : function(f) { }
46  bool operator()(Item* item) {
47  if(ItemType* casted = dynamic_cast<ItemType*>(item)){
48  return function(casted);
49  }
50  return false;
51  }
52  };
53 
54 protected:
55  Item();
56  Item(const Item& item);
57 
58 public:
59 
60  enum Attribute {
64  NUM_ATTRIBUTES
65  };
66 
67  virtual ~Item(); // The destructor should not be called in usual ways
68 
69  const std::string& name() const { return name_; }
70  virtual void setName(const std::string& name);
71 
72  bool hasAttribute(Attribute attribute) const { return attributes[attribute]; }
73 
74  Item* childItem() const { return firstChild_; }
75  Item* prevItem() const { return prevItem_; }
76  Item* nextItem() const { return nextItem_; }
77  Item* parentItem() const { return parent_; }
78 
79  bool addChildItem(Item* item, bool isManualOperation = false);
80  bool addSubItem(Item* item);
81  bool isSubItem() const;
82  //int subItemIndex() const;
83  //Item* subItem(int subItemIndex);
84  void detachFromParentItem();
85  void emitSigDetachedFromRootForSubTree();
86  bool insertChildItem(Item* item, Item* nextItem, bool isManualOperation = false);
87  bool insertSubItem(Item* item, Item* nextItem);
88 
89  bool isTemporal() const;
90  void setTemporal(bool on = true);
91 
92  RootItem* findRootItem() const;
93 
97  Item* findItem(const std::string& path) const;
98  template<class ItemType>
99  ItemType* findItem(const std::string& path) const {
100  return dynamic_cast<ItemType*>(findItem(path));
101  }
102 
103  static Item* find(const std::string& path);
104  template<class ItemType>
105  ItemType* find(const std::string& path) {
106  return dynamic_cast<ItemType*>(find(path));
107  }
108 
112  Item* findChildItem(const std::string& path) const;
113  template<class ItemType>
114  ItemType* findChildItem(const std::string& path) const {
115  return dynamic_cast<ItemType*>(findChildItem(path));
116  }
117 
121  Item* findSubItem(const std::string& path) const;
122  template<class ItemType>
123  ItemType* findSubItem(const std::string& path) const {
124  return dynamic_cast<ItemType*>(findSubItem(path));
125  }
126 
127  /*
128  The function 'template <class ItemType> ItemList<ItemType> getSubItems() const'
129  has been removed. Please use ItemList::extractChildItems(Item* item) instead of it.
130  */
131 
132  Item* headItem() const;
133 
134  template <class ItemType> ItemType* findOwnerItem(bool includeSelf = false) {
135  Item* parentItem__ = includeSelf ? this : parentItem();
136  while(parentItem__){
137  ItemType* ownerItem = dynamic_cast<ItemType*>(parentItem__);
138  if(ownerItem){
139  return ownerItem;
140  }
141  parentItem__ = parentItem__->parentItem();
142  }
143  return 0;
144  }
145 
146  bool isOwnedBy(Item* item) const;
147 
148  bool traverse(boost::function<bool(Item*)> function);
149 
150  template<class ItemType>
151  bool traverse(boost::function<bool(ItemType* item)> function){
152  return Item::traverse(ItemCallback<ItemType>(function));
153  }
154 
155  Item* duplicate() const;
156  Item* duplicateAll() const;
157 
158  void assign(Item* srcItem);
159 
160  bool load(const std::string& filename, const std::string& format = std::string());
161  bool load(const std::string& filename, Item* parent, const std::string& format = std::string());
162  bool save(const std::string& filename, const std::string& format = std::string());
163  bool overwrite(bool forceOverwrite = false, const std::string& format = std::string());
164 
165  const std::string& filePath() const { return filePath_; }
166  const std::string& fileFormat() const { return fileFormat_; }
167 
168 #ifdef CNOID_BACKWARD_COMPATIBILITY
169  const std::string& lastAccessedFilePath() const { return filePath_; }
170  const std::string& lastAccessedFileFormatId() const { return fileFormat_; }
171 #endif
172 
173  std::time_t fileModificationTime() const { return fileModificationTime_; }
174  bool isConsistentWithFile() const { return isConsistentWithFile_; }
175 
176  void clearFileInformation();
177 
178  void suggestFileUpdate() { isConsistentWithFile_ = false; }
179 
180  void putProperties(PutPropertyFunction& putProperty);
181 
182  virtual void notifyUpdate();
183 
185  return sigNameChanged_;
186  }
187 
192  return sigUpdated_;
193  }
194 
204  return sigPositionChanged_;
205  }
206 
211  return sigDetachedFromRoot_;
212  }
217  return sigDetachedFromRoot_;
218  }
219 
221  return sigSubTreeChanged_;
222  }
223 
224  virtual bool store(Archive& archive);
225  virtual bool restore(const Archive& archive);
226 
227  Referenced* customData(int id);
228  const Referenced* customData(int id) const;
229  void setCustomData(int id, ReferencedPtr data);
230  void clearCustomData(int id);
231 
233  return sigClassUnregistered_;
234  }
235 
236 protected:
237 
238  virtual void onConnectedToRoot();
239  virtual void onDisconnectedFromRoot();
240  virtual void onPositionChanged();
241  virtual bool onChildItemAboutToBeAdded(Item* childItem, bool isManualOperation);
242 
243  virtual Item* doDuplicate() const;
244  virtual void doAssign(Item* srcItem);
245  virtual void doPutProperties(PutPropertyFunction& putProperty);
246 
247  void setAttribute(Attribute attribute) { attributes.set(attribute); }
248  void unsetAttribute(Attribute attribute) { attributes.reset(attribute); }
249 
250 private:
251 
252  Item* parent_;
253  Item* firstChild_;
254  Item* lastChild_;
255  Item* prevItem_;
256  Item* nextItem_;
257 
258  int numChildren_;
259 
260  std::string name_;
261 
262  std::bitset<NUM_ATTRIBUTES> attributes;
263 
264  std::vector<int> extraStates;
265  std::vector<ReferencedPtr> extraData;
266 
268  Signal<void()> sigDetachedFromRoot_;
269  Signal<void()> sigUpdated_;
270  Signal<void()> sigPositionChanged_;
271  Signal<void()> sigSubTreeChanged_;
272 
273  static Signal<void(const char* type_info_name)> sigClassUnregistered_;
274 
275  // for file overwriting management, mainly accessed by ItemManagerImpl
276  bool isConsistentWithFile_;
277  std::string filePath_;
278  std::string fileFormat_;
279  std::time_t fileModificationTime_;
280 
281  // disable the assignment operator
282  Item& operator=(const Item& rhs);
283 
284  void init();
285  bool doInsertChildItem(Item* item, Item* nextItem, bool isManualOperation);
286  void callSlotsOnPositionChanged();
287  void callFuncOnConnectedToRoot();
288  void addToItemsToEmitSigSubTreeChanged();
289  void addToItemsToEmitSigSubTreeChangedSub(std::list<Item*>::iterator& pos);
290  void emitSigSubTreeChanged();
291 
292  void detachFromParentItemSub(bool isMoving);
293  bool traverse(Item* item, const boost::function<bool(Item*)>& function);
294  Item* duplicateAllSub(Item* duplicated) const;
295 
296  void updateFileInformation(const std::string& filename, const std::string& format);
297 
298  friend class RootItem;
299  friend class ItemTreeArchiver;
300  friend class ItemManagerImpl;
301 };
302 
303 #ifndef CNOID_BASE_MVOUT_DECLARED
304 #define CNOID_BASE_MVOUT_DECLARED
305 CNOID_EXPORT std::ostream& mvout(bool doFlush = false);
306 #endif
307 
308 }
309 
310 #endif
SignalProxy< void()> sigUpdated()
Definition: Item.h:191
SignalProxy< void()> sigDetachedFromRoot()
Definition: Item.h:210
const std::string & filePath() const
Definition: Item.h:165
bool isConsistentWithFile() const
Definition: Item.h:174
Definition: Archive.h:21
Item * nextItem() const
Definition: Item.h:76
Item * childItem() const
Definition: Item.h:74
Definition: ExtensionManager.h:26
ItemType * findSubItem(const std::string &path) const
Definition: Item.h:123
Definition: RootItem.h:23
Item * prevItem() const
Definition: Item.h:75
const std::string & fileFormat() const
Definition: Item.h:166
CNOID_EXPORT std::ostream & mvout(bool doFlush=false)
Definition: MessageView.cpp:1025
Definition: Item.h:61
Definition: Referenced.h:67
SignalProxy< void()> sigSubTreeChanged()
Definition: Item.h:220
void setAttribute(Attribute attribute)
Definition: Item.h:247
SignalProxy< void()> sigDisconnectedFromRoot()
Definition: Item.h:216
Definition: PutPropertyFunction.h:35
ItemType * find(const std::string &path)
Definition: Item.h:105
bool traverse(boost::function< bool(ItemType *item)> function)
Definition: Item.h:151
ItemType * findChildItem(const std::string &path) const
Definition: Item.h:114
Definition: Item.h:62
ItemType * findItem(const std::string &path) const
Definition: Item.h:99
Definition: Item.h:38
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
Attribute
Definition: Item.h:60
ItemType * findOwnerItem(bool includeSelf=false)
Definition: Item.h:134
SignalProxy< void(const std::string &oldName)> sigNameChanged()
Definition: Item.h:184
const std::string & name() const
Definition: Item.h:69
SignalProxy< void()> sigPositionChanged()
Definition: Item.h:203
Definition: Item.h:63
static SignalProxy< void(const char *type_info_name)> sigClassUnregistered()
Definition: Item.h:232
void unsetAttribute(Attribute attribute)
Definition: Item.h:248
#define CNOID_EXPORT
Definition: Util/exportdecl.h:37
bool traverse(boost::function< bool(Item *)> function)
Definition: Item.cpp:558
Definition: Signal.h:380
bool hasAttribute(Attribute attribute) const
Definition: Item.h:72
ref_ptr< Item > ItemPtr
Definition: Item.h:20
std::time_t fileModificationTime() const
Definition: Item.h:173
Item * parentItem() const
Definition: Item.h:77
Definition: ItemTreeArchiver.h:17
void suggestFileUpdate()
Definition: Item.h:178