J
Jon Slaughter
I have a class that is basicaly duplicated throughout several files with
only members names changing according to the class name yet with virtually
the exact same coding going on.
e.g.
class A
{
std::vector<B*> Bs;
public:
A() { blah; }
void SomeFunc_A() {blah blah blah};
}
class A
{
std::vector<C*> Cs;
public:
B() { blah; }
void SomeFunc_B() {blah blah blah};
}
and eventually one of the classes is a "terminal" in the sense it does not
continue the same abstract recursive process. My question is, is there any
way to automate this process? It seems I cannot use templates because I
cannot generate the proper method names are derived from the name of the
class?
i.e., I can't do something like
Metaclass template <class name, class subname>
class name
{
std::vector<subname *> subname's';
public:
name() {blah}
void SomeFunc_@'name' () { blah blah blah}
}
where say the 's' is appended to the subname literal and the @ appends the
string to the someFunc literal...
then I could do something like
Metaclass<A, B> and it produces the class Agiving above and Metaclass<B,C>
would produce B.
The reason I do this is because right now I have 7 files in my project that
all are virtually exactly the same as just 1(and how I got them was basicaly
using find and replace 3 times to turn the class into a "new" one that is
virtually identical with the other(excep ofcourse the coding is actualy the
same with different literals)).
My question is, is there any way in C++ to do something like this(I know my
code is not valid C++ code above but just giving some example). I just need
to generate a class from a meta class that also can change the method's
names that will be based on the name of the class.
Actually I will give a more concrete example that I actually kinda use in my
program:
class Score : public Base
{
public:
std::vector<Staff> Staffs;
int NumStaffs() { return (int)Staffs.size(); }
}
class Staff : public Base
{
public:
std::vector<Bar> Bars;
int NumBars() { return (int)Bars.size(); }
}
class Bar : public Base
{
private:
public:
}
class Base
{
public:
std::vector<Property> Properties;
int NumProperties() { return (int)Properties.size(); }
}
class Property
{
private:
public:
}
and I would want to access methods like:
Score.NumStaffs();
Score.Staffs[0].NumBars();
etc...
I know I could make some general template where all methods across the
different classes would have the same name so I might have
Score.NumObjects(); which would return the number of objects in in the
vector in that object and(Staffs in this cass)
Score.Objects[0].NumObjects(); would return same as above but for the Bars.
The problem with this is I only need the recursiveness for about 5 classes
and that I want the naming to follow the modelling I'm using(modeling of
Score->Staff->Bar->Notes of music) else it might be confusing to remeber
Score.Objects[0].Objects[3].NumObjects(); is refering to
So thats the idea... as you can see most the classes are exactly the same
except for the naming of functions. I do see a problem now with my example
above about just adding 's' to the end... I wouldn't want Propertys but
Properties so there would have to be a way of creating logical english
literal names(i.e., replace ending y with ies when adding s), etc...
Anything that can do something like this out there? (would save me a lot of
work of having to to mess with 7 files that are identical except for naming
and potential other problems)
Jon
only members names changing according to the class name yet with virtually
the exact same coding going on.
e.g.
class A
{
std::vector<B*> Bs;
public:
A() { blah; }
void SomeFunc_A() {blah blah blah};
}
class A
{
std::vector<C*> Cs;
public:
B() { blah; }
void SomeFunc_B() {blah blah blah};
}
and eventually one of the classes is a "terminal" in the sense it does not
continue the same abstract recursive process. My question is, is there any
way to automate this process? It seems I cannot use templates because I
cannot generate the proper method names are derived from the name of the
class?
i.e., I can't do something like
Metaclass template <class name, class subname>
class name
{
std::vector<subname *> subname's';
public:
name() {blah}
void SomeFunc_@'name' () { blah blah blah}
}
where say the 's' is appended to the subname literal and the @ appends the
string to the someFunc literal...
then I could do something like
Metaclass<A, B> and it produces the class Agiving above and Metaclass<B,C>
would produce B.
The reason I do this is because right now I have 7 files in my project that
all are virtually exactly the same as just 1(and how I got them was basicaly
using find and replace 3 times to turn the class into a "new" one that is
virtually identical with the other(excep ofcourse the coding is actualy the
same with different literals)).
My question is, is there any way in C++ to do something like this(I know my
code is not valid C++ code above but just giving some example). I just need
to generate a class from a meta class that also can change the method's
names that will be based on the name of the class.
Actually I will give a more concrete example that I actually kinda use in my
program:
class Score : public Base
{
public:
std::vector<Staff> Staffs;
int NumStaffs() { return (int)Staffs.size(); }
}
class Staff : public Base
{
public:
std::vector<Bar> Bars;
int NumBars() { return (int)Bars.size(); }
}
class Bar : public Base
{
private:
public:
}
class Base
{
public:
std::vector<Property> Properties;
int NumProperties() { return (int)Properties.size(); }
}
class Property
{
private:
public:
}
and I would want to access methods like:
Score.NumStaffs();
Score.Staffs[0].NumBars();
etc...
I know I could make some general template where all methods across the
different classes would have the same name so I might have
Score.NumObjects(); which would return the number of objects in in the
vector in that object and(Staffs in this cass)
Score.Objects[0].NumObjects(); would return same as above but for the Bars.
The problem with this is I only need the recursiveness for about 5 classes
and that I want the naming to follow the modelling I'm using(modeling of
Score->Staff->Bar->Notes of music) else it might be confusing to remeber
Score.Objects[0].Objects[3].NumObjects(); is refering to
So thats the idea... as you can see most the classes are exactly the same
except for the naming of functions. I do see a problem now with my example
above about just adding 's' to the end... I wouldn't want Propertys but
Properties so there would have to be a way of creating logical english
literal names(i.e., replace ending y with ies when adding s), etc...
Anything that can do something like this out there? (would save me a lot of
work of having to to mess with 7 files that are identical except for naming
and potential other problems)
Jon