Choreonoid  1.8
Archive.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_BASE_ARCHIVE_H
6 #define CNOID_BASE_ARCHIVE_H
7 
8 #include <cnoid/ValueTree>
9 #include <string>
10 #include <functional>
11 #include "exportdecl.h"
12 
13 namespace cnoid {
14 
15 class Item;
16 class View;
17 class ViewManager;
18 class ProjectManager;
19 class FilePathVariableProcessor;
20 class ArchiveSharedData;
21 
22 class CNOID_EXPORT Archive : public Mapping
23 {
24 public:
25  Archive();
26  Archive(int line, int column);
27  virtual ~Archive();
28 
29  void initSharedInfo(const std::string& projectFile, bool isSubProject);
30  void inheritSharedInfoFrom(Archive& archive);
31 
32  void addProcessOnSubTreeRestored(const std::function<void()>& func) const;
33  void addPostProcess(const std::function<void()>& func, int priority = 0) const;
34  void addFinalProcess(const std::function<void()>& func) const;
35 
36  Archive* findSubArchive(const std::string& name);
37  const Archive* findSubArchive(const std::string& name) const;
38  bool forSubArchive(const std::string& name, std::function<bool(const Archive& archive)> func) const;
39  Archive* openSubArchive(const std::string& name);
40  Archive* subArchive(Mapping* node);
41 
42  ValueNodePtr getItemId(const Item* item) const;
43  Item* findItem(const ValueNode* id) const;
44 
45  int getViewId(const View* view) const;
46  View* findView(int id) const;
47 
48  void clearIds();
49 
50  template<class ItemType> inline ItemType* findItem(ValueNode* id) const {
51  return dynamic_cast<ItemType*>(findItem(id));
52  }
53 
54  void writeItemId(const std::string& key, Item* item);
55 
56  template<class ItemType> inline ItemType* findItem(const std::string& key) const {
57  ValueNode* id = find(key);
58  return id->isValid() ? findItem<ItemType>(id) : 0;
59  }
60 
61  std::string resolveRelocatablePath(const std::string& relocatable, bool doAbsolutize = true) const;
62  bool readRelocatablePath(const std::string& key, std::string& out_value) const;
63  std::string readItemFilePath() const;
64 
66  [[deprecated("Use resolveRelocatablePath(path, false).")]]
67  std::string expandPathVariables(const std::string& path) const {
68  return resolveRelocatablePath(path, false);
69  }
70 
71  bool loadFileTo(Item* item) const;
72  bool loadFileTo(Item* item, const std::string& filepath) const;
73 
74  std::string getRelocatablePath(const std::string& path) const;
75  bool writeRelocatablePath(const std::string& key, const std::string& path);
76  bool writeFileInformation(Item* item);
77 
79  [[deprecated("Use loadFileTo(Item* item, const std::string& filepath)")]]
80  bool loadFileTo(const std::string& filepath, Item* item) const {
81  return loadFileTo(item, filepath);
82  }
84  [[deprecated]]
85  bool loadItemFile(Item* item, const std::string& fileNameKey, const std::string& fileFormatKey = std::string()) const;
86 
87  Item* currentParentItem() const;
88 
89  std::string projectDirectory() const;
90 
91  FilePathVariableProcessor* filePathVariableProcessor() const;
92 
93 private:
95 
96  Item* findItem(int id) const;
97  void setCurrentParentItem(Item* parentItem);
98  static Archive* invalidArchive();
99  void registerItemId(const Item* item, int id);
100  void registerViewId(const View* view, int id);
101 
102  // called from ItemTreeArchiver
103  void setPointerToProcessesOnSubTreeRestored(std::vector<std::function<void()>>* pfunc);
104  void callPostProcesses();
105  static void callFinalProcesses();
106 
107  friend class ItemTreeArchiver;
108  friend class ViewManager;
109  friend class ProjectManager;
110 };
111 
113 
114 }
115 
116 #endif
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::Archive::findItem
ItemType * findItem(const std::string &key) const
Definition: Archive.h:56
cnoid::View
Definition: View.h:27
cnoid::ValueNode
Definition: ValueTree.h:34
cnoid::Archive
Definition: Archive.h:22
cnoid::Archive::expandPathVariables
std::string expandPathVariables(const std::string &path) const
Definition: Archive.h:67
cnoid::ItemTreeArchiver
Definition: ItemTreeArchiver.h:17
cnoid::ref_ptr< ValueNode >
cnoid::ArchivePtr
ref_ptr< Archive > ArchivePtr
Definition: Archive.h:112
cnoid::FilePathVariableProcessor
Definition: FilePathVariableProcessor.h:18
cnoid::Archive::findItem
ItemType * findItem(ValueNode *id) const
Definition: Archive.h:50
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::ProjectManager
Definition: ProjectManager.h:18
cnoid::ViewManager
Definition: ViewManager.h:19
cnoid::Archive::loadFileTo
bool loadFileTo(const std::string &filepath, Item *item) const
Definition: Archive.h:80