TweetFollow Us on Twitter

August 91 - How to Cope with Nesting Instinct (in C++)

How to Cope with Nesting Instinct (in C++)

Eric M. Berdahl

MacApp 3 will add large amounts of spice to programming recipes. There are new classes, new twists on old classes, and new hoops to jump through.

However, the more interesting items are from the primary language of implementation changing from Pascal to C++. In fact, I propose that MacApp 3's best features are its solutions to the so-called "Nested Procedure" problem.

As a software author, I spend a fair amount of time listening to a short description of some problem a user is having and a longer description of what the user wants to do to solve it. Sound familiar? Sometimes the user is right on the money as to the nature of the "right" solution, but more often it is their description of the problem that is most revealing. I view the Nested Procedure problem in that light. I don't necessarily want nested procedural constructs, but I do have a problem to solve.

One solution is to implement language support for grouping sets of code in some logical order. The Pascal language provides this. However, it's not the only solution, nor is it the only solution for the MacApp programmer-at least not for the ones who use C++.

This article will examine the nested procedure problem, look for solutions, and discuss what this has to do with MacApp. First, I'll describe the problem in more detail. This may seem obvious and repetitive, but the goal is to come up with something that does what nested procedures in Pascal do. In addition, the solution should be link compatible with Object Pascal.

SCOPES?

Let's examine some textbook descriptions of Pascal. Pascal lore provides many ideas from which one may leverage; one of the most useful is the concept of scopes.

Simply put, a scope is the range over which a coding entity-variable, constant, or routine-is meaningful. Languages like assembly generally recognize only one scope, global. That is, once you get down to the assembly level, everything is visible to everyone all the time.

Moving up one step, there is C. C recognizes two basic scopes, global and local. That is, something is either scopeless-global-or it exists within a specific routine.

Languages like Pascal maintain a highly ordered, scoped hierarchy. Pascal's description of scoping is so good, in fact, that to many people, Pascal's implementation is scoping.

ABRACADABRA!

You might ask, at this point, "But when I write Pascal code, I don't really have to worry about scopes, do I?"

Probably not, but you do have to be careful. For example, many of you may remember something like the code in Listing 1 from a Computer Science 101 test. The exam would quiz you on the output of the program; to answer correctly, you would have to recognize that there are overlapping scopes and resolve them appropriately.

In a more practical world, you might see something like Example 2. Example 2 calls a procedure, DoSomethingToIntegers, telling it what action it wants performed, namely DoToInteger. Furthermore, DoToInteger has some magic knowledge of the local variables, oneInt and twoInt. Pascal accomplishes this by giving the nested procedure, DoToInteger, an additional parameter referred to as a "static link" variable. The static link is a pointer to the stack frame of the enclosing routine.

Thus, when a Pascal nested procedure is called, a pointer to the routine's stack frame is passed as a hidden argument. Also, when a routine is passed as a procedure parameter to a Pascal routine, as is done with DoToInteger, a pointer to the routine's stack frame is passed as an additional parameter. In the example, DoSomethingToIntegers actually takes two parameters, the routine to execute, DoToInteger, and a value to pass as the static link parameter to the indicated routine.

OH, I C

A person emulating this concept in a C-based language could write something like Example 3. This construct directly translates Pascal's mechanism into C's. It also looks ugly. For some reason, Pascal's elegance has been lost. Pascal deals elegantly with the static link problem; as a programmer, I don't need to deal with it. I just write my code and let Pascal take care of the overhead.

In Example 3, you could just pass DoSomethingToIntegers a pointer to oneInt, and have CBasedDoToInteger know that staticLink is a pointer to a short, namely oneInt. But that means it won't know about twoInt! Alternatively, you could define a structure that holds oneInt and twoInt, and pass a pointer to the structure for the static link, but that's unattractive. Clearly, it's better to refine and generalize the solution.

DOING IT right IN C++

Simple emulation doesn't seem to solve the nested procedure problem. Nonetheless, Pascal has provided a good starting point, and later we'll return to it. For the moment, however, let's look at another avenue-object technology. Regardless of the language to which it is bound, object concepts provide the programmer with another layer of scoping.

The key to an object-based approach is to recognize that, implicitly, a method is scoped to exist within an instance of an object. I tend to avoid hard-core C++ documentation (except during psychotically masochistic episodes), but in most C++ texts, the descriptions of objects as scopes-and the implications of this for visualizing program constructs-are very good.

