Advertisement
gluk47

seminar 2023-03-03

Mar 7th, 2023
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.15 KB | None | 0 0
  1. template <typename T>
  2. class TSharedPtr {
  3. public:
  4.     TSharedPtr(T* ptr = nullptr)
  5.     : ptr_(ptr)
  6.     {}
  7.  
  8.     ~TSharedPtr() {
  9.         reset();
  10.     }
  11.  
  12.     TSharedPtr(TSharedPtr& other)
  13.     : ptr_(other.ptr_)
  14.     : refcount_(other.refcount_)
  15.     {
  16.         ++*refcount_;
  17.     }
  18.  
  19.     TSharedPtr& operator=(TSharedPtr& other) {
  20.         if (this != &other) {
  21.             reset(other.ptr_, other.refcount_);
  22.             ++*refcount_;
  23.             return *this;
  24.         }
  25.     }
  26.    
  27.     void reset(T* ptr = nullptr) {
  28.         if (ptr == ptr_)
  29.             return;
  30.         unref();
  31.         if (ptr) {
  32.             ptr_ = ptr;
  33.             refcount_ = new int(1);
  34.         }
  35.     }
  36.  
  37.     T* release() {
  38.         T* response = ptr_;
  39.         unref();
  40.         return ptr;
  41.     }
  42.  
  43.     T& operator* () {
  44.         return *ptr_;
  45.     }
  46.     const T& operator* () const {
  47.         return *ptr_;
  48.     }
  49.  
  50. private:
  51.     void reset(T* ptr, int* refcount) {
  52.         ptr_ = ptr;
  53.         refcount_ = refcount;
  54.     }
  55.  
  56.     void unref() {
  57.         if (!ptr)
  58.             return;
  59.         if (--*refcount == 0) {
  60.             delete ptr_;
  61.             delete refcount_;
  62.         }
  63.     }
  64.  
  65.     std::atomic<int>* refcount_ = nullptr;
  66.     T* ptr_;
  67. };
  68.  
  69. int f(shared_ptr<int> arg);
  70.  
  71. std::shared_ptr<int> p = new int;
  72. auto q = p;
  73. f(p);
  74.  
  75. =================================================================
  76. ==111180==ERROR: LeakSanitizer: detected memory leaks
  77.  
  78. Direct leak of 4 byte(s) in 1 object(s) allocated from:
  79.     #0 0x55a4eb06b895 in operator new(unsigned long) (/home/gluk47/Yandex.Disk/src/hse/oimp/sem 7/lsan+0x32895) (BuildId: 6a26cc7d66d6b07bcde722d98dddfeef4666566c)
  80.     #1 0x55a4eb06d61f in DynamicIntHolder::DynamicIntHolder(int) /home/gluk47/Yandex.Disk/src/hse/oimp/sem 7/code.cpp:86:13
  81.     #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
  82.     #3 0x55a4eb06d25f in main /home/gluk47/Yandex.Disk/src/hse/oimp/sem 7/code.cpp:101:36
  83.     #4 0x7f3bc32f0d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
  84.  
  85. SUMMARY: LeakSanitizer: 4 byte(s) leaked in 1 allocation(s).
  86.  
  87.  
  88. class IImageProcessor {  // ABT, abstract base type
  89. public:
  90.     virtual void UpdateImage(TImage&) = 0;
  91.     virtual ~IImageProcessor() {}
  92. };
  93.  
  94. class TSepiaFilter : public IImageProcessor {
  95. public:
  96.     void UpdateImage(TImage&) override {}
  97.  
  98.     ~TSepiaFilter() {
  99.         cout << "~TSepiaFilter\n";
  100.     }
  101. private:
  102.     vector<int> pixels_;
  103. };
  104.  
  105. class TProgram {
  106. public:
  107.     void LoadProcessors(const TOptions& options) {
  108.         static unordered_map<string, std::function<IImageProcessor*()>> create_filter = {
  109.             {"sepia", []{ return new TSepiaFilter; }},
  110.             {"gauss", []{ return new TGaussFilter; }},
  111.         };
  112.         for (const string& name : options.GetFilterNames()) {
  113.             // unique_ptr<IImageProcessor> new_obj = std::make_unique<TSepiaFilter>();
  114.             Preprocessors.emplace_back(create_filter[name]());
  115.         }
  116.     }
  117.  
  118.     void Apply() {
  119.         for (auto* p : Processors)
  120.             p->UpdateImage(image_);
  121.     }
  122.  
  123.     ~TProgram() {
  124.         for (auto* p : Processors)
  125.             delete p;
  126.     }
  127.  
  128. private:
  129.     TImage image_;
  130.     vector<IImageProcessor*> Processors;
  131.     // vector<unique_ptr<IImageProcessor>> Processors;
  132. }
  133.  
  134. //----------------------------
  135.  
  136. class Base {
  137. public:
  138.     Base(int) {
  139.     }
  140.  
  141.     Base(string s)
  142.     : Base(std::stoi(s))
  143.     {}
  144.  
  145.     int pow(int, int){ return 1; }
  146.     int pow(int, string){ return 2; }
  147. };
  148.  
  149. class Derived : public Base{
  150. public:
  151.     using Base::Base; //<
  152.  
  153.     Derived()
  154.     : Base(42)
  155.     {}
  156.  
  157.     // int pow(int a, int b) { return Base::pow(a, b); }
  158.     using Base::pow;
  159.  
  160.     int pow(int, string){ return 3; }
  161.     // int pow(int, char){ return 4; }
  162. };
  163.  
  164. Derived d;
  165. d.pow(2, 3);  //< compilation failure. Function hiding
  166. // d.Derived::pow(int, int)
  167.  
  168. //----------------------------
  169.  
  170. class TUnixPath {
  171. public:
  172.     TUnixPath(string_view path)
  173.     : initial_path([path] {
  174.         vector<string> split;
  175.         Split(path, split);
  176.         return split;
  177.     }())
  178.     , current_path(initial_path)
  179.     {
  180.         // initial_path = Split(path);
  181.         // current_path = initial_path;
  182.     }
  183.    
  184.     void Split(string_view path, vector<string>& result) {
  185.         result = {};
  186.     }
  187.  
  188.     vector<string> Split(string_view path) {
  189.         vector<string> split;
  190.         Split(path, split);
  191.         return split;
  192.     }
  193.  
  194. private:
  195.     const vector<string> initial_path;
  196.     vector<string> current_path;
  197. };
  198.  
  199. //----------------------------
  200.  
  201. class Base {
  202.     int value;
  203. };
  204.  
  205. class Left : public virtual Base {
  206. public:
  207.     Left() {
  208.         value = 1;
  209.     }
  210. };
  211.  
  212. class Right : public virtual Base {
  213. public:
  214.     Right() {
  215.         value = 2;
  216.     }
  217. };
  218.  
  219.  
  220. class Derived : public Left, Right {
  221. };
  222.  
  223. Derived d;
  224. cout << d.Left::value << d.Right::value << d.value;  // 222
  225.  
  226. //----------------------------
  227.  
  228. class Base {
  229.     int value;
  230. };
  231.  
  232. class Left : public Base {
  233. public:
  234.     Left() {
  235.         value = 1;
  236.     }
  237. };
  238.  
  239. class Right : public Base {
  240. public:
  241.     Right() {
  242.         value = 2;
  243.     }
  244. };
  245.  
  246.  
  247. class Derived : public Left, Right {
  248. };
  249.  
  250. Derived d;
  251. cout << d.value; // не соберётся
  252. cout << d.Left::value << d.Right::value;  // 1 2
  253. cout << static_cast<Left&>(d).value;
  254.  
  255. //----------------------------
  256.  
  257. class IImageProcessor {
  258. public:
  259.     virtual void UpdateImage(TImage&) = 0;
  260. };
  261.  
  262. class TProgram {
  263. public:
  264.     void LoadProcessors(const TOptions& options) {
  265.       static unordered_map<string, std::function<IImageProcessor*()>> create_filter = {
  266.           {"sepia", []{ return new TSepiaFilter; }},
  267.           {"gauss", []{ return new TGaussFilter; }},
  268.       };
  269.       for (const string& name : options.GetFilterNames()) {              
  270.           Preprocessors.emplace_back(create_filter[name]());
  271.       }
  272.     }
  273.  
  274.     void Apply() {
  275.         for (auto* p : Processors)
  276.             p->UpdateImage(image_);
  277.     }
  278.  
  279. private:
  280.     TImage image_;
  281.     vector<IImageProcessor*> Processors;
  282. }
  283.  
  284.  
  285.  
  286. //----------------------------
  287.  
  288. class Base {
  289. public:
  290.     Base() {
  291.         // compute();
  292.         print();
  293.     }
  294.    
  295.     virtual void print() {
  296.         cout << "b" << compute();
  297.     }
  298.     virtual int compute() = 0;  // pure virtual
  299. };
  300.  
  301. class Derived : public Base {
  302. public:
  303.     Derived() {
  304.         print();
  305.     }
  306.  
  307.     void print() override {
  308.         cout << "d";
  309.     }
  310.  
  311.     int compute() override {
  312.         return 42;
  313.     }
  314. };
  315.  
  316. Derived obj;  // "bd
  317.  
  318. // object slicing
  319.  
  320. class string {
  321.     bool compare();
  322.     virtual string& swap(string& other) {
  323.         return *this;
  324.     }
  325. };
  326. class case_insensitive_string : public string {  // is-a
  327.     bool compare() {
  328.         return icase_compare();
  329.     }
  330.  
  331.     bool icase_compare(){}
  332.  
  333.     // не соберётся
  334.     void swap(case_insensitive_string& r) override {
  335.         swap(ignore_punctuation, r.ignore_punctuation);
  336.     }
  337.  
  338.     // ok, covariant return type
  339.     case_insensitive_string& swap(string& r) override {
  340.         if (auto* d = dynamic_cast<case_insensitive_string*>(&r))
  341.             swap(ignore_punctuation, d->ignore_punctuation);
  342.         return *this;
  343.     }
  344.  
  345. private:
  346.     bool ignore_punctuation = false;
  347. };
  348.  
  349. void f(const string& s) {
  350.     string tmp = s; //<
  351. }
  352.  
  353. void swap(string& l, string& r) {
  354.     // l = r
  355.     if (l > r) {
  356.         std::swap(l, r);
  357.     }
  358. }
  359.  
  360. case_insensitive_string d;
  361. f(d);
  362.  
  363. // pImpl = pointer to implementation
  364.  
  365. // image_filter.h
  366.  
  367. class TImageFilterImpl;
  368.  
  369. class TImageFilter {
  370. public:
  371.     TImageFilter(int l)
  372.     : level(l)
  373.     {}
  374.     void Process(TBitmap& img);
  375.  
  376. private:
  377.     // void Prepare();
  378.     // void Pass1();
  379.     // void Interpolate();
  380.     // friend TImageFilterImpl;
  381.     TImageFilterImpl* impl;
  382.  
  383.     int level;
  384. };
  385.  
  386. // image_filter.cpp
  387.  
  388. class TImageFilterImpl {
  389.     void Prepare;
  390.     void Process() {
  391.      
  392.     }
  393. };
  394.  
  395. void TImageFilter::Process() {
  396.     impl->Process();
  397. }
  398.  
  399. //----------------------------
  400.  
  401. // vector.h
  402. class Vector {
  403.     int* data;
  404. };
  405.  
  406. // matrix.h
  407. // #include "vector.h"
  408.  
  409. class Vector; // forward declaration
  410.  
  411. class Matrix {
  412.     Vector header; // < не соберётся
  413.     void f(Vector v);
  414.     Vector* ptr;
  415.     Vector& ref;
  416.     vector<Vector*> row;
  417. };
  418.  
  419. //----------------------------
  420.  
  421. // header.h
  422. static int answer;
  423.  
  424. // vector.cpp
  425. #include "header.h"
  426. void set(int v) {
  427.     answer = v;  // __namespace_vector_anon_answer
  428. }
  429.  
  430. // matrix.cpp
  431. #include "header.h"
  432. // int answer;  // redefinition (от компилятора)
  433. int get() {
  434.     return answer;
  435. }
  436.  
  437. // main.cpp
  438. #include "header.h"
  439. int main () {
  440.     set(7);
  441.     cout << get(); //< 0
  442. }
  443.  
  444. //----------------------------
  445.  
  446.  
  447. // header.h
  448. extern int answer;
  449.  
  450. // vector.cpp
  451. #include "header.h"
  452.  
  453. // matrix.cpp
  454. #include "header.h"
  455. int answer;  // иначе undefined reference
  456.  
  457. // main.cpp
  458. #include "header.h"
  459. int main () {
  460.     cout << answer; //<
  461. }
  462.  
  463.  
  464. //----------------------------
  465. // vector.cpp
  466. namespace {
  467. int answer;
  468. }  // namespace
  469.  
  470. // matrix.cpp
  471. static int answer;
  472.  
  473. // main.cpp
  474. int main () {
  475.    
  476. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement