<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>

<doc>
   <title>Metaprogramming</title>

   <!-- ************************************************************************* -->

   <body>

         <p>
         This page documents library components that provide metaprogramming sorts of functionality.  For
         the most part they are useful for putting design by contract checks into code or doing various kinds of
         clever things with templates. 
         </p>
         <p>
            For example, you might have a templated function that is templated on a type T and you want to
            make sure that T is either a char or wchar_t type.  You could place the following into your code
            and it would cause the compile to error out when T was set to something other than char or wchar_t.
            <br/>
            <tt>COMPILE_TIME_ASSERT((is_same_type&lt;T,char&gt;::value || is_same_type&lt;T,wchar_t&gt;::value));</tt>
         </p>

   </body>

   <!-- ************************************************************************* -->

   <menu width="200">
    <top>
      <section>
         <name>Objects</name>
         <item>is_pointer_type</item>
         <item>is_const_type</item>
         <item>is_reference_type</item>
         <item>is_same_type</item>
         <item>is_float_type</item>
         <item>is_convertible</item>
         <item>is_complex</item>
         <item>is_function</item>
         <item>is_signed_type</item>
         <item>is_unsigned_type</item>
         <item>static_switch</item>
         <item>noncopyable</item>
         <item>enable_if</item>
         <item>is_array2d</item>
         <item>is_array</item>
         <item>is_graph</item>
         <item>is_rand</item>
         <item>is_matrix</item>
         <item>is_config_reader</item>
         <item>is_std_vector</item>
         <item>is_pair</item>
         <item>is_directed_graph</item>
         <item>is_built_in_scalar_type</item>
         <item>promote</item>
         <item>basic_type</item>
         <item>unsigned_type</item> 
         <item>tabs</item> 
         <item>tmin</item> 
         <item>tmax</item> 
      </section>
 
      <section>
         <name>Global Functions</name>
         <item>DLIB_ASSERT</item> 
         <item>DLIB_STACK_TRACE</item> 
         <item>DLIB_STACK_TRACE_NAMED</item> 
         <item>get_stack_trace</item> 
         <item>DLIB_CASSERT</item> 
         <item>COMPILE_TIME_ASSERT</item> 
         <item>ASSERT_ARE_SAME_TYPE</item> 
         <item>DLIB_ASSERT_HAS_STANDARD_LAYOUT</item> 
         <item>ASSERT_ARE_NOT_SAME_TYPE</item> 
         <item>_dT</item> 
         <item>is_same_object</item> 
         <item>assign_zero_if_built_in_scalar_type</item>
         <item>wrap_function</item>
         <item>DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST</item>
      </section>

      <section>
         <name>Other</name>
         <item>portability_macros</item>
      </section>

    </top>  
   </menu>

   <!-- ************************************************************************* -->
   <!-- ************************************************************************* -->
   <!-- ************************************************************************* -->

   <components>
   
   <!-- ************************************************************************* -->

      <component>
         <name>tmin</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template to compute the min of two values at compile time.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      <component>
         <name>tmax</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template to compute the max of two values at compile time.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      <component>
         <name>tabs</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template to compute the absolute value a number at compile time.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>unsigned_type</name>
         <file>dlib/uintn.h</file>
         <spec_file link="true">dlib/uintn.h</spec_file>
         <description>
            This is a template that allows you to obtain the unsigned version 
            of any integral type.  For example, unsigned_type&lt;signed short&gt;::type ==
            unsigned short.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>static_switch</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
        To use this template you give it some number of boolean expressions and it
        tells you which one of them is true.   If more than one of them is true then
        it causes a compile time error.  It is useful for cases where you want to
        specialize a template and you want to specialize it not by
        the type of object it gets per say but instead according to the values of some
        type traits associated with the various template arguments.  A simple example of
        this can be seen in the <a href="imaging.html#assign_pixel">assign_pixel</a>'s
        implementation which can be found at the bottom of the <a href="dlib/pixel.h.html">
        dlib/pixel.h</a> file.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>enable_if</name>
         <file>dlib/enable_if.h</file>
         <description>
            This is a family of templates from the Boost C++ libraries that makes it somewhat easier to control
            template specialization.  For the details see <a href="enable_if.html">
               this page</a>.  Note that the header <tt>dlib/enable_if.h</tt> brings
               these templates into the dlib namespace.<br/>
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>noncopyable</name>
         <file>dlib/noncopyable.h</file>
         <spec_file link="true">dlib/noncopyable.h</spec_file>
         <description>
            This is a simple class that makes it easy to declare a non-copyable object.
            To use it to make your own class non-copyable just inherit from it.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_convertible</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template that can be used to determine if one type is convertible 
            into another type.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      <component>
         <name>is_complex</name>
         <file>dlib/matrix.h</file>
         <spec_file link="true">dlib/matrix/matrix_utilities.h</spec_file>
         <description>
            This is a template that can be used to determine if a type is a
            specialization of std::complex.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      <component>
         <name>is_same_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template where is_same_type&lt;T,U&gt;::value == true when T and U are 
            the same type and false otherwise. 
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      <component>
         <name>is_float_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template where is_float_type&lt;T&gt;::value == true when T is
            a floating point type (i.e. float, double, or long double) and false otherwise.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      <component>
         <name>is_same_object</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a templated function which checks if both of its arguments are actually
            references to the same object.  It returns true if they are and false otherwise.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      <component>
         <name>is_function</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template where is_function&lt;T&gt;::value == true when T is 
            a function type.  
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_signed_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template where is_signed_type&lt;T&gt;::value == true when T is
            a signed scalar type and false when it is an unsigned scalar
            type.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_unsigned_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template where is_unsigned_type&lt;T&gt;::value == true when T is
            an unsigned scalar type and false when it is a signed scalar
            type.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_directed_graph</name>
         <file>dlib/is_kind.h</file>
         <spec_file link="true">dlib/is_kind.h</spec_file>
         <description>
            This is a template where is_directed_graph&lt;T&gt;::value == true when T
            is a <a href="containers.html#directed_graph">directed_graph</a> object.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_built_in_scalar_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template where is_built_in_scalar_type&lt;T&gt;::value == true when T
            is a built in scalar type such as int, char, float, etc.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>promote</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
        This is a template that takes one of the built in scalar types and gives you another
        scalar type that should be big enough to hold sums of values from the original scalar 
        type.  The new scalar type will also always be signed.

         <p>
        For example, promote&lt;uint16&gt;::type == int32
         </p>
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>basic_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
        This is a template that takes a type and strips off any const, volatile, or reference
        qualifiers and gives you back the basic underlying type.  

         <p>
        For example, promote&lt;const int&amp;&gt;::type == int 
         </p>
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      
      <component>
         <name>is_std_vector</name>
         <file>dlib/is_kind.h</file>
         <spec_file link="true">dlib/is_kind.h</spec_file>
         <description>
            This is a template where is_std_vector&lt;T&gt;::value == true when T
            is a <a href="containers.html#std_vector_c">std_vector_c</a> or std::vector object.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_pair</name>
         <file>dlib/is_kind.h</file>
         <spec_file link="true">dlib/is_kind.h</spec_file>
         <description>
            This is a template where is_pair&lt;T&gt;::value == true when T
            is a std::pair object.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      
      <component>
         <name>is_matrix</name>
         <file>dlib/is_kind.h</file>
         <spec_file link="true">dlib/is_kind.h</spec_file>
         <description>
            This is a template where is_matrix&lt;T&gt;::value == true when T
            is a <a href="linear_algebra.html#matrix">matrix</a> object or some kind
            of matrix expression.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_config_reader</name>
         <file>dlib/is_kind.h</file>
         <spec_file link="true">dlib/is_kind.h</spec_file>
         <description>
            This is a template where is_config_reader&lt;T&gt;::value == true when T
            is a <a href="parsing.html#config_reader">config_reader</a> or
            config_reader_thread_safe object.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      
      <component>
         <name>is_graph</name>
         <file>dlib/is_kind.h</file>
         <spec_file link="true">dlib/is_kind.h</spec_file>
         <description>
            This is a template where is_graph&lt;T&gt;::value == true when T
            is a <a href="containers.html#graph">graph</a> object.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_array2d</name>
         <file>dlib/is_kind.h</file>
         <spec_file link="true">dlib/is_kind.h</spec_file>
         <description>
            This is a template where is_array2d&lt;T&gt;::value == true when T
            is an <a href="containers.html#array2d">array2d</a> object.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_array</name>
         <file>dlib/is_kind.h</file>
         <spec_file link="true">dlib/is_kind.h</spec_file>
         <description>
            This is a template where is_array&lt;T&gt;::value == true when T
            is an <a href="containers.html#array">array</a> object.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_rand</name>
         <file>dlib/is_kind.h</file>
         <spec_file link="true">dlib/is_kind.h</spec_file>
         <description>
            This is a template where is_rand&lt;T&gt;::value == true when T
            is a <a href="algorithms.html#rand">rand</a> object.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->
      
      <component>
         <name>is_reference_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template where is_reference_type&lt;T&gt;::value == true when T is a reference 
            type and false otherwise.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      <component>
         <name>is_const_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template where is_const_type&lt;T&gt;::value == true when T is a const 
            type and false otherwise.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      
      <component>
         <name>is_pointer_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a template where is_pointer_type&lt;T&gt;::value == true when T is a pointer 
            type and false otherwise.
         </description>
         
      </component>
      
   <!-- ************************************************************************* -->

      <component>
         <name>ASSERT_ARE_NOT_SAME_TYPE</name>
         <file>dlib/assert.h</file>
         <spec_file>dlib/assert.h</spec_file>
         <description>
      <p>
            This is a macro function for debugging.  Its form is <tt>ASSERT_ARE_NOT_SAME_TYPE(type1, type2)</tt>.  
            If type1 and type2 are the same type then the compile will fail.  This is sometimes useful 
            in validating template arguments.
      </p>
         </description>
                                 
      </component>
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>ASSERT_ARE_SAME_TYPE</name>
         <file>dlib/assert.h</file>
         <spec_file>dlib/assert.h</spec_file>
         <description>
      <p>
            This is a macro function for debugging.  Its form is <tt>ASSERT_ARE_SAME_TYPE(type1, type2)</tt>.  
            If type1 and type2 are not the same type then the compile will fail.  This is sometimes useful 
            in validating template arguments.
      </p>
         </description>
                                 
      </component>
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>DLIB_ASSERT_HAS_STANDARD_LAYOUT</name>
         <file>dlib/assert.h</file>
         <spec_file link="true">dlib/assert.h</spec_file>
         <description>
      <p>
        This macro is meant to cause a compiler error if a type doesn't have a simple
        memory layout (like a C struct). In particular, types with simple layouts are
        ones which can be copied via memcpy().
      </p>
        
        This was called a POD type in C++03 and in C++0x we are looking to check if 
        it is a "standard layout type".  Once we can use C++0x we can change this macro 
        to something that uses the std::is_standard_layout type_traits class.  
        See: http://www2.research.att.com/~bs/C++0xFAQ.html#PODs
         </description>
                                 
      </component>
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>COMPILE_TIME_ASSERT</name>
         <file>dlib/assert.h</file>
         <spec_file>dlib/assert.h</spec_file>
         <description>
      <p>
            This is a macro function for debugging.  Its form is <tt>COMPILE_TIME_ASSERT(condition that should 
            be true)</tt>.  The condition must be a compile time constant and if it is false then the compile
            will fail.
      </p>
         </description>
                                 
      </component>
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            <p>
            The DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST() macro is used to define traits templates
            that tell you if a class has a certain member function.  For example, to make a
            test to see if a class has a public method with the signature void print(int) you
            would say:
            </p>
            <blockquote>
               <tt>DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_print, void, print, (int))</tt>
            </blockquote>
            Then you can check if a class, T, has this method by looking at the boolean value:
            <blockquote>
               <tt>has_print&lt;T&gt;::value</tt>
            </blockquote>
            which will be true if the member function is in the T class.

         </description>
                                 
      </component>
            
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>DLIB_CASSERT</name>
         <file>dlib/assert.h</file>
         <spec_file>dlib/assert.h</spec_file>
         <description>
      <p>
         This is a macro function that is identical to the <a href="#DLIB_ASSERT">DLIB_ASSERT</a> macro
         except that it is always enabled.  Even if _DEBUG, DEBUG and ENABLE_ASSERTS are not defined.
      </p>
      <p>
         Note that when this macro fails and throws an exception it also calls the global
         C function dlib_assert_breakpoint().  This behavior makes it easy to set a debugging
         tool to break when DLIB_CASSERT fails by setting a breakpoint on dlib_assert_breakpoint().
      </p>
         </description>
                                 
      </component>
            
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>DLIB_ASSERT</name>
         <file>dlib/assert.h</file>
         <spec_file>dlib/assert.h</spec_file>
         <description>
      <p>
            This is a macro function for debugging.  Its form is 
