Click here to Skip to main content
15,888,803 members
Articles / Desktop Programming / MFC
Article

Object properties for C++

Rate me:
Please Sign up or sign in to vote.
4.21/5 (21 votes)
19 May 20052 min read 94.9K   675   30   22
A small library that gives C++ objects the ability to have properties.

Introduction

The purpose of this library is to provide object properties. Instead of coding setter and getter methods, it is better to use properties because it is a more intuitive interface. Unfortunately, C++ does not offer native properties, but they can be emulated using templates and operator overloading, with a small memory overhead.

Installation

In order to use properties, you have to do include the file "property.hpp" in your project, then use the following fragment in your code:

#include "property.hpp"
using namespace cpp::properties;

The library is documented using Doxygen.

Declaring properties

The main class of this library is the class 'property'. It can be used to declare a property member. For example:

class MyClass {
public:
    property<MyClass, int> data;

    MyClass() : data(this, &MyClass::data_changed, 5) {
    }

protected:
    virtual void data_changed() {
    }
};

In the above example, a property 'data' is declared. The property class has two main parameters: the type of owner class (needed in order to make a typesafe callback interface) and the type of the property value.

The property's callback (and optional initial value) must be declared at construction time. It can not be changed afterwards. Callback parameters must not be null, otherwise your application will crash.

Using properties

Usage of properties is like data members. For example:

MyClass obj;
obj.data = 5;
int i = obj.data + 1;
cout << obj.data() << endl;

Advanced options

The default property declaration declares a property that has a read-write value stored inside the property. The type of access (read-write, read-only, write-only) and the type of storage (variable or interface) can be changed by supplying different template parameters.

Interface properties are properties that don't store the value, but they call the owner object for getting and setting the value of the property.

For example, a read-write interface property must be declared like this:

class MyClass {
public:
    property<MyClass, int, read_write, interface> data;

    MyClass() :
        m_data(0),
        data(this, &MyClass::data_get, &MyClass::data_set) {
    }

private:
    int m_data;

    const int &data_get() const {
        return m_data;
    }

    void data_set(const int &value) {
        m_data = value;
    }
};

Usage of interface properties is exactly the same as variable properties. You can do different combinations of read_write, read_only, write_only and variable, interface to provide your own taste of a property.

License

As the included readme.txt explains, it's freeware, i.e. you can do whatever you like with it, except claim it for yours (of course!).

Notes

I have modified the library so that the declaration of different flavors of properties has become simpler. It works under MS VC++ 6.0 and DevCpp 4.9.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
ColdShine24-Nov-08 10:36
ColdShine24-Nov-08 10:36 
GeneralSize increase Pin
Michel Helms22-Nov-06 0:52
Michel Helms22-Nov-06 0:52 
GeneralMSVC versions Pin
Ernesto Savoretti7-Jun-06 15:31
Ernesto Savoretti7-Jun-06 15:31 
GeneralNice, but... Pin
Yevhen Fedin26-May-05 23:11
Yevhen Fedin26-May-05 23:11 
Generalimprovement! Pin
Achilleas Margaritis20-May-05 2:19
Achilleas Margaritis20-May-05 2:19 
GeneralRe: improvement! Pin
ChauJohnthan20-May-05 8:05
ChauJohnthan20-May-05 8:05 
GeneralRe: improvement! Pin
Achilleas Margaritis20-May-05 8:41
Achilleas Margaritis20-May-05 8:41 
GeneralMS specific solution Pin
cmk20-May-05 0:25
cmk20-May-05 0:25 
GeneralNot bad Pin
pshomov19-May-05 13:26
pshomov19-May-05 13:26 
GeneralRe: Not bad Pin
Achilleas Margaritis19-May-05 23:00
Achilleas Margaritis19-May-05 23:00 
GeneralRe: Not bad Pin
pshomov20-May-05 6:29
pshomov20-May-05 6:29 
GeneralRe: Not bad Pin
Achilleas Margaritis20-May-05 8:55
Achilleas Margaritis20-May-05 8:55 
GeneralRe: Not bad Pin
pshomov20-May-05 12:55
pshomov20-May-05 12:55 
GeneralRe: Not bad Pin
Achilleas Margaritis21-May-05 3:43
Achilleas Margaritis21-May-05 3:43 
GeneralRe: Not bad Pin
pshomov22-May-05 8:05
pshomov22-May-05 8:05 
GeneralRe: Not bad Pin
Achilleas Margaritis23-May-05 6:18
Achilleas Margaritis23-May-05 6:18 
GeneralRe: Not bad Pin
Corneliu Tusnea24-May-05 13:26
Corneliu Tusnea24-May-05 13:26 
GeneralRe: Not bad Pin
barok25-May-05 20:52
barok25-May-05 20:52 
GeneralRe: Not bad Pin
Achilleas Margaritis26-May-05 2:03
Achilleas Margaritis26-May-05 2:03 
GeneralRe: Not bad Pin
Achilleas Margaritis26-May-05 1:59
Achilleas Margaritis26-May-05 1:59 
Tutu wrote:
He does not get it.

It is Petar that does not get it.

Tutu wrote:
first of all I really do think your attitude was the rude one.

Your opinion is respected, but I think otherwise.

