Jump to content
joseLB

IsDeclared to detect FUNCtions

Recommended Posts

joseLB

Hi

Today I needed to know if Func Test1 exists or not.

At first glance Call ("Test1",...)  could be used ->  @error = 0xDEAD ,if Test1 does not exists.

The point is that if Test1 exists, I need to do some actions before call Test1, but only if Test1 exists.

My question: IsDeclared could not return 1 for variables and ALSO for functions? Or there is another solution?

Best Regards

Jose

Share this post


Link to post
Share on other sites
BrewManNH

#include <MsgBoxConstants.au3>

MsgBox($MB_SYSTEMMODAL, "", "Check if _Func1 is a function or not." & @CRLF & _
        @CRLF & _
        "This will return 1 as the function is user defined: " & IsFunc(_Func1))
Func _Func1()
EndFunc   ;==>_Func1

 

  • Like 1

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jchd

How can you ignore whether you created or not function XYZ in your own source and have to decide for that at runtime? Doesn't make sense to me.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
joseLB
 

BrewMan,Thanks you very much, but what a shame :>.    And believe it, I´m a heavy help user.

 

Edited by joseLB

Share this post


Link to post
Share on other sites
joseLB

How can you ignore whether you created or not function XYZ in your own source and have to decide for that at runtime? Doesn't make sense to me.

​Hy jchd, thanks for the question.

In fact, these funcs are in an #include file. They are supposed to be created (or not) by an "expert" user,    So, he can create or not an expected function.

But before to call it, I must check if he really created or not the function and gave it´s name in .INI.

If it exists, I must guidelete a "default"  Window before I call the function, -> the function creates a substitute window

If it does not exists, I shoud not  delete the "default" Windows.

It´s for a very parametric system, where user can create some control Windows. The original one is for the case he didn´t create the substitute one.

I tried to simplify the explanation, but it´s a more complex program.

Jose

Share this post


Link to post
Share on other sites
BrewManNH

Run Au3Check on the source and it will tell you that the function isn't there.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jchd

OK but when the include file is compiled along the bulk of your code, you do know (or can easily check by e.g. Au3Check) that the function exists.

 

Taken over by a faster keyboard!

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
jpm

In fact IsFunc() cannot be used.

from my point of view it would be coherent with other IsXXX() functions. Not sure that Jon will accept to change it.

Cheers

Edited IsDeclared->IsFunc bad typing from my part

Edited by jpm
  • Like 1

Share this post


Link to post
Share on other sites
gil900

Try this

#AutoIt3Wrapper_Run_AU3Check=n
#include <Array.au3>



Global $g_IFD_aDeclaredFunctions[0]



; Test
DeclareFunction('Func1')

If IsFuncDeclared('Func1') Then Func1()

If IsFuncDeclared('Func2') Then Func2()



Func Func1()
    ConsoleWrite('Func1' & @CRLF)
EndFunc

;Func Func2
;   ConsoleWrite('Func2' & @CRLF)
;EndFunc



; ________________________________ IsFuncDeclared and DeclareFunction ________________________________
Func IsFuncDeclared($sFuncName)
    If _ArraySearch($g_IFD_aDeclaredFunctions,$sFuncName) >= 0 Then Return True
    Return False
EndFunc


Func DeclareFunction($sFuncName)
    If _ArraySearch($g_IFD_aDeclaredFunctions,$sFuncName,1) >= 0 Then Return
    _ArrayAdd($g_IFD_aDeclaredFunctions,$sFuncName)
EndFunc
; --------------------------------------------------------------------------------------------------

 

Edit:

This is even simpler:

#AutoIt3Wrapper_Run_AU3Check=n


; Instead of using my DeclareFunction, just declare variable with the same name of the function
Global $Func1


; Now you can use IsDeclared for checking functions if you keep the above rule
If IsDeclared('Func1') Then Func1()

If IsDeclared('Func2') Then Func2()


Func Func1()
    ConsoleWrite('Func1' & @CRLF)
EndFunc

;Func Func2
;   ConsoleWrite('Func2' & @CRLF)
;EndFunc

 

Edit:

I din't know about IsFunc .

Sorry.. using IsFunc is much better method

Edited by gil900

Share this post


Link to post
Share on other sites
jpm

@gil900

