Boost logo

Boost :

From: Dan W. (danw_at_[hidden])
Date: 2004-01-03 16:30:10


Allright, how about something like this?
Overly simple, perhaps, but it seems to work.

---------------------------------------
//file: ensure.hpp
#ifndef ENSURE_HPP
#define ENSURE_HPP

#ifdef _DEBUG

//ensure you include "ensure.cpp" in your project

namespace boost { namespace DBC {

struct ensure
{
    ensure(){}
    static void stop_execution();
};
                 } }

template< typename bool_expr > inline
void operator,
(
    boost::DBC::ensure const & _,
    bool_expr const & x
)
{
    if( !x )
       boost::DBC::ensure::stop_execution();
}

# define ensure_ boost::DBC::ensure(),

#else //release mode:
# define COMMENT_MACRO ADDSLASH_MACRO(/)
# define ADDSLASH_MACRO(s) /##s
# define ensure_ COMMENT_MACRO
#endif

/*

   // Usage example:

   #include <string.h>
   #include "ensure.h"

   char first_char( char const * s )
   {
      ensure_ s && ::strlen(s);
      return s[0];
   }

   // Cheers!

*/

#endif
---------------------------------------
//file: ensure.cpp

#ifdef _DEBUG

# include "ensure.hpp"
# include <iostream>
# include <windows.h>

#undef ensure:

namespace boost { namespace DBC {

using std::cerr;

void ensure::stop_execution()
{
    cerr
    << " BOOST DESIGN-BY-CONTRACT - 2004\n"
    << "Execution halted by boost::DBC::ensure()\n"
    << "Go up one level in the stack to see the\n"
    << "\"ensure: <expr>;\" that sent us here...\n";
    ::DebugBreak();
}
                 } }
#endif
---------------------------------------

Donno how to place breaks in other platforms.
I'm sure it could use compile time MPL::IF, just
haven't looked into it yet.
Is there already some library in boost for portable development
utilities, like cross-platform in-code break-points, library linkage
directives, etc.? A way to enfore inclusion of a cpp file from a header
file would be nice.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk