]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/constdata.h
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / constdata.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // constdata - shared constant binary data objects
21 //
22 #ifndef _H_CONSTDATA
23 #define _H_CONSTDATA
24
25 #include <Security/utilities.h>
26 #include <Security/refcount.h>
27
28
29 namespace Security {
30
31
32 //
33 // ConstData represents a contiguous, binary blob of constant data.
34 // Assignment is by sharing (thus cheap).
35 // ConstData is a (constant) Dataoid type.
36 //
37 class ConstData {
38 private:
39 class Blob : public RefCount {
40 public:
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); }
44
45 const void *data() const { return mData; }
46 size_t length() const { return mSize; }
47
48 private:
49 const void *mData;
50 size_t mSize;
51 };
52
53 public:
54 ConstData() { } //@@@ use a nullBlob?
55 ConstData(const void *base, size_t size, bool takeOwnership = false)
56 : mBlob(new Blob(base, size, takeOwnership)) { }
57
58 template <class T>
59 static ConstData wrap(const T &obj, bool takeOwnership)
60 { return ConstData(&obj, sizeof(obj), takeOwnership); }
61
62 public:
63 const void *data() const { return mBlob ? mBlob->data() : NULL; }
64 size_t length() const { return mBlob ? mBlob->length() : 0; }
65
66 operator bool() const { return mBlob; }
67 bool operator !() const { return !mBlob; }
68
69 template <class T> operator const T *() const
70 { return reinterpret_cast<const T *>(data()); }
71
72 template <class T> const T &as() const
73 { return *static_cast<const T *>(reinterpret_cast<const T *>(data())); }
74
75 private:
76 RefPointer<Blob> mBlob;
77 };
78
79
80 } // end namespace Security
81
82
83 #endif //_H_CONSTDATA