Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / ASN1 / asn / per / enumerated.hpp
1 #pragma once
2 /******************************************************************************
3 *
4 *   Copyright (c) 2019 AT&T Intellectual Property.
5 *   Copyright (c) 2018-2019 Nokia.
6 *
7 *   Licensed under the Apache License, Version 2.0 (the "License");
8 *   you may not use this file except in compliance with the License.
9 *   You may obtain a copy of the License at
10 *
11 *       http://www.apache.org/licenses/LICENSE-2.0
12 *
13 *   Unless required by applicable law or agreed to in writing, software
14 *   distributed under the License is distributed on an "AS IS" BASIS,
15 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 *   See the License for the specific language governing permissions and
17 *   limitations under the License.
18 *
19 ******************************************************************************/
20
21 // Standard Includes: ANSI C/C++, MSA, and Third-Party Libraries
22
23 // Local Includes: Application specific classes, functions, and libraries
24 #include "asn/per/common.hpp"
25 #include "asn/per/whole_number.hpp"
26 #include "asn/per/binary_integer.hpp"
27
28 namespace asn {
29 namespace per {
30
31 template<class IE, class Enable = void>
32 struct Enumerated;
33
34 template<class IE>
35 struct Enumerated<IE, std::enable_if_t<(IE::constraint_t::type == constraint_type::CONSTRAINED)> >
36 {
37         static void inline run(IE const& ie, EncoderCtx& ctx)
38         {
39                 if (ie.get() > IE::constraint_t::upper_bound)
40                 {
41                         ctx.refErrorCtx().valueRangeError(static_cast<size_t>(ie.get()));
42                         return;
43                 }
44                 ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx, ie.get());
45         }
46         static void inline run(IE& ie, DecoderCtx& ctx)
47         {
48                 ie.set(ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx));
49         }
50 };
51
52 template<class IE>
53 struct Enumerated<IE, std::enable_if_t<(IE::constraint_t::type == constraint_type::CONSTRAINED_EXTENDED)> >
54 {
55         static void inline run(IE const& ie, EncoderCtx& ctx)
56         {
57                 if (ie.get() <= IE::constraint_t::upper_bound)
58                 {
59                         Tools::bit_accessor::put(0, 1, ctx.refBuffer());
60                         ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx, ie.get());
61                 }
62                 else
63                 {
64                         Tools::bit_accessor::put(1, 1, ctx.refBuffer());
65                         NormallySmallNonnegativeBinaryInteger<IE>::run(ie, ctx, ie.get() - IE::constraint_t::upper_bound - 1);
66                 }
67         }
68         static void inline run(IE& ie, DecoderCtx& ctx)
69         {
70                 u8 ext = Tools::bit_accessor::get(1, ctx.refBuffer());
71                 if (ext)
72                         ie.set(IE::constraint_t::upper_bound + 1 + NormallySmallNonnegativeBinaryInteger<IE>::run(ctx));
73                 else
74                         ie.set(ConstrainedWholeNumber<typename IE::constraint_t, typename IE::value_type>::run(ctx));
75         }
76 };
77
78 } //namespace per
79 } //namespace asn