473,416 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Designing a property system

While I was looking through the group's archives I came across a
discussion on doing properties (as known from Delphi/C#/etc) in C++. It
inspired me to do some experimenting. Here's what I came up with.

First we have an interface defining how a property can be used (read and
written - for simplicity, I ignored read-only and write-only variants).

template<typename Tclass Property
{
public:
virtual Property& operator=(const T& value) = 0;
virtual operator const T&() const = 0;
};

Then a simple implementation of that interface. Maybe "static" is not
the best word here, but what I meant was that this class implements the
property using a single, value encapsulation of the type, and not e.g. a
map or any other "dynamic" structure.

template<typename Tclass StaticProperty : public Property<T>
{
public:
StaticProperty (const T& value);
virtual Property<T>& operator=(const T& value);
virtual operator const T&() const;

private:
T value;
};

And finally an example object that publishes a property.

class Object
{
public:
Object();
const auto_ptr<Property<string property;
};

Object::Object() : property(new StaticProperty("sample value")) {}

And the pros and cons I managed to come up with:

+ the property has a simple interface which sends the message "I am
assignable to and from type T";

+ the property is a pointer, allowing for polymorphism (the sample
StaticProperty, but also more sophisticated properties with data
verification or other cool stuff);

+ access to the property is done through ordinary syntax one would
expect for a pointer to type T:

Object object;
*object.property = "another value";
string value = *object.property;

+ it avoids writing accessor functions for every property, which a)
saves time b) centralizes processing and verification of data (imagine
an EmailProperty descendant that checks e-mail address validity);

+ it avoids the unclear syntax of object.property() = "yet another" that
I've seen somewhere;

+ the const modifier ensurer that no one messes with the auto_ptr;

- it exposes the use of auto_ptr in a public interface (however I
haven't seen an intelligent pointer that would behave differently in
dereferencing contexts, which are only used by the client of the class),
which would mean bad things if one wanted to switch to something else
later on;

- code overriding the assignment and cast operators does not have access
to the internals of the class publishing the property (which is offered
by accessor functions), but:

+ it could be easily integrated with something like boost::signals,
providing everyone with be ability to track changes to a property and
react to them with zero effort on side of the containing class;

These are all the remarks I could think of. Please tell me what you
think, especially the cons, since I didn't produce much "-" points :-)
Please comment on the idea and on how it complies (or not) with good
class design practices (I mean especially the rules of hiding
implementation etc.) As for the general usefulness (or uselessness) of
properties: I did this for a very little university project involving
something similar to a database app, where things like Property<string>
where real type-savers.

--
Memfis
Nov 6 '06 #1
0 1957

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that...
2
by: Sky Sigal | last post by:
Hello: I'm currently messing around, and need as much feedback/help as I can get, trying to find the most economical/graceful way to build usercontrols that rely on styling to look any good... ...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
1
by: slonocode | last post by:
I'm wondering if there are certain processes that I could follow to learn to design better classes? Where could I find these processes? Is designing classes more of an art that comes from...
3
by: Jack | last post by:
I am trying to receive xml files on my server which are posted from another server using http post. I need help in designing a receiver which will accept this xml file and send back a success...
3
by: mystilleef | last post by:
Hello, I need to design a plug-in system for a project. The goal is to allow third party developers interact with an application via plug-ins in a clean and robust manner. At this point I am...
32
by: keri | last post by:
Hi everyone, Having learnt a little about the basics whilst creating my first db I am now about to start creating a good one! I am about to sit down with a large piece of paper and plan it all...
1
by: hottoku | last post by:
Hi All, I'm having quite a bit of trouble designing a search tool to work with my database. I have found lots of examples from Microsoft Templates to Allen Browne's sample search form. The...
4
by: =?Utf-8?B?Z2lkZHk=?= | last post by:
hi, I've finished all the base code to my App. Tested it and everything, now I need to make a UI. Its not going to be overly complex but it could get complex later on, so I want to design it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.