Two small infrastructure extensions for passing information into the

logging framework.

- To enable passing a type into a bif, there's now a new
  BroType-derived class TypeType and a corresponding TYPE_TYPE tag.
  With that, a Val can now have a type as its value.

  This is experimental for now.

- RecordVal's get a new method CoerceTo() to coerce their value into a
  another record type with the usual semantics. Most of the code in
  there was previously in RecordContructorExpr::InitVal(), which is
  now calling the new CoerceTo() method.
This commit is contained in:
Robin Sommer 2011-02-18 13:01:34 -08:00
parent ffa494e428
commit 9d407d882c
5 changed files with 119 additions and 40 deletions

View file

@ -39,6 +39,7 @@ class TableVal;
class RecordVal;
class ListVal;
class StringVal;
class EnumVal;
class MutableVal;
class StateAccess;
@ -163,6 +164,15 @@ public:
// class has ref'd it.
Val(BroFile* f);
Val(BroType* t, bool type_type) // Extra arg to differentiate from protected version.
{
type = new TypeType(t->Ref());
attribs = 0;
#ifdef DEBUG
bound_id = 0;
#endif
}
Val()
{
val.int_val = 0;
@ -245,6 +255,12 @@ public:
return &val.subnet_val;
}
const BroType* AsType() const
{
CHECK_TAG(type->Tag(), TYPE_TYPE, "Val::Type", type_name)
return type;
}
// ... in network byte order
const addr_type AsAddr() const
{
@ -295,6 +311,7 @@ public:
CONVERTER(TYPE_LIST, ListVal*, AsListVal)
CONVERTER(TYPE_STRING, StringVal*, AsStringVal)
CONVERTER(TYPE_VECTOR, VectorVal*, AsVectorVal)
CONVERTER(TYPE_ENUM, EnumVal*, AsEnumVal)
#define CONST_CONVERTER(tag, ctype, name) \
const ctype name() const \
@ -921,6 +938,15 @@ public:
void SetOrigin(BroObj* o) { origin = o; }
BroObj* GetOrigin() const { return origin; }
// Returns a new value representing the value coerced to the given type.
// If coercion is not possible, returns 0. The non-const version may
// return the current value ref'ed if its type matches directly.
//
// *aggr* is optional; if non-zero, we add to it. See Expr::InitVal(). We
// leave it out in the non-const version to make the choice unambigious.
RecordVal* CoerceTo(const RecordType* other, Val* aggr) const;
RecordVal* CoerceTo(RecordType* other);
unsigned int MemoryAllocation() const;
protected: