Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T>
- class TSharedPtr {
- public:
- TSharedPtr(T* ptr = nullptr)
- : ptr_(ptr)
- {}
- ~TSharedPtr() {
- reset();
- }
- TSharedPtr(TSharedPtr& other)
- : ptr_(other.ptr_)
- : refcount_(other.refcount_)
- {
- ++*refcount_;
- }
- TSharedPtr& operator=(TSharedPtr& other) {
- if (this != &other) {
- reset(other.ptr_, other.refcount_);
- ++*refcount_;
- return *this;
- }
- }
- void reset(T* ptr = nullptr) {
- if (ptr == ptr_)
- return;
- unref();
- if (ptr) {
- ptr_ = ptr;
- refcount_ = new int(1);
- }
- }
- T* release() {
- T* response = ptr_;
- unref();
- return ptr;
- }
- T& operator* () {
- return *ptr_;
- }
- const T& operator* () const {
- return *ptr_;
- }
- private:
- void reset(T* ptr, int* refcount) {
- ptr_ = ptr;
- refcount_ = refcount;
- }
- void unref() {
- if (!ptr)
- return;
- if (--*refcount == 0) {
- delete ptr_;
- delete refcount_;
- }
- }
- std::atomic<int>* refcount_ = nullptr;
- T* ptr_;
- };
- int f(shared_ptr<int> arg);
- std::shared_ptr<int> p = new int;
- auto q = p;
- f(p);
- =================================================================
- ==111180==ERROR: LeakSanitizer: detected memory leaks
- Direct leak of 4 byte(s) in 1 object(s) allocated from:
- #0 0x55a4eb06b895 in operator new(unsigned long) (/home/gluk47/Yandex.Disk/src/hse/oimp/sem 7/lsan+0x32895) (BuildId: 6a26cc7d66d6b07bcde722d98dddfeef4666566c)
- #1 0x55a4eb06d61f in DynamicIntHolder::DynamicIntHolder(int) /home/gluk47/Yandex.Disk/src/hse/oimp/sem 7/code.cpp:86:13
- #2 0x55a4eb06d3a9 in std::__detail::_MakeUniq<DynamicIntHolder>::__single_object std::make_unique<DynamicIntHolder, int>(int&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/unique_ptr.h:1065:34
- #3 0x55a4eb06d25f in main /home/gluk47/Yandex.Disk/src/hse/oimp/sem 7/code.cpp:101:36
- #4 0x7f3bc32f0d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
- SUMMARY: LeakSanitizer: 4 byte(s) leaked in 1 allocation(s).
- class IImageProcessor { // ABT, abstract base type
- public:
- virtual void UpdateImage(TImage&) = 0;
- virtual ~IImageProcessor() {}
- };
- class TSepiaFilter : public IImageProcessor {
- public:
- void UpdateImage(TImage&) override {}
- ~TSepiaFilter() {
- cout << "~TSepiaFilter\n";
- }
- private:
- vector<int> pixels_;
- };
- class TProgram {
- public:
- void LoadProcessors(const TOptions& options) {
- static unordered_map<string, std::function<IImageProcessor*()>> create_filter = {
- {"sepia", []{ return new TSepiaFilter; }},
- {"gauss", []{ return new TGaussFilter; }},
- };
- for (const string& name : options.GetFilterNames()) {
- // unique_ptr<IImageProcessor> new_obj = std::make_unique<TSepiaFilter>();
- Preprocessors.emplace_back(create_filter[name]());
- }
- }
- void Apply() {
- for (auto* p : Processors)
- p->UpdateImage(image_);
- }
- ~TProgram() {
- for (auto* p : Processors)
- delete p;
- }
- private:
- TImage image_;
- vector<IImageProcessor*> Processors;
- // vector<unique_ptr<IImageProcessor>> Processors;
- }
- //----------------------------
- class Base {
- public:
- Base(int) {
- }
- Base(string s)
- : Base(std::stoi(s))
- {}
- int pow(int, int){ return 1; }
- int pow(int, string){ return 2; }
- };
- class Derived : public Base{
- public:
- using Base::Base; //<
- Derived()
- : Base(42)
- {}
- // int pow(int a, int b) { return Base::pow(a, b); }
- using Base::pow;
- int pow(int, string){ return 3; }
- // int pow(int, char){ return 4; }
- };
- Derived d;
- d.pow(2, 3); //< compilation failure. Function hiding
- // d.Derived::pow(int, int)
- //----------------------------
- class TUnixPath {
- public:
- TUnixPath(string_view path)
- : initial_path([path] {
- vector<string> split;
- Split(path, split);
- return split;
- }())
- , current_path(initial_path)
- {
- // initial_path = Split(path);
- // current_path = initial_path;
- }
- void Split(string_view path, vector<string>& result) {
- result = {};
- }
- vector<string> Split(string_view path) {
- vector<string> split;
- Split(path, split);
- return split;
- }
- private:
- const vector<string> initial_path;
- vector<string> current_path;
- };
- //----------------------------
- class Base {
- int value;
- };
- class Left : public virtual Base {
- public:
- Left() {
- value = 1;
- }
- };
- class Right : public virtual Base {
- public:
- Right() {
- value = 2;
- }
- };
- class Derived : public Left, Right {
- };
- Derived d;
- cout << d.Left::value << d.Right::value << d.value; // 222
- //----------------------------
- class Base {
- int value;
- };
- class Left : public Base {
- public:
- Left() {
- value = 1;
- }
- };
- class Right : public Base {
- public:
- Right() {
- value = 2;
- }
- };
- class Derived : public Left, Right {
- };
- Derived d;
- cout << d.value; // не соберётся
- cout << d.Left::value << d.Right::value; // 1 2
- cout << static_cast<Left&>(d).value;
- //----------------------------
- class IImageProcessor {
- public:
- virtual void UpdateImage(TImage&) = 0;
- };
- class TProgram {
- public:
- void LoadProcessors(const TOptions& options) {
- static unordered_map<string, std::function<IImageProcessor*()>> create_filter = {
- {"sepia", []{ return new TSepiaFilter; }},
- {"gauss", []{ return new TGaussFilter; }},
- };
- for (const string& name : options.GetFilterNames()) {
- Preprocessors.emplace_back(create_filter[name]());
- }
- }
- void Apply() {
- for (auto* p : Processors)
- p->UpdateImage(image_);
- }
- private:
- TImage image_;
- vector<IImageProcessor*> Processors;
- }
- //----------------------------
- class Base {
- public:
- Base() {
- // compute();
- print();
- }
- virtual void print() {
- cout << "b" << compute();
- }
- virtual int compute() = 0; // pure virtual
- };
- class Derived : public Base {
- public:
- Derived() {
- print();
- }
- void print() override {
- cout << "d";
- }
- int compute() override {
- return 42;
- }
- };
- Derived obj; // "bd
- // object slicing
- class string {
- bool compare();
- virtual string& swap(string& other) {
- return *this;
- }
- };
- class case_insensitive_string : public string { // is-a
- bool compare() {
- return icase_compare();
- }
- bool icase_compare(){}
- // не соберётся
- void swap(case_insensitive_string& r) override {
- swap(ignore_punctuation, r.ignore_punctuation);
- }
- // ok, covariant return type
- case_insensitive_string& swap(string& r) override {
- if (auto* d = dynamic_cast<case_insensitive_string*>(&r))
- swap(ignore_punctuation, d->ignore_punctuation);
- return *this;
- }
- private:
- bool ignore_punctuation = false;
- };
- void f(const string& s) {
- string tmp = s; //<
- }
- void swap(string& l, string& r) {
- // l = r
- if (l > r) {
- std::swap(l, r);
- }
- }
- case_insensitive_string d;
- f(d);
- // pImpl = pointer to implementation
- // image_filter.h
- class TImageFilterImpl;
- class TImageFilter {
- public:
- TImageFilter(int l)
- : level(l)
- {}
- void Process(TBitmap& img);
- private:
- // void Prepare();
- // void Pass1();
- // void Interpolate();
- // friend TImageFilterImpl;
- TImageFilterImpl* impl;
- int level;
- };
- // image_filter.cpp
- class TImageFilterImpl {
- void Prepare;
- void Process() {
- }
- };
- void TImageFilter::Process() {
- impl->Process();
- }
- //----------------------------
- // vector.h
- class Vector {
- int* data;
- };
- // matrix.h
- // #include "vector.h"
- class Vector; // forward declaration
- class Matrix {
- Vector header; // < не соберётся
- void f(Vector v);
- Vector* ptr;
- Vector& ref;
- vector<Vector*> row;
- };
- //----------------------------
- // header.h
- static int answer;
- // vector.cpp
- #include "header.h"
- void set(int v) {
- answer = v; // __namespace_vector_anon_answer
- }
- // matrix.cpp
- #include "header.h"
- // int answer; // redefinition (от компилятора)
- int get() {
- return answer;
- }
- // main.cpp
- #include "header.h"
- int main () {
- set(7);
- cout << get(); //< 0
- }
- //----------------------------
- // header.h
- extern int answer;
- // vector.cpp
- #include "header.h"
- // matrix.cpp
- #include "header.h"
- int answer; // иначе undefined reference
- // main.cpp
- #include "header.h"
- int main () {
- cout << answer; //<
- }
- //----------------------------
- // vector.cpp
- namespace {
- int answer;
- } // namespace
- // matrix.cpp
- static int answer;
- // main.cpp
- int main () {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement