[xsd-users] Need help saving off a portion of an XML message using C++/Tree

Laura E Fowler Laura_E_Fowler at raytheon.com
Thu Sep 2 16:08:47 EDT 2010


Thank you Boris. You have saved me several hours trying to figure out how 
to do this.

Laura Fowler
Raytheon Company
www.raytheon.com



This message contains information that may be confidential and privileged. 
Unless you are the addressee (or authorized to receive mail for the 
addressee), you should not use, copy or disclose to anyone this message or 
any information contained in this message. If you have received this 
message in error, please so advise the sender by reply e-mail and delete 
this message. Thank you for your cooperation.




From:
Boris Kolpackov <boris at codesynthesis.com>
To:
Laura E Fowler <Laura_E_Fowler at raytheon.com>
Cc:
xsd-users at codesynthesis.com
Date:
09/02/2010 02:53 PM
Subject:
Re: [xsd-users] Need help saving off a portion of an XML message using 
C++/Tree



Hi Laura,

Laura E Fowler <Laura_E_Fowler at raytheon.com> writes:

> What is the best way to save a subCmd entry to a C++ list?  I'm
> getting myself confused about when the memory is freed pertaining to
> the iterator that was gotten when referencing the received message.
> I looked through the various documentation and wiki pages, but I
> wasn't able to locate an answer.

In C++/Tree when you create a copy of an object model node, you get
a completely independent copy of the sub-tree. In your case, we can
do something like this:

void* 
thread_func (void* arg)
{
  std::auto_ptr<subCmd> sc (static_cast<subCmd*> (arg));

  ...
}

command& c = ...

for (command::subCmd_iterator i (c.subCmd ().begin ());
     i != c.subCmd ().end (); 
     ++i)
{
  // Make a copy of the sub-command so that we can pass it to
  // the thread.
  //
  std::auto_ptr<subCmd> copy (new subCmd (*i));

  if (!start_thread (thread_func, copy.get ())
  {
    // Failed to start the thread.
  }

  // If the thread started successfully then it will free the
  // sub-command.
  //
  copy.release ();
}

Of course, you can use other smart pointers (or even plane pointers)
here instead of auto_ptr.

If you don't care about the original message and would like to avoid 
making copies then you can "deconstruct" the original object model
using the detach() functions (requires XSD 3.3.0):

for (command::subCmd_iterator i (c.subCmd ().begin ());
     i != c.subCmd ().end ();)
{
  std::auto_ptr<subCmd> sc;
  i = c.detach (i, sc);

  if (!start_thread (thread_func, sc.get ())
  {
    // Failed to start the thread.
  }

  // If the thread started successfully then it will free the
  // sub-command.
  //
  sc.release ();
}

The detach() call above has the semantics similar to erase().

Boris

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 4310 bytes
Desc: not available
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20100902/26bcaf51/attachment.gif


More information about the xsd-users mailing list