Nice use of IsDeclared()

  • Like 1

Share this post


Link to post
Share on other sites
gil900

@gil900

Nice use of IsDeclared()

Glad you liked.
Here is more elegant & stable & faster method
:

#AutoIt3Wrapper_Run_AU3Check=n

; Example:
DeclareFunction('Func1')
If IsFuncDeclared('Func1') Then Func1()

If IsFuncDeclared('Func2') Then Func2()

Func Func1()
    ConsoleWrite('Func1' & @CRLF)
EndFunc

;Func Func2
;   ConsoleWrite('Func2' & @CRLF)
;EndFunc



; ####################################################################################################


; #FUNCTION# ====================================================================================================================
; Name ..........: DeclareFunction
; Description ...: Declare a function. to check later if the function was declared, just use the IsFuncDeclared() function
; Syntax ........: DeclareFunction($sFuncName)
; Parameters ....: $sFuncName           - A string value.
; Return values .: None
; Author ........: Your Name
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func DeclareFunction($sFuncName)
    Assign('gg_f'&$sFuncName,0,2)
EndFunc
; --------------------------------------------------------------------------------------------------

Func IsFuncDeclared($sFuncName)
    Return IsDeclared('gg_f'&$sFuncName)
EndFunc

 

Edited by gil900
  • Like 1

Share this post


Link to post
Share on other sites
joseLB

Hi all

Thanks for all suggestions. Follows my solution:

1 - First I tried this, that does not work. IsFunc is not consistent with other IsXxx functions, it uses another kind of variable (see help).

If IsFunc ("ONE") Then
    MsgBox(1,1,"function one exists. Doing something before call it")
    ONE()
Else
    MsgBox(1,0,"function ONE does not exists")

EndIf


Func ONE ($par=1)
    MsgBox(1,1,$par)
EndFunc

Func TWO ($par=2)
    MsgBox(1,2,$par)
EndFunc

2- Based on the suggestions, finally I did this (here is just the concept to simplify)

;*****global variables. Can be used by main program or included functions
Global $somevar= "global variable used in function"

;***** functions included from an external AU3 ==> ex: #include <includedFunctions.au3>
Func ONE ($par=1)
    MsgBox(4096,1,$par &@cr& $somevar)
EndFunc

Func TWO ($par=2)
        MsgBox(4096,2,$par &@cr& $somevar)
EndFunc


;***** main program
Local $F= "ONE | TWO"       ;this should be read from an external file, ex: .INI


$funcToCall= InputBox("ENTER FUNCTION NAME","FUNCTION")

If StringInStr($F, $funcToCall) Then
    MsgBox(1,$funcToCall,"function typed exists. Doing something before call it")
    Call ($funcToCall,"param. passed=" & $funcToCall)
Else
    MsgBox(1,"ERROR","function does not exists= " & $funcToCall)
EndIf

While this does not check for real func existence, after the Call, whe coud put an If @error =  0xDEAD then .... to see if it was really executed or not.

Edited by joseLB

Share this post


Link to post
Share on other sites
DougShields

Here's an abstracted, parameterized version that perhaps resembles a real-world solution.  In fact I think you could pretty easily make this into a plug-in API where your own default version of the function (InternalFunc1) calls the user supplied Func1 if it exists, otherwise it implements a default version of Func1.

AutoItSetOption("MustDeclareVars", 1)
#AutoIt3Wrapper_Run_Au3Check=n

Func Func1(Const $sParm1, Const $sParm2)
    MsgBox(0, "Func1", $sParm1 & @CRLF & $sParm2)
EndFunc   ;==>Func1

CallIfExists("Func1", "Hello")
CallIfExists("Func2", "Hello")

Func CallIfExists(Const $sFuncName, Const $sParm1, Const $sParm2 = "Goodbye.")
    Local $FuncToTest = Execute($sFuncName)
    If IsFunc($FuncToTest) Then
        Call($sFuncName, $sParm1, $sParm2)
    EndIf

EndFunc   ;==>CallIfExists

 

Share this post


Link to post
Share on other sites
BrewManNH

Hi all

Thanks for all suggestions. Follows my solution:

1 - First I tried this, that does not work. IsFunc is not consistent with other IsXxx functions, it uses another kind of variable (see help).

 

