enforcing function calling order

D

ddtbhai

Hello folks,

Just wanted to know if there are some 'standard' approaches to
enforcing an order in the invocation of calling functions. It is
usually needed when initializing some object.

e.g

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3

At a quick glance some form of solution that stores some flags
internally and raises exceptions come to mind. But has anyone tried
some particularly elegant solution that achieves the same ends?

Thanks in advance,
ddtbhai
 
M

mlimber

Hello folks,

Just wanted to know if there are some 'standard' approaches to
enforcing an order in the invocation of calling functions. It is
usually needed when initializing some object.

e.g

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3

At a quick glance some form of solution that stores some flags
internally and raises exceptions come to mind. But has anyone tried
some particularly elegant solution that achieves the same ends?

Why can't you do initialize in the constructor? It's far better to have
the constructor throw and never have the chance of an invalid object
floating around (cf. http://www.artima.com/intv/goldilocks3.html).

Cheers! --M
 
G

Greg

Hello folks,

Just wanted to know if there are some 'standard' approaches to
enforcing an order in the invocation of calling functions. It is
usually needed when initializing some object.

e.g

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3

If there is some reason why these routines cannot be called from the
constructor - then the next best solution is to make all three private
and then declare one public function that calls the three private
methods in the "expected" order.

After all, if it is not obvious to the client of class "A" which method
has to be called before another, then there should be no requirement
that one has to be called before another. After all, how is the client
expected to know that a required order exists? The last thing any
programmer should want in their program is a set of unwritten
requirements that everyone working on the code is just expected to
"know".

Greg
 
D

ddtbhai

Greg said:
If there is some reason why these routines cannot be called from the
constructor - then the next best solution is to make all three private
and then declare one public function that calls the three private
methods in the "expected" order.

After all, if it is not obvious to the client of class "A" which method
has to be called before another, then there should be no requirement
that one has to be called before another. After all, how is the client
expected to know that a required order exists? The last thing any
programmer should want in their program is a set of unwritten
requirements that everyone working on the code is just expected to
"know".

Greg

Hi Greg, M,

The *main* issue that i was trying to avoid is the "you just know"
syndrome as Greg mentioned. In my example, each function has a single
argument, but this can also be arbitrarily long. Say each of the set
functions have 6 arguments each, then a single function that can order
the 3 invocations internally will end up with 18 arguments.

While there is nothing really wrong about that, it certainly isn't
pretty !

Which is why i was looking for an approach which can guarantee an order
of invocation, while barely being noticed by the end user of the class.

Best,
ddtbhai
 
H

Howard

Hello folks,

Just wanted to know if there are some 'standard' approaches to
enforcing an order in the invocation of calling functions. It is
usually needed when initializing some object.

e.g

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3

At a quick glance some form of solution that stores some flags
internally and raises exceptions come to mind. But has anyone tried
some particularly elegant solution that achieves the same ends?

One way would be to first make sure those functions are private (they are in
the above example, but I doubt you meant them to be, since then you couldn't
call them from outside in the first place). Then, add a public function
which accepts the parameters required to call all three functions itself.

For example,

class A
{
private:
void Func1( int a );
int Func2( int b ); // returning value here, just as an example
void Func3( int c );
public:
void Funcs( int a, int b );
};

A::Funcs( int a, int b )
{
Func1( a );
int c = Func2( b ); // compute value needed for Func3
Func3( c );
}


-Howard
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

The *main* issue that i was trying to avoid is the "you just know"
syndrome as Greg mentioned. In my example, each function has a single
argument, but this can also be arbitrarily long. Say each of the set
functions have 6 arguments each, then a single function that can order
the 3 invocations internally will end up with 18 arguments.

Write a class that collects the arguments, and pass an instance of it as
constructor's argument. This has also the advantage that you can easily
establish default arguments.
 
B

Bo Persson

Julián Albo said:
Write a class that collects the arguments, and pass an instance of
it
as constructor's argument. This has also the advantage that you can
easily establish default arguments.

Or split the class into several separate objects.

What kind of class would need 18 parameters for construction?! This is
almost always a sign that the class does more that one thing. Why not
have a separate class for each of the different tasks?


Bo Persson
 
S

shadowman615

Bo said:
Julián Albo wrote:

Or split the class into several separate objects.

What kind of class would need 18 parameters for construction?! This is
almost always a sign that the class does more that one thing. Why not
have a separate class for each of the different tasks?


How does that solve the problem of enforcing function calling order?
 
R

raxitsheth2000

ddtbhai,

do u want like this
//this is not c++ code...appropriate arguments and return type assumed
..

{
startup();
execute();
cleanup();
}


1. no one can able to call execute directly,
2. order of execution must be (start,execute,clean) or exception
throw.
3. Specify which function you want that some one can derived and able
to override, and all or directly specify which one is virtual/normal
and attribute like pub,protected,private.

It would help to understand your question...


chhers,
Raxit
 
A

aa

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3

That sounds like a "finite state machine" to me (although a very simple
one). You could try smc (I think on sourceforge) or Boost.Statechart
or sth. similar.

I wouldn't use that for initialisation though, but many people already
stated that.


cheers,

aa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top