#ifndef SERIALIZE_H
#define SERIALIZE_H

#include <iostream>
#include <string>

namespace Kalkulon{
namespace serialize{


// char
inline void put(std::ostream& os, char c)
{
  os.write((char *) &c, sizeof(c));
}

inline char getChar(std::istream& is)
{
  char c;
  is.read((char *) &c, sizeof(c));
  return c;
}


// integer
inline void put(std::ostream& os, int i)
{
  os.write((char *) &i, sizeof(i));
}

inline int getInt(std::istream& is)
{
  int i;
  is.read((char *) &i, sizeof(i));
  return i;
}


// size_t
inline void put(std::ostream& os, size_t i)
{
  os.write((char *) &i, sizeof(i));
}

inline size_t getSize_t(std::istream& is)
{
  size_t i;
  is.read((char *) &i, sizeof(i));
  return i;
}


// double
inline void put(std::ostream& os, double d)
{
  os.write((char *) &d, sizeof(d));
}

inline double getDouble(std::istream& is)
{
  double d;
  is.read((char *) &d, sizeof(d));
  return d;
}



// std::string
inline void put(std::ostream& os, const std::string& s)
{
  size_t len = s.size();
  os.write((char *) &len, sizeof(len));
  os.write((char *) s.c_str(), len);
}

inline std::string getString(std::istream& is)
{
  std::string str;

  size_t len;
  is.read((char *) &len, sizeof(len));
  // see http://herbsutter.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/
  // why this should work
  str.resize(len);
  is.read((char *) &str[0], len);
  return str;
}


}} // end of namespace


#endif // SERIALIZE_H
