I've obtained a function pointer at run-time, let's say through something like GetProcAddress (so a weakly-typed void*), and then I've got this metadata that tells me what the function's signature is at run-time. How can I call the function correctly, preferably in Standard code? C++0x solution is fine, and I don't mind having to enforce my own type-safety.
Er... no, you can't. There is no thing like reflection is C++ or C++0x, you can't get a type out of a string or whatever. Unfortunately :)
Related
Matrix<T, Dynamic, Dynamic>::InnerIterator doesn't seem to use the standard way (operator *) to dereference it.
It has a member function value().
I am rather surprised, as this would not interface well with third party algorithms, like STL algorithms.
Why it doesn't declare operator *? Even worse is that value() does not return by reference, so you can't change the underlying value of the pointed element. What is the proper way to use it?
This is because most of the times, the value alone in useless without the respective inner/row/column indices, as returned by index()/row()/col(). In other cases, you only care about the indices. Anyway, you can easily write a little wrapper to make it compatible with STL if needed.
Finally, if you want to modify the value, there is a valueRef() method returning by non const reference.
So I have a thing.
type Thing is new record
...elements...
end record;
I have a function which stringifies it.
function ToString(t: Thing) returns string;
I would like to be able to tell Ada to use this function for Thing'image, so that users of my library don't have to think about whether they're using a builtin type or a Thing.
However, the obvious syntax:
for Thing'image use ToString;
...doesn't work.
Is there a way to do this?
I don’t know why the language doesn’t support this, and I don’t know whether anyone has ever raised a formal proposal that it should (an Ada Issue or AI). The somewhat-related AI12-0020 (the 20th AI for Ada 2012) includes the remark "I don't think we rejected it for technical reasons as much as importance”.
You can see why the Ada Rapporteur Group might think this was relatively unimportant: you can always declare an Image function; the difference between
Pkg.Image (V);
and
Pkg.Typ’Image (V);
isn’t very large.
One common method is to create a unary + function...
function "+"(item : myType) return String;
which is syntactically very light.
Obvious disclaimer: may lead to some ambiguity when applied to numeric types (e.g. Put (+4);)
However there is still the distinction between prebuilt types and user defined types.
'img wouldn't be able to be used by your client's code though, unless you specified an interface that enforced this function to be present (what if the client called on 'img for a private type that didn't have a 'img function defined?).
If you end up having to have an interface, it really doesn't matter what the function is called.
Can anyone point me to documentation for CodeAuthzpComputeImageHash() in advapi32.dll?
I can't seem to find documentation anywhere.
The reason you can't find documentation for this function is that this function is undocumented.
Not all winapi functions are documented, unfortunately.
There is a mention of the function at http://technet.microsoft.com/en-us/library/cc786941(v=WS.10).aspx, though:
ItemData (REG_BINARY). The actual hash to the file. This value should always be 16 bytes and is generated with a call to CodeAuthzpComputeImageHash().
Also, you can use a trampoline to hook the function, and if you know how to cause it to be executed, you can then try to see its arguments and return type.
Also, try searching at WINE if they have implemented it. Ask in their mailing list. Since they try to implement winapi, they need to implement the undocumented parts, too, there's a good chance they have some understanding about the function.
I want to do something similar to how, in GCC, you can do syntax checking on printf-style calls (to make sure that the argument list is actually correct for the call).
I have some functions that take a variable number of parameters. Rather than enforce what parameters are sent, I need to ensure that the last parameter passed is a NULL, regardless of how many parameters are passed-in.
Is there a way to get GCC to do this type of syntax check during compile time?
You probably want the sentinel function attribute, so declare your function like
void foo(int,double,...) __attribute__((sentinel));
You might consider customizing your GCC with a plugin or a MELT extension to typecheck more precisely your variadic functions. That is, you could extend GCC with your own attributes which would do more precise checks (or simply make additional checks based on the names of your functions).
The ex06/ example of melt-examples is doing a similar check for the jansson library; unfortunately that example is incomplete today October 18th 2012, I am still working on it.
In addition, you could define a variadic macro to call such a function by always adding a NULL e.g. something like:
#define FOO(N,D,...) foo((N),(D),##__V_ARGS__,NULL)
Then by coding FOO(i+3,3.14,"a") you'll get foo((i+3),(3.14),"a",NULL) so you are sure that a NULL is appended.
Basile Starynkevitch is right, go with a function attribute. There are a ton of other useful function attributes, like being able to tell the compiler "If the caller doesn't check the return value of this function, it's an error."
You may also want to see if splint can check for you, but I don't think so. I think it would have stuck in my memory.
If you haven't read over this page of GCC compiler flags, do that, too. There are a ton of handy checks in there. http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
I've heard that recent versions of gcc are very good at converting calls through function pointers to direct calls. However, I can't find anything about it on the web or the quick look through gcc's source code. Does anyone know if this is actually true and if so, what algorithm does it use to do this?
You might find this article interesting. It's dated 2005, and I'm not sure if that's 'recent' enough, but it deals with the subject comprehensively:
http://www.codeproject.com/KB/cpp/FastDelegate.aspx
It's just a form of value propagation. If I can prove that object pointer p (to a virtual calss) always points to an object of a particular concrete class, then I can call that class's member functions directly. If not, I have to go through the vtable.