How to wrap a C structure which is defined with __attribute__((packed,aligned(1)))? - swig

How do I wrap a C structure which is defined with __attribute__((packed,aligned(1))) in SWIG?

You need to do almost nothing to make this work, simply #define the attribute away:
%module test
#define __attribute__(x)
struct foo {} __attribute__((packed,aligned(1)));
Will parse and work as intended.
The reason this works is that SWIG doesn't generate any code that cares about the layout or size of an object. Nor does it generate code that makes any assumptions about it either. Instead all accesses/malloc/new are handled via the compiler which compiles the generated code exactly as normal.
So as long as you make sure that the definition of your struct, as seen by the compiler that compiles the module is correct you won't have any problems.

Related

Separate files for functions outside classes

It's a good practice to separate each class from the main code using
headers and sources. But what about functions? Let's say, I have a
function I would like to use across multiple classes and I don't
want to include this function as a method of a class.
If I decide to create a separate file for this function(s), should I
put everything inside .h or should I do as I do with classes
(separate .h and .cpp)?
Yes, whether it's a class or not it's still good practice to separate the declaration / signature (in the header file) from the definition / implementation (in the cpp file).
The code that calls the function does not need to know about how the function actually works - just how to call it.
This separation can avoid circular references that can sometimes otherwise occur. It avoids the compiler having to re-parse the definition every time the declaration is included.
Basically the reasons for separation between header and cpp are much the same for class and non-class functions.
However if you're using templates you will need to include the definition not just the declaration ( as you would with template classes ).
I'd suggest you put the functions in a namespace even if you don't put them in a class.

find out what is inside a function

For example, there is a function like this:
function a(){
if(stage.color==0xffffff){
trace("The color of stage is White");
}
}
now, is it possible to get a String, XML or anything containing :
//these are the codes inside function "a"
if(stage.color==0xffffff){
trace("The color of stage is White");
}
to see what does a function do?
Thanks.
Reading lines of code at runtime is not possible with compiled languages like Flash technology. With this technology (like many others) you write a code using a language (AS3 for example) in what is very much like text files. Then to complete the process and generate a runnable file (.swf) you have to compile everything. During that compilation those text files full of code are converted to machine readable instructions. That means that the lines of code no longer exist and are converted to something else. So the simple answer to your question is that no it's not possible, the correct answer is that the question doesn't apply. A language like Javascript for example would be a candidate for that question since that code is not compiled.

Configure code folding in LightTable

LightTable has code folding since v0.6.1, it's key binding is C-= by default. It works for Python files out of the box, but it does nothing with Clojure files. The Codemirror code sets fold to "indent" at https://github.com/LightTable/Python/blob/master/codemirror/python.js#L351. My question is how can I add code folding to a file type that's not handled by Codemirror by default. I'd like to do it without having to touch a js file, hopefully writing only a little ClojureScript in my user settings.
Unfortunately folding needs a folding helper function that will starting from a given position seek the start and end position for the fold. These currently exist for languages that use braces (like java, c++) or indentation (python). So, unless someone writes a helper function that can parse s-expressions and find where to fold them, folding in clojure will not work.

GRMustache formatted numbers, or an HTML Template Engine with Number Formatting in iOS

I'm not sure how to best go about this. I've tried solving this my own way, but failed so far. I tried using GRMustache, only to realize that I'm trying to display floats that just look hideous in the template I'm trying to use.
Basically, I have a model that I'm trying to have output as HTML via a template. Ideally, I just put the variable names/keypaths into the template and the template just gets parsed with the actual values (pretty much) rendered in place. But, the model I'm using uses floats for all its calculations and I'd really like them rendered as comma-separated integer strings (e.g. (float)9382.233325 => "9,382").
I can't seem to find any documentation in GRMustache that covers a situation like this, but I imagine this can't be an uncommon requirement. Does anyone know how to do this with GRMustache or through some other technique?
I'm the author of GRMustache.
There isn't, and there will never be any float formatting features in GRMustache, because there is already a perfectly well suited tool in the OS: NSNumberFormatter.
Since you're giving to GRMustache your model objects, here is my advice:
Declare a category on your model, and add a specific method for each of your formatted value:
#interface MYModel(GRMustache)
// name would match your original value property name
- (NSString *)formattedValue;
#end
In the implementation file, use a NSNumberFormatter:
#implementation MYModel(GRMustache)
- (NSString *)formattedValue
{
// Check the NSNumberFormatter reference for initializing
// the NSSNumberFormatter for your desired output.
NSNumberFormatter *formatter = [NSSNumberFormatter ...];
return [formatter stringFromNumber: [self value]];
}
#end
Beware creating many NSNumberFormatter instances may be costly. A good practice is to provide a shared method that returns a shared one. The code above is just a hint for the technique.
Finally, in your template, replace {{value}} tags with {{formattedValue}}.
Happy GRMustache!
GRMustache 1.12 now features a better API for number formatting: https://github.com/groue/GRMustache/blob/master/Guides/sample_code/number_formatting.md

WebGL and HTML shader-type

I declare my GLSL ES shader program within a HTML file, using this code:
<script id="shader-fs" type="x-shader/x-fragment">..shader-code..</script>
as seen in the learning WebGL examples. Everything works fine, but I dont understand why I should use the type attribute of the script tag. I want to know where the "x-shader/x-fragment" value is specified. Who does that.. the W3C, the Khronos Group or the browser developers? Can anybody help me? Tahnk you.
There is no official organization who specified that GLSL code should be put within a <script> tag of type "x-shader/x-fragment".
The only reason the GLSL code is placed within that <script> tag is because the tutorial writer decided his code would be cleaner if he placed the GLSL code within a <script> tag rather than a normal string.
However since WebGL takes in GLSL code as a string value, the author had to write a helper function called getShader(gl, id) to grab the script tag from the page and convert it into a javascript string, before passing it to WebGL.
The reason the author chose a type value of "x-shader/x-fragment" is because "x-shader/x-fragment" is not a known script type by the browser and thus would be safely ignored by the browser.
The idea is that the browser doensn't know the type 'x-shader/x-fragment'. Your code will work fine if you change the type to something else (like 'foo').
In other words there is no standard for how to store shader source code. But you will need it as a string when the shaders are compiled.