Apache Mesos
values.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 __COMMON_VALUES_HPP__
18 #define __COMMON_VALUES_HPP__
19 
20 #include <limits>
21 #include <type_traits>
22 #include <vector>
23 
24 #include <mesos/mesos.hpp>
25 
26 #include <stout/foreach.hpp>
27 #include <stout/interval.hpp>
28 #include <stout/try.hpp>
29 
30 namespace mesos {
31 namespace internal {
32 namespace values {
33 
34 // Convert Ranges value to IntervalSet value.
35 template <typename T>
36 Try<IntervalSet<T>> rangesToIntervalSet(const Value::Ranges& ranges)
37 {
38  IntervalSet<T> set;
39 
40  static_assert(
41  std::is_integral<T>::value,
42  "IntervalSet<T> must use an integral type");
43 
44  foreach (const Value::Range& range, ranges.range()) {
45  if (range.begin() < std::numeric_limits<T>::min() ||
46  range.end() > std::numeric_limits<T>::max()) {
47  return Error("Range is out of bounds");
48  }
49 
50  set += (Bound<T>::closed(range.begin()),
51  Bound<T>::closed(range.end()));
52  }
53 
54  return set;
55 }
56 
57 
58 template <typename T>
59 Try<std::vector<T>> rangesToVector(const Value::Ranges& ranges)
60 {
61  std::vector<T> result;
62 
63  static_assert(
64  std::is_integral<T>::value,
65  "vector<T> must use an integral type");
66 
67  foreach (const Value::Range& range, ranges.range()) {
68  if (range.begin() < std::numeric_limits<T>::min() ||
69  range.end() > std::numeric_limits<T>::max()) {
70  return Error("Range is out of bounds");
71  }
72 
73  for (T value = range.begin(); value <= range.end(); value++) {
74  result.push_back(value);
75  }
76  }
77 
78  return result;
79 }
80 
81 
82 // Convert IntervalSet value to Ranges value.
83 template <typename T>
84 Value::Ranges intervalSetToRanges(const IntervalSet<T>& set)
85 {
86  Value::Ranges ranges;
87 
88  static_assert(
89  std::is_integral<T>::value,
90  "IntervalSet<T> must use an integral type");
91 
92  foreach (const Interval<T>& interval, set) {
93  Value::Range* range = ranges.add_range();
94  range->set_begin(interval.lower());
95  range->set_end(interval.upper() - 1);
96  }
97 
98  return ranges;
99 }
100 
101 } // namespace values {
102 } // namespace internal {
103 } // namespace mesos {
104 
105 #endif // __COMMON_VALUES_HPP__
Definition: interval.hpp:34
Definition: errorbase.hpp:36
Definition: check.hpp:33
Definition: interval.hpp:28
Definition: interval.hpp:24
Try< std::vector< T > > rangesToVector(const Value::Ranges &ranges)
Definition: values.hpp:59
T lower() const
Definition: interval.hpp:81
Option< T > max(const Option< T > &left, const Option< T > &right)
Definition: option.hpp:208
Value::Ranges intervalSetToRanges(const IntervalSet< T > &set)
Definition: values.hpp:84
Definition: spec.hpp:26
Option< T > min(const Option< T > &left, const Option< T > &right)
Definition: option.hpp:179
T upper() const
Definition: interval.hpp:84
Definition: attributes.hpp:24
Try< IntervalSet< T > > rangesToIntervalSet(const Value::Ranges &ranges)
Definition: values.hpp:36
static Bound< T > closed(const T &value)
Definition: interval.hpp:44