metaprogramming

Can you nest C preprocessor directives?

白昼怎懂夜的黑 提交于 2019-12-22 09:17:32
问题 For instance, is the following possible: #define definer(x) #define #x? 回答1: Though your syntax is invalid, the answer to your question is technically yes. But it can only be accomplished by nasty tricks that make your code unreadable and unmaintainable. See also: http://www.ioccc.org/years.html#1995_vanschnitz and http://www.ioccc.org/years.html#2004_vik2 回答2: No, you can't do that. The pound ( # ) symbol has a different meaning while in a definition. it means - if this is an argument, make

Embedding arbitrary objects in Clojure code

亡梦爱人 提交于 2019-12-22 06:38:28
问题 I want to embed a Java object (in this case a BufferedImage) in Clojure code that can be eval d later. Creating the code works fine: (defn f [image] `(.getRGB ~image 0 0)) => #'user/f (f some-buffered-image) => (.getRGB #<BufferedImage BufferedImage@5527f4f9: type = 2 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=ff000000 IntegerInterleavedRaster: width = 256 height = 256 #Bands = 4 xOff = 0 yOff = 0 dataOffset[0] 0> 0 0) However you get an exception when trying to eval it: (eval

Is model driven architecture worth it and what is the state of the art in the tooling?

假如想象 提交于 2019-12-22 06:00:11
问题 We have a recurring problem in our shop where we end up with 3 or 4 different representations of the same class/entity. A java version, an xml version, a hibernate version, a json version... you get the point. Obviously this creates maintenance problems. Model driven architecture is probably more than this, but what I'd really like is a system that lets me define a class or an entity once, in one way, and then generate various representations. (maybe I am not using the correct terminology for

How to use 'default' value within template metaprogramming

不羁的心 提交于 2019-12-22 05:28:09
问题 I'm facing following problem: I have some generic container, that is able to do some operations on types. The operations are, for simplicity, thread safe, when requested to. And, requested to means that the type in container has typedef std::true_type needs_thread_safety; . struct thread_safe_item { typedef std::true_type needs_thread_safety; /* */ }; struct thread_unsafe_item { typedef std::false_type needs_thread_safety; /* */ }; template<typename TItem> container { /* some algorithms, that

How to swap two parameters of a variadic template at compile time?

房东的猫 提交于 2019-12-22 05:23:15
问题 I'm trying to swap two parameters of a variadic template at compile time : template<int...Numbers>struct sequence{}; template<size_t first,size_t second> struct Swap_Pair { const static size_t First = first; const static size_t Second = second; }; template <int...Numbers,class swap_pair> struct Swap_Data { static std::array<int, sizeof...(Numbers)> data_;//How to swap Numbers base on the pair and store it in data_ ? }; The use case should be : sequence<1, 2, 3, 4, 5, 6> array; auto result =

Defining __getattr__ and __getitem__ on a function has no effect

。_饼干妹妹 提交于 2019-12-22 04:48:26
问题 Disclaimer This is just an exercise in meta-programming, it has no practical purpose. I've assigned __getitem__ and __getattr__ methods on a function object, but there is no effect... def foo(): print "foo!" foo.__getitem__ = lambda name: name foo.__getattr__ = lambda name: name foo.baz = 'baz' Sanity check that we can assign properties to a function: >>> foo.baz 'baz' Neat. How about the "magic getters"? >>> foo.bar Traceback (most recent call last): File "<stdin>", line 1, in <module>

Dynamically replace method implementation on an object in Ruby

非 Y 不嫁゛ 提交于 2019-12-22 04:25:07
问题 I'd like to replace the implementation of a method for an object with a block that the user specifies. In JavaScript, this is easily accomplished: function Foo() { this.bar = function(x) { console.log(x) } } foo = new Foo() foo.bar("baz") foo.bar = function(x) { console.error(x) } foo.bar("baz") In C# it is also quite easy class Foo { public Action<string> Bar { get; set; } public Foo() { Bar = x => Console.WriteLine(x); } } var foo = Foo.new(); foo.Bar("baz"); foo.Bar = x => Console.Error

Meta-programming: output method body as text

半腔热情 提交于 2019-12-22 03:22:05
问题 I'm dynamically defining a method in a module, and I'd like to check that once the method is bound to a class instance that the body of the method is what I'm expecting. Is there a way to output (as text) of the body of a method? Module controller_mixins.rb : module ControllerMixin instance_eval "def search_by_vendor (*args) \n" \ " @#{self.class.name.sub(/Controller/, '').tableize} = #{self.class.name.sub(/Controller/, '')}.find_all_by_vendor_id(params[:vendor_id]) \n"\ "respond_to do

How to retrieve caller context object in Ruby?

陌路散爱 提交于 2019-12-21 20:27:27
问题 hereafter is my piece of code that I want to simplify in order to avoid passing an extra argument on each call. In fact, my usecase is that M is a user library without the definition of context argument on each method. check is a method that is not defined by the user. # User code module M def do_something(context) puts "Called from #{context}" context.check end module_function :do_something end # Application code class Bar def check puts "Checking from #{self}..." end end class Foo < Bar def

When should I use “class Object”, “class Module”, “module Kernel” and nothing?

a 夏天 提交于 2019-12-21 19:56:28
问题 I'm new to ruby metaprogramming, and I see people metaprogramming code in different places, like class Object , class Module , module Kernel and "nothing" (ie, out of a class/module definition block). E.g.: I'm creating a c_attr_accessor method to access class variables, and I'm not sure where I must put the code, since it works in any of those cases. How to decide what place is more appropriate to put a new global code? 回答1: Each of these examples fall into different cases. If you are