]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/typedvalue.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 // typedvalue - type-safe reference transmission of arbitrary types.
22 // This slight-of-hand pair of classes allows arbitrary types to be passed through an API
23 // without losing C++ type safety. Specifically, the type is encapsulated in C++ polymorphism,
24 // and can be resolved through the C++ typeinfo/dynamic_cast system in a fairly
25 // convenient way. This is intended to replace passing (void *) arguments around.
26 // It is obviously not meant to replace proper class hierarchies in APIs.
31 #include <Security/utilities.h>
32 #include <Security/debugging.h>
39 // A GenericValue can represent any (one) type. Note the template assignment
40 // that works *only* for the type it actually represents. Note particularly that
41 // no automatic conversions are made (other than to subclass).
45 virtual ~GenericValue();
48 void operator = (const V
&value
);
53 // A TypedValue is a particular typed instance of a GenericValue
55 template <class Value
>
56 class TypedValue
: public GenericValue
{
58 TypedValue() : mValue() { }
59 TypedValue(const Value
&v
) : mValue(v
) { }
60 Value
&value() { return mValue
; }
61 operator Value
&() { return mValue
; }
62 void operator = (const Value
&value
) { mValue
= value
; }
70 // The polymorphic assignment access method
72 template <class Value
>
73 void GenericValue::operator = (const Value
&value
)
74 { safer_cast
<TypedValue
<Value
> &>(*this) = value
; }
77 } // end namespace Security
80 #endif //_H_TYPEDVALUE