Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / src / ASN1 / asn / ber / context.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 #include <climits>
23
24 // Local Includes: Application specific classes, functions, and libraries
25 #include "asn/utility.hpp"
26 #include "asn/buffer.hpp"
27 #include "asn/error_context.hpp"
28
29 namespace asn {
30 namespace ber {
31
32 /********************************************************************************
33 EncoderCtx
34 *********************************************************************************/
35 class EncoderCtx : boost::noncopyable
36 {
37 public:
38         typedef buffer<u8*> buf_type;
39
40         EncoderCtx(u8* data, size_t size)
41                 : m_buffer{ m_errCtx }
42         {
43                 Reset(data, size);
44         }
45
46         template <size_t SIZE>
47         explicit EncoderCtx(u8(&buff)[SIZE])
48                 : EncoderCtx(buff, SIZE)
49         {
50         }
51
52         explicit EncoderCtx()
53                 : EncoderCtx(nullptr, 0)
54         {
55         }
56
57         buf_type& refBuffer() { return m_buffer; }
58         error_context& refErrorCtx() { return m_errCtx; }
59         explicit operator bool() const { return static_cast<bool>(m_errCtx); }
60
61         void Reset(void* data = nullptr, size_t size = 0)
62         {
63                 m_buffer.reset(static_cast<u8*>(data), size);
64                 m_errCtx.reset();
65         }
66
67         //Name of the being processed IE
68         void ie_name(const char* name) { m_errCtx.ie_name(name); }
69         char const* ie_name() const { return m_errCtx.ie_name(); }
70
71 private:
72         template <class IE, int IE_TYPE>
73         friend struct Encode;
74
75         error_context                   m_errCtx;
76         buf_type                                m_buffer;
77 };
78
79
80 /********************************************************************************
81 DecoderCtx
82 *********************************************************************************/
83 class DecoderCtx : boost::noncopyable
84 {
85 public:
86         typedef buffer<u8 const*>       buf_type;
87         typedef allocator                       alloc_type;
88
89         DecoderCtx(const void* data = nullptr, size_t size = 0, void* alloc_buffer = nullptr, size_t alloc_buffer_size = 0)
90                 : m_buffer{ m_errCtx }
91         {
92                 reset(data, size, alloc_buffer, alloc_buffer_size);
93         }
94
95         buf_type& refBuffer() { return m_buffer; }
96         alloc_type& refAllocator() { return m_allocator; }
97         error_context& refErrorCtx() { return m_errCtx; }
98         explicit operator bool() const { return static_cast<bool>(m_errCtx); }
99
100         void reset(const void* data = nullptr, size_t size = 0, void* alloc_buffer = nullptr, size_t alloc_buffer_size = 0)
101         {
102                 m_allocator.reset(alloc_buffer, alloc_buffer_size);
103                 m_buffer.reset(static_cast<u8 const*>(data), size);
104                 m_errCtx.reset();
105         }
106
107         //Name of the being processed IE
108         void ie_name(const char* name) { m_errCtx.ie_name(name); }
109         char const* ie_name() const { return m_errCtx.ie_name(); }
110
111 private:
112         error_context                   m_errCtx;
113         buf_type                                m_buffer;
114         alloc_type                              m_allocator;
115 };
116
117 } //namespace ber
118 } //namespace asn