1479ca816SIngo Weinhold/*
2479ca816SIngo Weinhold * Copyright 2013, Haiku, Inc. All Rights Reserved.
3479ca816SIngo Weinhold * Distributed under the terms of the MIT License.
4479ca816SIngo Weinhold *
5479ca816SIngo Weinhold * Authors:
6479ca816SIngo Weinhold *		Ingo Weinhold <ingo_weinhold@gmx.de>
7479ca816SIngo Weinhold */
8479ca816SIngo Weinhold
9479ca816SIngo Weinhold
10479ca816SIngo Weinhold#include <package/solver/SolverPackageSpecifierList.h>
11479ca816SIngo Weinhold
12479ca816SIngo Weinhold#include <new>
13479ca816SIngo Weinhold#include <vector>
14479ca816SIngo Weinhold
15479ca816SIngo Weinhold#include <package/solver/SolverPackageSpecifier.h>
16479ca816SIngo Weinhold
17479ca816SIngo Weinhold
18479ca816SIngo Weinholdnamespace BPackageKit {
19479ca816SIngo Weinhold
20479ca816SIngo Weinholdclass BSolverPackageSpecifierList::Vector
21479ca816SIngo Weinhold	: public std::vector<BSolverPackageSpecifier> {
22479ca816SIngo Weinholdpublic:
23479ca816SIngo Weinhold	Vector()
24479ca816SIngo Weinhold		:
25479ca816SIngo Weinhold		std::vector<BSolverPackageSpecifier>()
26479ca816SIngo Weinhold	{
27479ca816SIngo Weinhold	}
28479ca816SIngo Weinhold
29479ca816SIngo Weinhold	Vector(const std::vector<BSolverPackageSpecifier>& other)
30479ca816SIngo Weinhold		:
31479ca816SIngo Weinhold		std::vector<BSolverPackageSpecifier>(other)
32479ca816SIngo Weinhold	{
33479ca816SIngo Weinhold	}
34479ca816SIngo Weinhold};
35479ca816SIngo Weinhold
36479ca816SIngo Weinhold
37479ca816SIngo WeinholdBSolverPackageSpecifierList::BSolverPackageSpecifierList()
38479ca816SIngo Weinhold	:
39479ca816SIngo Weinhold	fSpecifiers(NULL)
40479ca816SIngo Weinhold{
41479ca816SIngo Weinhold}
42479ca816SIngo Weinhold
439b2fd341SIngo Weinhold
44479ca816SIngo WeinholdBSolverPackageSpecifierList::BSolverPackageSpecifierList(
45479ca816SIngo Weinhold	const BSolverPackageSpecifierList& other)
46479ca816SIngo Weinhold	:
47479ca816SIngo Weinhold	fSpecifiers(NULL)
48479ca816SIngo Weinhold{
49479ca816SIngo Weinhold	*this = other;
50479ca816SIngo Weinhold}
51479ca816SIngo Weinhold
52479ca816SIngo Weinhold
53479ca816SIngo WeinholdBSolverPackageSpecifierList::~BSolverPackageSpecifierList()
54479ca816SIngo Weinhold{
55479ca816SIngo Weinhold	delete fSpecifiers;
56479ca816SIngo Weinhold}
57479ca816SIngo Weinhold
58479ca816SIngo Weinhold
59479ca816SIngo Weinholdbool
60479ca816SIngo WeinholdBSolverPackageSpecifierList::IsEmpty() const
61479ca816SIngo Weinhold{
62479ca816SIngo Weinhold	return fSpecifiers == NULL || fSpecifiers->empty();
63479ca816SIngo Weinhold}
64479ca816SIngo Weinhold
65479ca816SIngo Weinhold
66479ca816SIngo Weinholdint32
67479ca816SIngo WeinholdBSolverPackageSpecifierList::CountSpecifiers() const
68479ca816SIngo Weinhold{
69479ca816SIngo Weinhold	return fSpecifiers != NULL ? fSpecifiers->size() : 0;
70479ca816SIngo Weinhold}
71479ca816SIngo Weinhold
72479ca816SIngo Weinhold
73479ca816SIngo Weinholdconst BSolverPackageSpecifier*
74479ca816SIngo WeinholdBSolverPackageSpecifierList::SpecifierAt(int32 index) const
75479ca816SIngo Weinhold{
76479ca816SIngo Weinhold	if (fSpecifiers == NULL || index < 0
77479ca816SIngo Weinhold		|| (size_t)index >= fSpecifiers->size()) {
78479ca816SIngo Weinhold		return NULL;
79479ca816SIngo Weinhold	}
80479ca816SIngo Weinhold
81479ca816SIngo Weinhold	return &(*fSpecifiers)[index];
82479ca816SIngo Weinhold}
83479ca816SIngo Weinhold
84479ca816SIngo Weinhold
85479ca816SIngo Weinholdbool
86479ca816SIngo WeinholdBSolverPackageSpecifierList::AppendSpecifier(
87479ca816SIngo Weinhold	const BSolverPackageSpecifier& specifier)
88479ca816SIngo Weinhold{
89479ca816SIngo Weinhold	try {
90479ca816SIngo Weinhold		if (fSpecifiers == NULL) {
91479ca816SIngo Weinhold			fSpecifiers = new(std::nothrow) Vector;
92479ca816SIngo Weinhold			if (fSpecifiers == NULL)
93479ca816SIngo Weinhold				return false;
94479ca816SIngo Weinhold		}
95479ca816SIngo Weinhold
96479ca816SIngo Weinhold		fSpecifiers->push_back(specifier);
97479ca816SIngo Weinhold		return true;
98479ca816SIngo Weinhold	} catch (std::bad_alloc&) {
99479ca816SIngo Weinhold		return false;
100479ca816SIngo Weinhold	}
101479ca816SIngo Weinhold}
102479ca816SIngo Weinhold
103479ca816SIngo Weinhold
10401758ed3SIngo Weinholdbool
10501758ed3SIngo WeinholdBSolverPackageSpecifierList::AppendSpecifier(BSolverPackage* package)
10601758ed3SIngo Weinhold{
10701758ed3SIngo Weinhold	return AppendSpecifier(BSolverPackageSpecifier(package));
10801758ed3SIngo Weinhold}
10901758ed3SIngo Weinhold
11001758ed3SIngo Weinhold
11101758ed3SIngo Weinholdbool
11201758ed3SIngo WeinholdBSolverPackageSpecifierList::AppendSpecifier(const BString& selectString)
11301758ed3SIngo Weinhold{
11401758ed3SIngo Weinhold	return AppendSpecifier(BSolverPackageSpecifier(selectString));
11501758ed3SIngo Weinhold}
11601758ed3SIngo Weinhold
11701758ed3SIngo Weinhold
1189b2fd341SIngo Weinholdbool
1199b2fd341SIngo WeinholdBSolverPackageSpecifierList::AppendSpecifiers(const char* const* selectStrings,
1209b2fd341SIngo Weinhold	int32 count)
1219b2fd341SIngo Weinhold{
1229b2fd341SIngo Weinhold	for (int32 i = 0; i < count; i++) {
1239b2fd341SIngo Weinhold		if (!AppendSpecifier(selectStrings[i])) {
1249b2fd341SIngo Weinhold			for (int32 k = i - 1; k >= 0; k--)
1259b2fd341SIngo Weinhold				fSpecifiers->pop_back();
1269b2fd341SIngo Weinhold			return false;
1279b2fd341SIngo Weinhold		}
1289b2fd341SIngo Weinhold	}
1299b2fd341SIngo Weinhold
1309b2fd341SIngo Weinhold	return true;
1319b2fd341SIngo Weinhold}
1329b2fd341SIngo Weinhold
1339b2fd341SIngo Weinhold
1341a4d020dSIngo Weinholdvoid
1351a4d020dSIngo WeinholdBSolverPackageSpecifierList::MakeEmpty()
1361a4d020dSIngo Weinhold{
1371a4d020dSIngo Weinhold	fSpecifiers->clear();
1381a4d020dSIngo Weinhold}
1391a4d020dSIngo Weinhold
1401a4d020dSIngo Weinhold
141479ca816SIngo WeinholdBSolverPackageSpecifierList&
142479ca816SIngo WeinholdBSolverPackageSpecifierList::operator=(const BSolverPackageSpecifierList& other)
143479ca816SIngo Weinhold{
144479ca816SIngo Weinhold	if (this == &other)
145479ca816SIngo Weinhold		return *this;
146479ca816SIngo Weinhold
147479ca816SIngo Weinhold	delete fSpecifiers;
148479ca816SIngo Weinhold	fSpecifiers = NULL;
149479ca816SIngo Weinhold
150479ca816SIngo Weinhold	if (other.fSpecifiers == NULL)
151479ca816SIngo Weinhold		return *this;
152479ca816SIngo Weinhold
153479ca816SIngo Weinhold	try {
154479ca816SIngo Weinhold		fSpecifiers = new(std::nothrow) Vector(*other.fSpecifiers);
155479ca816SIngo Weinhold	} catch (std::bad_alloc&) {
156479ca816SIngo Weinhold	}
157479ca816SIngo Weinhold
158479ca816SIngo Weinhold	return *this;
159479ca816SIngo Weinhold}
160479ca816SIngo Weinhold
161479ca816SIngo Weinhold
162479ca816SIngo Weinhold}	// namespace BPackageKit
163