🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #4092

Re: methods for accessing attributes

#4092
From: Damian Conway
Date: Thu, 18 Apr 1996 00:47
152 lines
4465 bytes
Steve Clamage writes (or forwards, I suspect):

>Has there been any consideration of adding to the standard
>a way of overloading access to instance variables, so that
>code that directly accesses a variable can be made to call 
>an access function if the class is modified to need it?

>For instance, let's say we've got:
>	
>	class Display {
>	public:
>		int wid, hgt;
>	}

>If lots of code already exists like the following:

>	void blah(Display &d)
>	{
>		d.wid = 80;
>	}

>And now suppose that we want Display to do something when
>the width is changed--- has there been any consideration of adding
>a way of doing this?  (besides changing all the direct accesses
>to function calls)   

Something like this will do what you want (note that you'll still have to
recompile everything that accesses the data members):


    template <class Owner, class Type>
    class AccessControlled
    {
	typedef Type (Owner::*Filter)(const Type&);

    public:
	AccessControlled(Owner * owner, Filter read = 0, Filter write = 0)
	: This(owner)
	, readFilter(read)
	, writeFilter(write)
	{}

	operator Type(void)
	{
		if (readFilter)
			return (This->*readFilter)(value);
		else
			return value;
	}

	AccessControlled& operator= (const Type& newval)
	{
		if (writeFilter)
			value = (This->*writeFilter)(newval);
		else
			value = newval;
		return *this;
	}

    private:
	Owner* This;
	Type value;
	Filter readFilter;
	Filter writeFilter;
    };



And then you can change class Display thus:

    class Display
    {
    public:
	AccessControlled<Display,int> wid;
	AccessControlled<Display,int> hgt;
	AccessControlled<Display,int> dph;


	// NEED A CTOR TO INITIALIZE THE FILTERS FOR THE ABOVE DATA MEMBERS

	Display(void)
	: wid(this,logAccess)		// NO VALUE CHECKING FOR wgt
	, hgt(this,0,checkVal)		// NO ACCESS LOGGING FOR hgt
	, dph(this,logAccess,checkSize)	// BOTH LOGGING AND CHECKING FOR dph
	{}

    private:
	int logAccess(const int& val)
	{
		// DO WHATEVER WITH CURRENT VALUE OF WID BEFORE PASSING IT OUT
		// EG: LOG THE ACCESS.....

		clog << "Accessing value " << val << endl;
		return val;
	}

	int checkVal(const int& newval)
	{
		// DO WHATEVER WITH NEW VALUE FOR VAR BEFORE PASSING IT IN
		// EG: MAKE SURE IT'S POSITIVE, NON-ZERO....

		if (newval<=0)
			return 1;
		else
			return newval;
	}

	int checkSize(const int& newval)
	{
		// DO WHATEVER WITH NEW VALUE FOR VAR BEFORE PASSING IT IN
		// EG: MAKE SURE IT'S IN THE RANGE [-10..10]

		if (newval<-10)
			return -10;
		else if (newval>10)
			return 10;
		else
			return newval;
	}
    };


>If [access functions are required], then I'd like to stick with some sort
>of standard naming convention... but I've seen all kinds of different 
>variations...

>So, which of these do people think is the most acceptable?

>	1)
>		class Display {
>			int wid;
>		public:
>			int get_wid();
>			void set_wid(int);
>		}

I greatly prefer this approach.
It's clear, obvious, and says explicitly what it means.

damian
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  who: Damian Conway               email: damian@bruce.cs.monash.edu.au
where: Dept. Computer Science      phone: +61-3-565-5184
       Monash University             fax: +61-3-565-5146
       Clayton 3168                quote: "A pessimist is never disappointed."
       AUSTRALIA
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <damian.829693837@molly.cs.monash.edu.au>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <9604160147.AA25931@grace>

Show Full Headers
Complete Article Headers
From: damian@molly.cs.monash.edu.au (Damian Conway)
Message-ID: <damian.829693837@molly.cs.monash.edu.au>
X-Original-Date: Tue, 16 Apr 1996 22:30:37 +0000 (GMT)
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 18 Apr 96 00:47:16 GMT
Approved: fjh@cs.mu.oz.au
Subject: Re: methods for accessing attributes
Organization: Monash University
X-Nntp-Posting-User: damian
Newsgroups: comp.std.c++
References: <9604160147.AA25931@grace>
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMXWRN+EDnX0m9pzZAQGsSgGAg2t2+w2Ky0EfULMwXJ3EMhQuCPk+yZ8J
 v02Xzx6B7WfmWxGlGAQVhO+QFdDoQNpw
 =TSJr