Consider the case where there is not any layout name.
[lyx.git] / src / InsetList.C
blob5cd85c14daf946221c05f22b552d68045b7ec193
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  */
12 #include <config.h>
14 #include "InsetList.h"
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BranchList.h"
19 #include "BufferView.h"
20 #include "debug.h"
22 #include "insets/insetbranch.h"
24 using lyx::pos_type;
26 using std::endl;
27 using std::lower_bound;
30 namespace {
32 typedef InsetList::InsetTable Table;
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         }
42 } // namespace anon
46 InsetList::~InsetList()
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         }
58 InsetList::iterator InsetList::insetIterator(pos_type pos)
60         InsetTable search_elem(pos, 0);
61         return lower_bound(list_.begin(), list_.end(), search_elem,
62                            InsetTablePosLess());
66 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
68         InsetTable search_elem(pos, 0);
69         return lower_bound(list_.begin(), list_.end(), search_elem,
70                            InsetTablePosLess());
74 void InsetList::insert(InsetBase * inset, lyx::pos_type pos)
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         }
87 void InsetList::erase(pos_type pos)
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         }
98 InsetBase * InsetList::release(pos_type pos)
100         List::iterator end = list_.end();
101         List::iterator it = insetIterator(pos);
102         if (it != end && it->pos == pos) {
103                 InsetBase * tmp = it->inset;
104                 it->inset = 0;
105                 return tmp;
106         }
107         return 0;
111 InsetBase * InsetList::get(pos_type pos) const
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;
121 void InsetList::increasePosAfterPos(pos_type pos)
123         List::iterator end = list_.end();
124         List::iterator it = insetIterator(pos);
125         for (; it != end; ++it) {
126                 ++it->pos;
127         }
131 void InsetList::decreasePosAfterPos(pos_type pos)
133         List::iterator end = list_.end();
134         List::iterator it = insetIterator(pos);
135         for (; it != end; ++it) {
136                 --it->pos;
137         }