From 99f15267e1f6db299d87bfb9744dec11c40963e4 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Tue, 4 Aug 2015 17:38:49 +0200 Subject: Create endian enum and read/write functions with specified endian --- include/endian_str.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'include') diff --git a/include/endian_str.h b/include/endian_str.h index 3ccd9817c7..b1549bc27c 100644 --- a/include/endian_str.h +++ b/include/endian_str.h @@ -30,6 +30,12 @@ namespace endian { +enum endian { little, big }; + +///Reads number from stream in specified endian +template +void read(std::istream& is, T& x, endian e); + ///Reads number from stream in little endian void read_little(std::istream& is, uint8_t& x); void read_little(std::istream& is, uint16_t& x); @@ -54,6 +60,10 @@ void read_big(std::istream& is, int64_t& x); void read_big(std::istream& is, float& x); void read_big(std::istream& is, double& x); +///Writes number to stream in specified endian +template +void write(std::ostream& os, T x, endian e); + ///Writes number to stream in little endian void write_little(std::ostream& os, uint8_t x); void write_little(std::ostream& os, uint16_t x); @@ -78,6 +88,24 @@ void write_big(std::ostream& os, int64_t x); void write_big(std::ostream& os, float x); void write_big(std::ostream& os, double x); +template +void read(std::istream& is, T& x, endian e) +{ + if(e == little) + read_little(is, x); + else + read_big(is, x); +} + +template +void write(std::ostream& os, T x, endian e) +{ + if(e == little) + write_little(os, x); + else + write_big(os, x); +} + } #endif // ENDIAN_STR_H_INCLUDED -- cgit 0.0.5-2-1-g0f52