Choreonoid  1.8
DataMap.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_DATA_MAP_H
7 #define CNOID_UTIL_DATA_MAP_H
8 
9 #include <map>
10 #include <vector>
11 #include <string>
12 #include "exportdecl.h"
13 
14 namespace cnoid {
15 
16 class CNOID_EXPORT DataMapBase
17 {
18 public:
19  static const int MIN_DYNAMIC_ID = 10000;
20  virtual ~DataMapBase();
21 
22  int getDynamicID(const std::string& name);
23  const std::string& getDynamicIDname(int id);
24 
25 protected:
26  virtual std::map<std::string, int>& nameToIdMap();
27  virtual std::map<int, std::string>& idToNameMap();
28  virtual int nextDynamicId();
29 };
30 
31 
32 template <class ElementType = double, class Allocator = std::allocator<ElementType> >
33 class DataMap : public DataMapBase,
34  protected std::map<int, std::vector<ElementType, Allocator> >
35 {
36  typedef std::map<int, std::vector<ElementType, Allocator> > MapType;
37 
38  static const std::vector<ElementType, Allocator> emptyData;
39 
40 public:
41 
42  typedef std::vector<ElementType, Allocator> Data;
43 
44  DataMap() { }
45 
46  DataMap(const DataMap& org) : MapType(org) { }
47 
48  DataMap& operator=(const DataMap& rhs) {
49  MapType::operator=(rhs);
50  return *this;
51  }
52 
53 #ifndef _MSC_VER
54  using typename MapType::iterator;
55  using MapType::size;
56  using MapType::empty;
57  using MapType::clear;
58  using MapType::begin;
59  using MapType::end;
60  using MapType::find;
61 #else
62  // In VC++, the above declarations produce C2487 error in compiling a class
63  // which inherits this class. The following code is used instead of the above one.
64  typedef typename MapType::iterator iterator;
65  size_t size() const { return MapType::size(); }
66  bool empty() const { return MapType::empty(); }
67  void clear() { MapType::clear(); }
68  typename MapType::iterator begin() { return MapType::begin(); }
69  typename MapType::const_iterator begin() const { return MapType::begin(); }
70  typename MapType::iterator end() { return MapType::end(); }
71  typename MapType::const_iterator end() const { return MapType::end(); }
72  typename MapType::iterator find(const typename MapType::key_type& x) { return MapType::find(x); }
73  typename MapType::const_iterator find(const typename MapType::key_type& x) const { return MapType::find(x); }
74 #endif
75  Data& data(int id) { return (*this)[id]; }
76 
77  const Data& data(int id) const {
78  typename MapType::const_iterator p = find(id);
79  if(p != end()){
80  return p->second;
81  }
82  return emptyData;
83  }
84 };
85 
86 template <class ElementType, class Allocator>
87 const std::vector<ElementType, Allocator> DataMap<ElementType, Allocator>::emptyData;
88 
89 }
90 
91 #endif
cnoid::DataMap::data
Data & data(int id)
Definition: DataMap.h:75
cnoid::DataMap::operator=
DataMap & operator=(const DataMap &rhs)
Definition: DataMap.h:48
cnoid::DataMap::DataMap
DataMap()
Definition: DataMap.h:44
cnoid::DataMap::data
const Data & data(int id) const
Definition: DataMap.h:77
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::DataMap::Data
std::vector< ElementType, Allocator > Data
Definition: DataMap.h:42
cnoid::DataMap
Definition: DataMap.h:33
cnoid::DataMap::DataMap
DataMap(const DataMap &org)
Definition: DataMap.h:46
cnoid::DataMapBase
Definition: DataMap.h:16