-3

Check the functions below, please:

public void DoJob()
{
    CheckPrivilege();
    DoJob2();
}

public void DoJob2()
{
    CheckPrivilege();
    DoJob3();
}

As you can see, if I call DoJob(), the CheckPrivilege() function runs twice. Sometimes I can call DoJob2() directly so I cannot remove CheckPrivilege() function from DoJob2().

So I need to know that the CheckPrivilege() function is called in another function which is in same call stack.

That is, I want to share a Privilege object over a call stack.

Is this possible? If yes, how?

brtb
  • 2,201
  • 6
  • 34
  • 53
  • 7
    c# or c? Please take more time when posting a question. Also your question is unclear. I didn't downvote, so give me a reason for not doing so. Tell us what exactly is your goal, or you are not getting any presents from Santa. – gsamaras Dec 27 '15 at 20:27
  • i was just editing post, in c#, So as i sad i want to share an instance over a call stack. For example i want to store an instance in DoJob() function, and then in DoJob2() function i want to check is there any stored instance in call stack – brtb Dec 27 '15 at 20:35
  • What is the motivation for the question? Is the call expensive or are you just trying to avoid repeated code? – Martin Smith Dec 27 '15 at 20:51
  • yes CheckPrivilege() is very expensive and in many function it is called. Sometimes it is executed 7-8 times and gives exact same result with the previous call – brtb Dec 27 '15 at 20:56

2 Answers2

0

There are lots of ways you could do it but 'sharing over a callstack' is not possible. You could keep a state variable indicating if the current task has privileges or do something like this.

function void DoJob()
{
    CheckPrivilege();
    DoJob2(false);
}

function void DoJob2(bool doPrivCheck)
{
    if (doPrivCheck)
       CheckPrivilege();
    DoJob3();
}
JJF
  • 2,681
  • 2
  • 18
  • 31
  • CheckPrivilege() is called in many function. So i cannot pass doPrivCheck parameter over every function. Thats why i want to know , can i store this parameter in call stack. – brtb Dec 27 '15 at 20:49
  • @brtb can you store it as a property of the containing class these functions are in? – Martin Smith Dec 27 '15 at 20:56
  • Just out of curiosity why can't you pass a variable on all calls? Is it just a preference? Otherwise you've got to store the result of the privilege check somewhere. The stack is not the place to do it. See @MartinSmith's comment. – JJF Dec 27 '15 at 20:59
  • @MartinSmith there are lots of classes. – brtb Dec 27 '15 at 21:27
  • @brtb well I guess technically you could do it. In a similar way to how `TransactionScope` works. Not sure how that works. Possibly thread local storage. – Martin Smith Dec 28 '15 at 13:27
0

Define 'internal' and 'external' interface functions. The external functions do the privilege checking and can be called by anyone; the internal functions do not do the checking and should only be called by the implementation.

Treating your functions as the 'external' ones:

function void DoJob()
{
    CheckPrivilege();
    DoJob2_Internal();
}

function void DoJob2()
{
    CheckPrivilege();
    DoJob3_Internal();
}

function void DoJob2_Internal()
{
    DoJob3_Internal();
}

The principle applies to any language — I'm not a C# programmer so I can't tell you what specific features of C# would enforce the access controls or function visibility, or whether you have to rely on convention.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278