vsg  1.1.0
VulkanSceneGraph library
mem_stream.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2022 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/core/Export.h>
16 
17 #include <cstdint>
18 #include <istream>
19 
20 namespace vsg
21 {
22 
25  class VSG_DECLSPEC mem_stream : public std::istream
26  {
27  public:
28  mem_stream(const uint8_t* ptr, size_t length);
29  mem_stream(const std::string& str, std::string::size_type pos, std::string::size_type length);
30  explicit mem_stream(const std::string_view& sv);
31 
33  void set(const uint8_t* ptr, size_t length);
34 
36  void set(const std::string_view& sv) { set(reinterpret_cast<const uint8_t*>(sv.data()), sv.size()); }
37 
39  void set(const std::string& str, std::string::size_type pos, std::string::size_type length) { set(reinterpret_cast<const uint8_t*>(&(str[pos])), length); }
40 
41  private:
42  struct mem_buffer : public std::streambuf
43  {
44  mem_buffer(const uint8_t* ptr, size_t length);
45 
46  inline void set(const uint8_t* ptr, size_t length)
47  {
48  setg((char*)(ptr), (char*)(ptr), (char*)(ptr) + length);
49  }
50  };
51 
52  mem_buffer _buffer;
53  };
54 
55 } // namespace vsg
Definition: mem_stream.h:26
void set(const std::string_view &sv)
set the mem_stream to string_view
Definition: mem_stream.h:36
void set(const uint8_t *ptr, size_t length)
set the mem_stream to memory block
void set(const std::string &str, std::string::size_type pos, std::string::size_type length)
set the mem_stream to portion of string
Definition: mem_stream.h:39