cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
method_name_view.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
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 */
20#pragma once
21
22// IWYU pragma: no_include <typeindex>
23
24#include <cstddef>
25#include <functional>
26#include <string>
27#include <string_view>
28
29#include <fmt/base.h>
30#include <fmt/format.h>
31
33
34namespace msgpack_rpc::messages {
35
40public:
49 MethodNameView( // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
50 const MethodName& name) noexcept
51 : name_(name.name()) {
52 // Validation is done in MethodName.
53 }
54
63 MethodNameView( // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
64 const std::string& name)
65 : name_(name) {
66 // TODO Add a validation of UTF-8.
67 }
68
77 MethodNameView( // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
78 std::string_view name)
79 : name_(name) {
80 // TODO Add a validation of UTF-8.
81 }
82
92 MethodNameView(const char* name, std::size_t size) : name_(name, size) {
93 // TODO Add a validation of UTF-8.
94 }
95
104 MethodNameView( // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
105 const char* name)
106 : name_(name) {
107 // TODO Add a validation of UTF-8.
108 }
109
115 explicit operator MethodName() const { return MethodName(name_); }
116
122 [[nodiscard]] std::string_view name() const noexcept { return name_; }
123
124private:
126 std::string_view name_;
127};
128
137[[nodiscard]] inline bool operator==(
138 MethodNameView left, MethodNameView right) noexcept {
139 return left.name() == right.name();
140}
141
150[[nodiscard]] inline bool operator!=(
151 MethodNameView left, MethodNameView right) noexcept {
152 return left.name() != right.name();
153}
154
155} // namespace msgpack_rpc::messages
156
157namespace fmt {
158
163template <>
164class formatter<msgpack_rpc::messages::MethodNameView>
165 : public formatter<std::string_view> {
166public:
174 format_context::iterator format(
176 format_context& context) const {
177 return formatter<std::string_view>::format(val.name(), context);
178 }
179};
180
181} // namespace fmt
182
183namespace std {
184
188template <>
189struct hash<msgpack_rpc::messages::MethodNameView> {
190public:
197 std::size_t operator()(
198 const msgpack_rpc::messages::MethodNameView& value) const {
199 return string_view_hash_(value.name());
200 }
201
202private:
204 std::hash<std::string_view> string_view_hash_{};
205};
206
207} // namespace std
format_context::iterator format(const msgpack_rpc::messages::MethodNameView &val, format_context &context) const
Format a value.
MethodNameView(const std::string &name)
Constructor.
MethodNameView(const MethodName &name) noexcept
Constructor.
MethodNameView(const char *name, std::size_t size)
Constructor.
MethodNameView(std::string_view name)
Constructor.
std::string_view name() const noexcept
Get the method name.
std::string_view name_
Method name.
MethodNameView(const char *name)
Constructor.
Class of method names.
Definition method_name.h:35
Definition of MethodName class.
Namespace of fmt library.
Definition uri.h:113
Namespace of messages.
Definition buffer_view.h:24
bool operator!=(const MethodName &left, const MethodName &right) noexcept
Compare two method names.
bool operator==(const MethodName &left, const MethodName &right) noexcept
Compare two method names.
Namespace of cpp-msgpack-rpc library.
Definition i_address.h:26
STL namespace.
std::size_t operator()(const msgpack_rpc::messages::MethodNameView &value) const
Calculate the hash number of a value.
std::hash< std::string_view > string_view_hash_
Hash of string_view.