Get Function name inside a loop pass - clang

I am pretty amateur in LLVM. I am trying to write a loop pass in which I need to know which function I'm in. Is there any way to find that?
I want to do this in the following runOnLoop function:
virtual bool runOnLoop(Loop *L, LPPassManager &LPM) override {
}

You need
StringRef Name = L->getHeader()->getParent()->getName();

Related

Factory in Swift using Generic Types

I'm trying to achieve the following in Swift - I want to pass in a type to a generic "get" function, and based on that type and the parameter, use different repositories to get back a class.
I recognize this is a bit strange to do in this way, but it would save me a lot of time and allow me to more properly refactor something later.
Here is the code but it compile errors on the Foo1Repository and Foo2Repository lines "Cannot convert the type of Foo1Repository to T".
Basically Foo1Repository should return a Foo1, which inherits from BaseEntity of course (and the same is true for Foo2)
class func Get<T: BaseEntity>(id: Int) -> T
{
if (T is Foo1) {
return Foo1Repository.Get(id)
}
else if (T == Foo2) {
return Foo2Repository.Get(id)
}
return T()
}
I was hoping to invoke this function by doing:
let foo = FactoryClass.Get<Foo1>(1)
I understand immediately you would ask "why not just call the appropriate repository, i.e."
let foo = Foo1Repository.Get(1)
and you're all set! No need for some weird factory pattern here.
Let's just say at the moment I need to try to do it above without a lot of refactoring of some code I inherited. I'll get back to planning a proper refactor later.
So I've tried a combination of things but still can't seem to get past the compiler errors. Is something like this possible, or do I need to just bite the bullet and go a different route? Thanks so much!
So I figured it out! I wanted to share the answer so others can see how to do this. Hopefully it will help.
func Get<T: BaseEntity>(id: Int) -> T
{
if (T.self == Foo1.self) {
return Foo1Repository.Get() as! T
}
else if (T.self == Foo2.self) {
return Foo2Repository.Get() as! T
}
...
return T()
}
So you can see, the whole as! T at the end was key. Also == instead of is for type checks.

Overloading a method in Groovy using Closure arguments with different return types

I'm reasonably proficient with Groovy insofar as my job requires, but not having a background in OOP means that some things still elude me, so apologies if some of the wording is a little off here (feel free to edit if you can make the question clearer).
I'm trying to create an overloaded method where the signature (ideally) differs only in the return type of the single Closure parameter. The Closure contains a method call that returns either an ItemResponse or ListResponse object, both of which could contain an object/objects of any type (which is the type I would like to infer).
The following code is a simplified version of what I'm trying to implement - an error handling method which takes a reference to a service call, safely attempts to resolve it, and returns the item/items from the response as appropriate:
public <T> T testMethod(Closure<ItemResponse<T>> testCall) {
testCall.call().item as T
}
public <T> List<T> testMethod(Closure<ListResponse<T>> testCall) {
testCall.call().items as T
}
Obviously this doesn't work, but is there any alternate approach/workaround that would achieve the desired outcome?
I'm trying to create an overloaded method where the signature
(ideally) differs only in the return type of the single Closure
parameter.
You cannot do that because the return type is not part of the method signature. For example, the following is not valid:
class Demo {
int doit() {}
String doit() {}
}
As mentioned by yourself and #jeffscottbrown, you can't have two methods with the same parameters but different return value. The workaround I can see here is to use a call-back closure. The return value of your testMethod would default to Object and you would provide an "unwrapper" that would the bit after the closure call (extract item or items). Try this out in your GroovyConsole:
class ValueHolder <T> {
T value
}
Closure<List<Integer>> c = {
[1]
}
Closure<ValueHolder<String>> d = {
new ValueHolder(value:'hello world')
}
Closure liu = {List l ->
l.first()
}
Closure vhsu = {ValueHolder vh ->
vh.value
}
// this is the generic method
public <T> Object testMethod(Closure<T> testCall, Closure<T> unwrapper) {
unwrapper(testCall.call()) as T
}
println testMethod(c, liu)
println testMethod(d, vhsu)
It works with both a list or a value holder.

