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 
23 #include <mesos/mesos.hpp>
24 
25 #include <stout/foreach.hpp>
26 #include <stout/interval.hpp>
27 #include <stout/try.hpp>
28 
29 namespace mesos {
30 namespace internal {
31 namespace values {
32 
33 // Convert Ranges value to IntervalSet value.
34 template <typename T>
35 Try<IntervalSet<T>> rangesToIntervalSet(const Value::Ranges& ranges)
36 {
37  IntervalSet<T> set;
38 
39  static_assert(
40  std::is_integral<T>::value,
41  "IntervalSet<T> must use an integral type");
42 
43  foreach (const Value::Range& range, ranges.range()) {
44  if (range.begin() < std::numeric_limits<T>::min() ||
45  range.end() > std::numeric_limits<T>::max()) {
46  return Error("Range is out of bounds");
47  }
48 
49  set += (Bound<T>::closed(range.begin()),
50  Bound<T>::closed(range.end()));
51  }
52 
53  return set;
54 }
55 
56 
57 // Convert IntervalSet value to Ranges value.
58 template <typename T>
59 Value::Ranges intervalSetToRanges(const IntervalSet<T>& set)
60 {
61  Value::Ranges ranges;
62 
63  static_assert(
64  std::is_integral<T>::value,
65  "IntervalSet<T> must use an integral type");
66 
67  foreach (const Interval<T>& interval, set) {
68  Value::Range* range = ranges.add_range();
69  range->set_begin(interval.lower());
70  range->set_end(interval.upper() - 1);
71  }
72 
73  return ranges;
74 }
75 
76 } // namespace values {
77 } // namespace internal {
78 } // namespace mesos {
79 
80 #endif // __COMMON_VALUES_HPP__
Definition: interval.hpp:34
Definition: errorbase.hpp:35
Definition: check.hpp:33
Definition: interval.hpp:28
Definition: interval.hpp:24
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:59
Definition: spec.hpp:30
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:35
static Bound< T > closed(const T &value)
Definition: interval.hpp:44