diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-06-22 00:15:08 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-06-22 00:16:28 +0200 |
| commit | 22231518b9c2c0b7f73c72a6ca834df659c63c7f (patch) | |
| tree | 12b1d20efd3303583649966ddfe135a81ae8109b /mz | |
| parent | 46145fe7d5b1d9f0921121a088a3d61f968cc473 (diff) | |
| download | openwar-22231518b9c2c0b7f73c72a6ca834df659c63c7f.tar.gz openwar-22231518b9c2c0b7f73c72a6ca834df659c63c7f.tar.bz2 openwar-22231518b9c2c0b7f73c72a6ca834df659c63c7f.zip | |
Reduces the amount of boilerplate neccesary to parse the binary format.
Diffstat (limited to 'mz')
| -rw-r--r-- | mz/mz_header.cpp | 38 | ||||
| -rw-r--r-- | mz/mz_header.hpp | 12 |
2 files changed, 15 insertions, 35 deletions
diff --git a/mz/mz_header.cpp b/mz/mz_header.cpp index 02241cf..c33270f 100644 --- a/mz/mz_header.cpp +++ b/mz/mz_header.cpp @@ -1,18 +1,16 @@ #include "mz_header.hpp" -#include "index_list.hpp" - -#include "binparse.hpp" +#include "parse.hpp" +#include "output.hpp" #include <boost/fusion/adapted/struct.hpp> -#include <boost/fusion/adapted/struct/detail/extension.hpp> #include <vector> #include <iomanip> BOOST_FUSION_ADAPT_STRUCT( - mz::MZHeader, + mz::Header, (binparse::Magic16, magic) (binparse::Value16, bytes_in_last_block) (binparse::Value16, blocks_in_file) @@ -35,35 +33,15 @@ NotAMZFileException::NotAMZFileException() : std::runtime_error("This stream does not contain a valid MZ executable") {} -template <int... Indices> -MZHeader parse_header_impl(std::istream& is, indices<Indices...>) { - - return {parse<typename std::decay<typename boost::fusion::result_of::at_c<MZHeader, Indices>::type>::type>(is, boost::fusion::extension::struct_member_name<MZHeader, Indices>::call())...}; - -} - -MZHeader parse_header(std::istream& is) { - - typedef build_indices<boost::fusion::result_of::size<MZHeader>::value>::type indices; +Header parse_header(std::istream& is) { + using binparse::parse; - return parse_header_impl(is, indices{}); + return parse<Header>(is); } -std::ostream& output_impl(std::ostream& os, MZHeader const&, indices<>) { - return os; -} - -template <int I, int... Indices> -std::ostream& output_impl(std::ostream& os, const MZHeader& header, indices<I, Indices...>) { - os << boost::fusion::extension::struct_member_name<MZHeader, I>::call() << ": " << boost::fusion::at_c<I>(header) << std::endl; - return output_impl(os, header, indices<Indices...>{}); -} - -std::ostream& operator<<(std::ostream& os, MZHeader const& header) +std::ostream& operator<<(std::ostream& os, Header const& header) { - typedef build_indices<boost::fusion::result_of::size<MZHeader>::value>::type indices; - - return output_impl(os, header, indices{}); + return binparse::operator<<(os, header); } } diff --git a/mz/mz_header.hpp b/mz/mz_header.hpp index 9bef928..c783335 100644 --- a/mz/mz_header.hpp +++ b/mz/mz_header.hpp @@ -10,8 +10,6 @@ namespace mz { -using namespace binparse; - struct NotAMZFileException : public std::runtime_error { NotAMZFileException(); }; @@ -22,7 +20,11 @@ struct UnexpectedEOS : public std::runtime_error { UnexpectedEOS(std::string location); }; -struct MZHeader { +using binparse::Magic16; +using binparse::Value16; +using binparse::Offset16; + +struct Header { Magic16 magic; Value16 bytes_in_last_block; Value16 blocks_in_file; @@ -39,8 +41,8 @@ struct MZHeader { Value16 overlay_number; }; -MZHeader parse_header(std::istream& is); +Header parse_header(std::istream& is); -std::ostream& operator<<(std::ostream& os, MZHeader const& header); +std::ostream& operator<<(std::ostream& os, Header const& header); } |
