Solved

inline methods in Delphi 3

Posted on 1997-10-08
7
376 Views
Last Modified: 2010-04-04
How do I use the reserved word "inline" in Delphi 3?
I would also like to know if there is more than one way to use inline methods, like in C++ - you can write the implementation in the declaration part (.h file), or type "inline" and implement the method in the definition part (.cpp file).

Thanx, Westley.
0
Comment
Question by:Westley
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:dionysos_swamp
ID: 1346956
inline is not the same as in C++ it's a stupid way of throwing in hand-assembled code into the program, it's not really usefull as the asm .. end; can assemble everything now, it's a relic from Turbo Pascal.

an example:
 inline($cd, $19);

this would reboot (maybe) the PC under DOS-TP

... Afik


0
 

Author Comment

by:Westley
ID: 1346957
I read in a Delphi 2 book that Borland no longer supports inlines. It's still a reserved word, but it doesn't do anything.

But, OTOH, I heard there were inline methods in Turbo Pascal 7, and Borland said they keep backward compatibility. So who's right?
0
 
LVL 1

Expert Comment

by:AndersWP
ID: 1346958
The question has a simple anwer: You can not use the Inline directive in Delphi 3.0 (nor in the earlier versions of Delphi).

The word Inline is reserved but not understood by the compiler. This means that can not use it in the expected way:

Procedure Foo;  Inline;
Begin ... End;

nor can you define you own identifier called Inline.

So, what do you do in stead? Well, the closest you can come to an Inline procedure is to use the $INCLUDE compiler directive.
Suppose you want to make an inline procedure with a number of statements:

Procedure Foo;  Inline;
Begin
  Statement1;
  ...
  StatementN
End;

While you can not do this directly, you can create a file (e.g. FOO.PAS) containing the statements from the procedure body.  Now, at every point in your source code where you would like to 'call' this procedure, you put the command {$INCLUDE FOO.PAS}.

How this will help you.

AndersWP
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:Westley
ID: 1346959
AndersWP,

This still doesn't help me with the biggest advantage of the inline concept: The ability to use private members outside the class.
For example, in C++, if you have declared
  int X;
inside a class, and it's private, you can still access it using
  int GetX() { return(X); }
and you don't get the overhead of using a real function.
Now Delphi supports properties, but I don't think properties are implemented inline, and since I'm using some private fields and exporting them via one-line-methods, I would like to know that I don't pay a high price for them.
0
 
LVL 1

Expert Comment

by:AndersWP
ID: 1346960
If the purpose of the excercise is to expose private fields in a controlled manner, properties is the way to go. The Delphi analog of your C++ example would be:

TMyClass = Class
Private
  FX: Integer;    
Public
  Property X: Integer Read FX;    
End;

Note that there is no Get function involved - you access the variable directly without the function call overhead.

Greetings,
AndersWP
0
 

Author Comment

by:Westley
ID: 1346961
Sorry I have to reject it again.

As I already wrote, I know about properties, but I need to know how they are implemented - are they like a macro, or a full function call?

My class looks like this:

TPrivClass = class
  public
    function func1(S: String): Boolean;
    function func2(S: String): Boolean;

MyClass = class
  private
    PrivClass: TPrivClass;
...
end;

I want people to be able to use func2 only, therefore I made the class private. But I can't just write
  property funcy1[S; String]: // referring to MyFunc2
because I want to do some validity check on S, say - lowercase it. So I add:

function MyClass.MyFunc2(S:String): Boolean;
begin
  Result := PrivClass.func2(AnsiLowerCase(S));
end;

but now I'm adding the overhead of calling a function.
And I have dozens of those things all around my project.
Even worse - sometimes my property calls another property of the private class, so I end up having 3 or 4 one-line functions calling each other.

Hope this explains my problem, and thanks for the effort,

  Westley.
0
 
LVL 1

Accepted Solution

by:
AndersWP earned 100 total points
ID: 1346962
Okay - now I think I get the picture. Alas, to the best of my knowledge, what you want to do is not possible. I do not think that you can avoid the overhead of a function call.
But look at the bright side: As you add validation code to your function, the significance of the function call overhead is diminished. Plus, your code gets more compact with function calls than with inline code. Unless timing is really critical, I would let it rest if I were you.

Greetings,
AndersWP
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question