473,320 Members | 2,161 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,320 software developers and data experts.

proposed language extension: object state

Virtual functions could have multiple definitions, for different
object states.

The prototype of a virtual member function could have an optional
reserve word "for" followed by a comma-separated list of state names.

The statement:

virtual using <state>;

would put the object in the state named <state>. The statement:

virtual using 0;

would put the object back in the default state. The expression:

virtual <virt-mem-funusing <state>

would return true if, at the point of execution of the expression, a
call to the virtual member function <virt-mem-funwould execute the
definition of the function valid for state <state.

I think this feature would have many uses. For example, if the read
member function was called for a closed file, this could trigger a
version of read that would throw an exception.

A feasible implementation would be to have multiple v-tables, one
appropriate for each state. State changes would involve changing the
v-pointer(s). This would get complex with in large inheritance
trees. The function to change the v-pointer would itself have to be a
hidden virtual function.
Aug 11 '08 #1
1 1380
On Aug 11, 1:25*pm, W Karas <wka...@yahoo.comwrote:
Virtual functions could have multiple definitions, for different
object states.

The prototype of a virtual member function could have an optional
reserve word "for" followed by a comma-separated list of state names.

The statement:

virtual using <state>;

would put the object in the state named <state>. *The statement:

virtual using 0;

would put the object back in the default state. *The expression:

virtual <virt-mem-funusing <state>

would return true if, at the point of execution of the expression, a
call to the virtual member function <virt-mem-funwould execute the
definition of the function valid for state <state.

I think this feature would have many uses. *For example, if the read
member function was called for a closed file, this could trigger a
version of read that would throw an exception.

A feasible implementation would be to have multiple v-tables, one
appropriate for each state. *State changes would involve changing the
v-pointer(s). *This would get complex with in large inheritance
trees. *The function to change the v-pointer would itself have to be a
hidden virtual function.
Why should this be a language extension? It seems like it can be
almost as easily implemented by using a state machine such as the one
described in the GoF's _Design Patterns_, which might look something
like:

struct State
{
virtual ~State() {}
virtual void Foo() = 0;
virtual void Bar() = 0;
};

struct State1 : State
{
virtual void Foo() { /*...*/ }
virtual void Bar() { /*...*/ }
};

struct State2 : State
{
virtual void Foo() { /*...*/ }
virtual void Bar() { /*...*/ }
};

void ChangeState( int newState, std::tr1::shared_ptr<State>& state )
{
// Could use a factory or std::map to make this pretty
if( 1 == newState )
state.reset( new State1 );
else if( 2 == newState )
state.reset( new State2 );
}

void DoSomething( std::tr1::shared_ptr<State>& state )
{
state->Foo();
}

There doesn't seem to be a compelling reason to extend the language
for this sort of functionality, which is not rare but not so common as
to demand language support, especially when existing features permit
it without much hassle.

Cheers! --M
Aug 11 '08 #2

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

Similar topics

5
by: barnesc | last post by:
I'm revising my vote. Thanks for the responses. I vote against: class foo: @synchronized @types("o,i,i") @author('Chris King')
16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
2
by: Richard Cornford | last post by:
Anyone who has taken a look at the online FAQ today may have noticed that I have updated it. The majority of the changes are the updating of broken links and the implementation of that extensive...
0
by: Chris Travers | last post by:
Hi All; I may be able to do this in Perl, but if there is enough interest in doing something like this in C, maybe I can still help (however, consider your self warned about my skill at coding...
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
3
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for...
3
by: dgdev | last post by:
I would like to pickle an extension type (written in pyrex). I have it working thus far by defining three methods: class C: # for pickling __getstate__(self): ... # make 'state_obj' return...
27
by: notnorwegian | last post by:
(might not be the right forum for this but...) what is the definition of a highlevel-language? well there isnt one specifically and wikipedia and the like gives just a very general description...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.