Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Templates & Marshaling C++ Function Calls


July, 2004: Templates & Marshaling C++ Function Calls

Listing 5

template <bool, typename T> struct treat_enum_as_int_base
{
   typedef T type;
   static T & get_ref(T & val) { return val; };
};
template <typename T> struct treat_enum_as_int_base<true, T>
{
   typedef int type;
   static int & get_ref(T & val)
   { return reinterpret_cast<int&>(val); };
};
template <typename T> struct treat_enum_as_int_t : public
   treat_enum_as_int_base< ::boost::is_enum<T>::value, T>
{};
template <typename T> typename treat_enum_as_int_t<T>::type &
treat_enum_as_int(T & val)
{
   return treat_enum_as_int_t<T>::get_ref(val);
}
template <bool> struct read_if
{
   template <typename is_t, typename T>
   inline read_if(is_t & is, T & val)
   {
      is >> treat_enum_as_int(val);
      if (!is)
      {
         throw std::runtime_error("read failed on parameter");
      }
   }
};
// Ignore this parameter - do not read from stream
template <> struct read_if<false>
{
template <typename is_t, typename T> inline read_if(is_t &, T){}
};
// Read types that do not require to be indirected
template <class param_t, class is_t, class T>
void read_from_stream(is_t & is, T & val,
is_indirect_tag<false>, bool &)
{
   read_if<traits<param_t>::in>(is, val);
}
// Read types that are passed by pointer (and check for null)
template <class param_t, class is_t, typename T>
inline void read_from_stream(is_t & is, T & val,
is_indirect_tag<true>, bool & is_not_null)
{
   read_if<true>(is, is_not_null);
   if (is_not_null)
   {
      read_if<traits<param_t>::in>(is, val);
   }
}
// Read only [in] parameters from the input stream
template <class param_t, class is_t, class T>
void read_arg(is_t & is, T & val, bool & is_not_null)
{
   read_from_stream<param_t>(is, val,
is_indirect_tag<traits<param_t>::indirect>(), is_not_null);
}
// for functions with upto 2 arguments (download supports 7)
template <typename F, typename is_t, typename os_t >
void dispatch_call (is_t & is, os_t & os, F func)
{
   typedef func_type<F> func_t;
   typedef typename func_t::param1_t P1; // type of first param
   typedef typename func_t::param2_t P2; // type of second param
   typedef typename returnable_t<func_t::return_t>::type R;
   typedef typename traits< P1 >::value_type V1;
   typedef typename traits< P2 >::value_type V2;
   typedef typename traits< R >::value_type RT;
   // declare variables to receive data
   V1 arg1 = V1(); bool is_not_null_1 = true;
   V2 arg2 = V2(); bool is_not_null_2 = true;
   // read the [in] args from the input stream
   read_arg<P1>(is, arg1, is_not_null_1);
   read_arg<P2>(is, arg2, is_not_null_2);
   int function_result = call_returned;
   RT retval
   try
   {
      // call the specified function
      retval = func_t::call(func,
         make_arg< P1 >::to_arg(arg1, is_not_null_1),
         make_arg< P2 >::to_arg(arg2, is_not_null_2));
   }
   catch (...) { function_result = exception_thrown; }
   // marshal the return data [out] parameters
   // (and/or a flag for whether an exception occurred)
   //...
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.