<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://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"/> <title>ucommon: linked.cpp</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> </div> <div class="contents"> <h1>linked.cpp</h1><p>An example of the linked object classes and their use.</p> <div class="fragment"><pre class="fragment"><span class="comment">// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.</span> <span class="comment">//</span> <span class="comment">// This file is part of GNU uCommon C++.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ is free software: you can redistribute it and/or modify</span> <span class="comment">// it under the terms of the GNU Lesser General Public License as published</span> <span class="comment">// by the Free Software Foundation, either version 3 of the License, or</span> <span class="comment">// (at your option) any later version.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ is distributed in the hope that it will be useful,</span> <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment">// GNU Lesser General Public License for more details.</span> <span class="comment">//</span> <span class="comment">// You should have received a copy of the GNU Lesser General Public License</span> <span class="comment">// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.</span> <span class="preprocessor">#ifndef DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#define DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="preprocessor">#include <<a class="code" href="a00204.html" title="Top level include file for the GNU uCommon C++ core library.">ucommon/ucommon.h</a>></span> <span class="preprocessor">#include <stdio.h></span> <span class="keyword">using namespace </span>UCOMMON_NAMESPACE; <span class="keyword">typedef</span> linked_value<int> ints; <span class="keyword">static</span> OrderedIndex list; <span class="keyword">class </span>member : <span class="keyword">public</span> DLinkedObject { <span class="keyword">public</span>: <span class="keyword">inline</span> member(<span class="keywordtype">unsigned</span> v) : DLinkedObject() {value = v;} <span class="keywordtype">unsigned</span> value; }; <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <span class="keywordtype">int</span> main() { linked_pointer<ints> ptr; <span class="keywordtype">unsigned</span> count = 0; <span class="comment">// Since value templates pass by reference, we must have referencable</span> <span class="comment">// objects or variables. This avoids passing values by duplicating</span> <span class="comment">// objects onto the stack frame, though it causes problems for built-in</span> <span class="comment">// types...</span> <span class="keywordtype">int</span> xv = 3, xn = 5; ints v1(&list, xv); ints v2(&list); v2 = xn; ptr = &list; <span class="keywordflow">while</span>(ptr) { <span class="keywordflow">switch</span>(++count) { <span class="keywordflow">case</span> 1: assert(ptr->value == 3); <span class="keywordflow">break</span>; <span class="keywordflow">case</span> 2: assert(ptr->value == 5); } ++ptr; } assert(count == 2); member ov1 = 1, ov2 = 2, ov3 = 3; assert(ov2.value == 2); <a name="a0"></a><a class="code" href="a00208.html#ace8330e14d931878e7e3ea5f53c58b1c" title="Convenience type for a stack of linked objects.">objstack_t</a> st; <a name="a1"></a><a class="code" href="a00208.html#a36870182251dc08e9a3eac56452d6218" title="Convenience function to push an object onto a stack.">push</a>(st, &ov1); <a class="code" href="a00208.html#a36870182251dc08e9a3eac56452d6218" title="Convenience function to push an object onto a stack.">push</a>(st, &ov2); <a class="code" href="a00208.html#a36870182251dc08e9a3eac56452d6218" title="Convenience function to push an object onto a stack.">push</a>(st, &ov3); member *mv = (member *)<a name="a2"></a><a class="code" href="a00208.html#a8c39786e7fff8276be5ccaf1c962f1ce" title="Pop a linked object from a stack of linked objects.">pop</a>(st); assert(mv->value == 3); <a class="code" href="a00208.html#a8c39786e7fff8276be5ccaf1c962f1ce" title="Pop a linked object from a stack of linked objects.">pop</a>(st); <a class="code" href="a00208.html#a8c39786e7fff8276be5ccaf1c962f1ce" title="Pop a linked object from a stack of linked objects.">pop</a>(st); assert(NULL == <a class="code" href="a00208.html#a8c39786e7fff8276be5ccaf1c962f1ce" title="Pop a linked object from a stack of linked objects.">pop</a>(st)); objqueue<member> que; que.add(&ov1); que.add(&ov2); que.add(&ov3); mv = que.pop(); assert(mv->value == 3); mv = que.pull(); assert(mv != NULL); <span class="comment">// assert(mv->value == 1);</span> <span class="keywordflow">return</span> 0; } </pre></div> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Thu Jul 14 16:38:56 2011 for ucommon by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>