]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/typedvalue.h
Security-179.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / typedvalue.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 // typedvalue - type-safe reference transmission of arbitrary types.
21 //
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.
27 //
28 #ifndef _H_TYPEDVALUE
29 #define _H_TYPEDVALUE
30
31 #include <Security/utilities.h>
32 #include <Security/debugging.h>
33
34
35 namespace Security {
36
37
38 //
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).
42 //
43 class GenericValue {
44 public:
45 virtual ~GenericValue();
46
47 template <class V>
48 void operator = (const V &value);
49 };
50
51
52 //
53 // A TypedValue is a particular typed instance of a GenericValue
54 //
55 template <class Value>
56 class TypedValue : public GenericValue {
57 public:
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; }
63
64 private:
65 Value mValue;
66 };
67
68
69 //
70 // The polymorphic assignment access method
71 //
72 template <class Value>
73 void GenericValue::operator = (const Value &value)
74 { safer_cast<TypedValue<Value> &>(*this) = value; }
75
76
77 } // end namespace Security
78
79
80 #endif //_H_TYPEDVALUE