src/types/UnsignedIntegerDataTypeWriter.h
branchv_0
changeset 45 27d5335cd924
parent 44 3f7b5f3f3f84
equal deleted inserted replaced
44:3f7b5f3f3f84 45:27d5335cd924
       
     1 /**
       
     2  * Relational pipes (library)
       
     3  * Copyright © 2018 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:
       
     7  *  - GNU Lesser General Public License as published by the Free Software Foundation;
       
     8  *    version 3 of the License or (at your option)
       
     9  *  - GNU General Public License as published by the Free Software Foundation;
       
    10  *    version 2 of the License.
       
    11  *
       
    12  * This program is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    15  * GNU General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License
       
    18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    19  */
       
    20 #pragma once
       
    21 
       
    22 #include <string>
       
    23 #include <iostream>
       
    24 #include <cassert>
       
    25 #include <limits>
       
    26 
       
    27 #include <relpipe/protocol/constants.h>
       
    28 
       
    29 #include "../DataTypeWriter.h"
       
    30 
       
    31 namespace relpipe {
       
    32 namespace writer {
       
    33 namespace types {
       
    34 
       
    35 using namespace relpipe::protocol;
       
    36 using namespace relpipe::writer;
       
    37 
       
    38 /**
       
    39  * Unsigned variable-length integer.
       
    40  * ULEB128
       
    41  */
       
    42 class IntegerDataTypeWriter : public DataTypeWriter<integer_t> { // FIXME: relpipe::common::type::UnsignedInteger
       
    43 public:
       
    44 
       
    45 	IntegerDataTypeWriter() : DataTypeWriter<integer_t>(TypeId::INTEGER, DATA_TYPE_CODE_INTEGER) {
       
    46 	}
       
    47 
       
    48 	void writeValue(std::ostream &output, const integer_t &value) override {
       
    49 		integer_t v = value;
       
    50 		do {
       
    51 			octet_t octet = v & 0x7F;
       
    52 			v >>= 7;
       
    53 			if (v) octet |= 0x80; // more bytes follow
       
    54 			output << char(octet);
       
    55 		} while (v);
       
    56 	}
       
    57 
       
    58 	integer_t toValue(const string_t &stringValue) override {
       
    59 		// throws „terminate called after throwing an instance of 'std::invalid_argument'“ SIGABRT, core dumped on invalid number
       
    60 		return stoul(stringValue);
       
    61 	}
       
    62 
       
    63 };
       
    64 
       
    65 }
       
    66 }
       
    67 }