Imported Upstream version 2.2.1+dfsg
[shibboleth/sp.git] / shibsp / attribute / ExtensibleAttribute.h
1 /*
2  *  Copyright 2009 Internet2
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file shibsp/attribute/ExtensibleAttribute.h
19  *
20  * An Attribute whose values are arbitrary structures.
21  */
22
23 #ifndef __shibsp_extattr_h__
24 #define __shibsp_extattr_h__
25
26 #include <shibsp/attribute/Attribute.h>
27 #include <xmltooling/exceptions.h>
28
29 namespace shibsp {
30
31 #if defined (_MSC_VER)
32     #pragma warning( push )
33     #pragma warning( disable : 4251 )
34 #endif
35
36     /**
37      * An Attribute whose values are arbitrary structures.
38      */
39     class SHIBSP_API ExtensibleAttribute : public Attribute
40     {
41     public:
42         /**
43          * Constructor.
44          *
45          * @param ids       array with primary identifier in first position, followed by any aliases
46          * @param formatter template for serialization of values
47          */
48         ExtensibleAttribute(const std::vector<std::string>& ids, const char* formatter) : Attribute(ids) {
49             m_obj = Attribute::marshall();
50             m_obj.name("Extensible");
51             m_obj.addmember("_formatter").string(formatter);
52         }
53
54         /**
55          * Constructs based on a remoted ExtensibleAttribute.
56          *
57          * @param in    input object containing marshalled ExtensibleAttribute
58          */
59         ExtensibleAttribute(DDF& in) : Attribute(in), m_obj(in.copy()) {
60         }
61
62         virtual ~ExtensibleAttribute() {
63             m_obj.destroy();
64         }
65
66         /**
67          * Returns the set of values in a DDF list.
68          *
69          * @return  a mutable list object containing the values
70          */
71         DDF getValues() {
72             return m_obj.first();
73         }
74
75         size_t valueCount() const {
76             return m_obj.first().integer();
77         }
78
79         void clearSerializedValues() {
80             m_serialized.clear();
81         }
82
83         const char* getString(size_t index) const {
84             return m_obj.first()[static_cast<unsigned long>(index)].string();
85         }
86
87         const char* getScope(size_t index) const {
88             return NULL;
89         }
90
91         void removeValue(size_t index) {
92             Attribute::removeValue(index);
93             DDF vals = m_obj.first();
94             if (index < static_cast<size_t>(vals.integer()))
95                 vals[static_cast<unsigned long>(index)].remove().destroy();
96         }
97
98         const std::vector<std::string>& getSerializedValues() const;
99
100         DDF marshall() const {
101             if (!isCaseSensitive())
102                 m_obj.addmember("case_insensitive");
103             if (isInternal())
104                 m_obj.addmember("internal");
105             return m_obj.copy();
106         }
107
108     private:
109         mutable DDF m_obj;
110     };
111
112 #if defined (_MSC_VER)
113     #pragma warning( pop )
114 #endif
115
116 };
117
118 #endif /* __shibsp_nameidattr_h__ */