Use an NSString to programatically access or create a method in Objective-C

I am trying to use an array of strings dynamically access methods at runtime within my class. For now the methods are already there, eventually I want to create them.
Is this possible?
For example:
bool nextLevel=NO;
for(NSString * match in gameLevels)
{
if([match isEqualToString:self.level])
{
nextLevel=YES;
}
else if(nextLevel==YES)
{
self.level=match;
nextLevel=NO;
}
}
//access method named self.level
Thank you in advance!
I use:
NSSelectorFromString(selectorString)
In your case, the selectorString would be:
NSString * selectorString = #"setLevel:";
This is 'setLevel' instead of 'level' because the Objective-C runtime will automatically expand dot properties to these selector names when assignment occurs.
To access a method based on a string, check the other answer.
To add a method in the runtime you need to create a IMP function or block.
If using a function, could be something like:
void myMethodIMP(id self, SEL _cmd)
{
// implementation ....
}
You could also use a block like this:
IMP blockImplementation=imp_implementationWithBlock(^(id _self, ...){
//Your Code here
}
Then you need to add the method, like this:
class_addMethod(yourClass, #selector(selectorName), (IMP) blockImplementation, encoding);
The encoding part is a special runtime encoding to describe the type of parameters your method receives. You can find that on the Objective-C runtime reference.
If you receive dynamic arguments on your generated methods, you need to use the va_list to read the values.

Is there a way to dynamically call a method or set an instance variable in a class in Dart?

I would want to be able to do something like this with a Dart class constructor:
class Model {
// ... setting instance variables
Model(Map fields) {
fields.forEach((k,v) => this[k] = v);
}
}
Obviously, this doesn't work, because this doesn't have a []= method.
Is there a way to make it work or is it simply not "the dart way" of doing things? If it's not, could you show me what would be the right way to tackle this?
You can use Mirrors:
InstanceMirror im = reflect(theClassInstance);
im.invoke('methodName', ['param 1', 2, 'foo']).then((InstanceMirror value) {
print(value.reflectee); // This is the return value of the method invocation.
});
So, in your case you could do this (since you have setters):
import 'dart:mirrors';
class Model {
Model(Map fields) {
InstanceMirror im = reflect(this);
fields.forEach((k, v) => im.setField(k, v)); // or just fields.forEach(im.setField);
}
}
The documentation for InstanceMirror might come in handy.
Currently no. You will have to wait for reflection to arrive in Dart before something like this (hopefully) becomes possible. Until then your best bet is probably to do it in a constructor. Alternatively you could try to use something like JsonObject which allows you to directly initialize it from a Map (for an explanation on how this works, check this blog post).

How to Save private preferences with monodroid?

I am trying to save some settings but the tutorial I am following(android tutorial) is not helping as I am stuck on the first line of code since it seems monodroid does it differently?
select your mode to be either private or public.
int mode= Activity.MODE.PRIVATE;
// get the sharedPreference of your context.
SharedPreference s mySharedPreferences ; mySharedPreferences=getSharedPreferences(“Name_of_your_preference”,mode);
// retrieve an editor to modify the shared preferences
SharedPreferences.Editor editor= mySharedPreferences.edit();
/* now store your primitive type values. In this case it is true, 1f and Hello! World */
editor.putBolean(“myBoolean”,true);
editor.putFloat(“myFloat”,1f);
editor.putString(“myString”,” Hello! World”);
//save the changes that you made
editor.commit();
I don't see Activity.MODE.PRIVATE; in monodroid.
Here is my func to do this:
protected void SaveSetting(string name, string value)
{
var prefences = GetSharedPreferences(Consts.Application.SETTINGS_FILE, FileCreationMode.Private);
var editor = prefences.Edit();
editor.Remove(name);
editor.PutString(name, value);
editor.Commit();
}
Assuming you mean MODE_PRIVATE, it should be Android.Content.FileCreationMode.Private.
Fortunately you don't really have to know that, as we mapped the int in GetSharedPreferences to take the Android.Content.FileCreationMode enum, so intellisense should help you out.

Resources