introspection

Boost Fusion: convert adapted struct type to text

蹲街弑〆低调 提交于 2019-12-04 11:31:59
Given a struct like this: struct Foo { int x; int y; double z; }; BOOST_FUSION_ADAPT_STRUCT(Foo, x, y, z); I want to generate a string like this: "{ int x; int y; double z; }" I have seen how to print the values of a Fusion adapted struct, but here I need to print the types and names only. How can I do this mostly simply? I'm not married to Boost.Fusion if there's a better way. llonesmiz I think you can get something similar to what you want by making some slight modifications on the code in this answer . You can easily get the member name using boost::fusion::extension::struct_member_name but

WinRT Reflection (C++/CX)

梦想的初衷 提交于 2019-12-04 11:07:44
how can I introspect an object in C++/CX? I known how to get its class name (using IInspectable) but I wasn't able to figure out how to get a list of its properties or how to invoke methods if I have just a name of the method (string). I searched for an answer here and at Google but what I found is related to the .NET layer of WinRT (the System.Reflection namespace doesn't seem to be available in C++/CX). C++ doesn't provide any specific APIs to reflect on WinRT types, these types are fully defined in CX compliant metadata files and you can use the CLR native metadata APIs to read their

Introspection and iteration on an Enum

半世苍凉 提交于 2019-12-04 09:20:34
Is it possible to programatically find out how many "cases" an Enum has in Swift 2 and iterate over them? This code doesn't compile, but it gives yo an idea of what I'm trying to achieve: enum HeaderStyles{ case h1 case h2 case h3 } for item in HeaderStyles{ print(item) } You can write a generic struct that provide that iteration ability. In example below the enum raw values must start with 0 (it does it by default) and have no gaps between raw values (can't have raw values such as 0,1,2,3,5 -- 4 is missing) public struct EnumGenerator<T> : GeneratorType, SequenceType { private let enumInit:

Haskell: Function to determine the arity of functions?

血红的双手。 提交于 2019-12-04 08:56:33
问题 Is it possible to write a function arity :: a -> Integer to determine the arity of arbitrary functions, such that > arity map 2 > arity foldr 3 > arity id 1 > arity "hello" 0 ? 回答1: It's easy with OverlappingInstances : {-# LANGUAGE FlexibleInstances, OverlappingInstances #-} class Arity f where arity :: f -> Int instance Arity x where arity _ = 0 instance Arity f => Arity ((->) a f) where arity f = 1 + arity (f undefined) Upd Found problem. You need to specify non-polymorphic type for

define_method with predefined keyword arguments

大憨熊 提交于 2019-12-04 08:03:57
I want to define a method that takes keyword arguments. I would like it to raise when keyword arguments are not provided, and I can code this myself - but ideally I would like to let Ruby do that for me. Also I would like to be able to inspect the freshly defined method using Method#parameters . If I use a shorthand double-splat (like **kwargs ) the actual structure I expect is not visible to parameters . I can of course do this: define_method(:foo) do | foo:, bar: | # ... end which achieves the desired result: method(:foo).parameters # => [[:keyreq, :foo], [:keyreq, :bar]] but I cannot pass

Change reference to function in run-time in Python

随声附和 提交于 2019-12-04 07:53:21
I need to change a call to a function inside another function during run-time. Consider the following code: def now(): print "Hello World!" class Sim: def __init__(self, arg, msg): self.msg = msg self.func = arg self.patch(self.func) def now(self): print self.msg def run(self): self.func() def patch(self, func): # Any references to the global now() in func # are replaced with the self.now() method. def myfunc(): now() Then ... >>> a = Sim(myfunc, "Hello Locals #1") >>> b = Sim(myfunc, "Hello Locals #2") >>> b.run() Hello Locals #2 >>> a.run() Hello Locals #1 One user has written code, myfunc()

Java JDK 8 IndexedPropertyDescriptor has changed since JDK 7 with List object

懵懂的女人 提交于 2019-12-04 04:04:24
I have a simple issue. I have a program working in Java JDK7 but it doesn't work in JDK8 because of some introspection changes. Here is a test program to reproduce the issue: import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) throws IntrospectionException { BeanInfo info = Introspector.getBeanInfo(MyListClass.class); PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); for (int i = 0;

Printing names of variables passed to a function

人盡茶涼 提交于 2019-12-04 03:31:53
问题 In some circumstances, I want to print debug-style output like this: # module test.py def f() a = 5 b = 8 debug(a, b) # line 18 I want the debug function to print the following: debug info at test.py: 18 function f a = 5 b = 8 I am thinking it should be possible by using inspect module to locate the stack frame, then finding the appropriate line, looking up the source code in that line, getting the names of the arguments from there. The function name can be obtained by moving one stack frame

How do I loop over all the methods of a class in Perl?

北战南征 提交于 2019-12-04 02:38:19
How do you loop over all the methods of a class in Perl? Are there any good online references to Perl introspection or reflection? Dave Rolsky The recommendation Todd Gardner gave to use Moose is a good one, but the example code he chose isn't very helpful. If you're inspecting a non-Moose using class, you'd do something like this: use Some::Class; use Class::MOP; my $meta = Class::MOP::Class->initialize('Some::Class'); for my $meth ( $meta->get_all_methods ) { print $meth->fully_qualified_name, "\n"; } See the Class::MOP::Class docs for more details on how to do introspection. You'll also

How to introspect regexes in the Perl API

百般思念 提交于 2019-12-04 01:14:24
I'm working on some code that needs to serialize Perl regexes, including any regex flags. Only a subset of flags are supported, so I need to detect when unsupported flags like /u are in the regex object. The current version of the code does this: static void serialize_regex_flags(buffer *buf, SV *sv) { char flags[] = {0,0,0,0,0,0}; unsigned int i = 0, f = 0; STRLEN string_length; char *string = SvPV(sv, string_length); Then manually processes string char-by-char to find flags. The problem here is that the stringification of regex flags changed (I think in Perl 5.14) from e.g. (?i-xsm:foo) to (