<code_box>DLIB_ASSERT(condition that should be true, error message)</code_box> 
or you can omit the error message and call it like:
<code_box>DLIB_ASSERT(condition that should be true)</code_box>
            If the condition is false DLIB_ASSERT throws an exception of type 
            dlib::fatal_error with fatal_error::type == EBROKEN_ASSERT.  An error message detailing
            the nature of the problem is stored in the member variable info which is of type std::string.  
            Look in the following file for more details.  The exception classes are defined 
            <a href="dlib/error.h.html#error">here</a>.
      </p>
      <p>
         This macro is only enabled if _DEBUG, DEBUG or ENABLE_ASSERTS is defined.  Also, if this macro <i>is</i>
         enabled then ENABLE_ASSERTS will be defined even if you didn't define it.
      </p>
      <p>
         Note that when this macro fails and throws an exception it also calls the global
         C function dlib_assert_breakpoint().  This behavior makes it easy to set a debugging
         tool to break when DLIB_ASSERT fails by setting a breakpoint on dlib_assert_breakpoint().
      </p>
         </description>
                                 
      </component>
            
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>DLIB_STACK_TRACE</name>
         <file>dlib/assert.h</file>
         <spec_file>dlib/stack_trace.h</spec_file>
         <description>
         <p>
            This is a preprocessor macro that allows you to tag a function so 
            that dlib will keep track of it in a function call stack.  That is,
            you will be able to see a stack trace by calling <a href="#get_stack_trace">get_stack_trace</a>
            if you put this macro at the top of your functions.
         </p>
         <p>
            This macro is only enabled if DLIB_ENABLE_STACK_TRACE is defined.  If it isn't defined then
            this macro doesn't do anything.  Also note that when this macro is defined it will
            cause <a href="#DLIB_ASSERT">DLIB_ASSERT</a> and <a href="#DLIB_CASSERT">DLIB_CASSERT</a>
            to include a stack trace in their error messages.
         </p>
         </description>
                                 
      </component>
            
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>DLIB_STACK_TRACE_NAMED</name>
         <file>dlib/assert.h</file>
         <spec_file>dlib/stack_trace.h</spec_file>
         <description>
            This is a preprocessor macro just like <a href="#DLIB_STACK_TRACE">DLIB_STACK_TRACE</a>
            except that it allows you to supply your own string to use as the function name
            in the stack trace instead of the one deduced by DLIB_STACK_TRACE.
            <p>
               This macro is only enabled if DLIB_ENABLE_STACK_TRACE is defined.  
            </p>
         </description>
                                 
      </component>
            
   <!-- ************************************************************************* -->

      <component>
         <name>get_stack_trace</name>
         <file>dlib/assert.h</file>
         <spec_file>dlib/stack_trace.h</spec_file>
         <description>
            This function allows you to query the current stack trace.
            <p>
               This macro is only enabled if DLIB_ENABLE_STACK_TRACE is defined.  
            </p>
         </description>
                                 
      </component>
            
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>_dT</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>
            This is a macro function for converting a string/character literal to either a char or wchar_t literal. 
   Its form is <tt>_dT(target character type,string or character literal)</tt>
         </description>
                                 
      </component>
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>assign_zero_if_built_in_scalar_type</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>        
            <p>
               This function assigns its argument the value of 0 if it is a built in scalar
               type according to the <a href="#is_built_in_scalar_type">is_built_in_scalar_type</a>
               template.  If it isn't a built in scalar type then it does nothing.
            </p>
            <p>
               This function is useful for suppressing compiler warnings about uninitialized 
               types inside of templates that are designed to accept the built in types
               as well as user defined classes.
            </p>
            
         </description>
                                 
      </component>
            
            
   <!-- ************************************************************************* -->
      
      <component>
         <name>wrap_function</name>
         <file>dlib/algs.h</file>
         <spec_file link="true">dlib/algs.h</spec_file>
         <description>        
            This is a template that allows you to turn a global function into a 
            function object.  See the specs for more details. 
         </description>
                                 
      </component>
            

   <!-- ************************************************************************* -->

      <component>
         <name>portability_macros</name>
         <file>dlib/platform.h</file>
         <spec_file>dlib/platform.h</spec_file>
         <description>        
            This file #defines various macros depending on the platform being compiled under. 
            See the file itself for the specifics.
         </description>
                                 
      </component>
            
         
   <!-- ************************************************************************* -->
      
   </components>

   <!-- ************************************************************************* -->


</doc>

