Euphoria
memorychunk.cc
Go to the documentation of this file.
1 #include "base/memorychunk.h"
2 
3 #include <cstring> // for memcpy
4 #include <utility>
5 
6 #include "assert/assert.h"
7 #include "base/cint.h"
8 
9 namespace eu
10 {
11  char*
13  {
14  return data.get();
15  }
16 
17  const char*
19  {
20  return data.get();
21  }
22 
23  int
25  {
26  return size;
27  }
28 
29  char MemoryChunk::operator[](int index) const
30  {
31  return data[index];
32  }
33 
34  char& MemoryChunk::operator[](int index)
35  {
36  return data[index];
37  }
38 
39  std::shared_ptr<MemoryChunk>
41  {
42  std::shared_ptr<MemoryChunk> ret {new MemoryChunk(size)};
43  return ret;
44  }
45 
46  std::shared_ptr<MemoryChunk>
48  {
49  std::shared_ptr<MemoryChunk> ret;
50  return ret;
51  }
52 
53  MemoryChunk::MemoryChunk(int the_size) : size(the_size)
54  {
55  ASSERT(size > 0);
56  data = std::make_unique<char[]>(size);
57  }
58 
59  void
60  copy_to_memory(MemoryChunk* memory, const void* src)
61  {
62  std::memcpy(memory->get_data(), src, sizeof(char) * memory->get_size());
63  }
64 
65  std::shared_ptr<MemoryChunk>
66  create_memory_chunk_from_string(const std::string& content)
67  {
68  auto file = MemoryChunk::allocate
69  (
70  c_sizet_to_int(content.length() + 1)
71  );
72  copy_to_memory(file.get(), content.data());
73  return file;
74  }
75 
76  MemoryChunkFile::MemoryChunkFile(std::shared_ptr<MemoryChunk> d)
77  : data(std::move(d))
78  , position(0)
79  {
80  }
81 
82  void
83  MemoryChunkFile::write(const void* src, int size)
84  {
85  ASSERT(position + size <= data->get_size());
86  std::memcpy(data->get_data() + position, src, sizeof(char) * size);
87  position += size;
88  }
89 
90 }
#define ASSERT(x)
Definition: assert.h:29
Definition: assert.h:90
int c_sizet_to_int(size_t t)
Definition: cint.cc:11
std::shared_ptr< MemoryChunk > create_memory_chunk_from_string(const std::string &content)
Definition: memorychunk.cc:66
void copy_to_memory(MemoryChunk *memory, const void *src)
Definition: memorychunk.cc:60
void write(const void *src, int size)
Definition: memorychunk.cc:83
std::shared_ptr< MemoryChunk > data
Definition: memorychunk.h:34
MemoryChunkFile(std::shared_ptr< MemoryChunk > d)
Definition: memorychunk.cc:76
char * get_data()
Definition: memorychunk.cc:12
char operator[](int index) const
Definition: memorychunk.cc:29
static std::shared_ptr< MemoryChunk > allocate(int size)
Definition: memorychunk.cc:40
int get_size() const
Definition: memorychunk.cc:24
static std::shared_ptr< MemoryChunk > create_null()
Definition: memorychunk.cc:47