]> git.saurik.com Git - apple/security.git/blob - Network/parameters.h
Security-54.tar.gz
[apple/security.git] / Network / parameters.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 // parameters - dynamic parameter retrieval interface
21 //
22 #ifndef _H_PARAMETERS
23 #define _H_PARAMETERS
24
25 #include <Security/typedvalue.h>
26 #include <Security/debugging.h>
27 #include <vector>
28 #include <cstdarg>
29
30
31 namespace Security {
32 namespace Network {
33
34
35 class ParameterSource {
36 public:
37 // Keys are unsigned integers with integrated typing
38 typedef uint32 Key;
39 typedef GenericValue Value;
40
41 enum {
42 integerKey = 1, // int
43 stringKey = 2, // string
44 boolKey = 3, // bool
45 dataKey = 4 // ConstData
46 };
47 # define PARAMKEY(id,type) ((id) << 8 | (Security::Network::ParameterSource::type##Key))
48 inline int keyType(Key key) const { return key & 0xFF; }
49
50 public:
51 virtual ~ParameterSource() { }
52
53 public:
54 // core form: this can be virtually overridden
55 virtual bool getParams(Key key, Value &value) const = 0;
56
57 // convenience form: unwrap to Value base type
58 template <class T> bool get(Key key, T &result) const
59 {
60 TypedValue<T> value;
61 if (getParams(key, value)) {
62 result = value;
63 debug("paramsource", "%p key=0x%lx retrieved", this, key);
64 return true;
65 } else {
66 debug("paramsource", "%p key=0x%lx not found", this, key);
67 return false;
68 }
69 }
70
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
73 {
74 get(key, value); // overwrite value if successful
75 return value; // then return it or remaining default
76 }
77 };
78
79
80 //
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.
84 //
85 class ParameterPointer : public ParameterSource {
86 public:
87 ParameterPointer() : mBase(NULL) { }
88 ParameterPointer(ParameterSource *src) : mBase(src) { }
89
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; }
94
95 ParameterSource *parameters(ParameterSource &newBase)
96 { return parameters(&newBase); }
97
98 bool getParams(Key key, Value &value) const
99 { return mBase && mBase->getParams(key, value); }
100
101 private:
102 ParameterSource *mBase; // where to get it from...
103 };
104
105
106 //
107 // Here's an entire (ordered) "stack" of ParameterSources. Just build a vector
108 // of pointers to ParameterSources, and have them searched in order.
109 //
110 class ParameterStack : public ParameterSource, public vector<ParameterSource *> {
111 public:
112 bool getParams(Key key, Value &value) const;
113 };
114
115
116 } // end namespace Network
117 } // end namespace Security
118
119
120 #endif //_H_PARAMETERS