fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <memory>
  5.  
  6. class Visitor;
  7.  
  8. class Entry {
  9. public:
  10. virtual ~Entry() {};
  11. virtual std::string getName() const = 0;
  12. virtual size_t getSize() = 0;
  13. virtual size_t accept(std::shared_ptr<Visitor>) = 0;
  14. };
  15.  
  16. template <class T>
  17. class Iterator {
  18. public:
  19. virtual ~Iterator() {}
  20. virtual bool hasNext() = 0;
  21. virtual T next() = 0;
  22. };
  23.  
  24. template <class T>
  25. class VectorIterator : public Iterator<T> {
  26. std::vector<T> &v;
  27. size_t index = 0;
  28. public:
  29. VectorIterator(std::vector<T> &v) : v(v) {}
  30. bool hasNext() override {
  31. if (index < v.size())
  32. return true;
  33. else
  34. return false;
  35. }
  36. T next() {
  37. T retvalue = v[index];
  38. index++;
  39. return retvalue;
  40. }
  41. };
  42.  
  43. class File : public Entry {
  44. std::string name;
  45. size_t size;
  46. public:
  47. File(std::string name, size_t size) : name(name), size(size) {}
  48. File(const File &ob) { this->name = ob.name; this->size = ob.size; }
  49. File(const File *p) { this->name = p->name; this->size = p->size; }
  50. std::string getName() const override { return name; }
  51. size_t getSize() override { return size; }
  52. size_t accept(std::shared_ptr<Visitor>) override;
  53. };
  54.  
  55. class Directory : public Entry {
  56. std::string name;
  57. std::vector<std::shared_ptr<Entry>> v;
  58. public:
  59. Directory(std::string name) : name(name) {}
  60. Directory(const Directory &ob) { this->name = ob.name; this->v = ob.v; }
  61. Directory(const Directory *p) { this->name = p->name; this->v = p->v; }
  62.  
  63. // ~Directory() {
  64. // for (auto p : v) delete p;
  65. // }
  66. std::string getName() const override { return name; }
  67. size_t getSize() override;
  68. size_t accept(std::shared_ptr<Visitor> v) override;
  69. void treePrint();
  70. void add(std::shared_ptr<Entry> entry) { v.push_back(entry); }
  71. std::shared_ptr<Iterator<std::shared_ptr<Entry>>> iterator() { return std::make_shared<VectorIterator<std::shared_ptr<Entry>>>(v); }
  72. };
  73.  
  74. class Visitor {
  75. public:
  76. virtual ~Visitor() {}
  77. virtual size_t visit(std::shared_ptr<File>) = 0;
  78. virtual size_t visit(std::shared_ptr<Directory>) = 0;
  79. };
  80.  
  81. size_t File::accept(std::shared_ptr<Visitor> v) { return v->visit(std::make_shared<File>(this)); }
  82. size_t Directory::accept(std::shared_ptr<Visitor> v) { return v->visit(std::make_shared<Directory>(this)); }
  83.  
  84. class SizeVisitor : public Visitor {
  85.  
  86. public:
  87. SizeVisitor() {};
  88. SizeVisitor(const SizeVisitor &) {}
  89. SizeVisitor(const SizeVisitor *) {}
  90. size_t visit(std::shared_ptr<File> file) { return file->getSize(); }
  91. size_t visit(std::shared_ptr<Directory> directory) {
  92. size_t size = 0;
  93. std::shared_ptr<Iterator<std::shared_ptr<Entry>>> it = directory->iterator();
  94. while (it->hasNext()) {
  95. std::shared_ptr<Entry> entry = it->next();
  96. size += entry->accept(std::make_shared<SizeVisitor>(this));
  97. }
  98. // delete it;
  99. return size;
  100. }
  101. };
  102.  
  103. class ListVisitor : public Visitor {
  104. std::string currentDirectoryName;
  105. std::string saveDirectoryName;
  106.  
  107. public:
  108. ListVisitor() {}
  109. ListVisitor(const ListVisitor &ob) {
  110. this->currentDirectoryName = ob.currentDirectoryName;
  111. this->saveDirectoryName = ob.saveDirectoryName;
  112. }
  113. ListVisitor(const ListVisitor *p) {
  114. this->currentDirectoryName = p->currentDirectoryName;
  115. this->saveDirectoryName = p->saveDirectoryName;
  116. }
  117. size_t visit(std::shared_ptr<File> file) {
  118. std::cout << currentDirectoryName << "/" << file->getName() << "(" << file->getSize() << ")" << std::endl;
  119. return 0;
  120. }
  121. size_t visit(std::shared_ptr<Directory> dir) {
  122. std::cout << currentDirectoryName << "/" << dir->getName() << "(" << dir->getSize() << ")" << std::endl;
  123. saveDirectoryName = currentDirectoryName;
  124. currentDirectoryName += "/" + dir->getName();
  125. std::shared_ptr<Iterator<std::shared_ptr<Entry>>> it = dir->iterator();
  126. while (it->hasNext()) {
  127. std::shared_ptr<Entry> entry = it->next();
  128. entry->accept(std::make_shared<ListVisitor>(this));
  129. }
  130. // delete it;
  131. currentDirectoryName = saveDirectoryName;
  132. return 0;
  133. }
  134. };
  135.  
  136. size_t Directory::getSize() {
  137. auto visitor = std::make_shared<SizeVisitor>();
  138. size_t size = this->accept(visitor);
  139. // delete visitor;
  140. return size;
  141. }
  142. void Directory::treePrint() {
  143. auto visitor = std::make_shared<ListVisitor>();
  144. this->accept(visitor);
  145. // delete visitor;
  146. }
  147.  
  148. int main() {
  149. {
  150. auto rootdir = std::make_shared<Directory>("root");
  151. auto file1 = std::make_shared<File>("file1.txt", 100);
  152. auto file2 = std::make_shared<File>("file2.txt", 200);
  153. rootdir->add(file1);
  154. rootdir->add(file2);
  155. rootdir->treePrint();
  156.  
  157. // delete rootdir;
  158. }
  159. xmallocdump();
  160. return 0;
  161. }
  162.  
  163.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:36:5: warning: 'next' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
  T next() {
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/type_traits:1354:46: note: in instantiation of template class 'VectorIterator<std::shared_ptr<Entry> >' requested here
               typename = decltype(__test_aux<_To1>(std::declval<_From1>()))>
                                                    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/type_traits:1354:29: note: while substituting deduced template arguments into function template '__test_aux' [with _To1 = Iterator<std::shared_ptr<Entry> > *]
               typename = decltype(__test_aux<_To1>(std::declval<_From1>()))>
                                   ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/type_traits:1356:2: note: in instantiation of default argument for '__test<VectorIterator<std::shared_ptr<Entry> > *, Iterator<std::shared_ptr<Entry> > *>' required here
        __test(int);
        ^~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/type_traits:1363:24: note: while substituting deduced template arguments into function template '__test' [with _From1 = VectorIterator<std::shared_ptr<Entry> > *, _To1 = Iterator<std::shared_ptr<Entry> > *, $2 = (no value)]
      typedef decltype(__test<_From, _To>(0)) type;
                       ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/type_traits:1370:14: note: in instantiation of template class 'std::__is_convertible_helper<VectorIterator<std::shared_ptr<Entry> > *, Iterator<std::shared_ptr<Entry> > *, false>' requested here
    : public __is_convertible_helper<_From, _To>::type
             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/shared_ptr_base.h:926:7: note: (skipping 4 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
    : is_convertible<_Yp*, _Tp*>::type
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/type_traits:921:56: note: while substituting deduced template arguments into function template '__shared_ptr' [with _Yp = VectorIterator<std::shared_ptr<Entry> >, $1 = (no value)]
      : public __bool_constant<__is_constructible(_Tp, _Args...)>
                                                       ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/shared_ptr.h:107:4: note: in instantiation of template class 'std::is_constructible<std::__shared_ptr<Iterator<std::shared_ptr<Entry> >, __gnu_cxx::_S_atomic>, const std::shared_ptr<VectorIterator<std::shared_ptr<Entry> > > &>' requested here
          is_constructible<__shared_ptr<_Tp>, _Args...>::value
          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/shared_ptr.h:245:20: note: in instantiation of template type alias '_Constructible' requested here
               typename = _Constructible<const shared_ptr<_Yp>&>>
                          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/shared_ptr.h:246:2: note: in instantiation of default argument for 'shared_ptr<VectorIterator<std::shared_ptr<Entry> > >' required here
        shared_ptr(const shared_ptr<_Yp>& __r) noexcept
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:71:73: note: while substituting deduced template arguments into function template 'shared_ptr' [with _Yp = VectorIterator<std::shared_ptr<Entry> >, $1 = (no value)]
  std::shared_ptr<Iterator<std::shared_ptr<Entry>>> iterator() { return std::make_shared<VectorIterator<std::shared_ptr<Entry>>>(v); }
                                                                        ^
prog.cpp:21:13: note: overridden virtual function is here
  virtual T next() = 0;
            ^
prog.cpp:159:3: error: use of undeclared identifier 'xmallocdump'
  xmallocdump();
  ^
1 warning and 1 error generated.
stdout
Standard output is empty