fork(1) download
  1. #include <vector>
  2. #include <string>
  3. #include <initializer_list>
  4. #include <memory>
  5.  
  6. class StrBlobPtr;
  7.  
  8. class StrBlob
  9. {
  10. public:
  11. friend class StrBlobPtr;
  12. typedef std::vector<std::string>::size_type size_type;
  13.  
  14. StrBlob();
  15. StrBlob(std::initializer_list<std::string> il);
  16. size_type size() const { return data->size(); }
  17. bool empty() const { return data->empty(); }
  18. void push_back(const std::string &t) { data->push_back(t); }
  19. void pop_back();
  20. std::string& front();
  21. std::string& back();
  22. std::string& front() const;
  23. std::string& back() const;
  24.  
  25. StrBlobPtr begin() { return StrBlobPtr(*this); }
  26. StrBlobPtr end()
  27. {
  28. auto ret = StrBlobPtr(*this, data->size());
  29. return ret;
  30. }
  31.  
  32. private:
  33. std::shared_ptr<std::vector<std::string>> data;
  34. void check(size_type i, const std::string &msg) const;
  35. };
  36.  
  37. class StrBlobPtr
  38. {
  39. public:
  40. typedef std::shared_ptr<std::vector<std::string>> sh_ptr;
  41.  
  42. StrBlobPtr() : curr(0) {}
  43. StrBlobPtr(StrBlob &a, std::size_t sz = 0)
  44. : wptr(a.data), curr(sz) {}
  45. std::string& deref() const;
  46. StrBlobPtr& incr();
  47. private:
  48. sh_ptr check(std::size_t, const std::string&) const;
  49. std::weak_ptr<std::vector<std::string>> wptr;
  50. std::size_t curr;
  51. };
  52.  
  53. StrBlob::StrBlob()
  54. : data(std::make_shared<std::vector<std::string>>()) {}
  55.  
  56. StrBlob::StrBlob(std::initializer_list<std::string> il)
  57. : data(std::make_shared<std::vector<std::string>>(il)) {}
  58.  
  59. void StrBlob::check(size_type i, const std::string &msg) const
  60. {
  61. if (i >= data->size())
  62. throw std::out_of_range(msg);
  63. }
  64.  
  65. std::string& StrBlob::front()
  66. {
  67. check(0, "front on empty StrBlob");
  68. return data->front();
  69. }
  70.  
  71. std::string& StrBlob::back()
  72. {
  73. check(0, "back on empty StrBlob");
  74. return data->back();
  75. }
  76.  
  77. std::string& StrBlob::front() const
  78. {
  79. check(0, "const back on empty StrBlob");
  80. return data->front();
  81. }
  82.  
  83. std::string& StrBlob::back() const
  84. {
  85. check(0, "const back on empty StrBlob");
  86. return data->back();
  87. }
  88.  
  89. void StrBlob::pop_back()
  90. {
  91. check(0, "pop_back on empty StrBlob");
  92. return data->pop_back();
  93. }
  94.  
  95.  
  96.  
  97. StrBlobPtr::sh_ptr StrBlobPtr::check(std::size_t i, const std::string &msg) const
  98. {
  99. auto ret = wptr.lock();
  100. if (!ret)
  101. throw std::runtime_error("Unbound StrBlobPtr");
  102. if (i >= ret->size())
  103. throw std::out_of_range(msg);
  104. return ret;
  105. }
  106.  
  107. std::string& StrBlobPtr::deref() const
  108. {
  109. auto p = check(curr, "dereference past end");
  110. return (*p)[curr];
  111. }
  112.  
  113. StrBlobPtr& StrBlobPtr::incr()
  114. {
  115. auto p = check(curr, "increment past end");
  116. ++curr;
  117. return *this;
  118. }
  119.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function 'StrBlobPtr StrBlob::begin()':
prog.cpp:25:21: error: return type 'class StrBlobPtr' is incomplete
  StrBlobPtr begin() { return StrBlobPtr(*this); }
                     ^
prog.cpp:25:46: error: invalid use of incomplete type 'class StrBlobPtr'
  StrBlobPtr begin() { return StrBlobPtr(*this); }
                                              ^
prog.cpp:6:7: note: forward declaration of 'class StrBlobPtr'
 class StrBlobPtr;
       ^
prog.cpp: In member function 'StrBlobPtr StrBlob::end()':
prog.cpp:27:2: error: return type 'class StrBlobPtr' is incomplete
  {
  ^
prog.cpp:28:44: error: invalid use of incomplete type 'class StrBlobPtr'
   auto ret = StrBlobPtr(*this, data->size());
                                            ^
prog.cpp:6:7: note: forward declaration of 'class StrBlobPtr'
 class StrBlobPtr;
       ^
stdout
Standard output is empty