Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions 14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <cassert>
#include <iostream>
#include <cstring>
#include <type_traits>

template<size_t SIZE, typename... Types>
concept EnoughMemory = (sizeof(Types) + ... + 0) <= SIZE;

template<typename... Types>
concept AllCopyConstructable = (std::is_copy_constructible_v<Types> && ...);

template<size_t SIZE, typename... Types>
requires EnoughMemory<SIZE, Types...> && AllCopyConstructable<Types...>
void allocate(void* memory, Types... args) {
auto ptr = static_cast<char *>(memory);
auto copy = [&ptr]<typename T>(const T& arg) {
new (ptr) T(arg);
ptr += sizeof(T);
};
(copy(args), ...);
}
/*
int main() {
constexpr size_t SIZE = 17;
char static_arr[SIZE];
int a;
std::cin >> a;
allocate<SIZE>(static_arr, a, 4, 'a', "hi");

int val1 = *reinterpret_cast<int*>(static_arr);
assert(val1 == a);
int val2 = *reinterpret_cast<int*>(static_arr + sizeof(int));
assert(val2 == 4);
char val3 = static_arr[sizeof(int) * 2];
assert(val3 == 'a');
const char* val4 = *reinterpret_cast<const char**>(static_arr + sizeof(int) * 2 + sizeof(char));
assert(std::strcmp(val4, "hi") == 0);
}
*/
49 changes: 49 additions & 0 deletions 15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <vector>

#include "14.cpp"
#include "2.cpp"

template<typename... Types>
class Container {
size_t size{};
char* data{};
std::vector<size_t> types;

public:
Container(Types... args): size((sizeof(Types) + ... + 0)), data(new char[size]), types{sizeof(Types)...} {
allocate<(sizeof(Types) + ... + 0)>(data, args...);
}
~Container() { delete[] data; }

Container(const Container& other) = delete;
Container& operator=(const Container& other) = delete;

Container(Container&& other) {
std::swap(size, other.size);
std::swap(data, other.data);
std::swap(types, other.types);
}
Container& operator=(Container&& other) {
std::swap(size, other.size);
std::swap(data, other.data);
std::swap(types, other.types);
return *this;
}

template<typename T>
T getElement(const size_t idx) {
assert(sizeof(T) == types[idx]);
size_t offset = 0;
for (size_t i = 0; i < idx; ++i) offset += types[i];
return *reinterpret_cast<T*>(data + offset);
}
};

int main() {
Container c(12, 'c', Point{3, 4});
Container c2;
Container c3((std::move(c)));
std::cout << c3.getElement<int>(0) << std::endl;
std::cout << c3.getElement<char>(1) << std::endl;
std::cout << c3.getElement<Point>(2) << std::endl;
}
10 changes: 8 additions & 2 deletions 2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ struct Point {
friend bool operator==(const Point& p1, const Point& p2) {
return std::abs(p1.x - p2.x) < eps && std::abs(p1.y - p2.y) < eps;
}

friend std::ostream& operator<<(std::ostream& os, const Point& point) {
os << "(" << point.x << ", " << point.y << ")";
return os;
}
};

class Line {
Expand Down Expand Up @@ -58,7 +63,7 @@ class Line {
}
};


/*
int main() {
Point p1{42, 17}; Point p2{42, 17};
auto invalid_line = Line::fabric(p1, p2);
Expand All @@ -78,4 +83,5 @@ int main() {
Line perp = line.perpendicular({1, 1});
auto inter_perp = line.intersection(perp);
assert(inter_perp.has_value());
}
}
*/