Go to the documentation of this file.
5 #ifndef CNOID_UTIL_POLYMORPHIC_FUNCTION_SET_H
6 #define CNOID_UTIL_POLYMORPHIC_FUNCTION_SET_H
14 template<
class ObjectBase>
18 typedef std::function<void(ObjectBase* obj)>
Function;
22 int validDispatchTableSize;
23 std::vector<Function> dispatchTable;
24 std::vector<bool> isFixed;
30 validDispatchTableSize = 0;
34 return dispatchTable.empty();
39 int id = registry.
classId(type);
41 if(validDispatchTableSize >
id + 1){
42 validDispatchTableSize =
id + 1;
44 if(
id >=
static_cast<int>(dispatchTable.size())){
45 dispatchTable.resize(
id + 1);
46 isFixed.resize(
id + 1,
false);
48 dispatchTable[id] = func;
53 template <
class Object>
59 template <
class Object>
62 setFunction<Object>([func](ObjectBase* obj){ func(
static_cast<Object*
>(obj)); });
65 template <
class Object>
68 int id = registry.template classId<Object>();
69 if(
id >= 0 &&
id < dispatchTable.size()){
70 if(validDispatchTableSize >
id){
71 validDispatchTableSize = id;
73 dispatchTable[id] =
nullptr;
84 if(numClasses == validDispatchTableSize){
85 return idToCheck < numClasses;
88 if(dispatchTable.size() != numClasses){
89 dispatchTable.resize(numClasses);
90 isFixed.resize(numClasses,
false);
93 const int n = dispatchTable.size();
94 for(
int i = validDispatchTableSize; i < n; ++i){
96 dispatchTable[i] =
nullptr;
100 for(
int i = validDispatchTableSize; i < n; ++i){
103 validDispatchTableSize = n;
105 return idToCheck < validDispatchTableSize;
110 if(dispatchTable[
id]){
114 if(superClassId < 0){
119 dispatchTable[id] = dispatchTable[functionId];
126 auto id = obj->classId();
127 if(
id >= validDispatchTableSize){
132 return dispatchTable[id] !=
nullptr;
135 inline void dispatch(ObjectBase* obj,
const int id)
const
137 if(
id >= validDispatchTableSize){
142 auto&
function = dispatchTable[id];
153 template <
class Object>
156 dispatch(obj, registry.template classId<Object>(0));
163 pfs.dispatchAs<Object>(obj);
170 return Dispatcher(*
this);
bool empty() const
Definition: PolymorphicFunctionSet.h:33
Dispatcher dispatcher() const
Definition: PolymorphicFunctionSet.h:169
void setFunction(std::function< void(Object *obj)> func)
Definition: PolymorphicFunctionSet.h:60
std::function< void(ObjectBase *obj)> Function
Definition: PolymorphicFunctionSet.h:18
int superClassId(int classId) const
Definition: HierarchicalClassRegistry.cpp:88
int classId(int unknownClassId=-1) const
Definition: HierarchicalClassRegistry.h:51
void resetFunction(bool doUpdate=false)
Definition: PolymorphicFunctionSet.h:66
void dispatchAs(Object *obj) const
Definition: PolymorphicFunctionSet.h:162
Definition: AbstractSceneLoader.h:11
bool updateDispatchTable(int idToCheck=0)
Definition: PolymorphicFunctionSet.h:81
void setFunction(const std::type_info &type, Function func)
Definition: PolymorphicFunctionSet.h:37
void setFunction(Function func)
Definition: PolymorphicFunctionSet.h:54
int numRegisteredClasses() const
Definition: HierarchicalClassRegistry.cpp:95
Dispatcher(const PolymorphicFunctionSet< ObjectBase > &pfs)
Definition: PolymorphicFunctionSet.h:161
Definition: PolymorphicFunctionSet.h:159
void dispatch(ObjectBase *obj, const int id) const
Definition: PolymorphicFunctionSet.h:135
void dispatch(ObjectBase *obj) const
Definition: PolymorphicFunctionSet.h:148
void dispatchAs(Object *obj) const
Definition: PolymorphicFunctionSet.h:154
Definition: PolymorphicFunctionSet.h:15
PolymorphicFunctionSet(HierarchicalClassRegistry< ObjectBase > ®istry)
Definition: PolymorphicFunctionSet.h:27
bool hasFunctionFor(ObjectBase *obj) const
Definition: PolymorphicFunctionSet.h:124
int setSuperClassFunction(int id)
Definition: PolymorphicFunctionSet.h:108