Tutu wrote:
You placed your article on CP and expect people to make comments.

Yes, comments that are based on some arguments. The comments of Petar was "your code sucks 100%, here is the right way", while he has not understood the reasons behind the design. I wouldn't have a problem with that, if the guy did not insist and not presented books for me to read! It was like saying "you are lame, please read these books and then come back".

Tutu wrote:
you start (I would say quite aggressively) explaining that he is wrong.

Quite aggressively? not at all? where is the aggressiveness? show me, please!!!

Tutu wrote:
So... mate.. I really think you have an attitude problem.

I consider it much more rude to give "advice" without first understanding what's going on. At least the guy could have asked: "why do you do such and such?" before giving "advice".

Tutu wrote:
It's a matter of coding preference.

No, it's not. It's a matter of design. Let me repeat my argument, because it seems you have not understood it either:

The usage of 'read_write', 'read_only', 'write_only', 'variable', 'interface' as selectors of policy in a template class is a better way than having typedefs of all the cases, for the following reasons:


  1. It allows for less typing, since the user would not have to type the property kind in 99% of cases (not even bother with intellisense).
  2. It makes intellisense faster.
  3. It is more intuitive to combine things together than choosing from a list of predefined configurations. The mental model is better: the programmer selects behaviours by mixing and matching, instead of selecting from a ready-made recipie.
  4. It is easier to extend for the API user, since the user will only need to know new selector keywords.
  5. Extending the library with new keywords will not force old code to be recompiled. It's better for backwards compatibility.


Where are your arguments? Don't reject what you don't understand.

Tutu wrote:
(if you don't have copyright already, offcourse)

'Light' irony like yours makes me angry: it is the behaviour of cowards, of people that can't stand up and say their opinion.

Tutu wrote:
Neither of us is so smart that needs no help.

Help, yes. But help should come with arguments. Why do I need to do what Petar has said? there is no single argument that persuaded me he is right.

Tutu wrote:
So your I see no need to read 'books' when I know 100% of what I am doing it's quite aggressive

So why do I need to read something, when I know it 100%? the need to read about it means I don't know it 100%. In the field of C++ properties, I have done similar libraries for over 6 years now, so I know all problems associated with it. But then I accepted the 'challenge' and read what Petar said and counter-claimed all of his arguments.

And in a previous library, I have done what Petar suggested, and it was ugly.

Furthermore, my design comes from reading the boost development mailing list. Most boost developers are in favor of policy-based design, which has been used extensively in boost.

Tutu wrote:
You don't have to prove it in every other sentence.

I did not say what I said to prove I know everything. I said it to prove I know everything regarding C++ properties design. Notice that I place my 'everything' on a specific domain! What I feel is an insult from your part is that you fail to understand this little detail.

Tutu wrote:
If I would be you

You're not. Let's keep it real.

Tutu wrote:
I would apologize for the rude comments (that you did)

So me one rude comment that was not a response to a Petar's rude comment, and I will apologise. One!

Tutu wrote:
and maybe update your article to show "the other side of the story"

There is no other side of the story. I have proved, again and again, that Petar's design is wrong.

Tutu wrote:
Your "can't defend his initial arguments" clearly shows you don't care about criticism and making things better, you care only about being right.

If I did not care about criticism, I would not respond. Have you got any counter-arguments? you don't. Does Petar? nope. Do I have counter-arguments? I surely do! so stop whining and accept the facts.

Tutu wrote:
I like templates.

But my solution is all about templates, too.

Tutu wrote:
After typing "property[Foo, int]::" I get intellisense for the coming "rwi" or "read_write" (as I would rename it).

With my solution, you get intellisense, too. When you type 'r..', you can select from 'read_write' and 'read_only'.

With Petar's solution, one that writes properties would always have to select the proper typedef'd type.

With my solution, one does not need to select the proper property kind, because it is there in the default arguments of the template.

My solution also has other advantages, mentioned above.

Tutu wrote:
After "property[Foo, int," I get not intellisense to fill the "read_write", "interface".

My IDE (VC++) shows template parameters when I type a template type. Then, when I press 'r', 'read_write' and 'read_only' comes up.

And it is really not a good practice to code around one's IDE intellisense capabilities instead of what makes the language better.

Tutu wrote:
There might be more reasons. He presented an alternative.. that you really don't want to accept and consider it also very bad.

He presented an alternative with insufficient arguments to back it up. He also implied that I don't know what I am doing, therefore I need to educate myself.

Tutu wrote:
All I can say it: change your attitude.

All I can say is all you should change attitudes:


  • you should be more honest, more sincere. If you think my design sucks, tell me that "your design sucks". Don't wrap it up in a few "intelligent" wordisms.
  • learn how to debate. Debating means presenting arguments. You don't have arguments? shut the f*** up then. I know I do so when I don't have arguments.


By the way, I don't really want to get in a teaching mood, but you guys have way crossed the line, up to the fact of telling another guy to change his attitude! you should learn what democracy is: to accept the others as they are (amongst other things...).
GeneralRe: Not bad Pin
Anonymous14-Jul-05 14:53
Anonymous14-Jul-05 14:53 
GeneralNice Pin
Anonymous19-May-05 5:49
Anonymous19-May-05 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.