2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file abstract_input_reader.h
18 * @author Tomasz Iwanek (t.iwanek@samsung.com)
19 * @brief Simple output reader template
21 * This generic skeleton for parser which assume being composed from abstract two logical components:
25 * which implements token flow logic. Logic of components may be arbitrary. See depending change for uses.
27 * Components are created at start time of reader (constructor which moves arguments).
28 * Virtuality (abstract base classes) are for enforcing same token type.
29 * I assumed it's more clear than writen static asserts in code enforcing this.
32 #ifndef ABSTRACT_INPUT_READER_H
33 #define ABSTRACT_INPUT_READER_H
37 #include <dpl/optional.h>
38 #include <dpl/test/abstract_input_tokenizer.h>
39 #include <dpl/test/abstract_input_parser.h>
40 #include <dpl/abstract_input.h>
45 * Base reader class that can be used with any AbstractInput instance
47 * This class is encapsulation class for tokenizer and reader subelements
48 * and contains basic calculation pattern
50 * There a waste in form of virtuality for parser and tokenizer
51 * -> this for forcing same tokenT type in both components
53 template<class ResultT, class TokenT> class AbstractInputReader
56 typedef ResultT TokenType;
57 typedef TokenT ResultType;
58 typedef AbstractInputParser<ResultT, TokenT> ParserBase;
59 typedef AbstractInputTokenizer<TokenT> TokenizerBase;
64 typedef typename TokenizerBase::Exception::TokenizerError TokenizerError;
65 typedef typename ParserBase::Exception::ParserError ParserError;
68 AbstractInputReader(std::shared_ptr<AbstractInput> ia,
69 std::unique_ptr<ParserBase> && parser,
70 std::unique_ptr<TokenizerBase> && tokenizer)
71 : m_parser(std::move(parser)), m_tokenizer(std::move(tokenizer))
73 m_tokenizer->Reset(ia);
76 virtual ~AbstractInputReader() {}
80 typedef typename Exception::TokenizerError TokenizerError;
81 typedef typename Exception::ParserError ParserError;
85 std::unique_ptr<TokenT> token = m_tokenizer->GetNextToken();
88 if(!m_tokenizer->IsStateValid())
90 ThrowMsg(TokenizerError, "Tokenizer error");
92 if(!m_parser->IsStateValid())
94 ThrowMsg(ParserError, "Parser error");
97 return m_parser->GetResult();
99 m_parser->ConsumeToken(std::move(token));
104 std::unique_ptr<ParserBase> m_parser;
105 std::unique_ptr<TokenizerBase> m_tokenizer;