Howto:Extend Nasal: Difference between revisions

Jump to navigation Jump to search
m
m (→‎Hash Manipulation API: using a HASHSET macro)
Line 253: Line 253:
  int naMember_cget(naRef obj, const char* field, naRef* out);
  int naMember_cget(naRef obj, const char* field, naRef* out);


A common way to set hash field members easily, is using a simple macro like:
A common way to set up hash field members easily, is using a simple macro like:


<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
#define HASHSET(s,l,n) naHash_set(navdata, naStr_fromdata(naNewString(c),s,l),n)
#define HASHSET(s,l,n) naHash_set(naHash, naStr_fromdata(naNewString(c),s,l),n)
//... do your hash setup here
//... do your hash setup here
#undef HASHSET
#undef HASHSET
Line 262: Line 262:


As you can see, this allows you to easily set up hash fields.
As you can see, this allows you to easily set up hash fields.
This will call the naHash_set() helper, with the target hash being its first argument, the key name coming next and the naRef (n) being last.
The macro can be even further simplified by automatically computing the length of the hash key using strlen() instead of the explicit length argument:
The macro can be even further simplified by automatically computing the length of the hash key using strlen() instead of the explicit length argument:


<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
#define HASHSET(s,n) naHash_set(navdata, naStr_fromdata(naNewString(c),s,strlen(s)),n)
#define HASHSET(s,n) naHash_set(naHash, naStr_fromdata(naNewString(c),s,strlen(s)),n)
//... do your hash setup here
//... do your hash setup here
#undef HASHSET
#undef HASHSET

Navigation menu