I know this question may be very vague, and it's a bit of an extension to an answer I gave here. Basically, as a personal project I have been trying to replicate the "code-first" idiom, that is popular with C# programmers doing database-work, but in C++. Everything is great, and works well.
One of the great things about C# is things such as attributes. We don't have that novelty in C++, so my code for a particular class that is then structured in the database, looks like this:
class User : public Record {
public:
User(Model& m) : Record(L"_user", m)
, Username(L"_userName", 32, false)
, Nickname(L"_nickName", 32, false) {
field(Username);
field(Nickname);
}; // eo ctor
Field<String> Username;
Field<String> Nickname;
}; // eo class User
Ideally, I want to get rid of the field
calls in the constructor. All they do, essentially, is register the field with the base class which then does the real work. How might I accomplish this kind of thing, and pass the details to the base class as simply as I have specified in the constructor?
In a previous question that I answered I was able to give the base class information thanks to the way C++ constructs things. Is there a way I can leverage this in a constructor and get rid of the pesky field
calls that may introduce programmer error?
At this point I am happy that everything works wonderfully, but I'd love to cut down on clutter and have remove the need for this "registration" process in the constructors and somehow have them registered automatically with the base class.
All opinions welcomed :)
EDIT
The implentation of field
is thusly:
void Record::field(detail::field_base& field) {
field.owner_ = this;
fields_.push_back(&field);
}; // eo field**