BPicture: Fix archive constructor.
[haiku.git] / src / kits / package / solver / SolverPackageSpecifierList.cpp
blobfccf3d84d4bf4c272073eee16c8d549affe6795d
1 /*
2 * Copyright 2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
10 #include <package/solver/SolverPackageSpecifierList.h>
12 #include <new>
13 #include <vector>
15 #include <package/solver/SolverPackageSpecifier.h>
18 namespace BPackageKit {
20 class BSolverPackageSpecifierList::Vector
21 : public std::vector<BSolverPackageSpecifier> {
22 public:
23 Vector()
25 std::vector<BSolverPackageSpecifier>()
29 Vector(const std::vector<BSolverPackageSpecifier>& other)
31 std::vector<BSolverPackageSpecifier>(other)
37 BSolverPackageSpecifierList::BSolverPackageSpecifierList()
39 fSpecifiers(NULL)
44 BSolverPackageSpecifierList::BSolverPackageSpecifierList(
45 const BSolverPackageSpecifierList& other)
47 fSpecifiers(NULL)
49 *this = other;
53 BSolverPackageSpecifierList::~BSolverPackageSpecifierList()
55 delete fSpecifiers;
59 bool
60 BSolverPackageSpecifierList::IsEmpty() const
62 return fSpecifiers == NULL || fSpecifiers->empty();
66 int32
67 BSolverPackageSpecifierList::CountSpecifiers() const
69 return fSpecifiers != NULL ? fSpecifiers->size() : 0;
73 const BSolverPackageSpecifier*
74 BSolverPackageSpecifierList::SpecifierAt(int32 index) const
76 if (fSpecifiers == NULL || index < 0
77 || (size_t)index >= fSpecifiers->size()) {
78 return NULL;
81 return &(*fSpecifiers)[index];
85 bool
86 BSolverPackageSpecifierList::AppendSpecifier(
87 const BSolverPackageSpecifier& specifier)
89 try {
90 if (fSpecifiers == NULL) {
91 fSpecifiers = new(std::nothrow) Vector;
92 if (fSpecifiers == NULL)
93 return false;
96 fSpecifiers->push_back(specifier);
97 return true;
98 } catch (std::bad_alloc&) {
99 return false;
104 bool
105 BSolverPackageSpecifierList::AppendSpecifier(BSolverPackage* package)
107 return AppendSpecifier(BSolverPackageSpecifier(package));
111 bool
112 BSolverPackageSpecifierList::AppendSpecifier(const BString& selectString)
114 return AppendSpecifier(BSolverPackageSpecifier(selectString));
118 bool
119 BSolverPackageSpecifierList::AppendSpecifiers(const char* const* selectStrings,
120 int32 count)
122 for (int32 i = 0; i < count; i++) {
123 if (!AppendSpecifier(selectStrings[i])) {
124 for (int32 k = i - 1; k >= 0; k--)
125 fSpecifiers->pop_back();
126 return false;
130 return true;
134 void
135 BSolverPackageSpecifierList::MakeEmpty()
137 fSpecifiers->clear();
141 BSolverPackageSpecifierList&
142 BSolverPackageSpecifierList::operator=(const BSolverPackageSpecifierList& other)
144 if (this == &other)
145 return *this;
147 delete fSpecifiers;
148 fSpecifiers = NULL;
150 if (other.fSpecifiers == NULL)
151 return *this;
153 try {
154 fSpecifiers = new(std::nothrow) Vector(*other.fSpecifiers);
155 } catch (std::bad_alloc&) {
158 return *this;
162 } // namespace BPackageKit