Updated footer comment
[yazpp-moved-to-github.git] / src / gdu.cpp
index 676a334..8df705e 100644 (file)
@@ -1,11 +1,9 @@
-/*
- * Copyright (c) 1998-2005, Index Data.
+/* This file is part of the yazpp toolkit.
+ * Copyright (C) 1998-2009 Index Data and Mike Taylor
  * See the file LICENSE for details.
- * 
- * $Id: gdu.cpp,v 1.1 2005-06-21 17:37:15 adam Exp $
  */
 
-#include <yaz++/gdu.h>
+#include <yazpp/gdu.h>
 
 using namespace yazpp_1;
 
@@ -23,84 +21,70 @@ GDU::GDU(Z_GDU *gdu)
     base(gdu, odr_createmem(ODR_ENCODE));
 }
 
+GDU::GDU()
+{
+    base(0, odr_createmem(ODR_ENCODE));
+}
+
+GDU::GDU(const GDU &g)
+{
+    base(g.get(), odr_createmem(ODR_ENCODE));
+}
+
 void GDU::base(Z_GDU *gdu, ODR encode)
 {
     m_decode = odr_createmem(ODR_DECODE);
     m_gdu = 0;
-    if (z_GDU(encode, &gdu, 0, "encode"))
+    if (gdu && z_GDU(encode, &gdu, 0, "encode"))
     {
-       int len;
-       char *buf = odr_getbuf(encode, &len, 0);
-       
-       odr_setbuf(m_decode, buf, len, 0);
-       z_GDU(m_decode, &m_gdu, 0, 0);
+        int len;
+        char *buf = odr_getbuf(encode, &len, 0);
+        
+        odr_setbuf(m_decode, buf, len, 0);
+        z_GDU(m_decode, &m_gdu, 0, 0);
     }
     odr_destroy(encode);
 }
 
+
+GDU &GDU::operator=(const GDU &g)
+{
+    if (this != &g)
+    {
+        odr_destroy(m_decode);
+
+        base(g.get(), odr_createmem(ODR_ENCODE));
+    }
+    return *this;
+}
+
 GDU::~GDU()
 {
     odr_destroy(m_decode);
 }
 
-Z_GDU *GDU::get()
+Z_GDU *GDU::get() const
 {
     return m_gdu;
 }
 
-void GDU::extract_odr_to(ODR dst)
+void GDU::move_away_gdu(ODR dst, Z_GDU **gdu)
 {
+    *gdu = m_gdu;
+    m_gdu = 0;
     NMEM nmem = odr_extract_mem(m_decode);
     if (!dst->mem)
-       dst->mem = nmem_create();
+        dst->mem = nmem_create();
     nmem_transfer(dst->mem, nmem);
     nmem_destroy(nmem);
 }
 
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
 
-GDUQueue::GDUQueue()
-{
-    m_list = 0;
-}
-
-int GDUQueue::size()
-{
-    int no = 0;
-    GDUQueue_List *l;
-    for (l = m_list; l; l = l->m_next)
-        no++;
-    return no;
-}
-
-void GDUQueue::enqueue(GDU *gdu)
-{
-    GDUQueue_List *l = new GDUQueue_List;
-    l->m_next = m_list;
-    l->m_item = gdu;
-    m_list = l;
-}
-
-GDU *GDUQueue::dequeue()
-{
-    GDUQueue_List **l = &m_list;
-    if (!*l)
-        return 0;
-    while ((*l)->m_next)
-       l = &(*l)->m_next;
-    GDU *m = (*l)->m_item;
-    delete *l;
-    *l = 0;
-    return m;
-}
-
-void GDUQueue::clear()
-{
-    GDU *g;
-    while ((g = dequeue()))
-       delete g;
-}
-
-GDUQueue::~GDUQueue()
-{
-    clear();
-}