YPC  0.2.0
ntobject_file.h
1 #pragma once
2 #include "ypc/core/byte.h"
3 #include "ypc/core/exceptions.h"
4 #include "ypc/core/filesystem.h"
5 #include "ypc/corecommon/package.h"
6 #include <ff/net/middleware/ntpackage.h>
7 #include <fstream>
8 #include <string>
9 
10 namespace ypc {
11 namespace internal {
12 
13 template <typename NtObjTy> class ntobject_file_base {
14 public:
15  explicit ntobject_file_base(const std::string &file_path)
16  : m_path(file_path) {}
17 
18  inline NtObjTy &data() { return m_sfm_data; }
19 
20  inline const NtObjTy &data() const { return m_sfm_data; }
21 
22  void read_from() {
23  if (!is_file_exists(m_path)) {
24  throw file_not_found(m_path, "ntobject_file::read_from()");
25  }
26 
27  std::fstream fs;
28  fs.open(m_path, std::ios::in | std::ios::binary);
29 
30  fs.seekg(0, fs.end);
31  size_t size = fs.tellg();
32  fs.seekg(0, fs.beg);
33 
34  bytes buf(size);
35  fs.read((char *)buf.data(), size);
36  ff::net::marshaler m((const char *)buf.data(), size,
37  ff::net::marshaler::deserializer);
38  m_sfm_data.arch(m);
39 
40  fs.close();
41  }
42  void write_to() {
43  std::fstream fs;
44 
45  fs.open(m_path, std::ios::out | std::ios::binary);
46  if (!fs.is_open()) {
47  throw file_open_failure(m_path, "ntobject_file::write_to()");
48  }
49  ff::net::marshaler m(ff::net::marshaler::length_retriver);
50  m_sfm_data.arch(m);
51  size_t len = m.get_length();
52  bytes buf(len);
53 
54  ff::net::marshaler sm((char *)buf.data(), len,
55  ff::net::marshaler::serializer);
56  m_sfm_data.arch(sm);
57 
58  fs.write((const char *)buf.data(), len);
59  fs.close();
60  }
61 
62 protected:
63  NtObjTy m_sfm_data;
64  std::string m_path;
65 };
66 
67 template <typename NtObjTy> struct check_nt_type {
68  constexpr static uint32_t value = 0;
69 };
70 template <typename... ARGS>
71 struct check_nt_type<::ff::util::ntobject<ARGS...>> {
72  constexpr static uint32_t value = 1;
73 };
74 template <uint32_t PackageID, typename... ARGS>
75 struct check_nt_type<::ff::net::ntpackage<PackageID, ARGS...>> {
76  constexpr static uint32_t value = 2;
77 };
78 } // namespace internal
79 
80 template <typename NtObjTy,
83 };
84 template <typename NtObjTy>
85 class ntobject_file<NtObjTy, 1>
87  typename cast_obj_to_package<NtObjTy>::type> {
88  using base_t =
90 
91 public:
92  explicit ntobject_file(const std::string &file_path) : base_t(file_path) {}
93 
94  inline NtObjTy &data() { return m_local_data; }
95 
96  inline const NtObjTy &data() const { return m_local_data; }
97  void read_from() {
98  base_t::read_from();
99  m_local_data = base_t::m_sfm_data;
100  }
101  void write_to() {
102  base_t::m_sfm_data = m_local_data;
103  base_t::write_to();
104  }
105 
106 protected:
107  NtObjTy m_local_data;
108 };
109 
110 template <typename NtObjTy>
111 class ntobject_file<NtObjTy, 2> : public internal::ntobject_file_base<NtObjTy> {
113 
114 public:
115  explicit ntobject_file(const std::string &file_path) : base_t(file_path) {}
116 };
117 template <typename NtObjTy> class ntobject_file<NtObjTy, 0> {
118 public:
119  explicit ntobject_file(const std::string &file_path) {
120  static_assert(
122  "ntobject_file only support ff::util::ntobject and ff::net::ntpackage");
123  };
124 };
125 
126 } // namespace ypc
ypc::internal::check_nt_type
Definition: ntobject_file.h:67
ypc::ntobject_file
Definition: ntobject_file.h:82
ypc::internal::ntobject_file_base
Definition: ntobject_file.h:13
byte.h
ypc::file_not_found
Definition: exceptions.h:30
ypc::file_open_failure
Definition: exceptions.h:43
ypc::utc::bytes< byte_t, ::ypc::utc::byte_encode::raw_bytes >