metaprogramming

Callback for classes defined inside a Module

家住魔仙堡 提交于 2020-02-02 02:45:28
问题 Ruby already has several built-in callbacks. Is there a callback for such case? Kinda like method_added, but for classes (or constants) inside a module, instead of instance methods inside a class. 回答1: As far as I know, there is nothing exactly like what you're describing. However, here's how you could create your own, using Class::inherited . module MyModule def self.class_added(klass) # ... handle it end class ::Class alias_method :old_inherited, :inherited def inherited(subclass) MyModule

Bind metafunction: accept both types and template template parameters (accept anything)

拥有回忆 提交于 2020-02-01 05:33:05
问题 I'm trying to write a Bind metaprogramming template helper metafunction that binds a template parameter to something. I have a working implementation for simple template metafunctions: template<typename T0, typename T1> struct MakePair { using type = std::pair<T0, T1>; }; template<template<typename...> class TF, typename... Ts> struct Bind { template<typename... TArgs> using type = TF<Ts..., TArgs...>; }; using PairWithInt = typename Bind<MakePair, int>::type; static_assert(std::is_same

Boost.Fusion run-time switch

我与影子孤独终老i 提交于 2020-01-31 07:09:08
问题 I am reading the type of an object from a file: enum class type_index { ... }; type_index typeidx = read(file_handle, type_index{}); Depending on the type index, I want to create a type (out of a list of possible types), and do something generic with it (the same generic code for each type): std::tuple<type1, type2, ..., typeN> possible_types; boost::fusion::for_each(possible_types, [&](auto i) { if (i::typeidx != typeidx) { return; } // do generic stuff with i }); That is: I have the same

Boost.Fusion run-time switch

ⅰ亾dé卋堺 提交于 2020-01-31 07:07:51
问题 I am reading the type of an object from a file: enum class type_index { ... }; type_index typeidx = read(file_handle, type_index{}); Depending on the type index, I want to create a type (out of a list of possible types), and do something generic with it (the same generic code for each type): std::tuple<type1, type2, ..., typeN> possible_types; boost::fusion::for_each(possible_types, [&](auto i) { if (i::typeidx != typeidx) { return; } // do generic stuff with i }); That is: I have the same

C++ Array Subscript Operator Template

让人想犯罪 __ 提交于 2020-01-28 06:34:32
问题 After trying to make access to a storage class a little easier, I ended up in a situation that I don't have a lot of knowledge on. And, finding people that are trying to do the same thing as me isn't easy. What I'm trying to do, is have a class that stores an array of values as strings internally, but allows simple type casting from the user's end. What I had planned on doing is use the array subscript operator to return whichever type they specify through a template. Although, it sounds a

Inheriting off of a list of templated classes, when supplied with the list of template arguments

倾然丶 夕夏残阳落幕 提交于 2020-01-24 05:53:04
问题 I'm trying to write some metaprogramming code such that: Inheriting from some class foo<c1, c2, c3, ...> results in inheritance from key<c1>, key<c2>, key<c3>, ... The simplest approach doesn't quite work because you can't inherit from the same empty class more than once. Handling the "..." portion isn't pretty (since it's copy-pasta), but works. Okay, so, here's the attempt: template<char c0, typename THEN, typename ELSE> struct char_if { typename THEN type; }; template<typename THEN,

How to retrieve a class name?

本小妞迷上赌 提交于 2020-01-23 16:41:04
问题 I am using Ruby on Rails 3.0.7 and I would like to retrieve the class name, also if it is namespaced. For example, if I have a class named User::Profile::Manager I would retrieve the Manager string from that using some unknown to me Ruby or Ruby on Rails method and in a secure way. BTW : What other "usefull" information that are "commonly" used can I get for the class? 回答1: Some useful simple metaprogramming calls: user = User::Profile::Manager.new(some_params) user.class # => User::Profile:

Is there a relationship between untyped/typed code quotations in F# and macro hygiene?

最后都变了- 提交于 2020-01-23 07:20:31
问题 I wonder if there is a relationship between untyped/typed code quotations in F# and the hygiene of macro systems. Do they solve the same issues in their respective languages or are they separate concerns? 回答1: Quotations are a form of meta-programming. They allow you to manipulate abstract syntax trees programmatically, which can be in turned spliced into code, and evaluated. Typed quotations embed the reified type of the AST in the host language's type system, so they ensure you cannot

How to restore metaclass on object to original class definition

删除回忆录丶 提交于 2020-01-17 03:14:08
问题 I've been trying to create a TEMPORARY override on new objects, and then to remove the override on the objects themselves. I'm not sure if this can be done, but here is what I've tried so far. // Say I have a class like: class Validator { boolean validate() { println "code here to return actual true/false"; false } } // I have two integration points one of them is Here before construction: // First integration point: // Save actual validate function def realValidate = Validator.&validate //

Understanding the singleton class when aliasing a instance method

帅比萌擦擦* 提交于 2020-01-16 05:12:29
问题 I am using Ruby 1.9.2 and the Ruby on Rails v3.2.2 gem. I am trying to learn Metaprogramming "the right way" and at this time I am aliasing an instance method in the included do ... end block provided by the RoR ActiveSupport::Concern module: module MyModule extend ActiveSupport::Concern included do # Builds the instance method name. my_method_name = build_method_name.to_sym # => :my_method # Defines the :my_method instance method in the including class of MyModule. define_singleton_method(my