Choreonoid  1.8
CloneMap.h
Go to the documentation of this file.
1 #ifndef CNOID_UTIL_CLONE_MAP_H
2 #define CNOID_UTIL_CLONE_MAP_H
3 
4 #include "Referenced.h"
5 #include <functional>
6 #include <vector>
7 #include "exportdecl.h"
8 
9 namespace cnoid {
10 
11 class ClonableReferenced;
12 
13 class CNOID_EXPORT CloneMap
14 {
15 public:
16  typedef std::function<Referenced*(const Referenced* org)> CloneFunction;
17 
18  CloneMap();
19  CloneMap(const CloneFunction& cloneFunction);
20  CloneMap(const CloneMap& org);
21  virtual ~CloneMap();
22 
23  void clear();
24 
25  void setClone(const Referenced* org, Referenced* clone);
26 
27  template<class ObjectType>
28  ObjectType* findClone(const ObjectType* org){
29  return static_cast<ObjectType*>(findClone_(org));
30  }
31 
32  template<class ObjectType>
33  ObjectType* findClone(ref_ptr<ObjectType> org){
34  return findClone<ObjectType>(org.get());
35  }
36 
37  template<class ObjectType>
38  ObjectType* getClone(const ObjectType* org){
39  return static_cast<ObjectType*>(findOrCreateClone_(org));
40  }
41 
42  template<class ObjectType>
43  ObjectType* getClone(ref_ptr<ObjectType> org){
44  return getClone<ObjectType>(org.get());
45  }
46 
47  /*
48  template<class ObjectType>
49  ObjectType* getClone(ref_ptr<const ObjectType> org){
50  return getClone<ObjectType>(org.get());
51  }
52  */
53 
54  template<class ObjectType>
55  ObjectType* getClone(const ObjectType* org, std::function<ObjectType*(const ObjectType* org)> cloneFunction){
56  return static_cast<ObjectType*>(
57  findOrCreateClone_(
58  org,
59  [cloneFunction](const Referenced* org) -> Referenced* {
60  return cloneFunction(static_cast<const ObjectType*>(org));
61  }));
62  }
63 
64  template<class ObjectType>
65  ObjectType* getClone(ref_ptr<ObjectType> org, std::function<ObjectType*(const ObjectType* org)> cloneFunction){
66  return getClone(org.get(), cloneFunction);
67  }
68 
69  /*
70  template<class ObjectType>
71  ObjectType* getClone(ref_ptr<const ObjectType> org, const CloneFunction& cloneFunction){
72  return getClone(org.get(), cloneFunction);
73  }
74  */
75 
76  template<class ObjectType>
78  const ObjectType* org, std::function<void(ObjectType* clone)> replaceFunction){
79  return static_cast<ObjectType*>(
80  findCloneOrReplaceLater_(
81  org,
82  [replaceFunction](Referenced* clone){
83  replaceFunction(static_cast<ObjectType*>(clone)); }));
84  }
85 
86  void replacePendingObjects();
87 
88  void setOriginalAsClone(const Referenced* org);
89 
90  class FlagId {
91  int id_;
92  public:
93  FlagId(const char* name) : id_(getFlagId(name)) { }
94  operator int() const { return id_; }
95  };
96 
97  bool flag(int id) const { return flags[id]; }
98  void setFlag(int id, bool on) { flags[id] = on; }
99 
100 private:
101  class Impl;
102  Impl* impl;
103  std::vector<bool> flags;
104 
105  Referenced* findClone_(const Referenced* org);
106  Referenced* findOrCreateClone_(const Referenced* org);
107  Referenced* findOrCreateClone_(const ClonableReferenced* org);
108  Referenced* findOrCreateClone_(const Referenced* org, const CloneFunction& cloneFunction);
109  Referenced* findCloneOrReplaceLater_(
110  const Referenced* org, std::function<void(Referenced* clone)> replaceFunction);
111 
112  static int getFlagId(const char* name);
113 };
114 
115 }
116 
117 #endif
cnoid::CloneMap::flag
bool flag(int id) const
Definition: CloneMap.h:97
Referenced.h
cnoid::CloneMap::getClone
ObjectType * getClone(ref_ptr< ObjectType > org)
Definition: CloneMap.h:43
cnoid::CloneMap::FlagId
Definition: CloneMap.h:90
cnoid::CloneMap::findClone
ObjectType * findClone(const ObjectType *org)
Definition: CloneMap.h:28
cnoid::ref_ptr
Definition: Referenced.h:103
cnoid::CloneMap::FlagId::FlagId
FlagId(const char *name)
Definition: CloneMap.h:93
cnoid::CloneMap::findCloneOrReplaceLater
ObjectType * findCloneOrReplaceLater(const ObjectType *org, std::function< void(ObjectType *clone)> replaceFunction)
Definition: CloneMap.h:77
cnoid::CloneMap::getClone
ObjectType * getClone(ref_ptr< ObjectType > org, std::function< ObjectType *(const ObjectType *org)> cloneFunction)
Definition: CloneMap.h:65
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::ClonableReferenced
Definition: ClonableReferenced.h:10
cnoid::CloneMap
Definition: CloneMap.h:13
cnoid::CloneMap::CloneFunction
std::function< Referenced *(const Referenced *org)> CloneFunction
Definition: CloneMap.h:16
cnoid::Referenced
Definition: Referenced.h:54
cnoid::CloneMap::getClone
ObjectType * getClone(const ObjectType *org, std::function< ObjectType *(const ObjectType *org)> cloneFunction)
Definition: CloneMap.h:55
cnoid::CloneMap::findClone
ObjectType * findClone(ref_ptr< ObjectType > org)
Definition: CloneMap.h:33
cnoid::CloneMap::setFlag
void setFlag(int id, bool on)
Definition: CloneMap.h:98
cnoid::CloneMap::getClone
ObjectType * getClone(const ObjectType *org)
Definition: CloneMap.h:38
cnoid::ref_ptr::get
T * get() const
Definition: Referenced.h:168