Does time_t exist in JNA? - jna

I downloaded Java Native Access in Eclipse for use in a program, specifically to use a variable time_t date in order to get a unix timestamp. Does that structure exist, or is it in another form?
In case it doesn't exist, what would be my best alternative to get time to just use System.currentTimeMillis()?

JNA does not have a specific type for time_t because it's usually easily modeled on any particular operating system by an existing primitive type.
You can define one yourself easily... on macOS you could just replace it with a NativeLong or use:
class time_t extends IntegerType {
public time_t() {
super(Native.LONG_SIZE);
}
}
Since time_t is defined in seconds, you could use System.currentTimeMillis() if
you divided it by 1000. It all depends on what use you have for the variable. Generally if you're using JNA you're trying to use some native time value.

Related

How does Orika decide when to use converter

I am trying to understand when does Orika use converters to do the mapping versus direct casting.
I have the following mapping:
Class A {
Map<String, Object> props;
}
Class B {
String bStr;
int bInt;
}
My mapping is defined as props['aStr'] => bStr and props['aInt'] => bInt
When I look at the generated code, I see that for the String case, it uses a converter and calls its convert method for the transformation:
destination.setBStr("" + ((ma.glasnost.orika.Converter)usedConverters[0]).convert(
((java.lang.Object) ((java.util.Map) source.getProps().get("aStr"),
(ma.glasnost.orika.metadata.Type) usedTypes[0]))
But, for the integer case it directly casts it like this:
destination.setBInt((java.lang.Integer)(java.lang.Object) ((java.util.Map)
source.getProps().get("aInt")))
The above line of code ends up giving class cast exception.
For fixing this issue, I was thinking along the lines of using a custom converter but if the above line of code doesn't use the converter then that wont work.
Of course, I can always do this is in my custom mapper but just trying to understand how the code is generated for type conversion.
Thanks!!
In Orika there is two stages: config-time and runtime, as optimization Orika resolve all used converter in the config-time and cache them into each generated mapper so it will be accessible directly O(1) but in the config time it will try to find in a list O(n) of registered converters which one "canConvert" between two given types, canConvert is a method in Converter interface .
So this solution offer the best of the two worlds:
A very flexible way to register a converter with arbitrary conditions
An efficient resolution and conversion operation in the runtime.
Orika by default, leverage the existence of .toString in every object to offer implicit coercion to String for every Object. The problem here is that there is no Converter from Object to Integer.
Maybe this can be an issue of error reporting. Ideally Orika should report that an Object have to be converted to Integer and there is no appropriate converter registered.

Can I make multiple aliases of the same type in an F# type provider?

I'm using a type provider to create a versioned api, where you can choose to select older versions of a component to call than the current lastest version.
At the moment, it puts these in a namespace that looks something like:
Provided.ComponentName.VersionName(... this is the type constructor ...)
ComponentName is a namespace at the moment, if that makes a difference.
Once I've built all the specific versions of the component, I'd also like to expose:
Provided.ComponentName.Latest(... constructor ...)
as either a static method or a type alias that will return an instance of the most recent version.
What's the best way of doing this?
I don't think there is a way to generate F# type alias (which would be a nice way to alias Latest to a specific version of the type).
As an alternative, I suppose you could generate VersionName as a nested type and Latest as a static method that returns the appropriate version of the type. To do this, ComponentName would have to be a type containing other types & one static method:
// Creates an instance of `VersionName`
// (a nested type of `ComponentName`)
Provided.ComponentName.VersionName(....)
// Creates an instance of `SomeVersionName` type
// (a static method of the `ComponentName` type)
Provided.ComponentName.Latest(....)
The only ugly thing about this is that VersionName is a type (and can be created using new) while Latest is a static method and so it won't work with new.

Is there any way to localize numbers and dates in dart?

In C# we have CultureInfo which affects the way ToString() works for dates and numbers
eg. you can set CurrentCulture by doing:
Thread.CurrentThread.CurrentCulture = new CultureInfo("pl-PL");;
Is there any equivalent for the above in dart?
EDIT:
Short answer is: use intl package (thanks to all for answers)
As Alan Knight pointed below setting locale "by thread" does not make sense in Dart since we do not control threads explicite.
At the moment i write this NumberFormating is work in progress as far as i understand it
The intl library will offer you this, although it doesn't affect the behavior of toString().
Here's an example:
Add as a dependency to your pubspec.yaml:
dependencies:
intl: any # You might specify some version instead of _any_
Then a code sample:
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
main() {
initializeDateFormatting("en_US", null).then((_) {
var formatter = new DateFormat.yMd().add_Hm();
print(formatter.format(new DateTime.now()));
});
}
The output looks like this:
07/10/1996 12:08 PM
Yes, as per the previous entry the Intl library is what you want. You can set the default locale, or use the withLocale method to set it within a function. Setting it by thread doesn't work, as there are no threads. The other major difference is that, since this is all downloaded into the browser, you don't automatically have all the locale data available, but have to go through an async initialization step to load the data. That will probably be switched over to use the new lazy loading features soon.
Also, the locale doesn't affect the system toString() operation but rather you have to use a DateFormat object to print the date. And as it's still work in progress, NumberFormat doesn't work properly for locales yet, but should soon.
http://api.dartlang.org/docs/releases/latest/intl/Intl.html
From the page:
The Intl class provides a common entry point for internationalization related tasks.

"The type is not generic, it cannot be be parameterized with arguments"

I am trying to create a class implementing Blackberry's Comparator so I can easily sort 2D arrays.
import net.rim.device.api.util.Comparator;
class ArrayComparator implements Comparator<Object[]> {
...
}
This gives me the error:
The Type Comparator is not generic; it can not be parameterized with
arguments <Object[]>
This error goes away if I include the normal JRE library and import java.util.Comparator, but this won't compile because it is a mobile device, the library is not preverified, etc, etc, etc.
How can I resolve this issue?
net.rim.device.api.util.Comparator doesn't have Generic capabilities. You need to implement Comparator without any type information and compare all the items of the array. if the items of the array are objets, you need to implements the Comparator interface on this Objets too.
you can get more implementation information on this link
BlackBerry JRE is 1.3 (like CLDC 1.1) and don't support generics, non-synchronized collections and other things from modern java world.
Sicne you're defining a comparator for a particular class you don't need (and can't use) generics. Your array comparator should cast the Object type parameters to their corresponding type before comparing in the compare method you override.
For example, your ArrayComparator should cast o1 and o2 to an array. In the future if you make a, say, UserDataComparator the compare method should cast the parameters to your UserData class type.
FYI: Blackberry's compiler (RAPC) supports up to java 1.4 AFAIK (check this) so Generics, Enums and other Java 1.5 and above features are not supported.
EDIT As other persons pointed (and for the sake of a complete answer) I was not specific enough on my anwer. I have edited this answer to reflect some crucial points related to your issue.

C++: Save global value to memory address - or alternative way

I'm currently tweaking some pixel shader code written in C++ of a computer game. I need a way to store a global value (integer or float) in a way that it can be retrieved later (within same process). Since everything seems to be executed "statelessly" in runtime it won't help simply declaring and using a static variable (class variable) as I normally would do in this situation.
Thus I came across the idea of storing my global value to a specific memory address, and get it from there later. My problem is that I have no idea whether this is possible at all and how to do it. I read those questions but didn't find an answer so far:
Create new C++ object at specific memory address?
Pointer to a specific fixed address
Assigning A Specific Memory Address from another program, and changing it's value
Assign a value to a specific address
Is this even possible on Windows Vista or 7, and if so, how? I have no option to include any library but have to achieve anything with the built-in c++ functionality.
If not (or not easily) achievable, are there alternative ways to store some value really globally (not as class variable) such that it can be accessed by other classes/instances within the same process? Maybe some sort of session or application cache like in .NET or Java?
Any help would be appreciated.
I must admit I didnt get why static won't work in your case, so my suggestion might also not work.
Is it possible for you to simply use a singleton like this:
class VariableStorage
{
public:
int getVar(string name);
void setVar(string name, int value);
static VariableStorage* instance();
//...
protected:
VariableStorge() { static obj = 0; if(!obj) obj = new VariableStorage(); return obj; }
// ...
}

Resources