Btk
rwops.hpp
1 #if !defined(_BTK_RWOPS_HPP_)
2 #define _BTK_RWOPS_HPP_
3 #include <iosfwd>
4 #include <cstdint>
5 #include <cstddef>
6 #include "defs.hpp"
7 #include <SDL2/SDL_rwops.h>
8 namespace Btk{
9  //A Simple wrapper for SDL_RWOps
10  class BTKAPI RWops{
11  public:
12  RWops(SDL_RWops *r):fptr(r){};
13  RWops(const RWops &) = delete;
14  RWops(RWops && rw){
15  fptr = rw.fptr;
16  rw.fptr = nullptr;
17  }
18  ~RWops();
19  SDL_RWops *get() const noexcept{
20  return fptr;
21  }
28  bool close();
29  size_t write(const void *buf,size_t size,size_t n){
30  return SDL_RWwrite(fptr,buf,size,n);
31  }
32  size_t read(void *buf,size_t size,size_t n){
33  return SDL_RWread(fptr,buf,size,n);
34  }
35  size_t tell() const{
36  return SDL_RWtell(fptr);
37  }
38  size_t size() const{
39  return SDL_RWsize(fptr);
40  }
41  size_t seek(Sint64 offset,int whence){
42  return SDL_RWseek(fptr,offset,whence);
43  }
44  RWops &operator =(RWops &&);
45  static RWops FromStdIstream(std::istream &);
46  static RWops FromStdOstream(std::ostream &);
47  static RWops FromStdFstream(std::fstream &);
48  static RWops FromFile(const char *fname,const char *modes);
49  static RWops FromFD(int fd,const char *modes);
50  private:
51  SDL_RWops *fptr;
52  friend class MemBuffer;
53  };
54  //Memory buffer
55  class BTKAPI MemBuffer:public RWops{
56  public:
57  MemBuffer();
58  //MemBuffer(const MemBuffer &);
59  MemBuffer(MemBuffer &&);
60  ~MemBuffer();
61  //tell current pos
62  Sint64 tellp() const noexcept;
63  //seek
64  Sint64 seek(int64_t offset,int whence);
65  //RW
66  size_t write(const void *buf,size_t num,size_t n);
67  size_t read(void *buf,size_t num,size_t n);
68  //Get size
69  size_t size() const noexcept{
70  return buf_ptr - buf_base;
71  };
72  size_t capcitity() const noexcept{
73  return buf_end - buf_base;
74  };
75  //write all data out
76  friend std::ostream &operator <<(std::ostream&,const MemBuffer&);
77  private:
78  Uint8 *buf_base;//begining of buffer
79  Uint8 *buf_ptr;//current pos
80  Uint8 *buf_end;//end of buffer
81  };
88  BTKAPI void CreatePipe(RWops &r,RWops &w);
89 } // namespace Btk
90 
91 #endif // _BTK_RWOPS_HPP_
This header include many useful containers.
Definition: async.hpp:7
BTKAPI void CreatePipe(RWops &r, RWops &w)
Create a two binary pipes.
Definition: rwops.cpp:365
PStream & operator<<(PStream &stream, std::string_view data)
Definition: popen.hpp:174
Definition: rwops.hpp:55
Definition: rwops.hpp:10