<?xml version="1.0" encoding="utf-8" ?>
<XMLDocContent PageName="SupportLibraries.MarshalSupport.MarshalInOut" Title="Marshal::InOut" xmlns="http://www.exodusemulator.com/schema/XMLDocSchema.xsd">
  <Section Title="Declaration">
    <Code Language="C++"><![CDATA[template<class ContainerType,
         bool IsLastElement = MarshalSupport::Internal::is_last_nested_container_element<ContainerType>::value,
         bool IsThisOrNextElementLastElement = MarshalSupport::Internal::is_this_or_nested_element_last_nested_container_element<ContainerType>::value>
class InOut;]]></Code>
  </Section>

  <Section Title="Description">
    <Paragraph>
      The <TypeRef>Marshal::InOut</TypeRef> class is a helper class designed to allow largely transparent and automatic marshalling of types as bi-directional
      read/write arguments for functions. Through appropriate use, implicit conversions in the <TypeRef>Marshal::InOut</TypeRef> type allow identical code to
      be written by the caller whether marshalling is employed or not. An object supplied as an input/output argument can be marshalled into the target
      function, allowing the supplied value to be retrieved, while also allowing an updated value to be marshalled back out to the caller. Marshalling a
      parameter with <TypeRef>Marshal::InOut</TypeRef> provides similar functionality to passing an argument by non-const reference.
    </Paragraph>

    <Paragraph>
      Note that the <TypeRef>Marshal::InOut</TypeRef> template type makes use of template specialization to change its implementation based on the specific type
      supplied in its first template argument, and the characteristics of that type. As such, its list of members varies based on the type supplied.
    </Paragraph>
    <Paragraph>
      Note that as the <TypeRef>Marshal::InOut</TypeRef> type is effectively a compound of <TypeRef>Marshal::In</TypeRef> and <TypeRef>Marshal::Out</TypeRef>,
      method documentation for this type will usually redirect to the documentation for these other types, as the usage is the same.
    </Paragraph>
  </Section>

  <Section Title="Members">
    <SubSection Title="Constructors">
      <FunctionMemberList>
        <FunctionMemberListEntry Visibility="Public" Name="InOut" PageName="SupportLibraries.MarshalSupport.MarshalInOut.Constructor">
          Constructs the marshaller and binds the target object
        </FunctionMemberListEntry>
      </FunctionMemberList>
    </SubSection>
    <SubSection Title="Marshal methods">
      <FunctionMemberList>
        <FunctionMemberListEntry Visibility="Public" Name="Get" PageName="SupportLibraries.MarshalSupport.MarshalIn.Get">
          Marshals the bound element to a new object.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="Set" PageName="SupportLibraries.MarshalSupport.MarshalOut.Set">
          Marshals the supplied value to the bound element, performing a move if possible.
        </FunctionMemberListEntry>
      </FunctionMemberList>
    </SubSection>
    <SubSection Title="Capacity methods (varies between specializations)">
      <FunctionMemberList>
        <FunctionMemberListEntry Visibility="Public" Name="size" PageName="SupportLibraries.MarshalSupport.MarshalIn.size">
          Returns the number of elements in the bound collection.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="resize" PageName="SupportLibraries.MarshalSupport.MarshalOut.resize">
          Resizes the bound collection to contain the specified number of elements.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="capacity" PageName="SupportLibraries.MarshalSupport.MarshalIn.capacity">
          Returns the currently allocated memory capacity of the bound collection.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="empty" PageName="SupportLibraries.MarshalSupport.MarshalIn.empty">
          Returns true if the bound collection is currently empty.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="reserve" PageName="SupportLibraries.MarshalSupport.MarshalOut.reserve">
          Reserves memory capacity for the specified number of elements in the bound collection.
        </FunctionMemberListEntry>
      </FunctionMemberList>
    </SubSection>
    <SubSection Title="Element access methods (varies between specializations)">
      <FunctionMemberList>
        <FunctionMemberListEntry Visibility="Public" Name="GetElement" PageName="SupportLibraries.MarshalSupport.MarshalIn.GetElement">
          Marshals a single element from the bound collection.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="SetElement" PageName="SupportLibraries.MarshalSupport.MarshalOut.SetElement">
          Marshals a single element to the bound collection.
        </FunctionMemberListEntry>
      </FunctionMemberList>
    </SubSection>
    <SubSection Title="Modifier methods (varies between specializations)">
      <FunctionMemberList>
        <FunctionMemberListEntry Visibility="Public" Name="push_back" PageName="SupportLibraries.MarshalSupport.MarshalOut.push_back">
          Appends the specified value to the end of the bound collection.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="pop_back" PageName="SupportLibraries.MarshalSupport.MarshalOut.pop_back">
          Removes the last entry from the bound collection.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="clear" PageName="SupportLibraries.MarshalSupport.MarshalOut.clear">
          Removes all entries from the bound collection.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="assign" PageName="SupportLibraries.MarshalSupport.MarshalOut.assign">
          Replaces the current contents of the bound collection with the indicated number of copies of the specified value.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="AssignFrom" PageName="SupportLibraries.MarshalSupport.MarshalOut.AssignFrom">
          Replaces the current contents of the bound collection with the specified data.
        </FunctionMemberListEntry>
        <FunctionMemberListEntry Visibility="Public" Name="AssignFromWithMove" PageName="SupportLibraries.MarshalSupport.MarshalOut.AssignFromWithMove">
          Replaces the current contents of the bound collection with the specified data, performing a move operation if possible.
        </FunctionMemberListEntry>
      </FunctionMemberList>
    </SubSection>
    <SubSection Title="Conversion operators">
      <FunctionMemberList>
        <FunctionMemberListEntry Visibility="Public" Name="operator ContainerType()">
          Performs an implicit conversion from the marshaller to the native type.
        </FunctionMemberListEntry>
      </FunctionMemberList>
    </SubSection>
    <SubSection Title="Custom operators">
      <FunctionMemberList>
        <FunctionMemberListEntry Visibility="Public" Name="operator=">
          Performs the same operation as calling the <PageRef PageName="SupportLibraries.MarshalSupport.MarshalOut.Set">Set</PageRef> method.
        </FunctionMemberListEntry>
      </FunctionMemberList>
    </SubSection>
  </Section>

  <Section Title="Examples">
    <SubSection Title="Recommended usage">
      <Paragraph>
        Given the following code without any marshalling involved:
      </Paragraph>
      <Code Language="C++"><![CDATA[int SomeFunction(std::vector<int>& argByNonConstRef)
{
    int originalLastEntry = (argByNonConstRef.empty() ? 0 : argByNonConstRef.back());
    argByNonConstRef.clear();
    argByNonConstRef.push_back(1);
    return originalLastEntry;
}

int main()
{
    std::vector<int> test1;
    test1.push_back(1);
    test1.push_back(2);
    int originalLastEntry = SomeFunction(test1);
    std::cout << originalLastEntry << "\t" << test1.back() << std::endl;
    return 0;
}]]></Code>
      <Paragraph>
        The recommended conversion to use marshalling would take this form:
      </Paragraph>
      <Code Language="C++"><![CDATA[int SomeFunction(const Marshal::InOut<std::vector<int>>& argByNonConstRef)
{
    std::vector<int> localArg = argByNonConstRef.Get();
    int originalLastEntry = (localArg.empty() ? 0 : localArg.back());
    localArg.clear();
    localArg.push_back(1);
    argByNonConstRef.Set(std::move(localArg));
    return originalLastEntry;
}

int main()
{
    std::vector<int> test1;
    test1.push_back(1);
    test1.push_back(2);
    int originalLastEntry = SomeFunction(test1);
    std::cout << originalLastEntry << "\t" << test1.back() << std::endl;
    return 0;
}]]></Code>
      <Paragraph>
        Note that the syntax remains unchanged for the caller, making it relatively easy to add marshalling to existing APIs. The function argument is adapted
        to use marshalling simply by wrapping it in a <TypeRef>Marshal::InOut</TypeRef> helper. Note that the marshal helpers are always passed in by const
        reference, even though the original signature passed in the parameter by non-const reference. This is essential in order to allow implicit conversions
        to the marshal helper by the caller. In the function implementation, this example follows the simplest approach of constructing a local variable of the
        same type, populating it with the required data, and calling the <PageRef PageName="SupportLibraries.MarshalSupport.MarshalOut.Set">Set method</PageRef>
        on the marshal helper to explicitly marshal the object. The assignment operator could also be used. The use of <TypeRef>std::move</TypeRef> here can
        give a performance boost with some types.
      </Paragraph>
      <Paragraph>
        Be aware that this example shows the basic form of how any object type can be marshalled using the <TypeRef>Marshal::InOut</TypeRef> helper. The
        <TypeRef>std::vector</TypeRef> and <TypeRef>std::array</TypeRef> types have additional functions available that can be used to selectively marshal only
        certain elements, or perform more efficient marshalling of the entire collection in some cases. Please refer to the list of members above for more
        information on these specialized methods.
      </Paragraph>
    </SubSection>
  </Section>

  <Section Title="See also">
    <PageRefList>
      <PageRefListEntry PageName="SupportLibraries.MarshalSupport">MarshalSupport Library Overview</PageRefListEntry>
      <PageRefListEntry PageName="SupportLibraries.MarshalSupport.MarshalIn">Marshal::In</PageRefListEntry>
      <PageRefListEntry PageName="SupportLibraries.MarshalSupport.MarshalOut">Marshal::Out</PageRefListEntry>
      <PageRefListEntry PageName="SupportLibraries.MarshalSupport.MarshalRet">Marshal::Ret</PageRefListEntry>
    </PageRefList>
  </Section>
</XMLDocContent>
