Browse Source

formatted, commentje

master
Joshua Moerman 13 years ago
parent
commit
851017cc62
  1. 23
      binary_output/binary_output.hpp
  2. 4
      binary_output/main.cpp

23
binary_output/binary_output.hpp

@ -7,19 +7,19 @@
// Normal // Normal
template <typename T> template <typename T>
struct binary{ struct binary {
T thing; T thing;
binary(T const t):thing(t){} binary(T const t):thing(t) {}
binary(binary const& c):thing(c.thing){} binary(binary const& c):thing(c.thing) {}
}; };
template <typename T> template <typename T>
binary<T> make_binary(T const t){ binary<T> make_binary(T const t) {
return binary<T>(t); return binary<T>(t);
} }
template <typename T> template <typename T>
std::ostream& operator<<(std::ostream& out, binary<T> const& rhs){ std::ostream& operator<<(std::ostream& out, binary<T> const& rhs) {
static_assert(std::is_pod<T>::value, "Please specialise this function if your type is not POD"); static_assert(std::is_pod<T>::value, "Please specialise this function if your type is not POD");
static_assert(!std::is_pointer<T>::value, "Writing pointer"); static_assert(!std::is_pointer<T>::value, "Writing pointer");
out.write(reinterpret_cast<const char*>(&(rhs.thing)), sizeof(rhs.thing)); out.write(reinterpret_cast<const char*>(&(rhs.thing)), sizeof(rhs.thing));
@ -27,21 +27,22 @@ std::ostream& operator<<(std::ostream& out, binary<T> const& rhs){
} }
// Array // Array
// NOTE: it uses references...
template <typename T, size_t N> template <typename T, size_t N>
struct binary_arr{ struct binary_arr {
T const (& thing)[N]; T const(& thing)[N];
binary_arr(const T (& t)[N]):thing(t){} binary_arr(const T(& t)[N]):thing(t) {}
binary_arr(binary_arr const& c):thing(c.thing){} binary_arr(binary_arr const& c):thing(c.thing) {}
}; };
template <typename T, size_t N> template <typename T, size_t N>
binary_arr<T, N> make_binary(const T (& t)[N]){ binary_arr<T, N> make_binary(const T(& t)[N]) {
static_assert(N>=0, "lijp"); static_assert(N>=0, "lijp");
return binary_arr<T, N>(t); return binary_arr<T, N>(t);
} }
template <typename T, size_t N> template <typename T, size_t N>
std::ostream& operator<<(std::ostream& out, binary_arr<T, N> const& rhs){ std::ostream& operator<<(std::ostream& out, binary_arr<T, N> const& rhs) {
static_assert(std::is_pod<T>::value, "Please specialise this function if your type is not POD"); static_assert(std::is_pod<T>::value, "Please specialise this function if your type is not POD");
static_assert(!std::is_pointer<T>::value, "Writing pointer"); static_assert(!std::is_pointer<T>::value, "Writing pointer");
out.write(reinterpret_cast<const char*>(&(rhs.thing)), sizeof(rhs.thing)); out.write(reinterpret_cast<const char*>(&(rhs.thing)), sizeof(rhs.thing));

4
binary_output/main.cpp

@ -1,9 +1,9 @@
#include <iostream> #include <iostream>
#include "binary_output.hpp" #include "binary_output.hpp"
int main(){ int main() {
std::cout << make_binary(1684234849) << std::endl; std::cout << make_binary(1684234849) << std::endl;
std::cout << make_binary("A c-style array") << std::endl; std::cout << make_binary("A c-style array") << std::endl;
return 0; return 0;
} }