]> git.lyx.org Git - features.git/blob - src/BranchList.C
Overhaul the branches code.
[features.git] / src / BranchList.C
1 /**
2  * \file BranchList.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BranchList.h"
14
15 #include <boost/assert.hpp>
16
17 #include <functional>
18
19
20 using std::string;
21 using std::bind2nd;
22 using std::binary_function;
23
24
25 string const & Branch::getBranch() const
26 {
27         return branch_;
28 }
29
30
31 void Branch::setBranch(string const & s)
32 {
33         branch_ = s;
34 }
35
36
37 bool Branch::getSelected() const
38 {
39         return selected_;
40 }
41
42
43 bool Branch::setSelected(bool b)
44 {
45         if (b == selected_)
46                 return false;
47         selected_ = b;
48         return true;
49 }
50
51
52 string const & Branch::getColor() const
53 {
54         return color_;
55 }
56
57
58 void Branch::setColor(string const & c)
59 {
60         color_ = c;
61 }
62
63
64 namespace {
65
66 struct SameName {
67         SameName(string const & name) : name_(name) {}
68         bool operator()(Branch const & branch) const
69                 { return branch.getBranch() == name_; }
70 private:
71         string name_;
72 };
73
74 }// namespace anon
75
76
77 Branch * BranchList::find(std::string const & name)
78 {
79         List::iterator it =
80                 std::find_if(list.begin(), list.end(), SameName(name));
81         return it == list.end() ? 0 : &*it;
82 }
83
84         
85 Branch const * BranchList::find(std::string const & name) const
86 {
87         List::const_iterator it =
88                 std::find_if(list.begin(), list.end(), SameName(name));
89         return it == list.end() ? 0 : &*it;
90 }
91
92         
93 bool BranchList::add(string const & s)
94 {
95         bool added = false;
96         string::size_type i = 0;
97         while (true) {
98                 string::size_type const j = s.find_first_of(separator_, i);
99                 string name;
100                 if (j == string::npos)
101                         name = s.substr(i);
102                 else
103                         name = s.substr(i, j - i);
104                 // Is this name already in the list?
105                 List::const_iterator it = list.begin();
106                 List::const_iterator end = list.end();
107                 bool already = false;
108                 for (; it != end; ++it) {
109                         if (it->getBranch() == name) {
110                                 already = true;
111                                 break;
112                         }
113                 }
114                 if (!already) {
115                         added = true;
116                         Branch br;
117                         br.setBranch(name);
118                         br.setSelected(false);
119                         br.setColor("none");
120                         list.push_back(br);
121                 }
122                 if (j == string::npos)
123                         break;
124                 i = j + 1;
125         }
126         return added;
127 }
128
129
130 namespace {
131
132 struct match : public binary_function<Branch, string, bool> {
133         bool operator()(Branch const & br, string const & s) const {
134         return (br.getBranch() == s);
135         }
136 };
137
138 } // namespace anon.
139
140
141 bool BranchList::remove(string const & s)
142 {
143         List::size_type const size = list.size();
144         list.remove_if(bind2nd(match(), s));
145         return size != list.size();
146 }