Apollo 10.0
自动驾驶开放平台
context.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 The Apollo Authors. All Rights Reserved.
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#pragma once
17#include <map>
18#include <memory>
19#include <mutex>
20#include <shared_mutex>
21#include <string>
22#include <thread>
23
24namespace apollo {
25namespace cyber {
26namespace context {
27
28class Context {
29 public:
30 Context() = default;
31 ~Context() = default;
32
40 template <typename T>
41 void Set(const std::string &key, const std::shared_ptr<T> &value);
42
50 template <typename T>
51 void Set(const std::string &key, const T &value);
52
60 template <typename T>
61 std::shared_ptr<T> Get(const std::string &key) const;
62
70 template <typename T>
71 void SafeSet(const std::string &key, const std::shared_ptr<T> &value);
72
80 template <typename T>
81 void SafeSet(const std::string &key, const T &value);
82
90 template <typename T>
91 std::shared_ptr<T> SafeGet(const std::string &key) const;
92
93 private:
94 std::map<std::string, std::shared_ptr<void>> m_map_;
95 std::mutex m_mutex_;
96};
97
98template <typename T>
99void Context::Set(const std::string &key, const std::shared_ptr<T> &value) {
100 m_map_[key] = value;
101}
102
103template <typename T>
104void Context::Set(const std::string &key, const T &value) {
105 m_map_[key] = std::make_shared<T>(value);
106}
107template <typename T>
108std::shared_ptr<T> Context::Get(const std::string &key) const {
109 auto it = m_map_.find(key);
110 if (it != m_map_.end()) {
111 return std::static_pointer_cast<T>(it->second);
112 }
113 return nullptr;
114}
115
116template <typename T>
117void Context::SafeSet(const std::string &key, const std::shared_ptr<T> &value) {
118 std::lock_guard<std::mutex> lock(m_mutex_);
119 return Set(key, value);
120}
121
122template <typename T>
123void Context::SafeSet(const std::string &key, const T &value) {
124 std::lock_guard<std::mutex> lock(m_mutex_);
125 return Set(key, value);
126}
127
128template <typename T>
129std::shared_ptr<T> Context::SafeGet(const std::string &key) const {
130 std::lock_guard<std::mutex> lock(m_mutex_);
131 return Get<T>(key);
132}
133
134} // namespace context
135} // namespace cyber
136} // namespace apollo
std::shared_ptr< T > Get(const std::string &key) const
Get value from context.
Definition context.h:108
void Set(const std::string &key, const std::shared_ptr< T > &value)
Set value to context.
Definition context.h:99
std::shared_ptr< T > SafeGet(const std::string &key) const
Get value from context safely.
Definition context.h:129
void SafeSet(const std::string &key, const std::shared_ptr< T > &value)
Set value to context safely.
Definition context.h:117
class register implement
Definition arena_queue.h:37