]>
git.saurik.com Git - apple/security.git/blob - Network/parameters.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 // parameters - dynamic parameter retrieval interface
25 #include <Security/typedvalue.h>
26 #include <Security/debugging.h>
35 class ParameterSource
{
37 // Keys are unsigned integers with integrated typing
39 typedef GenericValue Value
;
42 integerKey
= 1, // int
43 stringKey
= 2, // string
45 dataKey
= 4 // ConstData
47 # define PARAMKEY(id,type) ((id) << 8 | (Security::Network::ParameterSource::type##Key))
48 inline int keyType(Key key
) const { return key
& 0xFF; }
51 virtual ~ParameterSource() { }
54 // core form: this can be virtually overridden
55 virtual bool getParams(Key key
, Value
&value
) const = 0;
57 // convenience form: unwrap to Value base type
58 template <class T
> bool get(Key key
, T
&result
) const
61 if (getParams(key
, value
)) {
63 debug("paramsource", "%p key=0x%lx retrieved", this, key
);
66 debug("paramsource", "%p key=0x%lx not found", this, key
);
71 // convenience form: return value, use default if not found (no failure indication)
72 template <class T
> T
getv(Key key
, T value
= T()) const
74 get(key
, value
); // overwrite value if successful
75 return value
; // then return it or remaining default
81 // A ParameterPointer is a ParameterSource that has an indirection to another
82 // ParameterSource. The underlying ("base") reference can be changed at will.
83 // If it is NULL, all lookups fail.
85 class ParameterPointer
: public ParameterSource
{
87 ParameterPointer() : mBase(NULL
) { }
88 ParameterPointer(ParameterSource
*src
) : mBase(src
) { }
90 operator bool () const { return mBase
; }
91 ParameterSource
*parameters() const { return mBase
; }
92 ParameterSource
*parameters(ParameterSource
*newBase
)
93 { ParameterSource
*old
= mBase
; mBase
= newBase
; return old
; }
95 ParameterSource
*parameters(ParameterSource
&newBase
)
96 { return parameters(&newBase
); }
98 bool getParams(Key key
, Value
&value
) const
99 { return mBase
&& mBase
->getParams(key
, value
); }
102 ParameterSource
*mBase
; // where to get it from...
107 // Here's an entire (ordered) "stack" of ParameterSources. Just build a vector
108 // of pointers to ParameterSources, and have them searched in order.
110 class ParameterStack
: public ParameterSource
, public vector
<ParameterSource
*> {
112 bool getParams(Key key
, Value
&value
) const;
116 } // end namespace Network
117 } // end namespace Security
120 #endif //_H_PARAMETERS