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 #pragma once
32 
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() = default;
56 };
57 
68 template <class ResourceHandle>
70 {
71 public:
77  std::vector<std::string> getNames() const
78  {
79  std::vector<std::string> out;
80  out.reserve(resource_map_.size());
81  for (const auto& resource_name_and_handle : resource_map_)
82  {
83  out.push_back(resource_name_and_handle.first);
84  }
85  return out;
86  }
87 
93  void registerHandle(const ResourceHandle& handle)
94  {
95  typename ResourceMap::iterator it = resource_map_.find(handle.getName());
96  if (it == resource_map_.end())
97  {
98  resource_map_.insert(std::make_pair(handle.getName(), handle));
99  }
100  else
101  {
102  ROS_WARN_STREAM("Replacing previously registered handle '" << handle.getName() << "' in '" +
103  internal::demangledTypeName(*this) + "'.");
104  it->second = handle;
105  }
106  }
107 
113  ResourceHandle getHandle(const std::string& name)
114  {
115  typename ResourceMap::const_iterator it = resource_map_.find(name);
116 
117  if (it == resource_map_.end())
118  {
119  throw std::logic_error("Could not find resource '" + name + "' in '" +
120  internal::demangledTypeName(*this) + "'.");
121  }
122 
123  return it->second;
124  }
125 
134  static void concatManagers(std::vector<ResourceManagerType*>& managers,
135  ResourceManagerType* result)
136  {
137  for (const auto& manager : managers) {
138  for (const auto& handle_name : manager->getNames()) {
139  result->registerHandle(manager->getHandle(handle_name));
140  }
141  }
142  }
143 
144  /*\}*/
145 
146 protected:
147  typedef std::map<std::string, ResourceHandle> ResourceMap;
149 };
150 
151 }
hardware_interface::ResourceManager::getNames
std::vector< std::string > getNames() const
Definition: resource_manager.h:77
hardware_interface::ResourceManager::ResourceMap
std::map< std::string, ResourceHandle > ResourceMap
Definition: resource_manager.h:147
hardware_interface::internal::demangledTypeName
std::string demangledTypeName()
Convenience method for demangling type names.
Definition: demangle_symbol.h:73
console.h
ROS_WARN_STREAM
#define ROS_WARN_STREAM(args)
hardware_interface
Definition: actuator_command_interface.h:36
hardware_interface::ResourceManagerBase
Non-templated Base Class that contains a virtual destructor.
Definition: resource_manager.h:52
hardware_interface::ResourceManager::ResourceManagerType
ResourceManager< ResourceHandle > ResourceManagerType
Definition: resource_manager.h:72
hardware_interface::ResourceManager::registerHandle
void registerHandle(const ResourceHandle &handle)
Register a new resource. If the resource name already exists, the previously stored resource value wi...
Definition: resource_manager.h:93
demangle_symbol.h
hardware_interface::ResourceManager
Class for handling named resources.
Definition: resource_manager.h:69
hardware_interface::ResourceManager::getHandle
ResourceHandle getHandle(const std::string &name)
Get a resource handle by name.
Definition: resource_manager.h:113
hardware_interface::ResourceManagerBase::~ResourceManagerBase
virtual ~ResourceManagerBase()=default
hardware_interface::ResourceManager::concatManagers
static void concatManagers(std::vector< ResourceManagerType * > &managers, ResourceManagerType *result)
Combine a list of interfaces into one.
Definition: resource_manager.h:134
hardware_interface::ResourceManager::resource_map_
ResourceMap resource_map_
Definition: resource_manager.h:148


hardware_interface
Author(s): Wim Meeussen, Adolfo Rodriguez Tsouroukdissian
autogenerated on Fri Nov 3 2023 02:07:57