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