SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
SmartFd.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_fs_SmartFd_h
7 #define swcdb_fs_SmartFd_h
8 
9 #include "swcdb/core/Compat.h"
10 #include "swcdb/core/MutexAtomic.h"
11 
12 namespace SWC {
13 
14 
29 namespace FS {
30 
31 
33 
34 class SmartFd {
35  public:
36 
37  typedef std::shared_ptr<SmartFd> Ptr;
38 
40  static Ptr make_ptr(const std::string& filepath, uint32_t flags,
41  int32_t fd=-1, uint64_t pos=0) {
42  return Ptr(new SmartFd(filepath, flags, fd, pos));
43  }
44 
46  static Ptr make_ptr(std::string&& filepath, uint32_t flags,
47  int32_t fd=-1, uint64_t pos=0) {
48  return Ptr(new SmartFd(std::move(filepath), flags, fd, pos));
49  }
50 
52  SmartFd(const std::string& filepath, uint32_t flags,
53  int32_t fd=-1, uint64_t pos=0)
55  }
56 
58  SmartFd(std::string&& filepath, uint32_t flags,
59  int32_t fd=-1, uint64_t pos=0) noexcept
60  : m_filepath(std::move(filepath)),
61  m_flags(flags), m_fd(fd), m_pos(pos) {
62  }
63 
64  virtual ~SmartFd() noexcept;
65 
66  constexpr SWC_CAN_INLINE
67  const std::string& filepath() const noexcept {
68  return m_filepath;
69  }
70 
71  constexpr SWC_CAN_INLINE
72  void flags(uint32_t flags) noexcept {
74  }
75 
76  constexpr SWC_CAN_INLINE
77  uint32_t flags() const noexcept {
78  return m_flags;
79  }
80 
81  constexpr SWC_CAN_INLINE
82  void fd(int32_t fd) noexcept {
83  m_fd.store(fd);
84  }
85 
86  constexpr SWC_CAN_INLINE
87  int32_t fd() const noexcept {
88  return m_fd;
89  }
90 
91  constexpr SWC_CAN_INLINE
92  bool valid() const noexcept {
93  return m_fd != -1;
94  }
95 
96  constexpr SWC_CAN_INLINE
97  int32_t invalidate() noexcept {
98  m_pos.store(0);
99  return m_fd.exchange(-1);
100  }
101 
102  constexpr SWC_CAN_INLINE
103  void pos(uint64_t pos) noexcept {
104  m_pos.store(pos);
105  }
106 
107  constexpr SWC_CAN_INLINE
108  uint64_t pos() const noexcept {
109  return m_pos;
110  }
111 
112  constexpr SWC_CAN_INLINE
113  void forward(uint64_t nbytes) noexcept {
114  m_pos.fetch_add(nbytes);
115  }
116 
117  std::string to_string() const;
118 
119  void print(std::ostream& out) const;
120 
121  protected:
122 
123  const std::string m_filepath;
124 
125  private:
126 
130 
131 };
132 
133 }}
134 
135 
136 
137 #ifdef SWC_IMPL_SOURCE
138 #include "swcdb/fs/SmartFd.cc"
139 #endif
140 
141 #endif // swcdb_fs_SmartFd_h
SWC::FS::SmartFd::make_ptr
static SWC_CAN_INLINE Ptr make_ptr(std::string &&filepath, uint32_t flags, int32_t fd=-1, uint64_t pos=0)
Definition: SmartFd.h:46
SWC::FS::SmartFd::m_fd
Core::Atomic< int32_t > m_fd
Definition: SmartFd.h:128
SWC::FS::SmartFd::invalidate
constexpr SWC_CAN_INLINE int32_t invalidate() noexcept
Definition: SmartFd.h:97
SWC::FS::SmartFd::SmartFd
SWC_CAN_INLINE SmartFd(const std::string &filepath, uint32_t flags, int32_t fd=-1, uint64_t pos=0)
Definition: SmartFd.h:52
SWC::FS::SmartFd::pos
constexpr SWC_CAN_INLINE uint64_t pos() const noexcept
Definition: SmartFd.h:108
SWC::Core::Atomic< uint32_t >
SWC::FS::SmartFd::pos
constexpr SWC_CAN_INLINE void pos(uint64_t pos) noexcept
Definition: SmartFd.h:103
SWC::FS::SmartFd::make_ptr
static SWC_CAN_INLINE Ptr make_ptr(const std::string &filepath, uint32_t flags, int32_t fd=-1, uint64_t pos=0)
Definition: SmartFd.h:40
SWC::FS::SmartFd::m_flags
Core::Atomic< uint32_t > m_flags
Definition: SmartFd.h:127
SWC::FS::SmartFd::SmartFd
SWC_CAN_INLINE SmartFd(std::string &&filepath, uint32_t flags, int32_t fd=-1, uint64_t pos=0) noexcept
Definition: SmartFd.h:58
SWC::FS::SmartFd::m_pos
Core::Atomic< uint64_t > m_pos
Definition: SmartFd.h:129
SWC::Core::AtomicBase::store
constexpr SWC_CAN_INLINE void store(T v) noexcept
Definition: Atomic.h:37
SWC::FS::SmartFd::m_filepath
const std::string m_filepath
Definition: SmartFd.h:123
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
Compat.h
SmartFd.cc
SWC::FS::SmartFd::valid
constexpr SWC_CAN_INLINE bool valid() const noexcept
Definition: SmartFd.h:92
MutexAtomic.h
SWC::FS::SmartFd::flags
constexpr SWC_CAN_INLINE uint32_t flags() const noexcept
Definition: SmartFd.h:77
SWC::FS::SmartFd
Smart FileDescriptor.
Definition: SmartFd.h:34
SWC::Core::AtomicBase::exchange
constexpr SWC_CAN_INLINE T exchange(T value) noexcept
Definition: Atomic.h:47
SWC::FS::SmartFd::forward
constexpr SWC_CAN_INLINE void forward(uint64_t nbytes) noexcept
Definition: SmartFd.h:113
SWC::FS::SmartFd::filepath
constexpr SWC_CAN_INLINE const std::string & filepath() const noexcept
Definition: SmartFd.h:67
SWC::FS::SmartFd::Ptr
std::shared_ptr< SmartFd > Ptr
Definition: SmartFd.h:37
SWC::FS::SmartFd::to_string
std::string to_string() const
Definition: SmartFd.cc:15
SWC::FS::SmartFd::fd
constexpr SWC_CAN_INLINE void fd(int32_t fd) noexcept
Definition: SmartFd.h:82
SWC::FS::SmartFd::flags
constexpr SWC_CAN_INLINE void flags(uint32_t flags) noexcept
Definition: SmartFd.h:72
SWC::FS::SmartFd::fd
constexpr SWC_CAN_INLINE int32_t fd() const noexcept
Definition: SmartFd.h:87
SWC::Core::Atomic::fetch_add
constexpr SWC_CAN_INLINE T fetch_add(T v) noexcept
Definition: Atomic.h:93
SWC::FS::SmartFd::print
void print(std::ostream &out) const
Definition: SmartFd.cc:20
SWC::FS::SmartFd::~SmartFd
virtual ~SmartFd() noexcept
Definition: SmartFd.cc:13