www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - #define trouble

reply "dnewbie" <run3 myopera.com> writes:
I have the following C struct from ldap.h

typedef struct ldapmod {
	int	mod_op;
	char *mod_type;
	union mod_vals_u {
		char **modv_strvals;
		struct berval **modv_bvals;
	} mod_vals;
#define mod_values	mod_vals.modv_strvals
#define mod_bvalues	mod_vals.modv_bvals
} LDAPMod;

It is used like this:
LDAPMod title;
title.mod_values = x;

I wonder how can I write the line 'title.mod_values = x;' in D.
Currently I do like this:
	
struct ldapmod {
	int	mod_op;
	char* mod_type;
	union mod_vals_u {
		char**  modv_strvals;
		berval** modv_bvals;
	}
	mod_vals_u mod_vals;
}
alias ldapmod LDAPMod;

LDAPMod title;
title.mod_vals.modv_strvals = x;
Nov 26 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/26/2012 10:05 PM, dnewbie wrote:
 I have the following C struct from ldap.h

 typedef struct ldapmod {
 int mod_op;
 char *mod_type;
 union mod_vals_u {
 char **modv_strvals;
 struct berval **modv_bvals;
 } mod_vals;
 #define mod_values mod_vals.modv_strvals
 #define mod_bvalues mod_vals.modv_bvals
 } LDAPMod;

 It is used like this:
 LDAPMod title;
 title.mod_values = x;

 I wonder how can I write the line 'title.mod_values = x;' in D.
 Currently I do like this:

 struct ldapmod {
 int mod_op;
 char* mod_type;
 union mod_vals_u {
 char** modv_strvals;
 berval** modv_bvals;
 }
 mod_vals_u mod_vals;
 }
 alias ldapmod LDAPMod;

 LDAPMod title;
 title.mod_vals.modv_strvals = x;
A pair of property functions would work, one of which may be unnecessary: alias int berval; struct ldapmod { int mod_op; char* mod_type; union mod_vals_u { char** modv_strvals; berval** modv_bvals; } mod_vals_u mod_vals; char** mod_values() property { return mod_vals.modv_strvals; } void mod_values(char** value) property { mod_vals.modv_strvals = value; } // similarly for mod_bvalues... } alias ldapmod LDAPMod; void main() { LDAPMod title; char** x; // title.mod_vals.modv_strvals = x; title.mod_values = x; } Ali
Nov 26 2012
parent "dnewbie" <run3 myopera.com> writes:
On Tuesday, 27 November 2012 at 06:27:49 UTC, Ali Çehreli wrote:
 On 11/26/2012 10:05 PM, dnewbie wrote:
 I have the following C struct from ldap.h

 typedef struct ldapmod {
 int mod_op;
 char *mod_type;
 union mod_vals_u {
 char **modv_strvals;
 struct berval **modv_bvals;
 } mod_vals;
 #define mod_values mod_vals.modv_strvals
 #define mod_bvalues mod_vals.modv_bvals
 } LDAPMod;

 It is used like this:
 LDAPMod title;
 title.mod_values = x;

 I wonder how can I write the line 'title.mod_values = x;' in D.
 Currently I do like this:

 struct ldapmod {
 int mod_op;
 char* mod_type;
 union mod_vals_u {
 char** modv_strvals;
 berval** modv_bvals;
 }
 mod_vals_u mod_vals;
 }
 alias ldapmod LDAPMod;

 LDAPMod title;
 title.mod_vals.modv_strvals = x;
A pair of property functions would work, one of which may be unnecessary: alias int berval; struct ldapmod { int mod_op; char* mod_type; union mod_vals_u { char** modv_strvals; berval** modv_bvals; } mod_vals_u mod_vals; char** mod_values() property { return mod_vals.modv_strvals; } void mod_values(char** value) property { mod_vals.modv_strvals = value; } // similarly for mod_bvalues... } alias ldapmod LDAPMod; void main() { LDAPMod title; char** x; // title.mod_vals.modv_strvals = x; title.mod_values = x; } Ali
Perfect! Thank you.
Nov 27 2012