​That's not how it works, it uses a variable that is mapped to a function. You could use this function like this.

If IsFunc (ONE) Then ; <<<<<<<<<< No quotes
    MsgBox(1,1,"function one exists. Doing something before call it")
    ONE()
Else
    MsgBox(1,0,"function ONE does not exists")

EndIf


Func ONE ($par=1)
    MsgBox(1,1,$par)
EndFunc

Func TWO ($par=2)
    MsgBox(1,2,$par)
EndFunc

The purpose of IsFunc is realistically to test if a variable is assigned to a function, not to determine if that function exists in the script. Au3Check can tell you if you're calling a function that exists or not.

If you have a script that you're using and you're not sure if a function is in it that you're expecting then you're not doing things correctly. You would NEVER want to use this function to test whether or not you forgot to check whether the functions in your script are there or not. This function would be used to dynamically assign functions to variables at run time and then test that that function exists. Probably based on a command line parameter passed to a compiled script, or taken from a user's input during the script's run.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
BrewManNH

Hi all

Thanks for all suggestions. Follows my solution:

1 - First I tried this, that does not work. IsFunc is not consistent with other IsXxx functions, it uses another kind of variable (see help).

 

​That's not how it works, it uses a variable that is mapped to a function. You could use this function like this.

If IsFunc (ONE) Then ; <<<<<<<<<< No quotes
    MsgBox(1,1,"function one exists. Doing something before call it")
    ONE()
Else
    MsgBox(1,0,"function ONE does not exists")

EndIf


Func ONE ($par=1)
    MsgBox(1,1,$par)
EndFunc

Func TWO ($par=2)
    MsgBox(1,2,$par)
EndFunc

The purpose of IsFunc is realistically to test if a variable is assigned to a function, not to determine if that function exists in the script. Au3Check can tell you if you're calling a function that exists or not.

If you have a script that you're using and you're not sure if a function is in it that you're expecting then you're not doing things correctly. You would NEVER want to use this function to test whether or not you forgot to check whether the functions in your script are there or not. This function would be used to dynamically assign functions to variables at run time and then test that that function exists. Probably based on a command line parameter passed to a compiled script, or taken from a user's input during the script's run.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
kylomas

joseLB,

IF you are trying to create self modifying code then you are going down the wrong path.  Perhaps you could explain a little more what you are doing.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
joseLB

Hi kylomas

It´s almost that, but not exactly. It´s a system that receives lots of data thru serial line from a bunch of PIC´s using ADC.

 After all these great sugestions, I´m something similar at what I described above. Follows a description of the system. I will try to simplify explanation to not bother all you, as I found a resonable solution for my specific case. But the discussion is going very well I think. In resume, a way to detect if a func exists or not.

So, In order to test the system previously in many environments, it has a simulation module that generate data that should come from serial line in real life. Depending what I want to test, I must do a distinct routine to generate a specific set of of data. For example, sinusoids, noise (random numbers), etc. and sometimes needs a window to interact with user.

So, as these routines are independent from the system, I did an #Include module, where I´m creating the routines, as needed. At the same time, I put their names in an .INI file. Of course, I must compile the program each time I create a routine, or run it from scite. That´s ok on this case.

Now, when system starts, it has a "initial" window that asks user if he want to simulate or run real. If he wants simulation AND the routine he selected from .INI existis, the system shoud close the "initial" screen and opens the simulation window, if it exists (there are situations where there is no simulation window, just a fixed simulation).

So in order to decide the deletion of the "initial" window, the system must know if that simulation has or not a window, that is, if the func exists or not. If it exists, it must delete the "initial" window and call the simulation window/func. If there is no simulation window/func on this simulation, system just need to start the simulation and not erase the "initial" window.

Uff, I hope you understand my explanation/english...

Again thanks

Jose

Share this post


Link to post
Share on other sites
joseLB

**** my sugstion whoud be my initial one, to IsDeclared to detect it. The reason is somewhat a "natural" thinking, I mean, I want to kow if something (variable and why not functions) exists or not. As funcs are always "global", it shout return the same as for a global variable. Or, best, a distinct code, like 3. That is:

If IsDeclared( "xxx") = true then ... it would work in any case. 1 for local, 2 for global and 3 for funcs if I want to be more specific.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×