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

Boris Kolpackov boris at codesynthesis.com
Thu Sep 2 16:04:11 EDT 2010


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



More information about the xsd-users mailing list