MotionLib  1.0.0
SamBuCa motion library
DeviceStore.hpp
1 /*
2  * Copyright (C) 2021 CERN
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  * Created on: 2021-10-27
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  */
24 
25 #ifndef DEVICE_STORE_HPP__
26 #define DEVICE_STORE_HPP__
27 
28 #include <cstdint>
29 #include <list>
30 #include <map>
31 #include <memory>
32 #include <mutex>
33 #include <set>
34 #include <string>
35 #include <unordered_map>
36 
37 #include <ccut/yml.hpp>
38 
39 #include "DeviceBase.hpp"
40 #include "platform/PlatformFactory.hpp"
41 
42 namespace smc {
43 namespace internal {
44 
52 {
53 public:
54  typedef std::shared_ptr<DeviceStore> Shared;
55 
62  size_t probeDevices(const PlatformBase::Shared &platform);
63 
69  size_t getDeviceCount() const;
70 
78  DeviceBase::Shared getDevice(const DeviceId &address) const;
79 
83  template<typename T>
84  std::shared_ptr<T> getDevice(const DeviceId &address)
85  {
86  std::shared_ptr<T> ret{std::dynamic_pointer_cast<T>(getDevice(address))};
87  if (!ret)
88  {
89  throw Exception(ErrorCode::InvalidArguments,
90  "no such device: " + address);
91  }
92  return ret;
93  }
94 
98  std::set<DeviceId> getDeviceList() const;
99 
106  std::set<DeviceId> getDeviceList(DeviceType type) const;
107 
111  std::set<std::string> getAliasList() const;
112 
118  DeviceId getAlias(const std::string &alias) const;
119 
128  bool addAlias(const std::string &alias,
129  const DeviceId &deviceId,
130  bool override = true);
131 
135  void removeAlias(const std::string &alias);
136 
140  void loadAlias(const ccut::yml::NodeRef &aliasNode,
141  const ccut::yml::Parser *parser = nullptr);
142 
143 protected:
144  std::map<DeviceId, DeviceBase::Shared> m_devices;
145  std::unordered_map<std::string, DeviceId> m_alias;
146  mutable std::mutex m_devicesMutex;
147 };
148 
151 } /* namespace internal */
152 } /* namespace smc */
153 
154 #endif /* DEVICE_STORE_HPP__ */
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
DeviceId getAlias(const std::string &alias) const
Get the given alias.
std::shared_ptr< T > getDevice(const DeviceId &address)
Convenience function to get a typed device.
Definition: DeviceStore.hpp:84
DeviceBase::Shared getDevice(const DeviceId &address) const
Get the Device with specified address.
Definition: DeviceStore.cpp:67
void removeAlias(const std::string &alias)
remove an alias
bool addAlias(const std::string &alias, const DeviceId &deviceId, bool override=true)
add an alias
size_t probeDevices(const PlatformBase::Shared &platform)
Get devices generated by platform and store them.
Definition: DeviceStore.cpp:45
std::set< std::string > getAliasList() const
Get current Alias list.
std::set< DeviceId > getDeviceList() const
Get the complete list of existing devices.
Definition: DeviceStore.cpp:84
size_t getDeviceCount() const
Get the number of devices.
Definition: DeviceStore.cpp:61
void loadAlias(const ccut::yml::NodeRef &aliasNode, const ccut::yml::Parser *parser=nullptr)
load alias from yml node
DeviceType
List of possible device types supported.
Definition: DeviceBase.hpp:52
main motion-lib namespace
Definition: Client.cpp:30