]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/typedvalue.h
5f7d22988a9640bacbbc787f06e48e91b9838a84
2 * Copyright (c) 2000-2001,2003-2004,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // typedvalue - type-safe reference transmission of arbitrary types.
28 // This slight-of-hand pair of classes allows arbitrary types to be passed through an API
29 // without losing C++ type safety. Specifically, the type is encapsulated in C++ polymorphism,
30 // and can be resolved through the C++ typeinfo/dynamic_cast system in a fairly
31 // convenient way. This is intended to replace passing (void *) arguments around.
32 // It is obviously not meant to replace proper class hierarchies in APIs.
37 #include <security_utilities/utilities.h>
38 #include <security_utilities/debugging.h>
45 // A GenericValue can represent any (one) type. Note the template assignment
46 // that works *only* for the type it actually represents. Note particularly that
47 // no automatic conversions are made (other than to subclass).
51 virtual ~GenericValue();
54 void operator = (const V
&value
);
59 // A TypedValue is a particular typed instance of a GenericValue
61 template <class Value
>
62 class TypedValue
: public GenericValue
{
64 TypedValue() : mValue() { }
65 TypedValue(const Value
&v
) : mValue(v
) { }
66 Value
&value() { return mValue
; }
67 operator Value
&() { return mValue
; }
68 void operator = (const Value
&value
) { mValue
= value
; }
76 // The polymorphic assignment access method
78 template <class Value
>
79 void GenericValue::operator = (const Value
&value
)
80 { safer_cast
<TypedValue
<Value
> &>(*this) = value
; }
83 } // end namespace Security
86 #endif //_H_TYPEDVALUE