Store Entire Namespace

R

Robby

Is it possible to save an entire namespace in some way? For example,
say I have this namespace:

foo.h
==================================================

namespace foo
{
int testVar1;
int testVar2
void testFunc1(void);
void testFunc2(void);
char testVar3[1024];
};

==================================================

And I use it for a while and then want to write the state of the
program to disk. Is there some way to copy all of the namespace
globals, without having to explicitly copy testVar1, testVar2, and
testVar3 - so that the code would not have to be modified if the
namespace is slightly changed?
 
A

anon

Robby said:
program to disk. Is there some way to copy all of the namespace
globals, without having to explicitly copy testVar1, testVar2, and
testVar3 - so that the code would not have to be modified if the
namespace is slightly changed?

No
 
V

Vallabha

{ Quoted clc++m banner removed. -mod }

Is it possible to save an entire namespace in some way? For example,
say I have this namespace:

foo.h
==================================================

namespace foo
{
int testVar1;
int testVar2
void testFunc1(void);
void testFunc2(void);
char testVar3[1024];

};

==================================================

And I use it for a while and then want to write the state of the
program to disk. Is there some way to copy all of the namespace
globals, without having to explicitly copy testVar1, testVar2, and
testVar3 - so that the code would not have to be modified if the
namespace is slightly changed?

See if 'using namespace foo' is of some help to solve your problem.

Cheers
-Vallabha
 
J

Jerry Coffin

Is it possible to save an entire namespace in some way? For example,
say I have this namespace:

[ ... code elided ]
And I use it for a while and then want to write the state of the
program to disk. Is there some way to copy all of the namespace
globals, without having to explicitly copy testVar1, testVar2, and
testVar3 - so that the code would not have to be modified if the
namespace is slightly changed?

No. If you use a class (or struct) instead of a namespace it gets you
closer to the capabilities you want -- it at least allows you to assign
all the variables as a group. OTOH, it still doesn't give the capability
to read/write the variables as a group, so you'd still need to modify
the code when you add/remove a variable.

--
Later,
Jerry.

The universe is a figment of its own imagination.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
V

Victor Bazarov

Jerry said:
Is it possible to save an entire namespace in some way? For example,
say I have this namespace:

[ ... code elided ]
And I use it for a while and then want to write the state of the
program to disk. Is there some way to copy all of the namespace
globals, without having to explicitly copy testVar1, testVar2, and
testVar3 - so that the code would not have to be modified if the
namespace is slightly changed?

No. If you use a class (or struct) instead of a namespace it gets you
closer to the capabilities you want -- it at least allows you to
assign all the variables as a group. OTOH, it still doesn't give the
capability to read/write the variables as a group, so you'd still
need to modify the code when you add/remove a variable.

Uh... Really? So, if those variables are all in a struct, and I use

somestream.write(static_cast<char*>(&myfoo), sizeof(myfoo));

I'd have to do something about it if the contents of the 'foo' type
change?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
R

Robster

Is it possible to save an entire namespace in some way? For example,
say I have this namespace:

foo.h

Short answer: "no."
Slightly longer answer: There are a number of ways to do this using
other constructs: class, struct, and some more creative solutions.
Namespaces were designed to address scoping and organizational issues.
 
N

nonicknameyet

Is it possible to save an entire namespace in some way?

You might want to look into using something like boost::Serialization
(www.boost.org) or
switch to a language featuring reflection, like C#. This wont help in
saving a namespace,
( you cant really) but might solve your problem. HTH

Ken Krovchuck
 
Z

zade

{ Quoted clc++m banner removed. -mod }

Is it possible to save an entire namespace in some way? For example,
say I have this namespace:

foo.h
==================================================

namespace foo
{
int testVar1;
int testVar2
void testFunc1(void);
void testFunc2(void);
char testVar3[1024];

};

==================================================

And I use it for a while and then want to write the state of the
program to disk. Is there some way to copy all of the namespace
globals, without having to explicitly copy testVar1, testVar2, and
testVar3 - so that the code would not have to be modified if the
namespace is slightly changed?

Certainly no.
I think the answer is : the namespace is open , not like struct or
class. Assume you add some variables in other files, how would the
comipler know that?
 
J

Jerry Coffin

