OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_RANGE_H_ |
| 6 #define COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_RANGE_H_ |
| 7 |
| 8 #include <algorithm> |
| 9 #include <limits> |
| 10 |
| 11 #include "base/macros.h" |
| 12 |
| 13 namespace framelet { |
| 14 |
| 15 enum class ResourceUsageRangeType { DEFAULT, LEFT_UNBOUNDED, RIGHT_UNBOUNDED }; |
| 16 |
| 17 template <class T> |
| 18 class ResourceUsageRange { |
| 19 public: |
| 20 ResourceUsageRange(ResourceUsageRangeType range_type, T bounded_end) |
| 21 : range_type_(range_type), |
| 22 low_(range_type_ == ResourceUsageRangeType::LEFT_UNBOUNDED |
| 23 ? std::numeric_limits<T>::lowest() |
| 24 : bounded_end), |
| 25 high_(range_type_ == ResourceUsageRangeType::RIGHT_UNBOUNDED |
| 26 ? std::numeric_limits<T>::max() |
| 27 : bounded_end) {} |
| 28 |
| 29 ResourceUsageRange(T start, T end) |
| 30 : range_type_(ResourceUsageRangeType::DEFAULT), |
| 31 low_(std::min(start, end)), |
| 32 high_(std::max(start, end)) {} |
| 33 |
| 34 ~ResourceUsageRange() {} |
| 35 |
| 36 T high() const { return high_; } |
| 37 T low() const { return low_; } |
| 38 |
| 39 bool IsLowerThan(T val) const { |
| 40 if (range_type_ == ResourceUsageRangeType::RIGHT_UNBOUNDED) |
| 41 return false; |
| 42 return val > high_; |
| 43 } |
| 44 |
| 45 bool IsHigherThan(T val) const { |
| 46 if (range_type_ == ResourceUsageRangeType::LEFT_UNBOUNDED) |
| 47 return false; |
| 48 return val < low_; |
| 49 } |
| 50 |
| 51 bool Contains(T val) const { return !IsLowerThan(val) && !IsHigherThan(val); } |
| 52 |
| 53 private: |
| 54 const ResourceUsageRangeType range_type_; |
| 55 const T low_; |
| 56 const T high_; |
| 57 DISALLOW_COPY_AND_ASSIGN(ResourceUsageRange); |
| 58 }; |
| 59 |
| 60 } // namespace framelet |
| 61 |
| 62 #endif // COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_RANGE_H_ |
OLD | NEW |