For the problem above, a class could be defined, like CDoToInteger in Example 4, to handle the hard work. CDoToInteger looks much closer to what's desired than CBasedDoToInteger in Example 3.

"But where did the mystical staticLink parameter go?" the intrepid reader asks. It's hidden by the magic of object programming, just as it was hidden by the magic of Pascal in the Pascal example. In the object programming case, the scope is determined by the implicit variable this (SELF for you Pascal fans, Current for you Eiffel fans).

Look again at CPlusFoo in Example 4 to see how the CDoToInteger class might be used. I've indicated a routine to invoke-CPlusDoToInteger in the scope of CDoToInteger-and indicated a context in which to execute the routine, aScope. That's it, right?

Not quite. I still have to find some way for aScope to know about oneInt and twoInt within CPlusFoo. One solution might be to set the individual instance variables of aScope to hold the same values as the local variables of CPlusFoo, but that's not what I really want. I want the oneInt and twoInt variables of CDoToInteger to be pointers to oneInt and twoInt in CPlusFoo, but I also need to ensure that those pointers are never nil.

This is the second and last reason that to reasonably use reference variables in C++ MacApp code. (The first involved parameter passing, and was discussed last issue.) So, the CDoToInteger class is finished in Example 5. The careful reader will notice that the declaration of CPlusDoToInteger changed subtly in this last revision. It now carries a Pascal calling sequence, so that it's compatible with Pascal. The difference here is the order in which parameters on the stack are evaluated. The declaration above indicates that the magic this parameter is to be the last parameter pushed on the stack, just like the Pascal magic static link is pushed on last. Ta da.

A PRACTICAL EXAMPLE

So, how might one use such information in a real MacApp application? Consider a common situation, that of iterating over items in a TList:
class CCountSubViews {
private:
long&           numberOfViews;
public:
    CCountSubViews(long& aNumberOfViews) :
    numberOfViews(aNumberOfViews) { }

    pascal void CountSubViews(TView* aView)
   {
    numberOfViews++;
   }
};

pascal void TMyView::CountSubViews()
{
   long             numberOfSubViews;
   CCountSubViews   aCounter(numberOfSubViews);
   fSubViews->Each(CCountSubViews::CountSubViews,
                          &aCounter);
}

You can come up with more relevant examples, I'm sure, but I chose this particular example for a reason. Each is a method that allows a routine to iterate over the items in a TList. This can be used with the magic of Pascal and nested procedures; however, MacApp 3 provides C++ users with another way to iterate.

ITERATORS

Often the best way to solve a problem is to avoid it. It would be nice to avoid writing helper classes, and just bring iteration into the mainstream of code. After all, anyone is used to iterating over integers and other built-in types with simple for loops:
// Find the sum of all integers on the interval [0..4]
short   a;
a = 0;
for (short i = 0; i < 5; i = i + 1)
{
    

a = a + i; }

MacApp 3 provides a nice mechanism for doing this-iterators. A library of iterator objects (sorry, C++ users only) is available for doing a variety of iterative actions. The following is an example of CObjectIterator, the class for iterating over objects in a TList:

CObjectIterator    iter(aTList);
for (TObject* anOb = iter.FirstObject(); iter.More();
    anOb = iter.NextObject())
{
    //  do something with anOb here
}

Within the body of the loop, you can manipulate anOb just as you would any local variable. The reason for this is that anOb is a full-fledged local variable. So, if you happen to know that aTList only contains instances of TFoo, you can easily (and legally) cast anOb to TFoo*.

Iterator classes are provided for most of the basic iterations. For example, the CSubViewIterator class, iterates over each subview of a given TView. You might use it in the following fashion:

CSubViewIterator   iter(aTView);
for (TView* aView = iter.FirstSubView(); iter.More(); aView = iter.NextSubView())
{
    //  do something with aView here
}

NEXT TIME…

Next issue, I'll look at the wild and woolly world of MacApp 3. I'll show what new functionality it provides, what you might want to tap into right away, and what you'll want to study first. Keep those cards and letters coming!

Tech Note #265

For more information on Pascal PROCEDURE parameters, I refer the inquisitive reader to Tech Note #265: Pascal to C - PROCEDURE parameters. n
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »

Price Scanner via MacPrices.net

Apple’s 24-inch M3 iMacs are on sale for $150...
Amazon is offering a $150 discount on Apple’s new M3-powered 24″ iMacs. Prices start at $1149 for models with 8GB of RAM and 256GB of storage: – 24″ M3 iMac/8-core GPU/8GB/256GB: $1149.99, $150 off... Read more
Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more
May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.