Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #9487 > unrolled thread
| Started by | "Gernot Frisch" <me@privacy.net> |
|---|---|
| First post | 2011-08-30 16:53 +0200 |
| Last post | 2011-08-31 04:08 -0700 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.c++
access members by string name "Gernot Frisch" <me@privacy.net> - 2011-08-30 16:53 +0200
Re: access members by string name Paavo Helde <myfirstname@osa.pri.ee> - 2011-08-30 10:26 -0500
Re: access members by string name "Gernot Frisch" <me@privacy.net> - 2011-08-31 11:58 +0200
Re: access members by string name Michael DOUBEZ <michael.doubez@free.fr> - 2011-08-31 04:08 -0700
| From | "Gernot Frisch" <me@privacy.net> |
|---|---|
| Date | 2011-08-30 16:53 +0200 |
| Subject | access members by string name |
| Message-ID | <9c4brfFfu2U1@mid.individual.net> |
Hi,
assume you have a class with members (or member get/set functions) and want
to call them by a string name with the least code to maintain.
I like to just have one macro line per member. The class should have a
working assignment operator.
The end class should look like:
class foo
{
int bar;
int goo;
public:
...
int GetGoo()const {return goo;}
void SetGoo(int g) {goo = g;}
...
std::map<std::string, something_to_map_the_GET_function> getfunctions;
static std::string GetAllParams() {return std::string("bar,goo");}
int GetVar(std::string name) {return getfunctions[name] () ; }
};
[toc] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2011-08-30 10:26 -0500 |
| Message-ID | <Xns9F51BB96E6D5Dmyfirstnameosapriee@216.196.109.131> |
| In reply to | #9487 |
"Gernot Frisch" <me@privacy.net> wrote in
news:9c4brfFfu2U1@mid.individual.net:
> Hi,
>
> assume you have a class with members (or member get/set functions) and
> want to call them by a string name with the least code to maintain.
>
> I like to just have one macro line per member. The class should have a
> working assignment operator.
>
> The end class should look like:
>
> class foo
> {
> int bar;
> int goo;
>
> public:
> ...
> int GetGoo()const {return goo;}
> void SetGoo(int g) {goo = g;}
> ...
>
> std::map<std::string, something_to_map_the_GET_function> getfunctions;
>
> static std::string GetAllParams() {return std::string("bar,goo");}
> int GetVar(std::string name) {return getfunctions[name] () ; }
> };
What's wrong with
class foo {
std::map<std::string, int> data_;
public:
int GetVar(const std::string& name) const {
std::map<std::string, int>::const_iterator p = data_.find(name);
return p==data_.end()? 0: p->second;
}
int GetGoo()const {return GetVar("goo");}
void SetGoo(int g) {data_["goo"] = g;}
};
cheers
Paavo
[toc] | [prev] | [next] | [standalone]
| From | "Gernot Frisch" <me@privacy.net> |
|---|---|
| Date | 2011-08-31 11:58 +0200 |
| Message-ID | <9c6etjFoqcU1@mid.individual.net> |
| In reply to | #9495 |
> class foo {
> std::map<std::string, int> data_;
It's a slight overhead, but it works properly for what I want. It's obvious,
but I was unable to see. Thanks for opening my eyes.
[toc] | [prev] | [next] | [standalone]
| From | Michael DOUBEZ <michael.doubez@free.fr> |
|---|---|
| Date | 2011-08-31 04:08 -0700 |
| Message-ID | <06e3990a-b24d-415c-9a7b-3cdd92a1da26@l4g2000vbz.googlegroups.com> |
| In reply to | #9487 |
On 30 août, 16:53, "Gernot Frisch" <m...@privacy.net> wrote:
> Hi,
>
> assume you have a class with members (or member get/set functions) and want
> to call them by a string name with the least code to maintain.
>
> I like to just have one macro line per member. The class should have a
> working assignment operator.
>
> The end class should look like:
>
> class foo
> {
> int bar;
> int goo;
>
> public:
> ...
> int GetGoo()const {return goo;}
> void SetGoo(int g) {goo = g;}
> ...
>
> std::map<std::string, something_to_map_the_GET_function> getfunctions;
>
> static std::string GetAllParams() {return std::string("bar,goo");}
// type for pointer to foo's member that is accessible.
typedef int (foo::*accessible_member_ptr);
static std::map<std::string,accessible_member_ptr> member_ptr;
static accessible_member_ptr
getfunctions(std::string name)
{
std::map<std::string,accessible_member_ptr>::iterator it =
member_ptr.find(name);
if( name == member_ptr.end() ) throw ParamDoesNotExists(name);
return it->second;
}
int GetVar(std::string name)
{
return this->*getfunctions(name);
}
int SetVar(std::string name, int newValue)
{
this->*getfunctions(name) = newValue;
}
};
// somewhere
member_ptr["bar"] = &foo::bar;
member_ptr["goo"] = &foo::goo;
--
Michael
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web