src/lib/AbstractParser.cpp
branchv_0
changeset 1 68a281aefa76
equal deleted inserted replaced
0:28294b895e5e 1:68a281aefa76
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #include "AbstractParser.h"
       
    19 #include "TransactionalBuffer.h"
       
    20 
       
    21 namespace relpipe {
       
    22 namespace in {
       
    23 namespace asn1 {
       
    24 namespace lib {
       
    25 
       
    26 class AbstractParserImpl {
       
    27 private:
       
    28 	friend AbstractParser;
       
    29 	AbstractParser* interface;
       
    30 	TransactionalBuffer buffer;
       
    31 public:
       
    32 
       
    33 	AbstractParserImpl(AbstractParser* interface) : interface(interface) {
       
    34 	}
       
    35 
       
    36 	virtual ~AbstractParserImpl() {
       
    37 	}
       
    38 };
       
    39 
       
    40 AbstractParser::AbstractParser() {
       
    41 	implementation = new AbstractParserImpl(this);
       
    42 }
       
    43 
       
    44 AbstractParser::~AbstractParser() {
       
    45 	delete implementation;
       
    46 }
       
    47 
       
    48 void AbstractParser::write(const uint8_t* buffer, const size_t length) {
       
    49 	try {
       
    50 		// TODO: do not write to the buffer, just append in read()/peek() and write just the part that was not read during this cycle
       
    51 		implementation->buffer.write(buffer, length);
       
    52 
       
    53 		// TODO: call an overridable method to get preferred minimum block size and run cycle only if we have enough data or EOF
       
    54 		// and/or remember the length of last failed (rollbacked) read() call
       
    55 		update();
       
    56 		commit();
       
    57 	} catch (const TransactionalBuffer::ReadBufferUnderflowException& e) {
       
    58 		rollback();
       
    59 	} catch (const AbstractParser::ExplicitRollbackException& e) {
       
    60 		rollback();
       
    61 	}
       
    62 }
       
    63 
       
    64 void AbstractParser::close() {
       
    65 	// TODO: check remaining data + call update() or just let parsers override this method?
       
    66 }
       
    67 
       
    68 void AbstractParser::rollback() {
       
    69 	// TODO: notify rollback listeners? (they can monitor the performance / frequency of rollbacks)
       
    70 	implementation->buffer.rollbackRead();
       
    71 }
       
    72 
       
    73 void AbstractParser::commit() {
       
    74 	implementation->buffer.commitRead();
       
    75 }
       
    76 
       
    77 void AbstractParser::read(uint8_t* buffer, const size_t length) {
       
    78 	implementation->buffer.read(buffer, length);
       
    79 }
       
    80 
       
    81 void AbstractParser::peek(uint8_t* buffer, const size_t length) {
       
    82 	implementation->buffer.peek(buffer, length);
       
    83 }
       
    84 
       
    85 size_t AbstractParser::getBytesRead() {
       
    86 	return implementation->buffer.getBytesRead();
       
    87 }
       
    88 
       
    89 }
       
    90 }
       
    91 }
       
    92 }