resource_manager.h
Go to the documentation of this file.
1 // Copyright (C) 2012, hiDOF INC.
3 // Copyright (C) 2013, PAL Robotics S.L.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of hiDOF, Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived from
14 // this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
28 
30 
31 #ifndef HARDWARE_INTERFACE_RESOURCE_MANAGER_H
32 #define HARDWARE_INTERFACE_RESOURCE_MANAGER_H
33 
34 #include <stdexcept>
35 #include <string>
36 #include <map>
37 #include <vector>
38 #include <utility> // for std::make_pair
39 
40 #include <ros/console.h>
41 
43 
44 namespace hardware_interface
45 {
46 
53 {
54 public:
55  virtual ~ResourceManagerBase() {}
56 };
57 
68 template <class ResourceHandle>
70 {
71 public:
76  virtual ~ResourceManager() {}
77 
79  std::vector<std::string> getNames() const
80  {
81  std::vector<std::string> out;
82  out.reserve(resource_map_.size());
83  for(typename ResourceMap::const_iterator it = resource_map_.begin(); it != resource_map_.end(); ++it)
84  {
85  out.push_back(it->first);
86  }
87  return out;
88  }
89 
95  void registerHandle(const ResourceHandle& handle)
96  {
97  typename ResourceMap::iterator it = resource_map_.find(handle.getName());
98  if (it == resource_map_.end())
99  {
100  resource_map_.insert(std::make_pair(handle.getName(), handle));
101  }
102  else
103  {
104  ROS_WARN_STREAM("Replacing previously registered handle '" << handle.getName() << "' in '" +
105  internal::demangledTypeName(*this) + "'.");
106  it->second = handle;
107  }
108  }
109 
115  ResourceHandle getHandle(const std::string& name)
116  {
117  typename ResourceMap::const_iterator it = resource_map_.find(name);
118 
119  if (it == resource_map_.end())
120  {
121  throw std::logic_error("Could not find resource '" + name + "' in '" +
122  internal::demangledTypeName(*this) + "'.");
123  }
124 
125  return it->second;
126  }
127 
136  static void concatManagers(std::vector<resource_manager_type*>& managers,
137  resource_manager_type* result)
138  {
139  for(typename std::vector<resource_manager_type*>::iterator it_man = managers.begin();
140  it_man != managers.end(); ++it_man) {
141  std::vector<std::string> handle_names = (*it_man)->getNames();
142  for(std::vector<std::string>::iterator it_nms = handle_names.begin();
143  it_nms != handle_names.end(); ++it_nms) {
144  result->registerHandle((*it_man)->getHandle(*it_nms));
145  }
146  }
147  }
148 
149  /*\}*/
150 
151 protected:
152  typedef std::map<std::string, ResourceHandle> ResourceMap;
153  ResourceMap resource_map_;
154 };
155 
156 }
157 
158 #endif // HARDWARE_INTERFACE_RESOURCE_MANAGER_H
std::string demangledTypeName()
Convenience method for demangling type names.
ResourceManager< ResourceHandle > resource_manager_type
Class for handling named resources.
ResourceHandle getHandle(const std::string &name)
Get a resource handle by name.
#define ROS_WARN_STREAM(args)
void registerHandle(const ResourceHandle &handle)
Register a new resource. If the resource name already exists, the previously stored resource value wi...
static void concatManagers(std::vector< resource_manager_type * > &managers, resource_manager_type *result)
Combine a list of interfaces into one.
std::map< std::string, ResourceHandle > ResourceMap
std::vector< std::string > getNames() const
Non-templated Base Class that contains a virtual destructor.


hardware_interface
Author(s): Wim Meeussen, Adolfo Rodriguez Tsouroukdissian
autogenerated on Mon Apr 20 2020 03:52:05