469,908 Members | 2,131 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,908 developers. It's quick & easy.

Store Entire Namespace

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 http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Apr 23 '07 #1
12 1333
Robby wrote:
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

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

Apr 23 '07 #2
In article <11*********************@p77g2000hsh.googlegroups. com>,
rs******@gmail.com says...
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! ]

Apr 23 '07 #3
{ Quoted clc++m banner removed. -mod }

On Apr 23, 10:43 am, Robby <rsimp...@gmail.comwrote:
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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Apr 23 '07 #4
Geo
On 23 Apr, 15:18, Vallabha <vsnadago...@gmail.comwrote:
>
See if 'using namespace foo' is of some help to solve your problem.
What possible help do you imagine that would be ? None, perhaps ?
Cheers
-Vallabha


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

Apr 23 '07 #5
Jerry Coffin wrote:
In article <11*********************@p77g2000hsh.googlegroups. com>,
rs******@gmail.com says...
>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! ]

Apr 23 '07 #6
On Apr 22, 10:43 pm, Robby <rsimp...@gmail.comwrote:
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.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Apr 23 '07 #7
On Apr 23, 1:43 am, Robby <rsimp...@gmail.comwrote:
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

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

Apr 23 '07 #8
{ Quoted clc++m banner removed. -mod }

On Apr 23, 1:43 pm, Robby <rsimp...@gmail.comwrote:
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?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Apr 24 '07 #9
In article <f0**********@news.datemas.de>, v.********@comAcast.net
says...
Jerry Coffin wrote:
In article <11*********************@p77g2000hsh.googlegroups. com>,
rs******@gmail.com says...
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! ]

Apr 24 '07 #10
On Apr 24, 9:08 am, nonicknameyet <krovch...@gmail.comwrote:
On Apr 23, 1:43 am, Robby <rsimp...@gmail.comwrote:
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
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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Apr 24 '07 #11
On Apr 24, 11:20 am, Jerry Coffin <jcof...@taeus.comwrote:
In article <f0id5r$at...@news.datemas.de>, v.Abaza...@comAcast.net
says...
Jerry Coffin wrote:
In article <1177283823.970324.49...@p77g2000hsh.googlegroups. com>,
rsimp...@gmail.com says...
>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.
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.

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

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

Apr 24 '07 #12
In article <11*********************@s33g2000prh.googlegroups. com>,
ja*********@gmail.com says...

[ ... ]
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.
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! ]

Apr 26 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Sanjay | last post: by
reply views Thread by Michael O'Brien | last post: by
7 posts views Thread by loial | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.