<?xml?>

<tm2>

  <typedef id="GWEN_BUFFER" type="pointer" lang="c">
    <identifier>GWEN_BUFFER</identifier>
    <aqdb_type>AQDB_DataType_Bin</aqdb_type>


    <codedefs>

      <codedef id="construct">
        <code>
          $(dst)=$(preset);
        </code>
      </codedef>

      <codedef id="destruct">
        <code>
          GWEN_Buffer_free($(src));
          $(src)=NULL;
        </code>
      </codedef>

      <codedef id="assign">
        <code>
          $(dst)=$(src);
        </code>
      </codedef>

      <codedef id="dup">
        <code>
          $(dst)=GWEN_Buffer_dup($(src));
        </code>
      </codedef>

      <codedef id="compare">
        <!-- TODO: actually compare -->
        <code>
          if ($(dst) &amp;&amp; $(src))
            $(retval)=0;
          else if ($(src))
            $(retval)=1;
          else if ($(dst))
            $(retval)=-1;
          else
            $(retval)=0;
        </code>
      </codedef>

      <codedef id="toXml">
        <!-- !attribute -->
        <code>
          if ($(src)) {
            GWEN_BUFFER *tbuf;
            
            tbuf=GWEN_Buffer_new(0, GWEN_Buffer_GetUsedBytes($(src))*2, 0, 1);
            GWEN_Text_ToHexBuffer(GWEN_Buffer_GetStart($(src)),
                                  GWEN_Buffer_GetUsedBytes($(src)),
                                  tbuf, 0, 0, 0);
            GWEN_XMLNode_SetCharValue($(db), "$(name)", GWEN_Buffer_GetStart(tbuf));
            GWEN_Buffer_free(tbuf);
          }
        </code>
      </codedef>

      <codedef id="fromXml">
        <code>
          {
            const char *s;
            
            s=GWEN_XMLNode_GetCharValue($(db), "$(name)", $(default));
            if (s) {
              $(dst)=GWEN_Buffer_new(0, strlen(s)/2, 0, 1);
              GWEN_Text_FromHexBuffer(s, $(dst));
            }
          }
        </code>
      </codedef>

      <codedef id="toDb">
        <code>
          if ($(src)) 
            $(retval)=GWEN_DB_SetBinValue($(db), 
                                           GWEN_DB_FLAGS_OVERWRITE_VARS, 
                                           "$(name)", 
                                           GWEN_Buffer_GetStart($(src)),
                                           GWEN_Buffer_GetUsedBytes($(src)));
          else {
            GWEN_DB_DeleteVar($(db), "$(name)");
            $(retval)=0;
          }
        </code>
      </codedef>



      <codedef id="fromDb">
        <code>
          {
            const uint8_t *ptr;
            uint32_t len;
            
            ptr=GWEN_DB_GetBinValue($(db), "$(name)", 0, NULL, 0, &amp;len);
            if (ptr &amp;&amp; len) {
              $(dst)=GWEN_Buffer_new(0, len, 0, 1);
              GWEN_Buffer_AppendBytes($(dst), (const char*)ptr, len);
            }
          }
        </code>
      </codedef>


      <codedef id="toObject">
        <code>
          if ($(src)) {
            $(retval)=AQDB_Object_SetField($(db), $(fieldId), 
                                           (const uint8_t*) GWEN_Buffer_GetStart($(src)), 
                                           GWEN_Buffer_GetUsedBytes($(src)));
          }
          else 
            $(retval)=AQDB_Object_SetField($(db), $(fieldId), NULL, 0);
        </code>
      </codedef>



      <codedef id="fromObject">
        <code>
          {
            uint8_t *p;
            uint32_t len;
            
            $(retval)=AQDB_Object_GetField($(db), $(fieldId), &amp;p, &amp;len);
            if ($(retval)&gt;=0 &amp;&amp; p &amp;&amp; len) {
              $(dst)=GWEN_Buffer_new(0, len, 0, 1);
              GWEN_Buffer_AppendBytes($(dst), (const char*) p, len);
            }
          }
        </code>
      </codedef>

    </codedefs>

    <defaults>
      <!-- defaults flags etc for member declarations of this type -->
      <default>NULL</default>
      <preset>NULL</preset>
      <flags>own</flags>
      <setflags>const dup</setflags>
      <getflags>const</getflags>
      <dupflags>const</dupflags>
    </defaults>
  
  </typedef>

</tm2>
