cwidget  0.5.17
fragment_contents.h
1 // fragment_contents.h -*-c++-*-
2 //
3 // Copyright 2004 Daniel Burrows
4 //
5 // A nice way of storing the contents of a fragment.
6 
7 #ifndef FRAGMENT_CONTENTS_H
8 #define FRAGMENT_CONTENTS_H
9 
10 #include "curses++.h"
11 
12 #include <vector>
13 
14 namespace cwidget
15 {
21 
31  {
32  class fragment_lines
33  {
34  std::vector<fragment_line> v;
35  mutable int refs;
36 
37  public:
38  class const_iterator;
39 
40  class iterator
41  {
42  typedef std::vector<fragment_line>::iterator ittype;
43 
44  fragment_lines *lines;
45  ittype i;
46 
47  public:
48  iterator(fragment_lines *_lines, const ittype &_i)
49  :lines(_lines), i(_i)
50  { lines->incref(); }
51 
52  iterator(const iterator &other)
53  :lines(other.lines), i(other.i)
54  { lines->incref(); }
55 
56  ~iterator() { lines->decref(); }
57 
58  iterator &operator++() { ++i; return *this; }
59  iterator &operator--() { --i; return *this; }
60 
61  fragment_line &operator*() { return *i; }
62  fragment_line *operator->() { return &*i; }
63 
64  iterator &operator=(const iterator &other) {
65  other.lines->incref();
66  lines->decref();
67  lines=other.lines;
68  i=other.i;
69 
70  return *this;
71  }
72 
73  bool operator==(const iterator &other) const {return i==other.i;}
74  bool operator!=(const iterator &other) const {return i!=other.i;}
75 
76  friend class const_iterator;
77  };
78 
80  {
81  typedef std::vector<fragment_line>::const_iterator ittype;
82 
83  const fragment_lines *lines;
84  ittype i;
85 
86  public:
87  const_iterator(const fragment_lines *_lines, const ittype &_i)
88  :lines(_lines), i(_i)
89  { lines->incref(); }
90 
91  const_iterator(const iterator &other)
92  :lines(other.lines), i(other.i)
93  { lines->incref(); }
94 
95  const_iterator(const const_iterator &other)
96  :lines(other.lines), i(other.i)
97  { lines->incref(); }
98 
99  ~const_iterator() { lines->decref(); }
100 
101  const_iterator &operator++() { ++i; return *this; }
102  const_iterator &operator--() { --i; return *this; }
103 
104  const fragment_line &operator*() { return *i; }
105  const fragment_line *operator->() { return &*i; }
106 
107  const_iterator &operator=(const const_iterator &other) {
108  other.lines->incref();
109  lines->decref();
110  lines=other.lines;
111  i=other.i;
112 
113  return *this;
114  }
115 
116  const_iterator &operator=(const iterator &other) {
117  other.lines->incref();
118  lines->decref();
119  lines=other.lines;
120  i=other.i;
121 
122  return *this;
123  }
124 
125  bool operator==(const iterator &other) const {return i==other.i;}
126  bool operator==(const const_iterator &other) const {return i==other.i;}
127 
128  bool operator!=(const iterator &other) const {return i!=other.i;}
129  bool operator!=(const const_iterator &other) const {return i!=other.i;}
130  };
131 
132  fragment_lines():refs(0) {}
133 
134  void incref() const {++refs;}
135  void decref() const {--refs; if(refs==0) delete this;}
136 
137  void push_back(const fragment_line &l) {v.push_back(l);}
138 
139  iterator begin() {return iterator(this, v.begin());}
140  const_iterator begin() const {return const_iterator(this, v.begin());}
141 
142  iterator end() {return iterator(this, v.end());}
143  const_iterator end() const {return const_iterator(this, v.end());}
144 
145  fragment_line &front() {return v.front();}
146  const fragment_line &front() const {return v.front();}
147 
148  fragment_line &back() {return v.back();}
149  const fragment_line &back() const {return v.back();}
150 
151  fragment_line &operator[](int i) { return v[i]; }
152  const fragment_line &operator[](int i) const { return v[i]; }
153 
154  size_t size() const {return v.size();}
155  };
156  public:
159 
161  fragment_contents():lines(new fragment_lines), final_nl(false)
162  {lines->incref();}
163 
166  :lines(other.lines), final_nl(other.final_nl)
167  {
168  lines->incref();
169  }
170 
172  ~fragment_contents() {lines->decref();}
173 
174  void push_back(const fragment_line &l) {lines->push_back(l);}
175 
176  iterator begin() {return lines->begin();}
177  const_iterator begin() const {return lines->begin();}
178 
179  iterator end() {return lines->end();}
180  iterator end() const {return lines->end();}
181 
182  fragment_line &front() {return lines->front();}
183  const fragment_line &front() const {return lines->front();}
184 
185  fragment_line &back() {return lines->back();}
186  const fragment_line &back() const {return lines->back();}
187 
188  size_t size() const {return lines->size();}
189 
190  void set_final_nl(bool final_nl_new) {final_nl=final_nl_new;}
191 
192  bool get_final_nl() {return final_nl;}
193 
194  fragment_line &operator[](int i) { return (*lines)[i]; }
195  const fragment_line &operator[](int i) const { return (*lines)[i]; }
196 
197  fragment_contents &operator=(const fragment_contents &other)
198  {
199  other.lines->incref();
200  lines->decref();
201 
202  lines=other.lines;
203  final_nl=other.final_nl;
204 
205  return *this;
206  }
207 
208  private:
210  fragment_lines *lines;
211 
215  bool final_nl;
216  };
217 }
218 
219 #endif
This class represents the formatted contents of a fragment.
Definition: fragment_contents.h:30
wchstring fragment_line
The type used to represent a line of a fragment; it might be worthwhile to change this to a rope<chty...
Definition: fragment_contents.h:20
Definition: curses++.h:199
The namespace containing everything defined by cwidget.
Definition: columnify.cc:26
fragment_contents()
Generate empty contents for a fragment.
Definition: fragment_contents.h:161
Definition: widget.h:83
~fragment_contents()
When this is destroyed, decrement the lines&#39; reference count.
Definition: fragment_contents.h:172
fragment_contents(const fragment_contents &other)
Copy constructor.
Definition: fragment_contents.h:165