pt_parser.h 1.29 KB
Newer Older
Gustavo Valiente's avatar
Gustavo Valiente committed
1
/*
Gustavo Valiente's avatar
Gustavo Valiente committed
2
 * pocket-tensor (c) 2018 Gustavo Valiente gustavo.valiente.m@gmail.com
Gustavo Valiente's avatar
Gustavo Valiente committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
 * Kerasify (c) 2016 Robert W. Rose
 *
 * MIT License, see LICENSE file.
 */

#ifndef PT_PARSER_H
#define PT_PARSER_H

#include <istream>
#include "pt_logger.h"

namespace pt
{

namespace Parser
{
    template<typename T>
    bool parse(std::istream& stream, T& output)
    {
        stream.read(reinterpret_cast<char*>(&output), sizeof(T));

        auto readCharsCount = stream.gcount();

        if(readCharsCount != sizeof(T))
        {
            PT_LOG_ERROR << "Parse failed: " << readCharsCount << " - " << sizeof(T) << std::endl;
            return false;
        }

        return true;
    }

    template<typename T>
    bool parse(std::istream& stream, T* outputPtr, std::size_t outputCount) noexcept
    {
        if(! outputPtr)
        {
            PT_LOG_ERROR << "Output ptr is null" << std::endl;
            return false;
        }

        auto size = sizeof(T) * outputCount;
        stream.read(reinterpret_cast<char*>(outputPtr), size);

        auto readCharsCount = stream.gcount();

        if(readCharsCount != std::streamsize(size))
        {
            PT_LOG_ERROR << "Parse failed: " << readCharsCount << " - " << size << std::endl;
            return false;
        }

        return true;
    }
}

}

#endif