Saturday, July 15, 2006

After a few frantic nights of debugging the debugger, I think I have a stable version again... (there are a few minor issues still, but no show stopper that I know of). Sheesh.

As I was cleaning up some code, I came upon a few warnings, where signed values are compared against unsigned. The types are well chosen IMHO, for example in one case the unsigned is a memory address, and the signed is a relative offset (which may be negative). The assertions are more informative than anything else. The code is prepared to deal with the case when the assertion fails, I am just curious to see whether my assumption that a given condition should never happen is correct (and it's not the end of the world if it isn't). I would call this a soft assert in case the term is not coined already.

Here's the code I came up with, that silences down the compiler warnings about signed vs. unsigned, and throws an exception rather than calling abort on failure.



#include <assert.h>
#include <limits>
#include <stdexcept>
#include <boost/format.hpp>


class assert_error : public std::logic_error
{
public:
assert_error(const char* file, size_t line)
: std::logic_error((boost::format(
"Assertion failed at: %1%:%2%") % file % line).str())
{ }
};



// Assert functions that avoid the signed vs. unsigned compiler warnings.
namespace detail
{
template<typename T, typename U, int>
struct numeric_assert
{
static bool gt(T lhs, U rhs) { return(lhs > rhs); }
static bool eq(T lhs, U rhs) { return(lhs == rhs); }
};
template<typename T, typename U>
struct numeric_assert<T, U, 1>
{
static bool gt(T lhs, U rhs)
{ return((lhs >= 0) && (static_cast<U>(lhs) > rhs)); }

static bool eq(T lhs, U rhs)
{ return((rhs >= 0) && (rhs == static_cast<U>(lhs))); }
};
template<typename T, typename U>
struct numeric_assert<T, U, -1>
{
static bool gt(T lhs, U rhs)
{ return((rhs < 0) || (lhs > static_cast<T>(rhs))); }

static bool eq(T lhs, U rhs)
{ return((rhs >= 0) && (lhs == static_cast<T>(rhs))); }
};
}
template<typename T, typename U>
void __assert_gt(T lhs, U rhs, const char* file, size_t line)
{
typedef detail::numeric_assert<T, U,
std::numeric_limits<T>::is_signed -
std::numeric_limits<U>::is_signed> Assert;
if (!Assert::gt(lhs, rhs))
{
throw assert_error(file, line);
}
}


template<typename T, typename U>
void __assert_eq(T lhs, U rhs, const char* file, size_t line)
{
typedef detail::numeric_assert<T, U,
std::numeric_limits<T>::is_signed -
std::numeric_limits<U>::is_signed> Assert;
if (!Assert::eq(lhs, rhs))
{
throw assert_error(file, line);
}
}
#define assert_gt(x,y) __assert_gt((x), (y), __FILE__, __LINE__)
#define assert_eq(x,y) __assert_eq((x), (y), __FILE__, __LINE__)




Oh, and the sed command I was talking about the other day is:

sed -e 's/</\&lt;/g; s/>/\&gt;/g'


Specializing the templates for double and float is left as an exercise for the reader.

Monday, July 10, 2006

Oops. Found quite a few issues with the Zero UI. The builds that I posted on my site on July 8th have a few known issues. Nobody has complained yet, though.

Anyway, here's a piece of code that I wrote in the process of fixing things.


/**
* Pick the first available monospace font in a widget's
* Pango context
*/
bool
get_monospace_font(Pango::FontDescription& font, Gtk::Widget& w)
{
bool success = false;

if (Glib::RefPtr<Pango::Context> ctxt = w.get_pango_context())
{
Glib::ArrayHandle<Glib::RefPtr<Pango::FontFamily> > ff =
ctxt->list_families();

Glib::ArrayHandle<Glib::RefPtr<
Pango::FontFamily> >::const_iterator
i = ff.begin(), end = ff.end();

for (; i != end; ++i)
{
if ((*i)->is_monospace())
{
font = Pango::FontDescription((*i)->get_name());

w.ensure_style();
if (Glib::RefPtr <Gtk::Style> style = w.get_style())
{
font.merge(style->get_font(), false);
}
if (Glib::RefPtr<Pango::Font> f = ctxt->load_font(font))
{
font = f->describe();
}
success = true;
break;
}
}
}
return success;
}



Note to self: write a sed script for "fixing" templates and pointers in code snippets posted on this blog.

Second note to self: make a custom T-shirt on Zazzle, printed with said sed script.

Friday, July 07, 2006

ZeroBugs' New Look


I am experimenting with the UI for the Zero debugger (www.zero-bugs.com) The new look and feel allows for several source files / disassembly views. This should alleviate the screen flickering that I have previously experienced with the gtksourceviewmm widget.