Apache Mesos
attributes.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 __ATTRIBUTES_HPP__
18 #define __ATTRIBUTES_HPP__
19 
20 #include <iterator>
21 #include <string>
22 
23 #include <mesos/mesos.hpp>
24 
25 #include <stout/option.hpp>
26 
27 namespace mesos {
28 
29 std::ostream& operator<<(std::ostream& stream, const Attribute& attribute);
30 
31 
33 {
34 public:
36 
37  /*implicit*/
38  Attributes(const google::protobuf::RepeatedPtrField<Attribute>& _attributes)
39  {
40  attributes.MergeFrom(_attributes);
41  }
42 
43  /*implicit*/
44  Attributes(const Attributes& that)
45  {
46  attributes.MergeFrom(that.attributes);
47  }
48 
50  {
51  if (this != &that) {
52  attributes.Clear();
53  attributes.MergeFrom(that.attributes);
54  }
55 
56  return *this;
57  }
58 
59  bool operator==(const Attributes& that) const;
60 
61 
62  bool operator!=(const Attributes& that) const
63  {
64  return !(*this == that);
65  }
66 
67  size_t size() const
68  {
69  return attributes.size();
70  }
71 
72  // Using this operator makes it easy to copy an attributes object into
73  // a protocol buffer field.
74  operator const google::protobuf::RepeatedPtrField<Attribute>&() const
75  {
76  return attributes;
77  }
78 
79  void add(const Attribute& attribute)
80  {
81  attributes.Add()->MergeFrom(attribute);
82  }
83 
84  const Attribute get(int index) const
85  {
86  return attributes.Get(index);
87  }
88 
89  // Gets an attribute with the same name and type as the given
90  // attribute.
91  const Option<Attribute> get(const Attribute& thatAttribute) const;
92 
93  template <typename T>
94  T get(const std::string& name, const T& t) const;
95 
96  // Checks if these Attributes contain an attribute with the same
97  // name, type and value as the given attribute.
98  bool contains(const Attribute& attribute) const;
99 
100  typedef google::protobuf::RepeatedPtrField<Attribute>::iterator
102 
103  typedef google::protobuf::RepeatedPtrField<Attribute>::const_iterator
105 
106  iterator begin() { return attributes.begin(); }
107  iterator end() { return attributes.end(); }
108 
109  const_iterator begin() const { return attributes.begin(); }
110  const_iterator end() const { return attributes.end(); }
111 
112  static Attribute parse(const std::string& name, const std::string& value);
113  static Attributes parse(const std::string& s);
114 
115  static bool isValid(const Attribute& attribute);
116 
117 private:
118  google::protobuf::RepeatedPtrField<Attribute> attributes;
119 };
120 
121 } // namespace mesos {
122 
123 #endif // __ATTRIBUTES_HPP__
std::ostream & operator<<(std::ostream &stream, const Attribute &attribute)
size_t size() const
Definition: attributes.hpp:67
Definition: option.hpp:28
const_iterator begin() const
Definition: attributes.hpp:109
bool contains(const Attribute &attribute) const
iterator begin()
Definition: attributes.hpp:106
google::protobuf::RepeatedPtrField< Attribute >::iterator iterator
Definition: attributes.hpp:101
static bool isValid(const Attribute &attribute)
google::protobuf::RepeatedPtrField< Attribute >::const_iterator const_iterator
Definition: attributes.hpp:104
const_iterator end() const
Definition: attributes.hpp:110
void add(const Attribute &attribute)
Definition: attributes.hpp:79
bool operator==(const Attributes &that) const
Attributes(const google::protobuf::RepeatedPtrField< Attribute > &_attributes)
Definition: attributes.hpp:38
static Attribute parse(const std::string &name, const std::string &value)
bool operator!=(const Attributes &that) const
Definition: attributes.hpp:62
iterator end()
Definition: attributes.hpp:107
Attributes & operator=(const Attributes &that)
Definition: attributes.hpp:49
Attributes(const Attributes &that)
Definition: attributes.hpp:44
constexpr const char * name
Definition: shell.hpp:41
Attributes()
Definition: attributes.hpp:35
Definition: attributes.hpp:32