]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/constdata.h
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 // constdata - shared constant binary data objects
25 #include <Security/utilities.h>
26 #include <Security/refcount.h>
33 // ConstData represents a contiguous, binary blob of constant data.
34 // Assignment is by sharing (thus cheap).
35 // ConstData is a (constant) Dataoid type.
39 class Blob
: public RefCount
{
41 Blob() : mData(NULL
), mSize(0) { }
42 Blob(const void *base
, size_t size
, bool takeOwnership
= false);
43 ~Blob() { delete[] reinterpret_cast<const char *>(mData
); }
45 const void *data() const { return mData
; }
46 size_t length() const { return mSize
; }
54 ConstData() { } //@@@ use a nullBlob?
55 ConstData(const void *base
, size_t size
, bool takeOwnership
= false)
56 : mBlob(new Blob(base
, size
, takeOwnership
)) { }
59 static ConstData
wrap(const T
&obj
, bool takeOwnership
)
60 { return ConstData(&obj
, sizeof(obj
), takeOwnership
); }
63 const void *data() const { return mBlob
? mBlob
->data() : NULL
; }
64 size_t length() const { return mBlob
? mBlob
->length() : 0; }
66 operator bool() const { return mBlob
; }
67 bool operator !() const { return !mBlob
; }
69 template <class T
> operator const T
*() const
70 { return reinterpret_cast<const T
*>(data()); }
72 template <class T
> const T
&as() const
73 { return *static_cast<const T
*>(reinterpret_cast<const T
*>(data())); }
76 RefPointer
<Blob
> mBlob
;
80 } // end namespace Security