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