Jerry said:
Is it possible to save an entire namespace in some way? For example,
say I have this namespace:

[ ... code elided ]
And I use it for a while and then want to write the state of the
program to disk. Is there some way to copy all of the namespace
globals, without having to explicitly copy testVar1, testVar2, and
testVar3 - so that the code would not have to be modified if the
namespace is slightly changed?

No. If you use a class (or struct) instead of a namespace it gets you
closer to the capabilities you want -- it at least allows you to
assign all the variables as a group. OTOH, it still doesn't give the
capability to read/write the variables as a group, so you'd still
need to modify the code when you add/remove a variable.

Uh... Really?

Yes, really (as you know quite well already, I'm quite certain).
So, if those variables are all in a struct, and I use

somestream.write(static_cast<char*>(&myfoo), sizeof(myfoo));

I'd have to do something about it if the contents of the 'foo' type
change?

That depends -- as long as it came out as a POD struct, this would
probably be at least semi-safe. OTOH, if (for example) one of the
variables was an instance of a class that contained any virtual
functions, this wouldn't normally work, even if the contents of the foo
type remained constant.

--
Later,
Jerry.

The universe is a figment of its own imagination.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
T

Tom Lynch

You might want to look into using something like boost::Serialization
(www.boost.org) or
switch to a language featuring reflection, like C#. This wont help in
saving a namespace,
( you cant really) but might solve your problem. HTH

Probably overkill, but the Fusion 2.0 quick start (see the
documentation for the Spirit project on sourceforge) includes an
example of how to create a type-to-type&value associative map that
allows the serialisation of a heterogeneous type sequence (such as
might be found in the namespace of the OP) via a generic algorithm
that refers to the individual serialisation facility of each type
within the sequence.

This is not the same as "saving a whole namespace" but it is a more
general case creating and maintaining a modifiable static sequence of
types and manipulating it generically at runtime. Disclaimer: I've
never used it.

Tom
 
J

James Kanze

Jerry said:
Is it possible to save an entire namespace in some way? For example,
say I have this namespace:
[ ... code elided ]
And I use it for a while and then want to write the state of the
program to disk. Is there some way to copy all of the namespace
globals, without having to explicitly copy testVar1, testVar2, and
testVar3 - so that the code would not have to be modified if the
namespace is slightly changed?
No. If you use a class (or struct) instead of a namespace it gets you
closer to the capabilities you want -- it at least allows you to
assign all the variables as a group. OTOH, it still doesn't give the
capability to read/write the variables as a group, so you'd still
need to modify the code when you add/remove a variable.
Uh... Really?
Yes, really (as you know quite well already, I'm quite certain).
That depends -- as long as it came out as a POD struct, this would
probably be at least semi-safe.

You'll never have any problem writing it. You just won't
necessarily be able to read it. About the only exception is if
the object contains only arithmetic types (no pointers!) and you
reread from the same program image that did the write.
OTOH, if (for example) one of the
variables was an instance of a class that contained any virtual
functions, this wouldn't normally work, even if the contents of the foo
type remained constant.

You don't even need virtual functions. Any indirection will do
the trick. And of course, the size and placement of padding can
vary depending on the compiler options and version; in one case,
I even had the byte order of a long change from one version of
the compiler to the next.
 
J

Jerry Coffin

[ ... ]
You don't even need virtual functions. Any indirection will do
the trick.

Oh, absolutely -- it's just that (I'd think) most people would probably
notice the problem more quickly when there are explicit pointers
involved, whereas a class with a virtual function makes the pointer
considerably less obvious.
And of course, the size and placement of padding can
vary depending on the compiler options and version; in one case,
I even had the byte order of a long change from one version of
the compiler to the next.

Right -- and even then, you don't get any guarantee, just a strong
likelihood. In particular, a fair number of compilers allow you to vary
the padding with something like a #pragma so you can change it even
between different parts of the same source file. If, for example, you
put your struct definition in a header and included it into two separate
source files for reading and writing respectively, it's possible (albeit
unusual and probably unintentional) that you could have different
padding in effect for each, so reading and writing wouldn't match, even
within the same image.

--
Later,
Jerry.

The universe is a figment of its own imagination.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,539
Members
48,274
Latest member
HowardKipp

Latest Threads

Top