2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * AuthorizationData.cpp
23 * Created by Michael Brouwer on Thu Oct 12 2000.
24 * Copyright (c) 2000 Apple Computer Inc. All rights reserved.
28 #include "AuthorizationData.h"
31 namespace Authorization
{
38 Right::overlay(AuthorizationItem
&item
)
40 return static_cast<Right
&>(item
);
44 Right::overlay(AuthorizationItem
*item
)
46 return static_cast<Right
*>(item
);
57 Right::Right(AuthorizationString inName
, size_t inValueLength
, const void *inValue
)
60 valueLength
= inValueLength
;
61 value
= const_cast<void *>(inValue
);
69 Right::operator < (const Right
&other
) const
71 return strcmp(name
, other
.name
) < 0;
78 const AuthorizationRights
RightSet::gEmptyRights
= { 0, NULL
};
80 RightSet::RightSet(const AuthorizationRights
*rights
) :
81 mRights(const_cast<AuthorizationRights
*>(rights
? rights
: &gEmptyRights
))
85 RightSet::RightSet(const RightSet
&other
)
87 mRights
= other
.mRights
;
94 RightSet::const_reference
95 RightSet::back() const
97 // @@@ Should this if empty::throwMe()?
98 return static_cast<const_reference
>(mRights
->items
[size() - 1]);
103 // MutableRightSet class
105 MutableRightSet::MutableRightSet(size_t count
, const Right
&element
) :
108 mRights
= new AuthorizationRights();
109 mRights
->items
= reinterpret_cast<pointer
>(malloc(sizeof(Right
) * mCapacity
));
113 throw std::bad_alloc();
116 mRights
->count
= count
;
117 for (size_type ix
= 0; ix
< count
; ++ix
)
118 mRights
->items
[ix
] = element
;
121 MutableRightSet::MutableRightSet(const RightSet
&other
)
123 size_type count
= other
.size();
125 mRights
= new AuthorizationRights();
127 mRights
->items
= reinterpret_cast<pointer
>(malloc(sizeof(Right
) * mCapacity
));
131 throw std::bad_alloc();
134 mRights
->count
= count
;
135 for (size_type ix
= 0; ix
< count
; ++ix
)
136 mRights
->items
[ix
] = other
.mRights
->items
[ix
];
139 MutableRightSet::~MutableRightSet()
141 free(mRights
->items
);
146 MutableRightSet::operator = (const RightSet
&other
)
148 size_type count
= other
.size();
149 if (capacity() < count
)
152 mRights
->count
= count
;
153 for (size_type ix
= 0; ix
< count
; ++ix
)
154 mRights
->items
[ix
] = other
.mRights
->items
[ix
];
160 MutableRightSet::swap(MutableRightSet
&other
)
162 AuthorizationRights
*rights
= mRights
;
163 size_t capacity
= mCapacity
;
164 mRights
= other
.mRights
;
165 mCapacity
= other
.mCapacity
;
166 other
.mRights
= rights
;
167 other
.mCapacity
= capacity
;
170 MutableRightSet::reference
171 MutableRightSet::back()
173 // @@@ Should this if empty::throwMe()?
174 return static_cast<reference
>(mRights
->items
[size() - 1]);
178 MutableRightSet::push_back(const_reference right
)
180 if (size() >= capacity())
181 grow(capacity() + 1);
183 mRights
->items
[mRights
->count
] = right
;
188 MutableRightSet::pop_back()
190 // @@@ Should this if empty::throwMe()?
196 MutableRightSet::grow(size_type min_capacity
)
198 size_type newCapacity
= mCapacity
* mCapacity
;
199 if (newCapacity
< min_capacity
)
200 newCapacity
= min_capacity
;
202 void *newItems
= realloc(mRights
->items
, sizeof(*mRights
->items
) * newCapacity
);
204 throw std::bad_alloc();
206 mRights
->items
= reinterpret_cast<pointer
>(newItems
);
207 mCapacity
= newCapacity
;
211 } // end namespace Authorization