Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
fio.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 26.08.2013 15:27:54 CEST
3 // File: fio.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __FIO_HEADER
12 #define __FIO_HEADER
13 
14 //THIS IS NOT A HEADER FOR THE ARDUINO
15 
16 #include <fstream>
17 #include "serializer.hpp"
18 
19 namespace com {
20 
21  class fio_class {
22  public:
23  template<typename T>
24  fio_class & operator<<(T & t) {
25  serialize(ofs_, t);
26  return (*this);
27  }
28  template<typename T>
29  fio_class & operator>>(T & t) {
30  serialize(ifs_, t);
31  return (*this);
32  }
33  void open_read(std::string const & path_name) {
34  ifs_.open(path_name, std::ios_base::in);// | std::ios_base::binary);
35  if(!ifs_.is_open()) {
36  std::cout << "fio.hpp error: file " << path_name << " not found" << std::endl;
37  }
38  }
39  void open_write(std::string const & path_name) {
40  ofs_.open(path_name, std::ios_base::out);// | std::ios_base::binary);
41  if(!ofs_.is_open()) {
42  std::cout << "fio.hpp error: file " << path_name << " not found" << std::endl;
43  }
44  }
45  void close() {
46  ifs_.close();
47  ofs_.close();
48  }
49  private:
50  struct ofs_wrapper: public std::ofstream {
51  typedef std::ofstream base;
52  static archive_enum const type = archive_enum::output;
53 
54  template<typename T>
55  ofs_wrapper & operator&(T & t) {
56  serialize(*this, t);
57  return (*this);
58  }
59 
60  void write(uint8_t const & arg) {
61  char c = arg;
62  base::write(&c, 1);
63  }
64  };
65  struct ifs_wrapper: public std::ifstream {
66  typedef std::ifstream base;
67  static archive_enum const type = archive_enum::input;
68 
69  template<typename T>
70  ifs_wrapper & operator&(T & t) {
71  serialize(*this, t);
72  return (*this);
73  }
74 
75  uint8_t read() {
76  char c;
77  base::read(&c, 1);
78  return c;
79  }
80  };
81  ofs_wrapper ofs_;
82  ifs_wrapper ifs_;
83 
84  };
85 
86 
87 }//end namespace
88 
89 #endif //__FIO_HEADER
void serialize(Archive &ar, T &t)
Definition: serializer.hpp:95
void open_read(std::string const &path_name)
Definition: fio.hpp:33
fio_class & operator<<(T &t)
Definition: fio.hpp:24
Definition: fio.hpp:21
archive_enum
Definition: archive_enum.hpp:14
struct ustd::endl_class endl
void close()
Definition: fio.hpp:45
void open_write(std::string const &path_name)
Definition: fio.hpp:39
ustd::cout_class cout
fio_class & operator>>(T &t)
Definition: fio.hpp:29