FAQ

svn commit: r835176 - in /lucene/lucy/trunk: boilerplater/lib/Boilerplater/ core/Lucy/Object/ core/Lucy/Test/Object/ perl/xs/Lucy/Object/

Marvin
Nov 12, 2009 at 12:42 am
Author: marvin
Date: Thu Nov 12 00:42:20 2009
New Revision: 835176

URL: http://svn.apache.org/viewvc?rev=835176&view=rev
Log:
Add DOWNCAST macro, which validates class ancestry but passes NULL through.
Rename ASSERT_IS_A macro to CERTIFY, which is like DOWNCAST, but throws an
exception if the object is NULL.

Modified:
lucene/lucy/trunk/boilerplater/lib/Boilerplater/Dumpable.pm
lucene/lucy/trunk/core/Lucy/Object/BitVector.c
lucene/lucy/trunk/core/Lucy/Object/ByteBuf.c
lucene/lucy/trunk/core/Lucy/Object/CharBuf.c
lucene/lucy/trunk/core/Lucy/Object/Err.bp
lucene/lucy/trunk/core/Lucy/Object/Err.c
lucene/lucy/trunk/core/Lucy/Object/Hash.c
lucene/lucy/trunk/core/Lucy/Object/Num.c
lucene/lucy/trunk/core/Lucy/Object/VArray.c
lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c
lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c

