How to put lambda in a different section with GCC? - gcc

I'm using C++11 compiler to generate embedded code and I'd like to use lambda functions in it. However I need to put the actual generated code for my lambda functions in a different section or always inline it into the function/method where it is used (by default if some lambda function is not inlined it is put into '.text' section). Is there a way to set section attribute for the lambda function? Alternatively is there a way to make GCC compiler always inline lambda functions?

This will put the lambda in the .mylambdas section
auto myLambda = [&]() __attribute__((section(".mylambdas"))
{
...
};
Note : There's a bug with gcc where it complains if you use the same section name for lambdas in both regular class methods and const class methods. Clang is ok. One workaround is to use another section name for const methods, if that's possible.

Related

How do I define myself how functions must use my objects when making them available to theses functions (using my object at the end of the spec)?

In this question, I've given examples of package/object that :
implement some functions.
are used to define other functions by only making them availaible to those functions. It means. I don't write a function body but only a spec with "using package_name" or "using package_body" in the end.
I suppose that the way to use my pakcage/object to implement another function is defined somewhere.
I would like to know if there a way to define myself how to use a package/object. I want a way to implement a functions without writing the body everytime but only by writing in the spec that the function is using my package.
What I'm asking for is similar to already implemented function in a interface in java or an extention method in c#.
P.S. : I don't think that title is very clear. I would be glad if someone would propose another title.
I want a way to implement a functions without writing the body everytime but only by writing in the spec that the function is using my package.
You cannot define a function/procedure that references an SQL stored function/procedure like that. If you declare a function or procedure then it must include the signature and the body.
From the documentation:
create_function::=
plsql_function_source ::=
For a standalone function the body is required.
For a call specification, you can reference a function but it will be an external Java method or a C function and NOT another function defined in SQL.

How to dynamically assemble a typelist for passing as a variadic template parameter

I have a function that takes a variadic template parameter as its input. What I really need to do is use some kind of magic container (typelist, tuple, etc.) to feed this function parameter. The main problem is that this magic container needs to be dynamically assembled at runtime based on inputs to previous function calls.
Standard tuple generation obviously cannot work in this environment, so I believe some type of wrapper or helper with some typename mangling is in order, but the way to do so eludes me. Some example pseudo-code of what I'm trying to do follows. The user will call AddComponent() any number of times to add a number of components to an owning manager. For each instance of AddComponent(), I need to store the passed in 'Component' type to the magic container so that I end up with a container of all the Component types that have been added. After all this, I need to call GetView() using the assembled typename list as the parameter to the variadic template. A tuple fits best here, but how to correctly assemble it? Here's the code:
template<typename Component, typename ... Args>
void Blueprint::AddComponent(ComponentUsage usage, Args&& ... args)
{
// Create component object with given args
// Add 'Component' type to magic container
}
template<typename ... Component>
EntityView<Component...> EntityManager::GetView()
{
// Create view from list of component types
}
What you're describing sounds a lot like a builder pattern and you can get similar behavior with syntax like this:
// view would be EntityView<Location, Enemy, Flying>
auto view = makeBlueprint()
.AddComponent<Location>(...)
.AddComponent<Enemy>(...)
.AddComponent<Flying>(...)
.GetView();
This would use a dynamic builder where each component added would create a slightly different builder like Builder<> then .AddComponent<Location>() would return a Builder<Location> and then Builder<Location, Enemy> and so on.
However, this is still does not allow for dynamic typing; something like this would not work:
auto blueprint = makeBlueprint()
.AddComponent<Location>(...)
.AddComponent<Enemy>(...);
if (... some check ...)
blueprint = blueprint.AddComponent<Flying>(...);
auto view = blueprint.GetView();
I doubt this solves your problem since must still be dynamically typed and is not "dynamically assembled at runtime". But I hope it offers you insight regardless.

Instantiate inline class method

I write a library, this library includes a function void f() this function is a one line function and when I compile the library to shared object with -O3 gcc optimization flag it is inlined. I call it in a critical place in the code (must be as fast as possible) and I don't want to call it not inlined (hits performance substantially). The problem is that this function is part of the API that my library exposes so when library users link with my library and call this function they get undefined reference linkage error. Is there a way for my code to use the function inlined but still instantiate it in the object file so library users will be able to link and use it? (When I say "instantiate it in the object file" I mean I'd like to see it when I run objdump -t on the shared object). Just to make it clear, I'm not interested in a solution to wrap it with a function
void F() __attribute__((noinline)) { f(); }
Because I have many functions like that and I don't want to keep a copy for every function due to the enormous amount of overhead. I'm looking for a way to tell the compiler to use it inline when the definition is available to it, but still instantiate the function in the object file, so library users can link to with it too.
Check out this How can I tell gcc not to inline a function?
I found this solution the most appropriate. I'd like to note the main thing is that the code inside the library is still inlined so there is no performance penalty but users can still use the API as all functions have instantiation
Also another possible solution in the compilation level is to use -fkeep-inline-functions gcc switch which also instantiates inline functions and and uses them inlined where possible (unlike -fno-inline switch). The main problem with this switch is that if your code is heavily templated compilation time is much longer and the binary product becomes much bigger

cocos2d-x write the callbacks using c++11 lambda

sometimes the usage of a callback is very limited, which makes it in-appropriate to be a member function. so at these times I always want to write the event call backs as lambda functions as the usage is limited the codeblock is compact if wrote in lambda
but this callback is generally defined member functions and there are interface limitations inforced on it
I wonder if it is possbile to rewrite the callback in lambda functions ?
pMenuOK->setTarget(this,menu_selector(PlayerLayer::onPlayed));
void PlayerLayer::onPlayed(cocos2d::CCObject *pSender);
For simple CCCallFunc callbacks that take no parameters, you may want to check out
MCBCallLambda.
I don't think it's possible. The way they are called by Cocos2d-x is by using a target pointer to a CCObject in combination with a method pointer. Thus, the target has to be a CCObject. As you said, these are defined for different types of parameters. Cocos2d-x need to be changed to support this.
It is possible for any method that accepts CCCallFunc [1] or its subclasses. Create own subclass of CCCallFunc which keeps std::function and overrides execute method and maybe some other methods (figure out which implementation needed from CCCallFunc sources).
[1] http://www.cocos2d-x.org/embedded/cocos2d-x/dd/d6e/classcocos2d_1_1_c_c_call_func.html

GCC plugin adding a pass to introduce new keyword

I have just started exploring into the world of GCC plugins API and I'm trying to extend GCC for the following example.
class Foo { ... };
Foo* f = __construct Foo(); // __construct is meant to be a new keyword
Ideally, I'd like the plugin to simply replace __construct with something like new (foo_class_info), so you'll end up with
Foo* f = new (foo_class_info) Foo();
where foo_class_info has been made available via PLUGIN_FINISH_TYPE callback (i.e. __construct cannot be replaced at the preprocessor pass).
What would be the right / easiest way to achieve this? I don't even mind combining some user-land template codes with the plugin.
Would adding a new pass be the way to go? If so, I could really use some pointers.
Plugins fundamentally cannot change the syntax.
The most you can do is add a new pragma, attribute, or builtin, then do something with that in a later pass.

Resources