Obtaining untyped pointers to procedures - freepascal

I'd like to be able to obtain untyped pointers to functions/procedures while in {$MODE FPC} so code illustrated by the following example can work instead of getting an "Incompatible types" error, anyone know how?
program Project1;
{$MODE FPC}
{$MODESWITCH POINTERTOPROCVAR ON} // doesn't stop error
{$TYPEDADDRESS OFF} // doesn't stop error
function new_getmem(asize:longint):pointer;
begin
new_getmem:=nil;
end;
var
mem_mgr_new:tmemorymanager;
begin
mem_mgr_new.GetMem:=#new_getmem; // Error: Incompatible types...
pointer(mem_mgr_new.GetMem):=#new_getmem; // both those work but I'd prefer the
mem_mgr_new.GetMem:=pointer(#new_getmem); // clean code of the line that doesn't work
end.
EDIT:
There used to be an answer below that answered this properly, but it went away. The discussion allowed me to find out that both {$MODESWITCH CLASSICPROCVARS ON} and {$MODESWITCH POINTERTOPROCVAR ON} need to be specified for the desired line to compile, but like was explained, this opens up lots of possibilities for errors, so use with care only when necessary.

Related

Facing problem while performing basic STL vector program

I am trying to implement a basic STL vector program but I am getting an error I can't understand and can't find a convincing solution either.Here is the image of the code
Here is the error:
*no suitable conversion function from "__gnu_cxx::__normal_iterator<int , std::vector<int, std::allocator>>" to "int" exists
Also, if someone can, please explain me the problem too so that I can prevent this in future?
If you look at cppreference, you'll see that max_element returns an iterator to the maximum element in the sequence.
That's not an int; when you try to assign it to an int, it fails, and gives you a message telling exactly why it's failing.

Throw Exceptions in CODESYS

How can I throw standard system exceptions (and if possible, create custom exceptions and throw them too).
The simplest thing that comes to mind, is to just divide a variable by zero, but that's just a hack at best.
I did find mentions of a function AppGenerateException in the CmpApp library, but I can't find any documentation for it, nor any examples. Can anybody point me to some documentation for this?
Out of curiosity I checked the AppGenerateException and got it working. Got some help from this Codesys help page.
I still think this is not the best way to achieve what you need, but it could work.
Unfortunately, I have 64 bit system and the TRY..CATCH is not yet working in other that 32 bit systems as far as I know, so I couldn't test catching.
Add the following libraries:
CmpApp
SysExcept
Then the following code seems to work. Set ThrowErr to true to get the system exception.
PROGRAM PRG_TestThrow
VAR CONSTANT
MY_PRIVATE_EXCEPTION : DWORD := 32001;
END_VAR
VAR
ThrowErr : BOOL; //Set this to TRUE to throw an error
//The next two are for getting App instance
_pApp : POINTER TO CmpApp.APPLICATION;
_Result : SysExcept.SysTypes.RTS_IEC_RESULT;
END_VAR
//Get App instance
_pApp := AppGetCurrent(pResult := ADR(_Result));
IF ThrowErr THEN
ThrowErr := FALSE;
IF _pApp <> 0 THEN
//Throw divide by zero exception
AppGenerateException(pApp:=_pApp, ulException:=RtsExceptions.RTSEXCPT_DIVIDEBYZERO);
//You could use probably any available number as your own custom exception.
//For example the following works BUT not sure if it's ok to this.
//AppGenerateException(pApp:=_pApp, ulException:=MY_PRIVATE_EXCEPTION);
//See RtsExceptions type for available exception codes.
//For my Codesys version, it has largest exception number of 0x2000 so all larger number are free (for now...)
END_IF
END_IF
It seems that it's possibly to use any custom exception code like:
AppGenerateException(pApp:=_pApp, ulException:=MY_PRIVATE_EXCEPTION);
But it's not documented so I would be careful.

How do I debug lua functions called from conky?

I'm trying to add some lua functionality to my existing conky setup so that repetitive "code" in my conky text can be cleaned up. For example, I have information for each mounted FS, each core, etc. where each row displayed in my panel differs ONLY by one parameter.
My first skeletal, attempt at using lua functions for this seems to run but displays nothing in my panel. I've only found very simple examples to base this on, so I may have made a simple error, but I don't even know how to diagnose it. My code here is modeled after what I HAVE been able to find regarding writing functions, such as this How to implement a basic Lua function in Conky? , but that's about all the depth I've found on the topic except for drawing and cairo examples.
Here's the code added to my conky config, as well as the contents of my functions.lua file
conky.config = {
...
lua_load = '/home/conky-manager/MyConky/functions.lua',
};
conky.text = [[
...
${voffset 5}${lua conky_test 'test'}
...
]]
file - functions.lua
function conky_test(parm1)
return 'result text'
end
What I would expect is to see is "result text" displayed in my panel at the location where that function call appears, but nothing shows.
Is there a log created by conky as it runs, or a way to provide some debug output? Even if I'd made a simple error here, I'd still like to have the ability to diagnose things as my code gets more complex.
Success!
After cobbling info from several articles together, I figured out my basic flaws -
1. Missing a 'conky_main' function,
2. Missing a 'lua_draw_hook_post' to invoke it, and
3. Realizing that if I invoke conky from a terminal, print statements in lua would appear there.
So, for anyone who sees this question and has the same issues, here's the corrected code.
conky.config = {
...
lua_load = '/home/conky-manager/MyConky/functions.lua',
lua_draw_hook_post = "main",
};
conky.text = [[
...
${lua conky_test 'test'}
...
]]
and the proper basics in my functions.lua file
function conky_test(parm1)
return 'result text'
end
function conky_main()
if conky_window == nil then
return
end
end
A few notes:
I still haven't determined if using 'lua_draw_hook_pre' instead of 'lua_draw_hook_post' makes any difference, but it doesn't seem to in this example.
Also, some examples showed actually calling this 'test' function instead of writing a 'main', but the 'main' seemed to have value in checking to see if conky_window existed.
Some examples seemed to state that naming functions with the prefix 'conky_' was required, but then showed examples of calling those functions without the prefix, so I assume the prefix is inferred during the call.
a major note: you should run conky from the directory containing the lua scripts.

IDL Function Is Reluctant To Define

Happy Monday Everyone!
So. I'm really, really new to IDL. I need to translate a program I have written in Python to IDL, and I can barely get it started.
I am trying to define a function, but I am given the following error each time I try to compile it.
% Compiled module: OSTN02.
% Compiled module: OSTN02.
% Attempt to call undefined procedure: 'OSTN02'.
% Execution halted at: $MAIN$
I have tried following the guide from Harris Geospatial, but I am getting nowhere.
The code is below:
FUNCTION OSTN02, DATA, EASTCOL, NORTHCOL
;MAY NEED TO ADD FILLNaN HERE
DATAFILE = READLIS(FILE = !DATA_DIR + 'PROJECT ONE/OSTN15_OSGM15_DataFile.CSV', SEP = ',')
RETURN, DATAFILE
STOP
END
Any help is much appreciated. Thank you.
The error message is telling you:
% Attempt to call undefined procedure: 'OSTN02'.
You have defined a function, but IDL is looking for a procedure (because you are calling it as a procedure). The call to your function should be:
datafile = ostn02(data, eastcol, northcol)
although you aren't using those parameters, so you might want to remove them from your function.

How to throw exception in a .oct file in octave?

I am currently developing geotiff reading and writing functions for octave using .oct files. I went through the octave documentation but could not find much on throwing exceptions. Does that mean I can throw exception the way I do it in C++ by just simply writing throw "error message"?
There are two ways, admittedly they are documented in two utterly separate places, not cross-linked/cross-referenced, which makes no sense, and if you didn't know the function/keyword you wouldn't find them:
error() raises an error, which stops the program. See 12.1 Raising Errors.
error("[%s] Here be wyrms", pkgname)
assert() both tests the condition then raises the error() with a customizable message (so don't do if (cond) ... error(...) ... endif).
See B.1 Test Functions.
% 1. Produce an error if the specified condition is zero (not met).
assert (cond)
assert (cond, errmsg)
assert (cond, errmsg, …)
assert (cond, msg_id, errmsg, …)
% 2a. Produce an error if observed (expression) is not the same as expected (expression); Note that observed and expected can be scalars, vectors, matrices, strings, cell arrays, or structures.
assert (observed, expected)
% 2b. a version that includes a (typically floating-point) tolerance
assert (observed, expected, tol)
See also the command fail()
Yes, you could just use something like
error ("mynewlib: Hello %s world!", "foo");
to signal errors which are catched and viewed.
(Personally I think such questions should really go to the GNU Octave mailing list where you'll find the core developers and octave-forge package maintainers).
I guess you want to build a wrapper around libgeotiff? Have a look at the octave-image package! Where do you host your code?
./examples/code/unwinddemo.cc might also be interesting for you. It shows how to use unwind_protect and define user error handlers.
http://hg.savannah.gnu.org/hgweb/octave/file/3b0a9a832360/examples/code/unwinddemo.cc
Perhaps your function should then be merged into the octave-forge mapping package: "http://sourceforge.net/p/octave/mapping/ci/default/tree/"