metaprogramming

What makes a CanvasRenderingContext2D a CanvasRenderingContext2D?

会有一股神秘感。 提交于 2021-02-08 07:57:02
问题 Consider the following web page. <html> <body> <canvas id="canvas" width="300" height="300" style="border:1px solid #000000;"> </canvas> </body> </html> I open this page in Firefox, open the JS console and type the following. > document.getElementById("canvas").getContext("2d") The output is as follows: CanvasRenderingContext2D { canvas: canvas#canvas, mozCurrentTransform: (6) […], mozCurrentTransformInverse: (6) […], mozTextStyle: "10px sans-serif", mozImageSmoothingEnabled: true,

How do you access private methods or attributes from outside the type they belong to?

末鹿安然 提交于 2021-02-07 11:43:06
问题 In some rare cases where this would actually be acceptable, like in unit tests, you may want to get or set the value of a private attribute, or call a private method of a type where it shouldn't be possible. Is it really impossible? If not, how can you do it? 回答1: There are two ways you can access a private method of a type, and one way to get private attributes. All require meta-programming except for the first way to invoke private methods, whose explanation still involves meta-programming

N-dimensionally nested metaloops with templates

丶灬走出姿态 提交于 2021-02-07 05:51:07
问题 I am trying to do N-dimensionally nested metaloops with template metaprogramming. The nesting part is trivial, however passing all the arbitrary number of iteration indices as template parameters to the most-inner loop seems problematic. A simple unnested metaloop looks like: template <size_t I, size_t N> struct meta_for { template <typename Lambda> inline meta_for(Lambda &&iteration) { iteration(I); meta_for<I+1, N> next(static_cast<Lambda&&>(iteration)); } }; template <size_t N> struct meta

Rewriting Java native methods using ASM

*爱你&永不变心* 提交于 2021-02-07 04:55:30
问题 I'm trying to do this by re-writing the bytecode of the class using ASM 4.0 to replace all the native methods with non- native stubs. So far I have this: class ClassAdapter extends ClassVisitor { public ClassAdapter(ClassVisitor cv) { super(Opcodes.ASM4, cv); } @Override public MethodVisitor visitMethod(int access, String base, String desc, String signature, String[] exceptions) { return cv.visitMethod(access & ~Opcodes.ACC_NATIVE, base, desc, signature, exceptions); } } which is executed by

Filter the types of a parameter pack

旧街凉风 提交于 2021-02-06 11:25:25
问题 I'd like to know if it's possible to filter the types passed to a variadic template (based on a predicate template) to produce another variadic template containing those types which satisfy the predicate: /** Filter a parameter pack */ template <template <class> class, template <class...> class, class...> struct filter; template <template <class> class Pred, template <class...> class Variadic> struct filter<Pred, Variadic> : Variadic<> {}; template <template <class> class Pred, template

Filter the types of a parameter pack

爱⌒轻易说出口 提交于 2021-02-06 11:24:39
问题 I'd like to know if it's possible to filter the types passed to a variadic template (based on a predicate template) to produce another variadic template containing those types which satisfy the predicate: /** Filter a parameter pack */ template <template <class> class, template <class...> class, class...> struct filter; template <template <class> class Pred, template <class...> class Variadic> struct filter<Pred, Variadic> : Variadic<> {}; template <template <class> class Pred, template

Filter the types of a parameter pack

孤街浪徒 提交于 2021-02-06 11:23:33
问题 I'd like to know if it's possible to filter the types passed to a variadic template (based on a predicate template) to produce another variadic template containing those types which satisfy the predicate: /** Filter a parameter pack */ template <template <class> class, template <class...> class, class...> struct filter; template <template <class> class Pred, template <class...> class Variadic> struct filter<Pred, Variadic> : Variadic<> {}; template <template <class> class Pred, template

Check whether method named in a string is defined before calling it with send

倖福魔咒の 提交于 2021-01-29 16:00:46
问题 #!/usr/bin/env ruby def say_hi puts 'hi' end greeting = 'say_hi' send greeting # works greeting = 'say_hix' send greeting # undefined method `say_hix' for main:Object (NoMethodError) Thus in case of a typo I want to first check if the method exists; something like: send greeting if greeting.is_a_method 回答1: You can use respond_to? Returns true if obj responds to the given method. Private and protected methods are included in the search only if the optional second parameter evaluates to true.

crystal-lang : how to fill an array with all modules defined in files in a specific package of the project?

℡╲_俬逩灬. 提交于 2021-01-29 09:20:34
问题 All in the question, I would like to know if it's possible to do something like this in Crystal (pseudo-code) : modules = Array(Module).new foreach (file in specific_package) do if (file.is_a(crystal_module) do modules.push(file.module) end end 回答1: Crystal doesn't have packages but modules. If you want to get all modules in a file you can try this: require "compiler/crystal/syntax" Modules = [] of String class ModuleVisitor < Crystal::Visitor def visit(node : Crystal::ModuleDef) Modules <<

Iterate transition table (boost:msm) in runtime in order to read the associated event to an specific transition

强颜欢笑 提交于 2021-01-29 05:11:37
问题 I am new in MPL and Finite MSM from boost. I would like to iterate the transition table in order to get the associated transition event given 2 the transition states (current, next). I define and I get the transition table (vector50) like this: struct<my_state_machine_>{ (...) struct transition_table : boost::mpl::vector50< a_row < StateA ,eventAB ,StateB ,&my_machine_state_::action>, (...) > {}; }; typedef boost::msm::back::state_machine<my_state_machine_> my_fsm; typedef boost::msm::back: