-- A more complete answer since your through with your test. And yes
-- you can copy and paste this code into a a file and use the gnat
-- system to compile/bind/link and then excute the program.
--
-- In most cases there are a number of ways to create code. Some are
-- more direct or aids the compiler to help you while others are more
-- indirect or toward the system performance and code usage. I did have
-- seven ways of answering the question but the other 3 were too
-- complex for a beginner. Even one use the generic package
-- caled "System.Address_To_Access_Conversions"
--
-- But in true Generic the function should be able to handle all
-- types of calls both Ada and non-Ada procedure.
--
-- In GNAT both Access and Address are the same but with other
-- system where memory is protected or even using seperate
-- memories this may not be the case.
--
with Ada.text_IO ;
with System ;
--
-- see function_3, might be too complex.
--
with Ada.Unchecked_Conversion ;
procedure zzero is
--
-- This allows all types of procedures both Ada and external
-- with or without parameters. Creates a static procedure
-- design effect.
--
function function_0 ( func : System.Address ; Data : Float )
return float is
procedure tst ;
pragma Import ( Ada, tst ) ;
for tst'address use func ;
begin -- function_0
tst ;
return Data * 2.0 ;
end function_0 ;
------------------------------------------------------------------------------
-- The Probability the answer that she ask for: --
-- --
-- type proc_access is access procedure ; -- is the generic statement --
-- -- that she needed. --
-- --
-- tst.all ; -- statement to call set --
-- -- procedure --
-- --
------------------------------------------------------------------------------
--
-- This version allows only procedures that are Ada without
-- parameters.
--
type proc_access is access procedure ;
function function_1 ( tst : proc_access ; Data : Float )
return float is
begin -- function_1
tst.all ;
return Data * 2.0 ;
end function_1 ;
--
-- This version allows only procedures that are Ada without
-- parameters. Creates a static procedure design effect for
-- calling procedure.
--
type func_access is access procedure ;
function function_2 ( func : func_access ; Data : Float )
return float is
procedure tst ;
pragma Import ( Ada, tst ) ;
for tst'address use func.all'address ;
begin -- function_2
tst ;
return Data * 2.0 ;
end function_2 ;
--
-- This version allows only procedures that are Ada without
-- parameters. And also use another Generic package to
-- convert the addressing schemes that creates a static
-- procedure design effect. But shows that both can be used
-- in the same package.
--
type procedure_access is access procedure ;
function function_3 ( func : procedure_access ; Data : Float )
return float is
-- --------------------------------------- --
-- use for calling method version two only --
-- --------------------------------------- --
--
-- limit this conversion function to function_3 usage only
--
function PA_To_A is new Ada.Unchecked_Conversion
( Source => procedure_access,
Target => System.Address ) ;
procedure tst ;
pragma Import ( Ada, tst ) ;
for tst'address use PA_To_A ( func ) ;
begin -- function_3
--
-- simple dynamic call version method 1
--
func.all ;
--
-- simple static call version method 2
--
tst ;
return Data * 2.0 ;
end function_3 ;
-- ---------- --
-- Test: Proc --
-- ---------- --
procedure test is
begin -- test
Ada.Text_IO.Put_Line ( "Testing" ) ;
end test ;
Result : Float ;
begin
Result := function_0 ( test'address, 0.5 ) ;
--
Result := function_1 ( test'access, 0.7 ) ;
Result := function_2 ( test'access, 0.9 ) ;
--
Result := function_3 ( test'access, 0.1 ) ;
end zzero ;
Post by s***@gmail.comPost by anonPost by s***@gmail.comHi, First, I'm a female.
Sorry about calling you a guy, it is an easy mistake to make on the net.
Post by s***@gmail.comPlease don't post any more answers or such.
If you look here I will always answer a question with code if
possible! Its just the way I am. I even jump of people here for not
posting code. Because in a test my code or anyone else code is
not going to help, unless you understand it! And the only way is to
see the code.
Post by s***@gmail.comI understand according to what you wrote that I probably did not fully
understand the generics mechanism. In this case, even if you will
answer my question, it will not help me because my exam starts in an
hour.
I posted the question here, after trying to figure things out by my
self, and I had no intention to insult any one of you or to take
advantage of you.
No insult or advantage of taken!
Post by s***@gmail.comIn a way, I'm not worried of not passing the exam. The worse thing
that can happen is that I will take the course again, and gain my
knowledge from the teacher as well as from the books, because studying
ADA by your self is not simple.
Actually, installing a free GNAT Ada version and starting to program
is the BEST way to learn. Teachers only have time to give you an
brief understand of any language. In the US that less than 45 hours
spanning 15 weeks. Outside programming is the only way to gain a
deeper understanding. And asking question with an expectation of an
answer is the second best way to gain a direction for that deeper
understand.
Plus, in my many years experience if a person just say read a section
or chapter than they also do not understand the problem or the
language. Even my professor would not do that, they would give an
example (like I did) and then assign someone an extra assignment that
would include the example as a small part of the whole project.
The only thing was their projects, either homework or extra
assignments were too simple for me, (bored me to death). I actually
had to expand them to make the worth my time. My Ada professor
use most of my work in Ada to teach his other classes at two
universities and wrote a number of text books using my classwork
that were used in both Europe and US universities.
What I did for my first Ada assignment after modifying it, was
rated at doctoral level programming. I even had to have permission
from 3 universities and their computer systems to fully execute the
assignment.
Post by s***@gmail.comThanks a lot for your help.
I hope that some day I will be able to join this group as an equal
member and not just a student.
I kind of believe that 50+ % of this group is students.
Hi, Thanks for your kind response.
I just came back from the exam. It was not very difficult but I can't
say it was easy. I don't believe I passed, but it's ok. I don't mind
attending the course again, Ada is a nice and very powerful language,
and I wouldn't mind getting to know it better.
I'm going to get some rest now. Thanks :)