]> git.lyx.org Git - features.git/blob - src/InsetList.cpp
hand-crafted LyXErr
[features.git] / src / InsetList.cpp
1 /**
2  * \file InsetList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Martin Vermeer
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetList.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BranchList.h"
19 #include "debug.h"
20
21 #include "insets/InsetBranch.h"
22
23 using std::lower_bound;
24
25
26 namespace lyx {
27
28
29 namespace {
30
31 typedef InsetList::InsetTable Table;
32
33 struct InsetTablePosLess
34 {
35         bool operator()(Table const & t1, Table const & t2) const
36         {
37                 return t1.pos < t2.pos;
38         }
39 };
40
41 } // namespace anon
42
43
44
45 InsetList::~InsetList()
46 {
47         // If we begin storing a shared_ptr in the List
48         // this code can be removed. (Lgb)
49         List::iterator it = list_.begin();
50         List::iterator end = list_.end();
51         for (; it != end; ++it) {
52                 delete it->inset;
53         }
54 }
55
56
57 InsetList::iterator InsetList::insetIterator(pos_type pos)
58 {
59         InsetTable search_elem(pos, 0);
60         return lower_bound(list_.begin(), list_.end(), search_elem,
61                            InsetTablePosLess());
62 }
63
64
65 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
66 {
67         InsetTable search_elem(pos, 0);
68         return lower_bound(list_.begin(), list_.end(), search_elem,
69                            InsetTablePosLess());
70 }
71
72
73 void InsetList::insert(Inset * inset, pos_type pos)
74 {
75         List::iterator end = list_.end();
76         List::iterator it = insetIterator(pos);
77         if (it != end && it->pos == pos) {
78                 LYXERR0("ERROR (InsetList::insert): "
79                        << "There is an inset in position: " << pos);
80         } else {
81                 list_.insert(it, InsetTable(pos, inset));
82         }
83 }
84
85
86 void InsetList::erase(pos_type pos)
87 {
88         List::iterator end = list_.end();
89         List::iterator it = insetIterator(pos);
90         if (it != end && it->pos == pos) {
91                 delete it->inset;
92                 list_.erase(it);
93         }
94 }
95
96
97 Inset * InsetList::release(pos_type pos)
98 {
99         List::iterator end = list_.end();
100         List::iterator it = insetIterator(pos);
101         if (it != end && it->pos == pos) {
102                 Inset * tmp = it->inset;
103                 it->inset = 0;
104                 return tmp;
105         }
106         return 0;
107 }
108
109
110 Inset * InsetList::get(pos_type pos) const
111 {
112         List::const_iterator end = list_.end();
113         List::const_iterator it = insetIterator(pos);
114         if (it != end && it->pos == pos)
115                 return it->inset;
116         return 0;
117 }
118
119
120 void InsetList::increasePosAfterPos(pos_type pos)
121 {
122         List::iterator end = list_.end();
123         List::iterator it = insetIterator(pos);
124         for (; it != end; ++it) {
125                 ++it->pos;
126         }
127 }
128
129
130 void InsetList::decreasePosAfterPos(pos_type pos)
131 {
132         List::iterator end = list_.end();
133         List::iterator it = insetIterator(pos);
134         for (; it != end; ++it) {
135                 --it->pos;
136         }
137 }
138
139
140 InsetList::InsetList(InsetList const & il)
141 {
142         list_ = il.list_;
143         List::iterator it = list_.begin();
144         List::iterator end = list_.end();
145         for (; it != end; ++it)
146                 it->inset = it->inset->clone();
147 }
148
149
150 pos_type InsetList::find(InsetCode code, pos_type startpos) const
151 {
152         List::const_iterator it = insetIterator(startpos);
153         List::const_iterator end = list_.end();
154         for (; it != end ; ++it) {
155                 if (it->inset->lyxCode() == code)
156                         return it->pos;
157         }
158         return -1;
159 }
160
161
162 int InsetList::count(InsetCode code, pos_type startpos) const
163 {
164         int num = 0;
165         List::const_iterator it = insetIterator(startpos);
166         List::const_iterator end = list_.end();
167         for (; it != end ; ++it) {
168                 if (it->inset->lyxCode() == code)
169                         ++num;
170         }
171         return num;
172 }
173
174 } // namespace lyx