]> git.lyx.org Git - lyx.git/blob - src/InsetList.C
changelogs
[lyx.git] / src / InsetList.C
1 /**
2  * \file InsetList.C
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 "BufferView.h"
20 #include "debug.h"
21
22 #include "insets/insetbranch.h"
23
24 using lyx::pos_type;
25
26 using std::endl;
27 using std::lower_bound;
28
29
30 namespace {
31
32 typedef InsetList::InsetTable Table;
33
34 struct InsetTablePosLess : public std::binary_function<Table, Table, bool> {
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(InsetBase * inset, lyx::pos_type pos)
74 {
75         List::iterator end = list_.end();
76         List::iterator it = insetIterator(pos);
77         if (it != end && it->pos == pos) {
78                 lyxerr << "ERROR (InsetList::insert): "
79                        << "There is an inset in position: " << pos << endl;
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 InsetBase * 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                 InsetBase * tmp = it->inset;
103                 it->inset = 0;
104                 return tmp;
105         }
106         return 0;
107 }
108
109
110 InsetBase * 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 }