Click here to Skip to main content
Click here to Skip to main content

Object properties for C++

, 19 May 2005
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Achilleas Margaritis
Software Developer (Senior)
Greece Greece
No Biography provided

Comments and Discussions

 
GeneralMy vote of 2 PinmemberColdShine24-Nov-08 11:36 
GeneralSize increase PinmemberMichel Helms22-Nov-06 1:52 
GeneralMSVC versions PinmemberErnesto Savoretti7-Jun-06 16:31 
GeneralNice, but... PinmemberYevhen Fedin27-May-05 0:11 
Generalimprovement! PinmemberAchilleas Margaritis20-May-05 3:19 
GeneralRe: improvement! PinmemberChauJohnthan20-May-05 9:05 
GeneralRe: improvement! PinmemberAchilleas Margaritis20-May-05 9:41 
GeneralMS specific solution Pinmembercmk20-May-05 1:25 
GeneralNot bad PinmemberPetar K. Shomov19-May-05 14:26 
Hello,
 
I personally believe that this line of solutions will not help as much as you probably hope it will.
The trouble is that it requires (even) more typing then just writing the accessor methods and declaring the private member variable to persist the property. The good side is that it would be probably easier(given that the user knows already how "your" properties behave like) for whomever is using a class, which takes advantage of your properties. It is definitely a possibility to be considered, but I would like to throw in some comments. Feel free to fire back Smile | :)
 
1. class *property* is totally unnecessary. It adds no functionality and is basically a 1:1 proxy for the policy class (from which it inherits! but I will get to that in a short while). It also offers constructors which if you try to use will cause compiler errors.
What I suggest here is the following: 1. remove class property completely, and 2. define a few types instead. Something like this:
 
<code>
template <class C, class T>
struct property
{
   typedef read_write<C,T,interface<C,T> > rwi;
   typedef read_write<C,T,variable<C,T> > rwv;
   typedef read_only<C,T,interface<C,T> > roi;
   typedef <all the others ... >
}
</code>
 
Too bad C++ does not support templated templates Frown | :( But I guess this will do.
 
2. the inheritance of the policy class from the actual implementation class is unnecessary and increases the coupling. The classes *variable* and *interface* "feel" a little strange with no public part, don't they ? What would be probably better is to declare instances of those classes as member variables in the corresponding policy classes. After all the relationship between the policy classes and the implementation classes is IS_IMPLMENTED_IN_TERMS_OF and not IS_A.
 
3. The private constructors do not have to be defined. It is enough to declare them as private. Any attempt to use them would end up with a compiler error.
 
All these suggestions and comments do not add or remove any functionality. They are merely a better, safer design considerations.
 
I hope this is useful to you. Sorry if I have any mistakes, I have not actually compiled or tried to use your class.
 
Regards,
 
Petar

GeneralRe: Not bad PinmemberAchilleas Margaritis20-May-05 0:00 
GeneralRe: Not bad PinmemberPetar K. Shomov20-May-05 7:29 
GeneralRe: Not bad PinmemberAchilleas Margaritis20-May-05 9:55 
GeneralRe: Not bad PinmemberPetar K. Shomov20-May-05 13:55 
GeneralRe: Not bad PinmemberAchilleas Margaritis21-May-05 4:43 
GeneralRe: Not bad PinmemberPetar K. Shomov22-May-05 9:05 
GeneralRe: Not bad PinmemberAchilleas Margaritis23-May-05 7:18 
GeneralRe: Not bad PinmemberTutu24-May-05 14:26 
GeneralRe: Not bad Pinmemberbarok25-May-05 21:52 
GeneralRe: Not bad PinmemberAchilleas Margaritis26-May-05 3:03 
GeneralRe: Not bad PinmemberAchilleas Margaritis26-May-05 2:59 
GeneralRe: Not bad PinsussAnonymous14-Jul-05 15:53 
GeneralNice PinsussAnonymous19-May-05 6:49 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 19 May 2005
Article Copyright 2005 by Achilleas Margaritis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid