1 //
2 // VMime library (http://www.vmime.org)
3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // Linking this library statically or dynamically with other modules is making
20 // a combined work based on this library.  Thus, the terms and conditions of
21 // the GNU General Public License cover the whole combination.
22 //
23 
24 #ifndef VMIME_PARAMETER_HPP_INCLUDED
25 #define VMIME_PARAMETER_HPP_INCLUDED
26 
27 
28 #include "vmime/base.hpp"
29 #include "vmime/component.hpp"
30 #include "vmime/word.hpp"
31 
32 
33 namespace vmime
34 {
35 
36 
37 class VMIME_EXPORT parameter : public component
38 {
39 	friend class parameterizedHeaderField;
40 
41 private:
42 
43 	parameter(const parameter&);
44 
45 public:
46 
47 	/** Construct a parameter with no value.
48 	  * Charset is set to the current locale charset.
49 	  *
50 	  * @param name parameter name
51 	  */
52 	parameter(const string& name);
53 
54 	/** Construct a parameter given a name and a value.
55 	  *
56 	  * @param name parameter name
57 	  * @param value parameter value
58 	  */
59 	parameter(const string& name, const word& value);
60 
61 	/** Construct a parameter given a name and a string value
62 	  * expressed in the current locale charset.
63 	  *
64 	  * @param name parameter name
65 	  * @param value parameter value
66 	  */
67 	parameter(const string& name, const string& value);
68 
69 
70 #ifndef VMIME_BUILDING_DOC
71 
72 	/** A single section of a multi-section parameter,
73 	  * as defined in RFC-2231/3. This is used when
74 	  * calling parse() on the parameter.
75 	  */
76 	struct valueChunk
77 	{
78 		bool encoded;
79 		string data;
80 	};
81 
82 #endif // VMIME_BUILDING_DOC
83 
84 	shared_ptr <component> clone() const;
85 	void copyFrom(const component& other);
86 	parameter& operator=(const parameter& other);
87 
88 	size_t getGeneratedSize(const generationContext& ctx);
89 
90 	const std::vector <shared_ptr <component> > getChildComponents();
91 
92 	/** Return the name of this parameter.
93 	  *
94 	  * @return name of this parameter
95 	  */
96 	const string& getName() const;
97 
98 	/** Return the raw value of this parameter.
99 	  *
100 	  * @return read-only value
101 	  */
102 	const word& getValue() const;
103 
104 	/** Return the value of this object in the specified type.
105 	  * For example, the following code:
106 	  *
107 	  * <pre>
108 	  *    getParameter("creation-date")->getValueAs <vmime::dateTime>()
109 	  * </pre>
110 	  *
111 	  * is equivalent to:
112 	  *
113 	  * <pre>
114 	  *    shared_ptr <vmime::word> rawValue = getParameter("creation-date");
115 	  *
116 	  *    vmime::dateTime theDate;
117 	  *    theDate.parse(rawValue->getBuffer());
118 	  * </pre>
119 	  *
120 	  * @param T type to which convert the value
121 	  * @return value
122 	  */
123 	template <typename T>
getValueAs() const124 	const T getValueAs() const
125 	{
126 		T ret;
127 		ret.parse(m_value->getBuffer());
128 
129 		return ret;
130 	}
131 
132 	/** Set the value of this parameter.
133 	  *
134 	  * @param value new value
135 	  */
136 	void setValue(const component& value);
137 
138 	/** Set the raw value of this parameter.
139 	  *
140 	  * @param value new value
141 	  */
142 	void setValue(const word& value);
143 
144 
145 protected:
146 
147 	void parseImpl
148 		(const parsingContext& ctx,
149 		 const string& buffer,
150 		 const size_t position,
151 		 const size_t end,
152 		 size_t* newPosition = NULL);
153 
154 	void generateImpl
155 		(const generationContext& ctx,
156 		 utility::outputStream& os,
157 		 const size_t curLinePos = 0,
158 		 size_t* newLinePos = NULL) const;
159 
160 private:
161 
162 	void parse(const parsingContext& ctx, const std::vector <valueChunk>& chunks);
163 
164 
165 	string m_name;
166 	shared_ptr <word> m_value;
167 };
168 
169 
170 } // vmime
171 
172 
173 #endif // VMIME_PARAMETER_HPP_INCLUDED
174