Skip to content

Commit 5849a45

Browse files
committed
distant with IVAN NIKOLAEVIC
1 parent 0495145 commit 5849a45

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

vector_hw/hw_6_raw_memory.h

+32-8
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33
#pragma once
44

55
#include <memory>
6+
#include <utility>
67

78
namespace bmstu {
89
template<typename Z>
910
class raw_memory {
10-
public: // NOLINT
11+
public: // NOLINT
1112
raw_memory() = default;
1213

1314
explicit raw_memory(size_t size): buffer_(allocate_(size)), capacity_(size) {
1415
}
1516

16-
~raw_memory() { deallocate_(buffer_); }
17+
~raw_memory() {
18+
deallocate_(buffer_);
19+
capacity_ = 0;
20+
}
1721

1822
raw_memory(const raw_memory& other) = delete;
23+
1924
// мы не хотим копировать указатель на память одну и ту же, вдруг один удалится указатель, а второй - нет, будет UB.
2025

2126
raw_memory& operator=(const raw_memory& other) = delete;
@@ -44,17 +49,36 @@ class raw_memory {
4449
}
4550

4651
const Z& operator[](size_t index) const noexcept {
47-
assert(index < capacity_);
48-
return const_cast<raw_memory>(*this)[index];
52+
return const_cast<raw_memory &>(*this)[index];
4953
// todo: возможно нужен raw_memory&
5054
}
5155

52-
private: // NOLINT
53-
void swap_(raw_memory other) noexcept {
54-
std::swap(buffer_, other.buffer_);
55-
std::swap(capacity_, other.capacity_);
56+
Z* operator+(size_t offset) noexcept {
57+
assert(offset <= capacity_); // = потому что мы хотим еще и end хранить,
58+
return buffer_ + offset;
59+
}
60+
61+
const Z* operator+(size_t offset) const noexcept {
62+
return const_cast<raw_memory &>(*this) + offset;
5663
}
5764

65+
size_t capacity() const {
66+
return capacity_;
67+
}
68+
69+
Z* buffer() {
70+
return buffer_;
71+
}
72+
73+
const Z* buffer() const {
74+
return buffer_;
75+
}
76+
77+
void swap(raw_memory other) noexcept {
78+
std::swap(buffer_, other.buffer_);
79+
std::swap(capacity_, other.capacity_);
80+
} // todo: возможно в прайват надо
81+
private: // NOLINT
5882
// нужны методы, которые аллоцируют и деаллоцируют сырую память.
5983
static Z* allocate_(size_t size) {
6084
return size != 0

vector_hw/hw_6_raw_memory_tests.cpp renamed to vector_hw/hw_6_tests.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <numeric>
88

99
#include "hw_6_raw_memory.h"
10+
#include "hw_6_true_vector.h"
1011

1112
class WithoutDefaultConstructor {
1213
public: // NOLINT

vector_hw/hw_6_true_vector.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
#pragma once
44

55
namespace bmstu {
6-
}
6+
template<typename Z>
7+
class vector {
8+
};
9+
} // namespace bmstu

0 commit comments

Comments
 (0)