metaprogramming

About using method "print" etc. in ERB for metaprogramming

I am using ERB via console for metaprogramming (for math software). For example, I have file test.erb containing text line before ruby <%= 'via <%=' %> <% print 'print' %> <% puts 'puts' %> text line after ruby When I parse it by $ erb test.erb, I get the following output printputs text line before ruby via <%= text line af...

Ruby - update class attributes hash when a property changes

Hi, I'm trying to write a ruby class that works similarly to rails activerecord model in the way that attributes are handled: class Person attr_accessor :name, :age # init with Person.new(:name => 'John', :age => 30) def initialize(attributes={}) attributes.each { |key, val| send("#{key}=", val) if respond_to?("#{key}=") } ...

Is it possible to transform the types in a parameter pack?

Is it possible to transform the types of a parameter pack and pass it on? E.g. given the following: template<class... Args> struct X {}; template<class T> struct make_pointer { typedef T* type; }; template<class T> struct make_pointer<T*> { typedef T* type; }; Can we define a template magic or something similar so that the follow...

lua - metamethod lookup through __index ?

I've implemented my own class system and I'm having trouble with __tostring; I suspect a similar issue can happen with other metamethods, but I haven't tried. (Brief detour: each class has a __classDict attribute, holding all methods. It is used as the class instances' __index. At the same time, the __classDict's __index is the supercla...

Rails: Overriding const_missing within a module

Within my Rails application I have a module defined this way: module WhateverModule class WhateverClass ... end end This file (whatever_class.rb) is located under /app/models/whatever_module const_missing is being overriden by Rails and despite I did some workarounds, involving initializers, I wish I could make it in a better...

C++ compile-time constant detection

Tbere're cases when a library source is available, and it has to support variable parameters in general, but in practice these parameters are commonly constants. Then it may be possible to optimize things by special handling of constant parameters (eg. use static arrays instead of heap allocation), but for that its necessary to determin...

In what ways can a class access members of another class?

Earlier, I asked a question on how to call a static member's member functions so as to initialize it before making actual use of the static object. Then, I realized that I was perhaps making use of the static member in a wrong way, which led to this question: Given a particular class, MyClass, in how many ways can we design our code so ...

C++ template meta-programming kung-fu challenge (replacing a macro function definition)

Situation I want to implement the Composite pattern: class Animal { public: virtual void Run() = 0; virtual void Eat(const std::string & food) = 0; virtual ~Animal(){} }; class Human : public Animal { public: void Run(){ std::cout << "Hey Guys I'm Running!" << std::endl; } void Eat(const std::string & food) { ...

How to make a less than comparison in template meta-programming?

I had this question asked of me on Monday and for the life of me I don't know how to answer. Since I don't know, I now want to very much find out. Curiosity is killing this cat. Given two integers, return the lesser at compile time. template<int M, int N> struct SmallerOfMandN{ //and magic happenes here }; Got pointers or how t...

How to extend Ruby ERB for handling %= tags as well?

I am using ERB for metaprogramming of some math language. If I could extend ERB functionality to handle %= tags, it would allow me to simplify my sources significantly. I simply want to get output of the line in analogy with <%= %>. I have tried to dig into /usr/lib/ruby/1.9.1/erb.rb file, but got lost very quickly. May be you can help w...

Combination of types using boost::mpl

I have a list of types, from which I want to construct the list of all combinations with two elements. For example: namespace mpl = boost::mpl; typedef mpl::vector<int, long> typelist; // mpl magic... // the wanted list is equivalent to: typedef mpl::vector<pair<int, int>, pair<int, long>, pair<long, int>, pair<long,...

What resources explain Reflection and Meta Programming in C#?

I've been programming in C# for a while now and would say I can use it and the .Net library fairly well. One huge gap in my knowledge though is how to use Reflection and Meta Programming with .Net/C#. Can someone point me to some good resources on the subject and how to get started with it? ...

is it possible to markup all programming languages under object oriented paradigm using a common markup schema?

i have planned to develop a tool that converts a program written in a programming language (eg: Java) to a common markup language (eg: XML) and that markup code is converted to another language (eg: C#). in simple words, it is a programming language converter that converts program written in one language to another language. i think i...

Testing function signatures with ParameterTypeTuple

I'm writing a module with mixin templates to supply a main function for unit-testing purposes. Usage is as follows: /* For modules without their own main, e.g. libraries. * main is conditionally supplied using version(unittest). */ mixin Main; /* For executable modules defining function "realMain". * Generated main has the same sig...

How can i change the __cmp__ function of an instance (not in class)?

How can i change the __cmp__ function of an instance (not in class)? Ex: class foo: def __init__(self, num): self.num = num def cmp(self, other): return self.num - other.num # Change __cmp__ function in class works foo.__cmp__ = cmp a = foo(1) b = foo(1) # returns True a == b # Change __cmp__ function in instance ...

Dynamically building up types in python

Suppose I am building a composite set of types: def subordinate_type(params): #Dink with stuff a = type(myname, (), dict_of_fields) return a() def toplevel(params) lots_of_types = dict(keys, values) myawesomedynamictype = type(toplevelname, (), lots_of_types) #Now I want to edit some of the values in myawesomedyna...

How can a class method (inside a module) update an instance variable?

How can a class method (inside a module) update an instance variable? Consider the code bellow: module Test def self.included(klass) klass.extend ClassMethods end module ClassMethods def update_instance_variable @temp = "It won't work, bc we are calling this on the class, not on the instance." puts "How can I ...

Partial specialization of existing metafunction using mpl

Maybe I'm not all there today, but I'm wondering how to get this to work. I'd like to partially specialize range_mutable_iterator and range_const_iterator from the boost library but only for specific types that I'd rather avoid mentioning explicitly, instead only letting the partial specialization be chosen if the enable_if test criteria...

Is it possible to match templated base in template specializations?

I could of course use is_base if the base class where not a template. However, when it is, I just don't see any way to generically match any derived type. Here's a basic example of what I mean: #include <boost/mpl/bool.hpp> template < typename T > struct test_base { }; template < typename T > struct check : boost::mpl::false_ {}; t...

Meta Programming, whats it good for?

So Meta Programming -- the idea that you can modify classes/objects at runtime, injecting new methods and properties. I know its good for framework development; been working with Grails, and that framework adds a bunch of methods to your classes at runtime. You have a name property on a User object, and bamm, you get a findByName metho...