1 /* 2 * Copyright 2013, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Ingo Weinhold <ingo_weinhold@gmx.de> 7 */ 8 9 10 #include <package/solver/SolverPackageSpecifierList.h> 11 12 #include <new> 13 #include <vector> 14 15 #include <package/solver/SolverPackageSpecifier.h> 16 17 18 namespace BPackageKit { 19 20 class BSolverPackageSpecifierList::Vector 21 : public std::vector<BSolverPackageSpecifier> { 22 public: 23 Vector() 24 : 25 std::vector<BSolverPackageSpecifier>() 26 { 27 } 28 29 Vector(const std::vector<BSolverPackageSpecifier>& other) 30 : 31 std::vector<BSolverPackageSpecifier>(other) 32 { 33 } 34 }; 35 36 37 BSolverPackageSpecifierList::BSolverPackageSpecifierList() 38 : 39 fSpecifiers(NULL) 40 { 41 } 42 43 44 BSolverPackageSpecifierList::BSolverPackageSpecifierList( 45 const BSolverPackageSpecifierList& other) 46 : 47 fSpecifiers(NULL) 48 { 49 *this = other; 50 } 51 52 53 BSolverPackageSpecifierList::~BSolverPackageSpecifierList() 54 { 55 delete fSpecifiers; 56 } 57 58 59 bool 60 BSolverPackageSpecifierList::IsEmpty() const 61 { 62 return fSpecifiers == NULL || fSpecifiers->empty(); 63 } 64 65 66 int32 67 BSolverPackageSpecifierList::CountSpecifiers() const 68 { 69 return fSpecifiers != NULL ? fSpecifiers->size() : 0; 70 } 71 72 73 const BSolverPackageSpecifier* 74 BSolverPackageSpecifierList::SpecifierAt(int32 index) const 75 { 76 if (fSpecifiers == NULL || index < 0 77 || (size_t)index >= fSpecifiers->size()) { 78 return NULL; 79 } 80 81 return &(*fSpecifiers)[index]; 82 } 83 84 85 bool 86 BSolverPackageSpecifierList::AppendSpecifier( 87 const BSolverPackageSpecifier& specifier) 88 { 89 try { 90 if (fSpecifiers == NULL) { 91 fSpecifiers = new(std::nothrow) Vector; 92 if (fSpecifiers == NULL) 93 return false; 94 } 95 96 fSpecifiers->push_back(specifier); 97 return true; 98 } catch (std::bad_alloc&) { 99 return false; 100 } 101 } 102 103 104 bool 105 BSolverPackageSpecifierList::AppendSpecifier(BSolverPackage* package) 106 { 107 return AppendSpecifier(BSolverPackageSpecifier(package)); 108 } 109 110 111 bool 112 BSolverPackageSpecifierList::AppendSpecifier(const BString& selectString) 113 { 114 return AppendSpecifier(BSolverPackageSpecifier(selectString)); 115 } 116 117 118 bool 119 BSolverPackageSpecifierList::AppendSpecifiers(const char* const* selectStrings, 120 int32 count) 121 { 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; 127 } 128 } 129 130 return true; 131 } 132 133 134 void 135 BSolverPackageSpecifierList::MakeEmpty() 136 { 137 fSpecifiers->clear(); 138 } 139 140 141 BSolverPackageSpecifierList& 142 BSolverPackageSpecifierList::operator=(const BSolverPackageSpecifierList& other) 143 { 144 if (this == &other) 145 return *this; 146 147 delete fSpecifiers; 148 fSpecifiers = NULL; 149 150 if (other.fSpecifiers == NULL) 151 return *this; 152 153 try { 154 fSpecifiers = new(std::nothrow) Vector(*other.fSpecifiers); 155 } catch (std::bad_alloc&) { 156 } 157 158 return *this; 159 } 160 161 162 } // namespace BPackageKit 163