1/*
2 * Copyright (c) 2000-2001,2003-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// typedvalue - type-safe reference transmission of arbitrary types.
27//
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.
33//
34#ifndef _H_TYPEDVALUE
35#define _H_TYPEDVALUE
36
37#include <security_utilities/utilities.h>
38#include <security_utilities/debugging.h>
39
40
41namespace Security {
42
43
44//
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).
48//
49class GenericValue {
50public:
51    virtual ~GenericValue();
52
53    template <class V>
54    void operator = (const V &value);
55};
56
57
58//
59// A TypedValue is a particular typed instance of a GenericValue
60//
61template <class Value>
62class TypedValue : public GenericValue {
63public:
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; }
69
70private:
71    Value mValue;
72};
73
74
75//
76// The polymorphic assignment access method
77//
78template <class Value>
79void GenericValue::operator = (const Value &value)
80{ safer_cast<TypedValue<Value> &>(*this) = value; }
81
82
83}	// end namespace Security
84
85
86#endif //_H_TYPEDVALUE
87