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
28 /***************************************************************************************
30 ***************************************************************************************/
33 VisitorEncoder(EncoderCtx& ctx) : m_ctx(ctx){}
35 template<typename ELM> bool operator() (ELM const& elm)
38 Element<ELM>::run(elm, m_ctx);
39 return static_cast<bool>(m_ctx);
45 /***************************************************************************************
47 ***************************************************************************************/
50 VisitorDecoder(DecoderCtx& ctx, tag_value_t tag) : m_ctx(ctx), m_tag(tag){}
52 template<typename ELM> bool operator() (ELM& elm)
55 Element<ELM>::run(elm, m_ctx, &m_tag);
56 return static_cast<bool>(m_ctx);
64 /***************************************************************************************
66 ***************************************************************************************/
67 template <class Container, class IE, class Enabler = void>
68 struct VisitorAdapter //default
70 static void inline run(IE const& ie, EncoderCtx& ctx, Container const& cont)
72 Element<IE>::run(ie, ctx);
74 static void inline run(IE& ie, DecoderCtx& ctx, Container const& cont, tag_value_t tag)
76 Element<IE>::run(ie, ctx, &tag);
80 template <class Container, class IE>
81 struct VisitorAdapter<Container, IE, std::enable_if_t<
82 (IE::ie_type == element_type::T_OBJFIELD_FTV)
83 || (IE::ie_type == element_type::T_OBJFIELD_TF)
86 static void inline run(IE const& ie, EncoderCtx& ctx, Container const& cont)
88 VisitorEncoder v(ctx);
89 bool rv = ie.encode(v, cont);
90 if(!rv && static_cast<bool>(ctx))
92 ctx.refErrorCtx().errorNoObject(Container::name());
95 static void inline run(IE& ie, DecoderCtx& ctx, Container const& cont, tag_value_t tag)
97 VisitorDecoder v(ctx, tag);
98 bool rv = ie.decode(v, cont);
99 if(!rv && static_cast<bool>(ctx))
101 ctx.refErrorCtx().errorNoObject(Container::name());
106 /***************************************************************************************
108 ***************************************************************************************/
109 template<typename CONT>
110 struct VisitorEncoderSeq
112 VisitorEncoderSeq(EncoderCtx& ctx, CONT const& cont) : m_ctx(ctx), m_cont(cont){}
114 template<typename ELM> void operator() (ELM const& elm)
119 VisitorAdapter<CONT, ELM>::run(elm, m_ctx, m_cont);
122 VisitorAdapter<CONT, ELM>::run(elm, m_ctx, m_cont);
130 /***************************************************************************************
132 ***************************************************************************************/
133 template<typename CONT>
134 struct VisitorDecoderSeq
136 VisitorDecoderSeq(DecoderCtx& ctx, CONT const& cont) : m_ctx(ctx), m_cont(cont){}
138 template<typename ELM> void operator() (ELM& elm)
144 if(invalid_tag == m_tag && m_ctx.refBuffer().getBytesLeft())
146 m_tag = get_tag(m_ctx);
152 if (!Element<ELM>::is_matched(tag))
156 m_ctx.refErrorCtx().tagError(static_cast<uint32_t>(tag));
162 VisitorAdapter<CONT, ELM>::run(elm, m_ctx, m_cont, tag);
163 elm.setpresent(true);
169 tag_value_t get_unhandled_tag() const {return m_tag;}
172 tag_value_t m_tag {invalid_tag};