linked.cpp

An example of the linked object classes and their use.

00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #ifndef DEBUG
00019 #define DEBUG
00020 #endif
00021 
00022 #include <ucommon/ucommon.h>
00023 
00024 #include <stdio.h>
00025 
00026 using namespace UCOMMON_NAMESPACE;
00027 
00028 typedef linked_value<int> ints;
00029 
00030 static OrderedIndex list;
00031 
00032 class member : public DLinkedObject
00033 {
00034 public:
00035     inline member(unsigned v) : DLinkedObject() {value = v;}
00036 
00037     unsigned value;
00038 };
00039 
00040 extern "C" int main()
00041 {
00042     linked_pointer<ints> ptr;
00043     unsigned count = 0;
00044     // Since value templates pass by reference, we must have referencable
00045     // objects or variables.  This avoids passing values by duplicating
00046     // objects onto the stack frame, though it causes problems for built-in
00047     // types...
00048     int xv = 3, xn = 5;
00049     ints v1(&list, xv);
00050     ints v2(&list);
00051     v2 = xn;
00052 
00053     ptr = &list;
00054     while(ptr) {
00055         switch(++count)
00056         {
00057         case 1:
00058             assert(ptr->value == 3);
00059             break;
00060         case 2:
00061             assert(ptr->value == 5);
00062         }
00063         ++ptr;
00064     }
00065     assert(count == 2);
00066 
00067     member ov1 = 1, ov2 = 2, ov3 = 3;
00068 
00069     assert(ov2.value == 2);
00070 
00071     objstack_t st;
00072     st.push(&ov1);
00073     st.push(&ov2);
00074     st.push(&ov3);
00075 
00076     member *mv = (member *)st.pop();
00077     assert(mv->value == 3);
00078     st.pop();
00079     st.pop();
00080     assert(NULL == st.pop());
00081 
00082     objqueue<member> que;
00083     que.add(&ov1);
00084     que.add(&ov2);
00085     que.add(&ov3);
00086     mv = que.pop();
00087     assert(mv->value == 3);
00088     mv = que.pull();
00089     assert(mv != NULL);
00090 //  assert(mv->value == 1);
00091 
00092     return 0;
00093 }

Generated on 14 Aug 2013 for UCommon by  doxygen 1.4.7