#include <iostream>
#include <memory>
#include <string>
#include <utility>
template<typename T>
class Resource {
T* data;
public:
explicit Resource(T* ptr) : data(ptr) {}
~Resource() { delete data; }
Resource(Resource&& other) noexcept : data(other.data) {
other.data = nullptr;
}
Resource& operator=(Resource&& other) noexcept {
if (this != &other) {
delete data;
data = other.data;
other.data = nullptr;
}
return *this;
}
Resource(const Resource&) = delete;
Resource& operator=(const Resource&) = delete;
T* get() const { return data; }
T& operator*() const { return *data; }
};
class Document {
std::string title;
std::unique_ptr<std::string> content;
public:
explicit Document(std::string t)
: title(std::move(t)), content(std::make_unique<std::string>()) {}
void append(const std::string& text) {
content->append(text);
}
std::shared_ptr<Document> clone() const {
auto copy = std::make_shared<Document>(title);
*copy->content = *content;
return copy;
}
};
class Widget {
public:
virtual ~Widget() = default;
virtual void draw() const = 0;
virtual void resize(int w, int h) = 0;
};
class Button : public Widget {
int width, height;
std::string label;
public:
Button(std::string l, int w, int h)
: width(w), height(h), label(std::move(l)) {}
void draw() const override {
std::cout << "Button: " << label << "\n";
}
void resize(int w, int h) override {
width = w;
height = h;
}
};
int main() {
Resource<int> res(new int(42));
std::cout << *res << "\n";
std::shared_ptr<Document> doc = std::make_shared<Document>("Notes");
doc->append("Hello, World!");
std::unique_ptr<Widget> btn = std::make_unique<Button>("Click", 100, 30);
btn->draw();
return 0;
}