Modified: lucene/lucy/trunk/boilerplater/lib/Boilerplater/Dumpable.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/boilerplater/lib/Boilerplater/Dumpable.pm?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/boilerplater/lib/Boilerplater/Dumpable.pm (original)
+++ lucene/lucy/trunk/boilerplater/lib/Boilerplater/Dumpable.pm Thu Nov 12 00:42:20 2009
@@ -167,7 +167,7 @@
lucy_Obj*
$full_func_sym($full_struct *self, lucy_Obj *dump)
{
- lucy_Hash *source = (lucy_Hash*)LUCY_ASSERT_IS_A(dump, LUCY_HASH);
+ lucy_Hash *source = (lucy_Hash*)LUCY_CERTIFY(dump, LUCY_HASH);
$full_struct *loaded
= ($full_struct*)$super_load(($super_type*)self, dump);
CHY_UNUSED_VAR(self);
@@ -179,8 +179,8 @@
lucy_Obj*
$full_func_sym($full_struct *self, lucy_Obj *dump)
{
- lucy_Hash *source = (lucy_Hash*)LUCY_ASSERT_IS_A(dump, LUCY_HASH);
- lucy_CharBuf *class_name = (lucy_CharBuf*)LUCY_ASSERT_IS_A(
+ lucy_Hash *source = (lucy_Hash*)LUCY_CERTIFY(dump, LUCY_HASH);
+ lucy_CharBuf *class_name = (lucy_CharBuf*)LUCY_CERTIFY(
Lucy_Hash_Fetch_Str(source, "_class", 6), LUCY_CHARBUF);
lucy_VTable *vtable = lucy_VTable_singleton(class_name, NULL);
$full_struct *loaded = ($full_struct*)Lucy_VTable_Make_Obj(vtable);
@@ -210,7 +210,7 @@
= $type->is_integer ? qq|($type_str)Lucy_Obj_To_I64(var)|
: $type->is_floating ? qq|($type_str)Lucy_Obj_To_F64(var)|
: $type->is_object
- ? qq|($struct_sym*)LUCY_ASSERT_IS_A(Lucy_Obj_Load(var, var), $vtable_var)|
+ ? qq|($struct_sym*)LUCY_CERTIFY(Lucy_Obj_Load(var, var), $vtable_var)|
: confess( "Don't know how to load " . $type->get_specifier );
return <<END_STUFF;
{

Modified: lucene/lucy/trunk/core/Lucy/Object/BitVector.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/BitVector.c?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/BitVector.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/BitVector.c Thu Nov 12 00:42:20 2009
@@ -101,7 +101,7 @@
void
BitVec_mimic(BitVector *self, Obj *other)
{
- BitVector *evil_twin = (BitVector*)ASSERT_IS_A(other, BITVECTOR);
+ BitVector *evil_twin = (BitVector*)CERTIFY(other, BITVECTOR);
const u32_t my_byte_size = (u32_t)ceil(self->cap / 8.0);
const u32_t evil_twin_byte_size = (u32_t)ceil(evil_twin->cap / 8.0);
if (my_byte_size > evil_twin_byte_size) {

Modified: lucene/lucy/trunk/core/Lucy/Object/ByteBuf.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/ByteBuf.c?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/ByteBuf.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/ByteBuf.c Thu Nov 12 00:42:20 2009
@@ -143,7 +143,7 @@
void
BB_mimic(ByteBuf *self, Obj *other)
{
- ByteBuf *evil_twin = (ByteBuf*)ASSERT_IS_A(other, BYTEBUF);
+ ByteBuf *evil_twin = (ByteBuf*)CERTIFY(other, BYTEBUF);
SI_mimic_bytes(self, evil_twin->buf, evil_twin->size);
}


Modified: lucene/lucy/trunk/core/Lucy/Object/CharBuf.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/CharBuf.c?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/CharBuf.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/CharBuf.c Thu Nov 12 00:42:20 2009
@@ -450,7 +450,7 @@
CharBuf*
CB_load(CharBuf *self, Obj *dump)
{
- CharBuf *source = (CharBuf*)ASSERT_IS_A(dump, CHARBUF);
+ CharBuf *source = (CharBuf*)CERTIFY(dump, CHARBUF);
UNUSED_VAR(self);
return CB_Clone(source);
}
@@ -469,7 +469,7 @@
void
CB_mimic(CharBuf *self, Obj *other)
{
- CharBuf *evil_twin = (CharBuf*)ASSERT_IS_A(other, CHARBUF);
+ CharBuf *evil_twin = (CharBuf*)CERTIFY(other, CHARBUF);
SI_maybe_grow(self, evil_twin->size);
memmove(self->ptr, evil_twin->ptr, evil_twin->size);
self->size = evil_twin->size;

Modified: lucene/lucy/trunk/core/Lucy/Object/Err.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/Err.bp?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/Err.bp (original)
+++ lucene/lucy/trunk/core/Lucy/Object/Err.bp Thu Nov 12 00:42:20 2009
@@ -108,15 +108,24 @@
make_mess(const char *file, int line, const char *func,
const char *pattern, ...);

- /** Test that <code>obj</code> belongs to the class represented by
- * <code>vtable</code>, and throw an error if it's not.
+ /** Verify that <code>obj</code> is either NULL or inherits from
+ * the class represented by <code>vtable</code>.
*
* @return the object.
*/
inert Obj*
- assert_is_a(Obj *obj, VTable *vtable, const char *file, int line,
+ downcast(Obj *obj, VTable *vtable, const char *file, int line,
const char *func);

+ /** Verify that <code>obj</code> is not NULL and inherits from the class
+ * represented by <code>vtable</code>.
+ *
+ * @return the object.
+ */
+ inert Obj*
+ certify(Obj *obj, VTable *vtable, const char *file, int line,
+ const char *func);
+
/** Verify that an object belongs to a subclass and not an abstract class.
*/
inert inline void
@@ -169,8 +178,13 @@
LUCY_MAKE_MESS(char* format, ...);
#endif

-#define LUCY_ASSERT_IS_A(_obj, _vtable) \
- lucy_Err_assert_is_a((lucy_Obj*)_obj, (_vtable), \
+#define LUCY_DOWNCAST(_obj, _vtable) \
+ lucy_Err_downcast((lucy_Obj*)(_obj), (_vtable), \
+ __FILE__, __LINE__, LUCY_ERR_FUNC_MACRO)
+
+
+#define LUCY_CERTIFY(_obj, _vtable) \
+ lucy_Err_certify((lucy_Obj*)(_obj), (_vtable), \
__FILE__, __LINE__, LUCY_ERR_FUNC_MACRO)

static CHY_INLINE void
@@ -195,7 +209,8 @@
#define MAKE_MESS LUCY_MAKE_MESS
#define ERR_ADD_FRAME LUCY_ERR_ADD_FRAME
#define ERR_FUNC_MACRO LUCY_ERR_FUNC_MACRO
- #define ASSERT_IS_A LUCY_ASSERT_IS_A
+ #define DOWNCAST LUCY_DOWNCAST
+ #define CERTIFY LUCY_CERTIFY
#define ABSTRACT_CLASS_CHECK LUCY_ABSTRACT_CLASS_CHECK
#endif
__END_C__

Modified: lucene/lucy/trunk/core/Lucy/Object/Err.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/Err.c?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/Err.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/Err.c Thu Nov 12 00:42:20 2009
@@ -59,8 +59,8 @@
{
va_list args;
Err_make_t make
- = (Err_make_t)METHOD(ASSERT_IS_A(vtable, VTABLE), Err, Make);
- Err *err = ASSERT_IS_A(make(NULL), ERR);
+ = (Err_make_t)METHOD(CERTIVY(vtable, VTABLE), Err, Make);
+ Err *err = (Err*)CERTIFY(make(NULL), ERR);
CharBuf *mess = Err_Get_Mess(err);

va_start(args, pattern);
@@ -165,8 +165,8 @@
{
va_list args;
Err_make_t make
- = (Err_make_t)METHOD(ASSERT_IS_A(vtable, VTABLE), Err, Make);
- Err *err = (Err*)ASSERT_IS_A(make(NULL), ERR);
+ = (Err_make_t)METHOD(CERTIFY(vtable, VTABLE), Err, Make);
+ Err *err = (Err*)CERTIFY(make(NULL), ERR);
CharBuf *mess = Err_Get_Mess(err);

va_start(args, pattern);
@@ -192,7 +192,18 @@
}

Obj*
-lucy_Err_assert_is_a(Obj *obj, VTable *vtable, const char *file, int line,
+lucy_Err_downcast(Obj *obj, VTable *vtable, const char *file, int line,
+ const char *func)
+{
+ if (obj && !SI_obj_is_a(obj, vtable)) {
+ Err_throw_at(ERR, file, line, func, "Can't downcast from %o to %o",
+ Obj_Get_Class_Name(obj), VTable_Get_Name(vtable));
+ }
+ return obj;
+}
+
+Obj*
+lucy_Err_certify(Obj *obj, VTable *vtable, const char *file, int line,
const char *func)
{
if (!obj) {
@@ -200,8 +211,8 @@
VTable_Get_Name(vtable));
}
else if ( !SI_obj_is_a(obj, vtable) ) {
- Err_throw_at(ERR, file, line, func, "Object isn't a %o, it's a %o",
- VTable_Get_Name(vtable), Obj_Get_Class_Name(obj));
+ Err_throw_at(ERR, file, line, func, "Can't downcast from %o to %o",
+ Obj_Get_Class_Name(obj), VTable_Get_Name(vtable));
}
return obj;
}

Modified: lucene/lucy/trunk/core/Lucy/Object/Hash.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/Hash.c?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/Hash.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/Hash.c Thu Nov 12 00:42:20 2009
@@ -96,7 +96,7 @@
while (Hash_Iter_Next(self, &key, &value)) {
/* Since JSON only supports text hash keys, Dump() can only support
* text hash keys. */
- ASSERT_IS_A(key, CHARBUF);
+ CERTIFY(key, CHARBUF);
Hash_Store(dump, key, Obj_Dump(value));
}

@@ -106,7 +106,7 @@
Obj*
Hash_load(Hash *self, Obj *dump)
{
- Hash *source = (Hash*)ASSERT_IS_A(dump, HASH);
+ Hash *source = (Hash*)CERTIFY(dump, HASH);
CharBuf *class_name = (CharBuf*)Hash_Fetch_Str(source, "_class", 6);
UNUSED_VAR(self);


Modified: lucene/lucy/trunk/core/Lucy/Object/Num.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/Num.c?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/Num.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/Num.c Thu Nov 12 00:42:20 2009
@@ -34,7 +34,7 @@
i32_t
Num_compare_to(Num *self, Obj *other)
{
- Num *evil_twin = (Num*)ASSERT_IS_A(other, NUM);
+ Num *evil_twin = (Num*)CERTIFY(other, NUM);
double f64_diff = Num_To_F64(self) - Num_To_F64(evil_twin);
if (f64_diff) {
if (f64_diff < 0) { return -1; }

Modified: lucene/lucy/trunk/core/Lucy/Object/VArray.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/VArray.c?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/VArray.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/VArray.c Thu Nov 12 00:42:20 2009
@@ -70,7 +70,7 @@
VArray*
VA_load(VArray *self, Obj *dump)
{
- VArray *source = (VArray*)ASSERT_IS_A(dump, VARRAY);
+ VArray *source = (VArray*)CERTIFY(dump, VARRAY);
VArray *loaded = VA_new(source->size);
u32_t i, max;
UNUSED_VAR(self);

Modified: lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c (original)
+++ lucene/lucy/trunk/core/Lucy/Test/Object/TestVArray.c Thu Nov 12 00:42:20 2009
@@ -58,7 +58,7 @@
ASSERT_TRUE(batch, VA_Fetch(array, 2) == NULL, "Fetch beyond end");

VA_Store(array, 2, (Obj*)CB_newf("foo"));
- elem = (CharBuf*)ASSERT_IS_A(VA_Fetch(array, 2), CHARBUF);
+ elem = (CharBuf*)CERTIFY(VA_Fetch(array, 2), CHARBUF);
ASSERT_INT_EQ(batch, 3, VA_Get_Size(array), "Store updates size");
ASSERT_TRUE(batch, CB_Equals_Str(elem, "foo", 3), "Store");

@@ -69,7 +69,7 @@
ASSERT_INT_EQ(batch, 1, CB_Get_RefCount(elem),
"Displacing elem via Store updates refcount");
DECREF(elem);
- elem = (CharBuf*)ASSERT_IS_A(VA_Fetch(array, 2), CHARBUF);
+ elem = (CharBuf*)CERTIFY(VA_Fetch(array, 2), CHARBUF);
ASSERT_TRUE(batch, CB_Equals_Str(elem, "bar", 3), "Store displacement");

DECREF(array);
@@ -87,20 +87,20 @@
VA_Push(array, (Obj*)CB_newf("c"));

ASSERT_INT_EQ(batch, VA_Get_Size(array), 3, "size after Push");
- ASSERT_TRUE(batch, NULL != ASSERT_IS_A(VA_Fetch(array, 2), CHARBUF), "Push");
+ ASSERT_TRUE(batch, NULL != DOWNCAST(VA_Fetch(array, 2), CHARBUF), "Push");

- elem = (CharBuf*)ASSERT_IS_A(VA_Shift(array), CHARBUF);
+ elem = (CharBuf*)CERTIFY(VA_Shift(array), CHARBUF);
ASSERT_TRUE(batch, CB_Equals_Str(elem, "a", 1), "Shift");
ASSERT_INT_EQ(batch, VA_Get_Size(array), 2, "size after Shift");
DECREF(elem);

- elem = (CharBuf*)ASSERT_IS_A(VA_Pop(array), CHARBUF);
+ elem = (CharBuf*)CERTIFY(VA_Pop(array), CHARBUF);
ASSERT_TRUE(batch, CB_Equals_Str(elem, "c", 1), "Pop");
ASSERT_INT_EQ(batch, VA_Get_Size(array), 1, "size after Pop");
DECREF(elem);

VA_Unshift(array, (Obj*)CB_newf("foo"));
- elem = (CharBuf*)ASSERT_IS_A(VA_Fetch(array, 0), CHARBUF);
+ elem = (CharBuf*)CERTIFY(VA_Fetch(array, 0), CHARBUF);
ASSERT_TRUE(batch, CB_Equals_Str(elem, "foo", 3), "Unshift");
ASSERT_INT_EQ(batch, VA_Get_Size(array), 2, "size after Shift");


Modified: lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c?rev=835176&r1=835175&r2=835176&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c (original)
+++ lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c Thu Nov 12 00:42:20 2009
@@ -48,8 +48,8 @@
lucy_Err_throw_mess(lucy_VTable *vtable, lucy_CharBuf *message)
{
lucy_Err_make_t make = (lucy_Err_make_t)LUCY_METHOD(
- LUCY_ASSERT_IS_A(vtable, LUCY_VTABLE), Err, Make);
- lucy_Err *err = (lucy_Err*)LUCY_ASSERT_IS_A(make(NULL), LUCY_ERR);
+ LUCY_CERTIFY(vtable, LUCY_VTABLE), Err, Make);
+ lucy_Err *err = (lucy_Err*)LUCY_CERTIFY(make(NULL), LUCY_ERR);
Lucy_Err_Cat_Mess(err, message);
LUCY_DECREF(message);
lucy_Err_do_throw(err);
reply

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post

1 user in discussion

Marvin: 1 post