Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Getting some namespace features in C with structs (http://www.velocityreviews.com/forums/t960323-getting-some-namespace-features-in-c-with-structs.html)

James Harris 05-03-2013 08:51 AM

Getting some namespace features in C with structs
 
One thing I like about some other (non-C) languages is their
management of names so I got to wondering: Would it be a good or a bad
idea to use C's structs to provide some of the features of namespaces?

Unless the idea is a total non-starter are there *any* ways in which
it could usefully be used? The more I think about it the more options
come to mind but maybe I am not familiar enough with C to see why I
have never come across C programs which work that way.

James

Ian Collins 05-03-2013 09:13 AM

Re: Getting some namespace features in C with structs
 
James Harris wrote:
> One thing I like about some other (non-C) languages is their
> management of names so I got to wondering: Would it be a good or a bad
> idea to use C's structs to provide some of the features of namespaces?
>
> Unless the idea is a total non-starter are there *any* ways in which
> it could usefully be used? The more I think about it the more options
> come to mind but maybe I am not familiar enough with C to see why I
> have never come across C programs which work that way.


C lacks scoping syntax, so you can only group items with an instance of
a struct wither than the struct type. For example in C++ you can write

struct X
{
static int n;
};

and you can refer to "X::n" without having to have an instance of X.
Thus C++ can (and did once upon a time) use structs as simple
namespaces. The main limitation was you had to put everything within
one struct declaration, hence one header.

--
Ian Collins

Malcolm McLean 05-03-2013 10:42 AM

Re: Getting some namespace features in C with structs
 
On Friday, May 3, 2013 9:51:15 AM UTC+1, James Harris wrote:
> One thing I like about some other (non-C) languages is their
> management of names so I got to wondering: Would it be a good or a bad
> idea to use C's structs to provide some of the features of namespaces?
>
>
> Unless the idea is a total non-starter are there *any* ways in which
> it could usefully be used? The more I think about it the more options
> come to mind but maybe I am not familiar enough with C to see why I
> have never come across C programs which work that way.
>
>

One trick to is declare a struct "globals". Then you know where your global
variable are. Also, you can convert any function from using globals to
being a parameterised function by adding a GLOBALS * and mechanically going
through converting all the dots to arrows.

If you merge two programs then, as you say, you effectively have namespaces.



Paul N 05-03-2013 12:37 PM

Re: Getting some namespace features in C with structs
 
On May 3, 9:51*am, James Harris <james.harri...@gmail.com> wrote:
> One thing I like about some other (non-C) languages is their
> management of names so I got to wondering: Would it be a good or a bad
> idea to use C's structs to provide some of the features of namespaces?
>
> Unless the idea is a total non-starter are there *any* ways in which
> it could usefully be used? The more I think about it the more options
> come to mind but maybe I am not familiar enough with C to see why I
> have never come across C programs which work that way.


Wouldn't it be easier to use #defines? For instance,

#define name mynamespace_name

?

Keith Thompson 05-03-2013 03:05 PM

Re: Getting some namespace features in C with structs
 
James Harris <james.harris.1@gmail.com> writes:
> One thing I like about some other (non-C) languages is their
> management of names so I got to wondering: Would it be a good or a bad
> idea to use C's structs to provide some of the features of namespaces?
>
> Unless the idea is a total non-starter are there *any* ways in which
> it could usefully be used? The more I think about it the more options
> come to mind but maybe I am not familiar enough with C to see why I
> have never come across C programs which work that way.


You can group object definitions within a struct; if there's only one
instance of the struct, it can act like a namespace:

struct {
int this;
char *that;
double the_other_thing;
} pseudo_namespace;

But you can't initialize them ("int this = 42;"), and you can't put
other declarations within a struct (typedefs, enum types, other structs,
functions, etc.).

You can have pointer-to-function members, but you have to initialize
them explicitly to point to the functions you want.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

James Harris 05-04-2013 08:31 AM

Re: Getting some namespace features in C with structs
 
On May 3, 1:37*pm, Paul N <gw7...@aol.com> wrote:
> On May 3, 9:51*am, James Harris <james.harri...@gmail.com> wrote:
>
> > One thing I like about some other (non-C) languages is their
> > management of names so I got to wondering: Would it be a good or a bad
> > idea to use C's structs to provide some of the features of namespaces?

>
> > Unless the idea is a total non-starter are there *any* ways in which
> > it could usefully be used? The more I think about it the more options
> > come to mind but maybe I am not familiar enough with C to see why I
> > have never come across C programs which work that way.

>
> Wouldn't it be easier to use #defines? For instance,
>
> #define name mynamespace_name
>
> ?


That seems traditional but deals only with named constants and not
other names.

How would you avoid the potential for collision of the name prefix?
Modules published by two independent people could use the same prefix
AFAICS.

James

James Harris 05-04-2013 08:38 AM

Re: Getting some namespace features in C with structs
 
On May 3, 4:05*pm, Keith Thompson <ks...@mib.org> wrote:
> James Harris <james.harri...@gmail.com> writes:
> > One thing I like about some other (non-C) languages is their
> > management of names so I got to wondering: Would it be a good or a bad
> > idea to use C's structs to provide some of the features of namespaces?

>
> > Unless the idea is a total non-starter are there *any* ways in which
> > it could usefully be used? The more I think about it the more options
> > come to mind but maybe I am not familiar enough with C to see why I
> > have never come across C programs which work that way.

>
> You can group object definitions within a struct; if there's only one
> instance of the struct, it can act like a namespace:
>
> * * struct {
> * * * * int this;
> * * * * char *that;
> * * * * double the_other_thing;
> * * } pseudo_namespace;
>
> But you can't initialize them ("int this = 42;"), and you can't put
> other declarations within a struct (typedefs, enum types, other structs,
> functions, etc.).


For manifest constants couldn't they be initialised by assigning as in

... pseudo_namespace = {...};

Other types could be 'manually' initialised as in

pseudo_namespace.the_other_thing = 4.25;

Of course, I would choose shorter names!

Once initialised I would hope that the compiler would be smart enough
in its constant propagation to recognise that where the code later
refers to the initialised value that it is a constant.

> You can have pointer-to-function members, but you have to initialize
> them explicitly to point to the functions you want.


That sounds OK. Does that mean that functions could subsequently be
called with such as the following?

namespace.init();
namespace.process(namespace.CONSTANT);

James

James Harris 05-04-2013 08:53 AM

Re: Getting some namespace features in C with structs
 
On May 3, 4:05*pm, Keith Thompson <ks...@mib.org> wrote:
> James Harris <james.harri...@gmail.com> writes:
> > One thing I like about some other (non-C) languages is their
> > management of names so I got to wondering: Would it be a good or a bad
> > idea to use C's structs to provide some of the features of namespaces?

>
> > Unless the idea is a total non-starter are there *any* ways in which
> > it could usefully be used? The more I think about it the more options
> > come to mind but maybe I am not familiar enough with C to see why I
> > have never come across C programs which work that way.

>
> You can group object definitions within a struct; if there's only one
> instance of the struct, it can act like a namespace:


Just picking up on the "one instance" part of your comment I wonder if
multiple instances could be used by passing a pointer to the requisite
instance to a function.

For example, say there are two units which are functionally identical
but exist at different addresses how about

#import foreign struct-defining header
dev0 = {...}; /* Set up addresses etc for this device */
dev1 = {...}; /* Set up addresses etc for this device */
x = dev.poll(&dev0);
y = dev.poll($dev1);

Then in the entirely separate device management module

#import own struct-defining header
int poll(struct dev *d)
{
return port_read(d->STATUS_REG);
}

The example cannot use d.STATUS_REG but d->STATUS_REG is as good.

Anyone know of a more reasonable way to do the same?

James

Jorgen Grahn 05-07-2013 09:36 AM

Re: Getting some namespace features in C with structs
 
On Fri, 2013-05-03, Malcolm McLean wrote:
> On Friday, May 3, 2013 9:51:15 AM UTC+1, James Harris wrote:
>> One thing I like about some other (non-C) languages is their
>> management of names so I got to wondering: Would it be a good or a bad
>> idea to use C's structs to provide some of the features of namespaces?
>>
>>
>> Unless the idea is a total non-starter are there *any* ways in which
>> it could usefully be used? The more I think about it the more options
>> come to mind but maybe I am not familiar enough with C to see why I
>> have never come across C programs which work that way.
>>
>>

> One trick to is declare a struct "globals". Then you know where your global
> variable are. [...]
> If you merge two programs then, as you say, you effectively have namespaces.


I do that rather often. In general, I find structs to organize data
like that is underused in much of the source code I come across. I
may combine a whole bunch of global variables or arrays and next
structs to get some, well, structure:

static int rxBytes[30];
static int rxDatagrams[30];
static int txBytes[30];
static int txDatagrams[30];

/* turns into */

static struct {
struct {
struct {
int bytes;
int datagrams;
} rx;
struct {
int bytes;
int datagrams;
} tx;
} statistics[30];
} global;

I.e. global.statistics[5].rx.bytes = foo instead of rxBytes[5] = foo.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .


All times are GMT. The time now is 05:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.