$darkmode
uid_tracker.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
29 #pragma once
30 #ifndef CLOE_UTILITY_UID_TRACKER_HPP_
31 #define CLOE_UTILITY_UID_TRACKER_HPP_
32 
33 #include <cassert> // for assert
34 #include <functional> // for std::function
35 #include <map> // for std::map
36 #include <memory> // for std::shared_ptr
37 #include <set> // for std::set
38 #include <vector> // for std::vector
39 
40 namespace cloe {
41 namespace utility {
42 
62  // Tracklet is used internally
63  struct Tracklet {
64  int in_id;
65  int out_id;
66  int age;
67  };
68 
69  int ttl_; // time to live before reusing
70  std::map<int, std::shared_ptr<Tracklet>> assigned_;
71  std::set<int> free_;
72  std::vector<std::function<void(int in_id, int out_id)>> observers_;
73 
74  public:
80  UniqueIDTracker(int min, int max, int ttl = 1) : ttl_(ttl) {
81  assert(min < max);
82  assert(ttl_ >= 1); // TODO(unknown): perhaps 0 is ok too
83 
84  for (int i = min; i <= max; i++) {
85  free_.insert(i);
86  }
87  }
88 
89  virtual ~UniqueIDTracker() = default;
90 
104  int assign(int id);
105 
110  void next_cycle();
111 
116  void add_delete_observer(void f(int in_id, int out_id)) { observers_.emplace_back(f); }
117 };
118 
119 } // namespace utility
120 } // namespace cloe
121 
122 #endif // CLOE_UTILITY_UID_TRACKER_HPP_
void next_cycle()
Definition: uid_tracker.cpp:62
int assign(int id)
Definition: uid_tracker.cpp:38
Definition: coordinator.hpp:36
UniqueIDTracker(int min, int max, int ttl=1)
Definition: uid_tracker.hpp:80
Definition: uid_tracker.hpp:61
void add_delete_observer(void f(int in_id, int out_id))
Definition: uid_tracker.hpp:116