From a0cff27590cc10a8bef3ef26a2ebbfa5bea7373d Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Sun, 22 May 2011 16:52:37 +0200 Subject: [PATCH] first commit --- binary_output/binary_output.hpp | 52 +++++++++++++++++++++++++++++++++ binary_output/main.cpp | 9 ++++++ 2 files changed, 61 insertions(+) create mode 100644 binary_output/binary_output.hpp create mode 100644 binary_output/main.cpp diff --git a/binary_output/binary_output.hpp b/binary_output/binary_output.hpp new file mode 100644 index 0000000..f45ddd1 --- /dev/null +++ b/binary_output/binary_output.hpp @@ -0,0 +1,52 @@ +#ifndef BINARY_OUTPUT_HPP +#define BINARY_OUTPUT_HPP + +#include + +// static_asserts are not too good... + +// Normal +template +struct binary{ + T thing; + binary(T const t):thing(t){} + binary(binary const& c):thing(c.thing){} +}; + +template +binary make_binary(T const t){ + return binary(t); +} + +template +std::ostream& operator<<(std::ostream& out, binary const& rhs){ + static_assert(std::is_pod::value, "Please specialise this function if your type is not POD"); + static_assert(!std::is_pointer::value, "Writing pointer"); + out.write(reinterpret_cast(&(rhs.thing)), sizeof(rhs.thing)); + return out; +} + +// Array +template +struct binary_arr{ + T const (& thing)[N]; + binary_arr(const T (& t)[N]):thing(t){} + binary_arr(binary_arr const& c):thing(c.thing){} +}; + +template +binary_arr make_binary(const T (& t)[N]){ + static_assert(N>=0, "lijp"); + return binary_arr(t); +} + +template +std::ostream& operator<<(std::ostream& out, binary_arr const& rhs){ + static_assert(std::is_pod::value, "Please specialise this function if your type is not POD"); + static_assert(!std::is_pointer::value, "Writing pointer"); + out.write(reinterpret_cast(&(rhs.thing)), sizeof(rhs.thing)); + return out; +} + +#endif // BINARY_OUTPUT_HPP + diff --git a/binary_output/main.cpp b/binary_output/main.cpp new file mode 100644 index 0000000..5b77412 --- /dev/null +++ b/binary_output/main.cpp @@ -0,0 +1,9 @@ +#include +#include "binary_output.hpp" + +int main(){ + std::cout << make_binary(1684234849) << std::endl; + std::cout << make_binary("A c-style array") << std::endl; + + return 0; +}