blob: dbb04bcd9c878387a50432478efefc97353b0892 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.20"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Qpid Proton C++ API: message_properties.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Qpid Proton C++ API
&#160;<span id="projectnumber">0.32.0</span>
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.svg"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.20 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('message_properties_8cpp-example.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">message_properties.cpp</div> </div>
</div><!--header-->
<div class="contents">
<p>Shows how to set and examine message properties.</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Licensed to the Apache Software Foundation (ASF) under one</span></div>
<div class="line"><span class="comment"> * or more contributor license agreements. See the NOTICE file</span></div>
<div class="line"><span class="comment"> * distributed with this work for additional information</span></div>
<div class="line"><span class="comment"> * regarding copyright ownership. The ASF licenses this file</span></div>
<div class="line"><span class="comment"> * to you under the Apache License, Version 2.0 (the</span></div>
<div class="line"><span class="comment"> * &quot;License&quot;); you may not use this file except in compliance</span></div>
<div class="line"><span class="comment"> * with the License. You may obtain a copy of the License at</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * http://www.apache.org/licenses/LICENSE-2.0</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * Unless required by applicable law or agreed to in writing,</span></div>
<div class="line"><span class="comment"> * software distributed under the License is distributed on an</span></div>
<div class="line"><span class="comment"> * &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span></div>
<div class="line"><span class="comment"> * KIND, either express or implied. See the License for the</span></div>
<div class="line"><span class="comment"> * specific language governing permissions and limitations</span></div>
<div class="line"><span class="comment"> * under the License.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="message_8hpp.html">proton/message.hpp</a>&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="types_8hpp.html">proton/types.hpp</a>&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;map&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) {</div>
<div class="line"> <span class="keywordflow">try</span> {</div>
<div class="line"> <a name="_a0"></a><a class="code" href="classproton_1_1message.html">proton::message</a> m;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Setting properties: legal types are converted automatically to their</span></div>
<div class="line"> <span class="comment">// AMQP counterpart.</span></div>
<div class="line"> m.<a name="a1"></a><a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a name="a2"></a><a class="code" href="classproton_1_1map.html#ac310ae7d64b7ad8a70b200c07a55a736">put</a>(<span class="stringliteral">&quot;short&quot;</span>, int16_t(123));</div>
<div class="line"> m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#ac310ae7d64b7ad8a70b200c07a55a736">put</a>(<span class="stringliteral">&quot;string&quot;</span>, <span class="stringliteral">&quot;foo&quot;</span>);</div>
<div class="line"> m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#ac310ae7d64b7ad8a70b200c07a55a736">put</a>(<span class="stringliteral">&quot;symbol&quot;</span>, <a name="_a3"></a><a class="code" href="classproton_1_1symbol.html">proton::symbol</a>(<span class="stringliteral">&quot;sym&quot;</span>));</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Examining properties using proton::get()</span></div>
<div class="line"> </div>
<div class="line"> <span class="comment">// 1 argument get&lt;&gt;() template specifies expected type of property.</span></div>
<div class="line"> std::string s = proton::get&lt;std::string&gt;(m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a name="a4"></a><a class="code" href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">get</a>(<span class="stringliteral">&quot;string&quot;</span>));</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// 2 argument get, property must have matching type to output argument.</span></div>
<div class="line"> int16_t i;</div>
<div class="line"> <a name="a5"></a><a class="code" href="namespaceproton.html#a61968283b9e1fc067dcad67e9fe58f6a">proton::get</a>(m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">get</a>(<span class="stringliteral">&quot;short&quot;</span>), i);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Checking property types</span></div>
<div class="line"> <a class="code" href="namespaceproton.html#a83c2656d467d69eb49725c18f5aa13a9">proton::type_id</a> type = m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">get</a>(<span class="stringliteral">&quot;symbol&quot;</span>).type();</div>
<div class="line"> <span class="keywordflow">if</span> (type != <a name="a6"></a><a class="code" href="namespaceproton.html#a83c2656d467d69eb49725c18f5aa13a9ae3d30ffaffa3c06ed917e565dda1fbc5">proton::SYMBOL</a>) {</div>
<div class="line"> <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">&quot;wrong type!&quot;</span>);</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// proton::scalar has its own ostream &lt;&lt;</span></div>
<div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;using put/get:&quot;</span></div>
<div class="line"> &lt;&lt; <span class="stringliteral">&quot; short=&quot;</span> &lt;&lt; i</div>
<div class="line"> &lt;&lt; <span class="stringliteral">&quot; string=&quot;</span> &lt;&lt; s</div>
<div class="line"> &lt;&lt; <span class="stringliteral">&quot; symbol=&quot;</span> &lt;&lt; m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">get</a>(<span class="stringliteral">&quot;symbol&quot;</span>)</div>
<div class="line"> &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Converting properties to a convertible type</span></div>
<div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;using coerce:&quot;</span></div>
<div class="line"> &lt;&lt; <span class="stringliteral">&quot; short(as long)=&quot;</span></div>
<div class="line"> &lt;&lt; proton::coerce&lt;long&gt;(m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">get</a>(<span class="stringliteral">&quot;short&quot;</span>))</div>
<div class="line"> &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Extract the properties as a std::map for more complex map operations.</span></div>
<div class="line"> <span class="comment">// You can use other map and sequence types to hold a map, see @ref types_page</span></div>
<div class="line"> <span class="keyword">typedef</span> std::map&lt;std::string, proton::scalar&gt; property_map;</div>
<div class="line"> property_map props;</div>
<div class="line"> <a class="code" href="namespaceproton.html#a61968283b9e1fc067dcad67e9fe58f6a">proton::get</a>(m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>(), props);</div>
<div class="line"> <span class="keywordflow">for</span> (property_map::iterator i = props.begin(); i != props.end(); ++i) {</div>
<div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;props[&quot;</span> &lt;&lt; i-&gt;first &lt;&lt; <span class="stringliteral">&quot;]=&quot;</span> &lt;&lt; i-&gt;second &lt;&lt; std::endl;</div>
<div class="line"> }</div>
<div class="line"> props[<span class="stringliteral">&quot;string&quot;</span>] = <span class="stringliteral">&quot;bar&quot;</span>;</div>
<div class="line"> props[<span class="stringliteral">&quot;short&quot;</span>] = 42;</div>
<div class="line"> <span class="comment">// Update the properties in the message from the modified props map</span></div>
<div class="line"> m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>() = props;</div>
<div class="line"> </div>
<div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;short=&quot;</span> &lt;&lt; m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">get</a>(<span class="stringliteral">&quot;short&quot;</span>)</div>
<div class="line"> &lt;&lt; <span class="stringliteral">&quot; string=&quot;</span> &lt;&lt; m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">get</a>(<span class="stringliteral">&quot;string&quot;</span>)</div>
<div class="line"> &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// proton::get throws an exception if types do not match exactly.</span></div>
<div class="line"> <span class="keywordflow">try</span> {</div>
<div class="line"> proton::get&lt;uint32_t&gt;(m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">get</a>(<span class="stringliteral">&quot;short&quot;</span>)); <span class="comment">// bad: uint32_t != int16_t</span></div>
<div class="line"> <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">&quot;expected exception&quot;</span>);</div>
<div class="line"> } <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a name="_a7"></a><a class="code" href="structproton_1_1conversion__error.html">proton::conversion_error</a>&amp; e) {</div>
<div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;expected conversion_error: \&quot;&quot;</span> &lt;&lt; e.what() &lt;&lt; <span class="charliteral">&#39;&quot;&#39;</span> &lt;&lt; std::endl;</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// proton::coerce throws an exception if types are not convertible.</span></div>
<div class="line"> <span class="keywordflow">try</span> {</div>
<div class="line"> proton::get&lt;uint32_t&gt;(m.<a class="code" href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">properties</a>().<a class="code" href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">get</a>(<span class="stringliteral">&quot;string&quot;</span>)); <span class="comment">// bad: string to uint32_t</span></div>
<div class="line"> <span class="keywordflow">throw</span> std::logic_error(<span class="stringliteral">&quot;expected exception&quot;</span>);</div>
<div class="line"> } <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="structproton_1_1conversion__error.html">proton::conversion_error</a>&amp; e) {</div>
<div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;expected conversion_error: \&quot;&quot;</span> &lt;&lt; e.what() &lt;&lt; <span class="charliteral">&#39;&quot;&#39;</span> &lt;&lt; std::endl;</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line"> } <span class="keywordflow">catch</span> (<span class="keyword">const</span> std::exception&amp; e) {</div>
<div class="line"> std::cerr &lt;&lt; <span class="stringliteral">&quot;unexpected exception: &quot;</span> &lt;&lt; e.what() &lt;&lt; std::endl;</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<div class="ttc" id="aclassproton_1_1message_html_ac163919aa25a6ed5b33a29a96d04e028"><div class="ttname"><a href="classproton_1_1message.html#ac163919aa25a6ed5b33a29a96d04e028">proton::message::properties</a></div><div class="ttdeci">property_map &amp; properties()</div><div class="ttdoc">Get the application properties map.</div></div>
<div class="ttc" id="amessage_8hpp_html"><div class="ttname"><a href="message_8hpp.html">message.hpp</a></div><div class="ttdoc">An AMQP message.</div></div>
<div class="ttc" id="atypes_8hpp_html"><div class="ttname"><a href="types_8hpp.html">types.hpp</a></div><div class="ttdoc">Proton types used to represent AMQP types.</div></div>
<div class="ttc" id="aclassproton_1_1map_html_aa5c42da16d5df4154dc8af4c39fbd8e0"><div class="ttname"><a href="classproton_1_1map.html#aa5c42da16d5df4154dc8af4c39fbd8e0">proton::map::get</a></div><div class="ttdeci">T get(const K &amp;k) const</div><div class="ttdoc">Get the map entry for key k.</div></div>
<div class="ttc" id="astructproton_1_1conversion__error_html"><div class="ttname"><a href="structproton_1_1conversion__error.html">proton::conversion_error</a></div><div class="ttdoc">An error converting between AMQP and C++ data.</div><div class="ttdef"><b>Definition:</b> error.hpp:55</div></div>
<div class="ttc" id="anamespaceproton_html_a83c2656d467d69eb49725c18f5aa13a9"><div class="ttname"><a href="namespaceproton.html#a83c2656d467d69eb49725c18f5aa13a9">proton::type_id</a></div><div class="ttdeci">type_id</div><div class="ttdoc">An identifier for AMQP types.</div><div class="ttdef"><b>Definition:</b> type_id.hpp:37</div></div>
<div class="ttc" id="aclassproton_1_1symbol_html"><div class="ttname"><a href="classproton_1_1symbol.html">proton::symbol</a></div><div class="ttdoc">A string that represents the AMQP symbol type.</div><div class="ttdef"><b>Definition:</b> symbol.hpp:35</div></div>
<div class="ttc" id="anamespaceproton_html_a61968283b9e1fc067dcad67e9fe58f6a"><div class="ttname"><a href="namespaceproton.html#a61968283b9e1fc067dcad67e9fe58f6a">proton::get</a></div><div class="ttdeci">T get(const scalar &amp;s)</div><div class="ttdoc">Get a contained value of type T.</div><div class="ttdef"><b>Definition:</b> scalar.hpp:60</div></div>
<div class="ttc" id="aclassproton_1_1map_html_ac310ae7d64b7ad8a70b200c07a55a736"><div class="ttname"><a href="classproton_1_1map.html#ac310ae7d64b7ad8a70b200c07a55a736">proton::map::put</a></div><div class="ttdeci">void put(const K &amp;k, const T &amp;v)</div><div class="ttdoc">Put a map entry for key k.</div></div>
<div class="ttc" id="anamespaceproton_html_a83c2656d467d69eb49725c18f5aa13a9ae3d30ffaffa3c06ed917e565dda1fbc5"><div class="ttname"><a href="namespaceproton.html#a83c2656d467d69eb49725c18f5aa13a9ae3d30ffaffa3c06ed917e565dda1fbc5">proton::SYMBOL</a></div><div class="ttdeci">@ SYMBOL</div><div class="ttdoc">Variable-length encoded string.</div><div class="ttdef"><b>Definition:</b> type_id.hpp:58</div></div>
<div class="ttc" id="aclassproton_1_1message_html"><div class="ttname"><a href="classproton_1_1message.html">proton::message</a></div><div class="ttdoc">An AMQP message.</div><div class="ttdef"><b>Definition:</b> message.hpp:50</div></div>
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by <a href="http://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.8.20 </li>
</ul>
</div>
</body>
</html>