EpcTools
An event based multi-threaded C++ development framework.
eteid.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2020 Sprint
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #ifndef __ETEID_H
18 #define __ETEID_H
19 
20 #include <atomic>
21 #include <limits>
22 
23 #include "eerror.h"
24 
26 DECLARE_ERROR_ADVANCED(ETeidManager_InvalidNumberOfRangeBits);
27 inline ETeidManager_InvalidNumberOfRangeBits::ETeidManager_InvalidNumberOfRangeBits(){
28  setText("The range bits must be between 0 and 7");
29 }
31 
33 public:
34  ETeidManager_InvalidRangeValue(UChar min, UChar max) {
35  setTextf("The TEID range value must be between %u and %u", min, max);
36  }
37 private:
39 };
40 
42 {
43 public:
44 
45  ETeidManager(Int rangeBits=0, Int rangeValue=0)
46  : bits_(rangeBits),
47  range_(rangeValue)
48  {
49  if (bits_ < 0 || bits_ > 7)
50  throw ETeidManager_InvalidNumberOfRangeBits();
51 
52  Int minRange = 0;
53  Int maxRange = (bits_ == 0) ? 0 : ((1 << (bits_)) - 1);
54 
55  if (rangeValue > maxRange)
56  throw ETeidManager_InvalidRangeValue(minRange,maxRange);
57 
58  min_ = static_cast<ULongLong>(rangeValue) << ((sizeof(ULong)*8) - bits_);
59  max_ = ((1ull << (32 - bits_)) - 1) | min_;
60  if (min_ == 0)
61  min_++;
62 
63  next_ = min_;
64  }
65 
66  Int rangeBits() const { return bits_; }
67  Int rangeValue() const { return range_; }
68  ULong min() const { return min_; }
69  ULong max() const { return max_; }
70 
71  ULong alloc()
72  {
73  // unsigned long long nxt=0;
74  unsigned long long nxt;
75  while ((nxt=next_.fetch_add(1)) > max_)
76  next_.compare_exchange_strong(++nxt, min_);
77  return static_cast<ULong>(nxt);
78  }
79 
80 private:
81  std::atomic_ullong next_;
82  Int bits_;
83  Int range_;
84  ULongLong min_;
85  ULongLong max_;
86 };
87 
88 #endif // __ETEID_H
Int rangeValue() const
Definition: eteid.h:67
ETeidManager(Int rangeBits=0, Int rangeValue=0)
Definition: eteid.h:45
Void setTextf(cpStr pszFormat,...)
Sets the text associated with this error.
Definition: eerror.cpp:29
Defines base class for exceptions and declaration helper macros.
The base class for exceptions derived from std::exception.
Definition: eerror.h:94
Definition: eteid.h:32
ULong alloc()
Definition: eteid.h:71
ULong min() const
Definition: eteid.h:68
ULong max() const
Definition: eteid.h:69
Int rangeBits() const
Definition: eteid.h:66
#define DECLARE_ERROR_ADVANCED(__e__)
Declares exception class derived from EError with no constructor parameters and developer defined con...
Definition: eerror.h:61
ETeidManager_InvalidRangeValue(UChar min, UChar max)
Definition: eteid.h:34
Definition: eteid.h:41