Re: C++ Templates - passing a pointer to the member to that member's base class
On Apr 21, 12:27 am, jonas.and...@hotmail.com wrote:
I'm writing a little framework for interface programming, where the
interfaces are represented by template classes with modern features
like properties ? la C#. While these properties are member variables
with Get and Set operations, it is the implementing class that shall
handle these calls and hence I want Get and Set to be members of the
class that implements the interface.
I'm not 100% sure what you're trying to achieve, but I wrote up the
technique that we use in FOST.3 to handle attributes with reflection
for the O/RM part of the framework.
http://www.kirit.com/C%2B%2B%20killed%20the%20get%20%26%20set%20accessors
The write-up is probably only enough to get yourself started with, but
I think using this sort of technique you can drastically cut down on
the amount of code needed to use the properties in classes.
Here is some FOST.3 code to show a simple Person class:
class FSLib::Contacts::Person :
public FSLib::Contacts::LEntity {
public:
Person( const FSLib::Nullable< FSLib::ID > &id, const FSLib::ID
&type, const FSLib::wstring &displayName );
static const FSLib::Definition::ConcreteObject< Person > s_table;
static const FSLib::Definition::Column s_extraName;
static const FSLib::Definition::Column s_firstName;
static const FSLib::Definition::Column s_honorific;
static const FSLib::Definition::Column s_secondName;
FSLib::Attribute::NullableText extraName;
FSLib::Attribute::NullableText firstName;
FSLib::Attribute::NullableText honorific;
FSLib::Attribute::NullableText secondName;
};
The statics are used to handle the O/RM bindings (at least those parts
that are static). The implementation is fairly simple:
const FSLib::Definition::ConcreteObject< FSLib::Contacts::Person >
FSLib::Contacts::Person::s_table(FSLib::Contacts::LEntity::s_table,
FSLib::wstring( L"class FSLib::Contacts::Person" ) );
const FSLib::Definition::Column
FSLib::Contacts::Person::s_extraName( FSLib::Contacts::Person::s_table,
L"extraName" );
const FSLib::Definition::Column
FSLib::Contacts::Person::s_firstName( FSLib::Contacts::Person::s_table,
L"firstName" );
const FSLib::Definition::Column
FSLib::Contacts::Person::s_honorific( FSLib::Contacts::Person::s_table,
L"honorific" );
const FSLib::Definition::Column
FSLib::Contacts::Person::s_secondName( FSLib::Contacts::Person::s_table,
L"secondName" );
FSLib::Contacts::Person::Person( const FSLib::Nullable< FSLib::ID >
&id, const FSLib::ID &type, const FSLib::wstring &displayName )
: LEntity( id, type, displayName ),
extraName( s_extraName, m_attributes ),
firstName( s_firstName, m_attributes ),
honorific( s_honorific, m_attributes ),
secondName( s_secondName, m_attributes ) {
}
The static model built contains things like column names
(L"firstName") and connections between database tables (the
inheritance for example). There's also a factory (ConcreteObject<>)
which is used to create new instances. The attribute constructor tie
in with their static definitions and register themselves with a
collection (m_attributes) that can be used for reflection.
Some code that uses it might look like this:
object_ptr< Person > person( 12344 ); // Some ID
person->firstName(); // return the first name
person->firstName( L"Fred" ); // set the first name
person->m_attributes[ L"firstName" ]; // returns the attribute called
"firstName"
person->m_attributes[ Person::firstName ]; // returns the attribute
firstName
The implementation we have does the SQL and COM bindings too.
It is possible to create some fairly easy to use implementations of
properties. For example, notice how when the class is used the
attributes look just like normal accessors.
Even if you do nothing else I think you should lose the Get and Set
names and use functors.
K
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]