/src/opendnp3/cpp/lib/src/app/Range.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2013-2022 Step Function I/O, LLC |
3 | | * |
4 | | * Licensed to Green Energy Corp (www.greenenergycorp.com) and Step Function I/O |
5 | | * LLC (https://stepfunc.io) under one or more contributor license agreements. |
6 | | * See the NOTICE file distributed with this work for additional information |
7 | | * regarding copyright ownership. Green Energy Corp and Step Function I/O LLC license |
8 | | * this file to you under the Apache License, Version 2.0 (the "License"); you |
9 | | * may not use this file except in compliance with the License. You may obtain |
10 | | * a copy of the License at: |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | #ifndef OPENDNP3_RANGE_H |
21 | | #define OPENDNP3_RANGE_H |
22 | | |
23 | | #include <ser4cpp/util/Comparisons.h> |
24 | | |
25 | | #include <cstddef> |
26 | | #include <cstdint> |
27 | | |
28 | | namespace opendnp3 |
29 | | { |
30 | | |
31 | | class Range |
32 | | { |
33 | | public: |
34 | | static Range From(uint16_t start, uint16_t stop) |
35 | 0 | { |
36 | 0 | return Range(start, stop); |
37 | 0 | } |
38 | | |
39 | | static Range Invalid() |
40 | 0 | { |
41 | 0 | return Range(1, 0); |
42 | 0 | } |
43 | | |
44 | 31.6k | Range() : start(1), stop(0) {} |
45 | | |
46 | | size_t Count() const |
47 | 45.1k | { |
48 | 45.1k | return IsValid() ? (static_cast<size_t>(stop) - static_cast<size_t>(start) + 1) : 0; |
49 | 45.1k | } |
50 | | |
51 | | bool Advance() |
52 | 0 | { |
53 | 0 | if (this->IsValid()) |
54 | 0 | { |
55 | 0 | if (start < stop) |
56 | 0 | { |
57 | 0 | ++start; |
58 | 0 | } |
59 | 0 | else |
60 | 0 | { |
61 | 0 | // make the range invalid |
62 | 0 | start = 1; |
63 | 0 | stop = 0; |
64 | 0 | } |
65 | 0 |
|
66 | 0 | return true; |
67 | 0 | } |
68 | 0 | else |
69 | 0 | { |
70 | 0 | return false; |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | bool Contains(uint16_t index) const |
75 | 0 | { |
76 | 0 | return (index >= start) && (index <= stop); |
77 | 0 | } |
78 | | |
79 | | /// @return A new range with only values found in both ranges |
80 | | Range Intersection(const Range& other) const |
81 | 0 | { |
82 | 0 | return Range(ser4cpp::max<uint16_t>(start, other.start), ser4cpp::min<uint16_t>(stop, other.stop)); |
83 | 0 | } |
84 | | |
85 | | /// @return A new range with min start and the max stop of both ranges |
86 | | Range Union(const Range& other) const |
87 | 0 | { |
88 | 0 | if (!this->IsValid()) |
89 | 0 | { |
90 | 0 | return other; |
91 | 0 | } |
92 | 0 |
|
93 | 0 | return Range(ser4cpp::min<uint16_t>(start, other.start), ser4cpp::max<uint16_t>(stop, other.stop)); |
94 | 0 | } |
95 | | |
96 | | bool Equals(const Range& other) const |
97 | 0 | { |
98 | 0 | return (other.start == start) && (other.stop == stop); |
99 | 0 | } |
100 | | |
101 | | bool IsValid() const |
102 | 76.7k | { |
103 | 76.7k | return start <= stop; |
104 | 76.7k | } |
105 | | |
106 | | bool IsOneByte() const |
107 | 0 | { |
108 | 0 | return IsValid() && (start <= 255) && (stop <= 255); |
109 | 0 | } |
110 | | |
111 | | uint16_t start; |
112 | | uint16_t stop; |
113 | | |
114 | | private: |
115 | 0 | Range(uint16_t index_) : start(index_), stop(index_) {} |
116 | | |
117 | 0 | Range(uint16_t start_, uint16_t stop_) : start(start_), stop(stop_) {} |
118 | | }; |
119 | | |
120 | | } // namespace opendnp3 |
121 | | |
122 | | #endif |