]> git.lyx.org Git - lyx.git/blob - src/InsetList.cpp
* some more traces of the signals in CursorSlice
[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 "debug.h"
20
21 #include "insets/InsetBranch.h"
22
23
24 namespace lyx {
25
26 using std::endl;
27 using std::lower_bound;
28
29
30 namespace {
31
32 typedef InsetList::InsetTable Table;
33
34 class InsetTablePosLess : public std::binary_function<Table, Table, bool> {
35 public:
36         bool operator()(Table const & t1, Table const & t2) const
37         {
38                 return t1.pos < t2.pos;
39         }
40 };
41
42 } // namespace anon
43
44
45
46 InsetList::~InsetList()
47 {
48         // If we begin storing a shared_ptr in the List
49         // this code can be removed. (Lgb)
50         List::iterator it = list_.begin();
51         List::iterator end = list_.end();
52         for (; it != end; ++it) {
53                 delete it->inset;
54         }
55 }
56
57
58 InsetList::iterator InsetList::insetIterator(pos_type pos)
59 {
60         InsetTable search_elem(pos, 0);
61         return lower_bound(list_.begin(), list_.end(), search_elem,
62                            InsetTablePosLess());
63 }
64
65
66 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
67 {
68         InsetTable search_elem(pos, 0);
69         return lower_bound(list_.begin(), list_.end(), search_elem,
70                            InsetTablePosLess());
71 }
72
73
74 void InsetList::insert(Inset * inset, pos_type pos)
75 {
76         List::iterator end = list_.end();
77         List::iterator it = insetIterator(pos);
78         if (it != end && it->pos == pos) {
79                 lyxerr << "ERROR (InsetList::insert): "
80                        << "There is an inset in position: " << pos << endl;
81         } else {
82                 list_.insert(it, InsetTable(pos, inset));
83         }
84 }
85
86
87 void InsetList::erase(pos_type pos)
88 {
89         List::iterator end = list_.end();
90         List::iterator it = insetIterator(pos);
91         if (it != end && it->pos == pos) {
92                 delete it->inset;
93                 list_.erase(it);
94         }
95 }
96
97
98 Inset * InsetList::release(pos_type pos)
99 {
100         List::iterator end = list_.end();
101         List::iterator it = insetIterator(pos);
102         if (it != end && it->pos == pos) {
103                 Inset * tmp = it->inset;
104                 it->inset = 0;
105                 return tmp;
106         }
107         return 0;
108 }
109
110
111 Inset * InsetList::get(pos_type pos) const
112 {
113         List::const_iterator end = list_.end();
114         List::const_iterator it = insetIterator(pos);
115         if (it != end && it->pos == pos)
116                 return it->inset;
117         return 0;
118 }
119
120
121 void InsetList::increasePosAfterPos(pos_type pos)
122 {
123         List::iterator end = list_.end();
124         List::iterator it = insetIterator(pos);
125         for (; it != end; ++it) {
126                 ++it->pos;
127         }
128 }
129
130
131 void InsetList::decreasePosAfterPos(pos_type pos)
132 {
133         List::iterator end = list_.end();
134         List::iterator it = insetIterator(pos);
135         for (; it != end; ++it) {
136                 --it->pos;
137         }
138 }
139
140
141 } // namespace lyx