From a4e6f67bc57da98147c45d5178cf3f4eaeefb88f Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 29 Aug 2011 14:32:12 +1000 Subject: [PATCH] Add fourcc type --- types.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ types.hpp | 28 ++++++++++++++++++---------- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/types.cpp b/types.cpp index 00d82b96..c84d0def 100644 --- a/types.cpp +++ b/types.cpp @@ -118,3 +118,53 @@ template uint64_t hton (uint64_t); #endif + +fourcc +fourcc::from_chars (uint8_t a, uint8_t b, uint8_t c, uint8_t d) { + fourcc lhs; + + lhs.data[0] = a; + lhs.data[1] = b; + lhs.data[2] = c; + lhs.data[3] = d; + + return lhs; +} + + +fourcc +fourcc::from_str (const char data[4]) { + fourcc lhs; + + lhs.data[0] = (uint8_t)data[0]; + lhs.data[1] = (uint8_t)data[1]; + lhs.data[2] = (uint8_t)data[2]; + lhs.data[3] = (uint8_t)data[3]; + + return lhs; +} + + +bool +fourcc::operator== (const char rhs[4]) const { + return data[0] == rhs[0] && + data[1] == rhs[1] && + data[2] == rhs[2] && + data[3] == rhs[3]; +} + + +fourcc::operator uint32_t (void) const { + return (uint32_t)(data[0] << 24U | + data[1] << 16U | + data[2] << 8U | + data[3]); +} + + +ostream& +operator<< (ostream &os, fourcc f) { + os << f.data[0] << f.data[1] << f.data[2] << f.data[3]; + return os; +} + diff --git a/types.hpp b/types.hpp index eee1e84c..32de0fcf 100644 --- a/types.hpp +++ b/types.hpp @@ -143,20 +143,28 @@ size_t elems(T (&)[N]) { return N; } -/// Convert a scalar from host byte order to network byte order -template -T hton (T) pure; - - -/// Convert a scalar from network byte order to host byte order -template -T ntoh (T) pure; - - template std::unique_ptr make_unique (const Args &...args) { return std::unique_ptr (new T (args...)); } +struct fourcc { + uint8_t data[4]; + + static fourcc from_str(const char[4]); + static fourcc from_chars(uint8_t, uint8_t, uint8_t, uint8_t); + + bool operator== (const char[4]) const; + operator uint32_t (void) const; +}; + +std::ostream& operator<< (std::ostream&, fourcc); + + +inline double sign (double x) { return x >= 0 ? 1.0 : -1.0; } +inline float sign (float x) { return x >= 0 ? 1.0 : -1.0; } +inline int sign (int x) { return x >= 0 ? 1 : -1 ; } + + #endif // __TYPES_HPP