YARP
Yet Another Robot Platform
Election.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_OS_ELECTION_H
8 #define YARP_OS_ELECTION_H
9 
10 #include <yarp/os/Log.h>
11 
12 #include <map>
13 #include <mutex>
14 #include <string>
15 
16 namespace yarp {
17 namespace os {
18 
19 template <class T>
21 {
22 public:
23  typedef T peer_type;
24  typedef std::map<T*, bool> map_type;
26  typedef typename map_type::iterator iterator;
27  typedef typename map_type::const_iterator const_iterator;
28 
29  PeerRecord() = default;
30  PeerRecord(const PeerRecord& alt) = default;
31 
32  void add(T* entity)
33  {
34  peerSet[entity] = true;
35  }
36 
37  void remove(T* entity)
38  {
39  peerSet.erase(entity);
40  }
41 
42  T* getFirst()
43  {
44  if (peerSet.begin() != peerSet.end()) {
45  return peerSet.begin()->first;
46  }
47  return nullptr;
48  }
49 };
50 
51 
59 template <class PR>
61 {
62 private:
63  typedef void* voidPtr;
64 
65  std::mutex mutex;
66 
67  typedef typename std::map<std::string, PR> map_type;
68  map_type nameMap;
69  long ct{0};
70 
71  PR* getRecordRaw(const std::string& key, bool create = false)
72  {
73  typename map_type::iterator entry = nameMap.find(key);
74  if (entry == nameMap.end() && create) {
75  nameMap[key] = PR();
76  entry = nameMap.find(key);
77  }
78  if (entry == nameMap.end()) {
79  return nullptr;
80  }
81  return &(entry->second);
82  }
83 
84 public:
85  ElectionOf() = default;
86  virtual ~ElectionOf() = default;
87 
88  PR* add(const std::string& key, typename PR::peer_type* entity)
89  {
90  mutex.lock();
91  ct++;
92  PR* rec = getRecordRaw(key, true);
93  yAssert(rec);
94  rec->add(entity);
95  mutex.unlock();
96  return rec;
97  }
98 
99  void remove(const std::string& key, typename PR::peer_type* entity)
100  {
101  mutex.lock();
102  ct++;
103  PR* rec = getRecordRaw(key, false);
104  yAssert(rec);
105  rec->remove(entity);
106  mutex.unlock();
107  }
108 
109  typename PR::peer_type* getElect(const std::string& key)
110  {
111  mutex.lock();
112  PR* rec = getRecordRaw(key, false);
113  mutex.unlock();
114  if (rec) {
115  return rec->getFirst();
116  }
117  return nullptr;
118  }
119 
120  PR* getRecord(const std::string& key)
121  {
122  mutex.lock();
123  PR* rec = getRecordRaw(key, false);
124  mutex.unlock();
125  return rec;
126  }
127 
129  {
130  return ct;
131  }
132 
133  void lock()
134  {
135  mutex.lock();
136  }
137 
138  void unlock()
139  {
140  mutex.unlock();
141  }
142 };
143 
144 } // namespace os
145 } // namespace yarp
146 
147 #endif // YARP_OS_ELECTION_H
#define yAssert(x)
Definition: Log.h:294
Pick one of a set of peers to be "active".
Definition: Election.h:61
PR::peer_type * getElect(const std::string &key)
Definition: Election.h:109
virtual ~ElectionOf()=default
PR * add(const std::string &key, typename PR::peer_type *entity)
Definition: Election.h:88
PR * getRecord(const std::string &key)
Definition: Election.h:120
void remove(const std::string &key, typename PR::peer_type *entity)
Definition: Election.h:99
void remove(T *entity)
Definition: Election.h:37
PeerRecord(const PeerRecord &alt)=default
map_type::iterator iterator
Definition: Election.h:26
void add(T *entity)
Definition: Election.h:32
map_type::const_iterator const_iterator
Definition: Election.h:27
std::map< T *, bool > map_type
Definition: Election.h:24
map_type peerSet
Definition: Election.h:25
The main, catch-all namespace for YARP.
Definition: dirs.h:16