Choreonoid  1.5
DataMap.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_DATA_MAP_H_INCLUDED
7 #define CNOID_UTIL_DATA_MAP_H_INCLUDED
8 
9 #include <map>
10 #include <vector>
11 #include <string>
12 #include "exportdecl.h"
13 
14 namespace cnoid {
15 
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 #ifndef _MSC_VER
45  using typename MapType::iterator;
46  using MapType::size;
47  using MapType::empty;
48  using MapType::clear;
49  using MapType::begin;
50  using MapType::end;
51  using MapType::find;
52 #else
53  // In VC++, the above declarations produce C2487 error in compiling a class
54  // which inherits this class. So we use the following code instead of the above one.
55  typedef typename MapType::iterator iterator;
56  size_t size() const { return MapType::size(); }
57  bool empty() const { return MapType::empty(); }
58  void clear() { MapType::clear(); }
59  typename MapType::iterator begin() { return MapType::begin(); }
60  typename MapType::const_iterator begin() const { return MapType::begin(); }
61  typename MapType::iterator end() { return MapType::end(); }
62  typename MapType::const_iterator end() const { return MapType::end(); }
63  typename MapType::iterator find(const typename MapType::key_type& x) { return MapType::find(x); }
64  typename MapType::const_iterator find(const typename MapType::key_type& x) const { return MapType::find(x); }
65 #endif
66  Data& data(int id) { return (*this)[id]; }
67 
68  const Data& data(int id) const {
69  typename MapType::const_iterator p = find(id);
70  if(p != end()){
71  return p->second;
72  }
73  return emptyData;
74  }
75 
77  return *this == rhs;
78  }
79 };
80 
81 template <class ElementType, class Allocator>
82 const std::vector<ElementType, Allocator> DataMap<ElementType, Allocator>::emptyData;
83 }
84 
85 #endif
const Data & data(int id) const
Definition: DataMap.h:68
Definition: DataMap.h:33
std::vector< ElementType, Allocator > Data
Definition: DataMap.h:42
bool operator==(const DataMap< ElementType, Allocator > &rhs) const
Definition: DataMap.h:76
Definition: DataMap.h:16
Data & data(int id)
Definition: DataMap.h:66
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
#define CNOID_EXPORT
Definition: Util/exportdecl.h:37