Apache Mesos
v1.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __MESOS_CSI_V1_HPP__
18 #define __MESOS_CSI_V1_HPP__
19 
20 #include <ostream>
21 #include <type_traits>
22 
23 // ONLY USEFUL AFTER RUNNING PROTOC.
24 #include <csi/v1/csi.pb.h>
25 
26 // ONLY USEFUL AFTER RUNNING PROTOC WITH GRPC CPP PLUGIN.
27 #include <csi/v1/csi.grpc.pb.h>
28 
29 #include <google/protobuf/message.h>
30 
31 #include <google/protobuf/util/json_util.h>
32 #include <google/protobuf/util/message_differencer.h>
33 
34 #include <stout/check.hpp>
35 
36 namespace mesos {
37 namespace csi {
38 namespace v1 {
39 
40 using namespace ::csi::v1;
41 
42 constexpr char API_VERSION[] = "v1";
43 
44 } // namespace v1 {
45 } // namespace csi {
46 } // namespace mesos {
47 
48 
49 namespace csi {
50 namespace v1 {
51 
52 // Default implementation for comparing protobuf messages in namespace
53 // `csi::v1`. Note that any non-template overloading of the equality operator
54 // would take precedence over this function template.
55 template <
56  typename Message,
57  typename std::enable_if<std::is_convertible<
58  Message*, google::protobuf::Message*>::value, int>::type = 0>
59 bool operator==(const Message& left, const Message& right)
60 {
61  // NOTE: `MessageDifferencer::Equivalent` would ignore unknown fields and load
62  // default values for unset fields (which are indistinguishable in proto3).
63  return google::protobuf::util::MessageDifferencer::Equivalent(left, right);
64 }
65 
66 
67 template <
68  typename Message,
69  typename std::enable_if<std::is_convertible<
70  Message*, google::protobuf::Message*>::value, int>::type = 0>
71 bool operator!=(const Message& left, const Message& right)
72 {
73  return !(left == right);
74 }
75 
76 
77 std::ostream& operator<<(
78  std::ostream& stream,
80 
81 
82 // Default implementation for output protobuf messages in namespace `csi::v1`.
83 // Note that any non-template overloading of the output operator would take
84 // precedence over this function template.
85 template <
86  typename Message,
87  typename std::enable_if<std::is_convertible<
88  Message*, google::protobuf::Message*>::value, int>::type = 0>
89 std::ostream& operator<<(std::ostream& stream, const Message& message)
90 {
91  // NOTE: We use Google's JSON utility functions for proto3.
92  std::string output;
93  google::protobuf::util::Status status =
94  google::protobuf::util::MessageToJsonString(message, &output);
95 
96  CHECK(status.ok())
97  << "Could not convert messages to string: " << status.error_message();
98 
99  return stream << output;
100 }
101 
102 } // namespace v1 {
103 } // namespace csi {
104 
105 #endif // __MESOS_CSI_V1_HPP__
std::ostream & operator<<(std::ostream &stream, const Attribute &attribute)
constexpr char API_VERSION[]
Definition: v1.hpp:42
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
bool operator==(const Resource::ReservationInfo &left, const Resource::ReservationInfo &right)
Definition: v0.hpp:49
bool operator!=(const Resource::ReservationInfo &left, const Resource::ReservationInfo &right)
Definition: agent.hpp:25
Definition: v1.hpp:50
Type
Definition: capabilities.hpp:82
Try< uint32_t > type(const std::string &path)