metaprogramming

How do you write code whose logic is protected against future additional enumerations?

I'm having a hard time describing this problem. Maybe that's why I'm having a hard time finding a good solution (the words just aren't cooperating). Let me explain via code: // original code enum Fruit { Apple, Orange, Banana, } ... Fruit fruit = acquireFruit(); if (fruit != Fruit.Orange && fruit != Fruit.Banana) co...

How to list all methods of an object in ActionScript 3?

How do I get the list of all methods from an object? I know I can get the object class in this way: var className:String = flash.utils.getQualifiedClassName( myObject ); var objClass:Class = flash.utils.getDefinitionByName( className ) as Class; It gives me an the class prototype, but and can't do anything with it... In JavaScript I c...

Difference between Module#const_set and Module#module_eval

OK, real quick, here's the code: class A def foo FOO end def self.foo FOO end end module B class C < A end end B.const_set(:FOO,'asdf') >> B::C.foo NameError: uninitialized constant A::FOO from ./foo.rb:6:in `foo' from (irb):1 >> B.module_eval {FOO='asdf'} => "asdf" >> B::C.foo => "asdf" Aren't they suppos...

Structure or Class with variable number of members

I want to create a structure/class with a variable number of class members which could be decided at compilation stage (like done in template metaprogramming) Example : Its hypothetical in which both type and variable names are to be specified like Type T1 variable name should be varName1 and so on ..... template <class T1 (varName1) >...

Python: how to use value stored in a variable to decide which class instance to initiate?

I'm building a Django site. I need to model many different product categories such as TV, laptops, women's apparel, men's shoes, etc. Since different product categories have different product attributes, each category has its own separate Model: TV, Laptop, WomensApparel, MensShoes, etc. And for each Model I created a ModelForm. Hence ...

Django decorator, adding method to WSGIRequest

Hello Djangoists, Using a decorator I was trying to add a method to WSGIRequest request, just like is_ajax(). Since I could not find a proper way I just updated request.META with the info needed. Should I look into adding method at runtime in Python ? ...

Javascript Metaprogramming

Is there a way to specify something similar to the following in javascript? var c = {}; c.a = function() { } c.__call__ = function (function_name, args) { c[function_name] = function () { }; //it doesn't have to capture c... we can also have the obj passed in return c[function_name](args); } c.a(); //calls c.a() directly c.b()...

Descriptor that auto-detects the name of another attribute passed to it?

Can a descriptor auto-detect the name of an object passed to it? class MyDecorator( object ): def __init__(self, wrapped): # Detect that wrapped's name is 'some_attr' here pass class SomeClass( object ): some_attr = dict() wrapper = MyDecorator( some_attr ) ...

Getting/setting an argument's default value dynamically

Start with the following scenario: class Foo def bar(baz={}) p baz end end foo = Foo.new p meth = foo.method(:bar) # => #<Method: Foo#bar> p meth.parameters # => [[:opt, :baz]] So I can figure out that the method bar is optional, but how do I find the default value ({}) for the method? ...

Python Metaclass to Parametrize Inheritance

I've read some tutorials on Python metaclasses. I've never used one before, but I need one for something relatively simple and all the tutorials seem geared towards much more complex use cases. I basically want to create a template class that has some pre-specified body, but takes its base class as a parameter. Since I got the idea fr...

How do I validate template parameters in compile time when a templated class contains no usable member functions?

I have a following templated struct: template<int Degree> struct CPowerOfTen { enum { Value = 10 * CPowerOfTen<Degree - 1>::Value }; }; template<> struct CPowerOfTen<0> { enum { Value = 1 }; }; which is to be used like this: const int NumberOfDecimalDigits = 5; const int MaxRepresentableValue = CPowerOfTen<NumberOfDecimalDigits>...

ruby metaprogramming - getting method names and parameter info for a class

Hi All, I want to get class methods in an object. Please see the following example I have a class "user.rb" class User def say_name end def walk(p1) end def run(p1, p2) end end and I wrote the following code require 'user.rb' a = User.new arr = a.public_methods(all = false) Above code will return the method ...

Can a C program modify its executable file?

I had a little too much time on my hands and started wondering if I could write a self-modifying program. To that end, I wrote a "Hello World" in C, then used a hex editor to find the location of the "Hello World" string in the compiled executable. Is it possible to modify this program to open itself and overwrite the "Hello World" strin...

How is swap implemented in c++

I just start learning metaprogramming, I wonder the implementation of swap. Can anyone help me and explain the idea of traits in metaprogramming? thanks. ...

What are methods of programmatically detecting many-to-many relationships in a RDMBS?

Hello stackoverflow, I'm currently busy making a Python ORM which gets all of its information from a RDBMS via introspection (I would go with XRecord if I was happy with it in other respects) — meaning, the end-user only tells which tables/views to look at, and the ORM does everything else automatically (if it makes you actually write s...

Metaprogram for bit counting

I need bit counter utility in C++ that is capable of counting number of the most significant bit in a numeric constant value and present this number as compile-time constant. Just to make everything clear - number of the most significant bit for a set of numeric values: 255 => 8 (11111111b) 7 => 3 (111b) 1024 => 11 (1000000000...

mem_fun_ref question.

Why is this code resulting in a compiler error? #include <iostream> #include <algorithm> using namespace std; class X { public: void Print(int x) { cout << x << endl; } }; int main() { X x; mem_fun_ref<void, X, int>(&X::Print) p; }; Error main.cpp:18: error: expected ; before p ...

Getting a list of classes that include a module

I have a mixin for which I would like to get a list of all the classes that have included it. In the mixin module, I did the following: module MyModule def self.included(base) @classes ||= [] @classes << base.name end def self.classes @classes end end class MyClass include MyModule end This works pretty well: ...

Can I override a constructor using metaprogramming limited to a scope in groovy?

I'd like to override the constructor of a class for testing. I can do it like this: SomeClass.metaClass.constructor = { Map params -> def instance = BeanUtils.instantiateClass(SomeClass) instance.apply(params) instance } That works, but I need the new constructor to apply to only some instances. In particular, I'd ...

Instance Eval in Javascript around browsers

From Coffeekup and JAML's source, (while working on question), we can see a way to hack ruby's instance eval into Javascript (JAML author explains more). It involves decompiling the function, and evaluating it around a with block. The question is: is this supported all around browsers/js runtimes? I know it works on firefox, opera and c...