src/lib/AbstractParser.cpp
branchv_0
changeset 1 2179f13227f4
child 2 8b8175615adb
equal deleted inserted replaced
0:bfee08a31fdc 1:2179f13227f4
       
     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 
       
    20 namespace relpipe {
       
    21 namespace in {
       
    22 namespace asn1 {
       
    23 namespace lib {
       
    24 
       
    25 class AbstractParserImpl {
       
    26 private:
       
    27 	AbstractParser* interface;
       
    28 public:
       
    29 
       
    30 	AbstractParserImpl(AbstractParser* interface) : interface(interface) {
       
    31 	}
       
    32 
       
    33 	virtual ~AbstractParserImpl() {
       
    34 	}
       
    35 
       
    36 	void write(const char* buffer, const size_t length) {
       
    37 		// TODO: update pointers and positions
       
    38 		try {
       
    39 			// TODO: call an overridable method to get preferred minimum block size and run cycle only if we have enough data or EOF
       
    40 			interface->update();
       
    41 			commit();
       
    42 		} catch (const AbstractParser::ReadBufferUnderflowException& e) {
       
    43 			rollback();
       
    44 		} catch (const AbstractParser::ExplicitRollbackException& e) {
       
    45 			rollback();
       
    46 		}
       
    47 	}
       
    48 
       
    49 	void rollback() {
       
    50 		// FIXME: store content of the current buffer + update pointers and positions
       
    51 		// TODO: notify rollback listeners? (they can monitor the performance / frequency of rollbacks)
       
    52 	}
       
    53 
       
    54 	void commit() {
       
    55 	}
       
    56 
       
    57 	void read(char* buffer, const size_t length) {
       
    58 	}
       
    59 
       
    60 	void peek(char* buffer, const size_t length) {
       
    61 	}
       
    62 };
       
    63 
       
    64 AbstractParser::AbstractParser() {
       
    65 	implementation = new AbstractParserImpl(this);
       
    66 }
       
    67 
       
    68 AbstractParser::~AbstractParser() {
       
    69 	delete (AbstractParserImpl*) implementation;
       
    70 }
       
    71 
       
    72 void AbstractParser::write(const char* buffer, const size_t length) {
       
    73 	((AbstractParserImpl*) implementation)->write(buffer, length);
       
    74 }
       
    75 
       
    76 void AbstractParser::commit() {
       
    77 	((AbstractParserImpl*) implementation)->commit();
       
    78 }
       
    79 
       
    80 void AbstractParser::rollback() {
       
    81 	((AbstractParserImpl*) implementation)->rollback();
       
    82 }
       
    83 
       
    84 void AbstractParser::read(char* buffer, const size_t length) {
       
    85 	((AbstractParserImpl*) implementation)->read(buffer, length);
       
    86 }
       
    87 
       
    88 void AbstractParser::peek(char* buffer, const size_t length) {
       
    89 	((AbstractParserImpl*) implementation)->peek(buffer, length);
       
    90 }
       
    91 
       
    92 }
       
    93 }
       
    94 }
       
    95 }