2 /******************************************************************************
4 * Copyright (c) 2019 AT&T Intellectual Property.
5 * Copyright (c) 2018-2019 Nokia.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************************/
21 // Standard Includes: ANSI C/C++, MSA, and Third-Party Libraries
23 // Local Includes: Application specific classes, functions, and libraries
24 #include "asn/per/common.hpp"
25 #include "asn/per/visitor.hpp"
30 /********************************************************************************
32 *********************************************************************************/
34 /**************************************************/
36 template <class IE, class Enabler = void>
39 static void inline run(IE const& ie, EncoderCtx& ctx)
41 ConstrainedWholeNumber<typename IE::constraint_t, typename IE::index_type>::run(ctx, IE::normalize(ie.get_index()));
42 VisitorEncoder ve{ ctx };
45 static void inline run(IE& ie, DecoderCtx& ctx)
47 typename IE::index_type idx = ConstrainedWholeNumber<typename IE::constraint_t, typename IE::index_type>::run(ctx);
48 VisitorDecoder ve{ ctx };
49 ie.decode(IE::denormalize(idx), ve);
53 //22.4 One alternative
55 struct ChoiceNoExt<IE, std::enable_if_t<(IE::constraint_t::upper_bound == 0)> >
57 static void inline run(IE const& ie, EncoderCtx& ctx)
59 VisitorEncoder ve{ ctx };
62 static void inline run(IE& ie, DecoderCtx& ctx)
64 VisitorDecoder ve{ ctx };
65 ie.decode(IE::denormalize(0), ve);
69 /**************************************************/
71 template <class IE, class Enable = void>
74 //22.5 Extension present
76 struct Choice<IE, std::enable_if_t<(IE::constraint_t::extended == true)> >
78 static void inline run(IE const& ie, EncoderCtx& ctx)
80 auto idx = IE::normalize(ie.get_index());
81 if (idx <= IE::constraint_t::upper_bound)
83 Tools::bit_accessor::put(0, 1, ctx.refBuffer());
84 ChoiceNoExt<IE>::run(ie, ctx);
88 Tools::bit_accessor::put(1, 1, ctx.refBuffer());
89 NormallySmallNonnegativeBinaryInteger<IE>::run(ie, ctx, idx - IE::constraint_t::upper_bound - 1);
90 OpenTypeVisitorEncoder ve{ ctx };
94 static void inline run(IE& ie, DecoderCtx& ctx)
96 u8 ext = Tools::bit_accessor::get(1, ctx.refBuffer());
99 typename IE::index_type idx = IE::constraint_t::upper_bound + 1 + NormallySmallNonnegativeBinaryInteger<IE, typename IE::index_type>::run(ctx);
100 if (idx >= IE::constraint_t::total_num_entries)
103 size_t size = LengthDeterminantDefault::run(ctx);
104 DecoderCtx::buf_type& buffer = ctx.refBuffer();
105 if (buffer.getBytesLeft() < size)
106 ctx.refErrorCtx().lengthErrorBytes(buffer.getBytesLeft(), size);
108 buffer.advance(size);
112 OpenTypeVisitorDecoder ve{ ctx };
113 ie.decode(IE::denormalize(idx), ve);
117 ChoiceNoExt<IE>::run(ie, ctx);
123 struct Choice<IE, std::enable_if_t<(IE::constraint_t::extended == false)> >
125 static void inline run(IE const& ie, EncoderCtx& ctx)
127 ChoiceNoExt<IE>::run(ie, ctx);
129 static void inline run(IE& ie, DecoderCtx& ctx)
131 ChoiceNoExt<IE>::run(ie, ctx);