How to pass 64-bit value in vxworks shell - vxworks

How would you pass a 64-bit value to a function in the vxworks console?
For instance this works:
[C INTERP]->tickSet 0x1fffff
value = 2097151 = 0x1fffff
Whereas this:
[C INTERP]->tick64Set 0xfffffffff
C interp: '0xfffffffff' is not a valid integer value.
Doesn't.

You can add 'ull' to denote that it is an unsigned-long-long:
-> 0xfffffffff
C interp: '0xfffffffff' is not a valid integer value.
-> 0xfffffffffull
value = 68719476735 = 0xfffffffff
->

Related

Ensure same results from multiple calls to a ref

I call a function in a module to generate unique labels, eg.
MyMod.gensym
defined as
let gensym : string -> string =
let c = ref 0 in
fun s -> incr c; Printf.sprintf "!%s%d" s (!c)
But, I want to be able to get reproducible results at certain times from functions that use this gensym, eg.
let reproducible = SomeMod.call x
may return ["!a1"; "!a2"] the first time and ["!a3"; ...] the second
How can I ensure reproducible output in this case (eg. force ref to start from the same value), but without needing to change the implementation of gensym in its module?
You could add an optional argument to reset it:
let gensym : ?reset:bool -> string -> string =
let c = ref 0 in
fun ?(reset=false) s ->
if reset then
c := 1
else
incr c;
Printf.sprintf "!%s%d" s (!c)

type module with function already implemented ocaml

I have a module type Order which will be implemented in several modules.
The function compare will be implemented in the modules.
module type Order =
sig
type t
val compare: t -> t -> int
end
I want also create a function max :
max a b = if (compare a b > 0) then a else b
I would like to write the definition (Not just declaring ) of this function in my module Order in order to avoid to rewrite the same definition in the submodule which are numerous.
I have tried :
val max a b = if (compare a b > 0) then a else b
and
let max a b = if (compare a b > 0) then a else b
but it doesn't work
You can't implement functions in the signature of a module.
I think that the problem you are having is solved using functors in OCaml.
A code example you can look at to understand how it works is the implementation of Set.
In your case it would look something like:
EDIT: taking into consideration Richard Degenne, octachron and PatJ contributions:
module type Order =
sig
type t
val compare: t -> t -> int
end
module type Util =
sig
type t
val compare: t -> t -> int
val max: t -> t -> t
end
module Make(Ord: Order): Util with type t := Ord.t =
struct
type t = Ord.t
let compare = Ord.compare
let max a b = if (Ord.compare a b > 0) then a else b
end
In order to use it you can do:
(*You first define a module for the specific case of int*)
module IntOrder = struct
type t = int
let compare = compare
end
(*You use the new module to build the corresponding Util module*)
module IntUtil = Make(IntOrder)
(*You can now use the functions defined in Util as if it was any other module*)
let x = IntUtil.max 1 2
let y = IntUtil.compare 1 2
(*But if you try to call it with the wrong type you get an error*)
let z = IntUtil.compare 1.6 2.5

How to create a variable that can take strings and functions in Kotlin?

Is there a way to create a variable to store strings and functions? Like var x:dynamic where x can be any type or a function: x="foo"; x= {print (...)}
dynamic isn't a type (it just turns off type checking) and works only in kotlin.js (JavaScript). Is there a type that includes function types and Any?
I try this code and works fine.
The var x is Any so it can hold any kind of data (not nullable ) in it. To hold nullable data use Any?
var x: Any = "foo"
println( x )
x = { println("") }
x.invoke()
The IDE smart cast the variable but you can help the cast using this
(x as ()->Unit).invoke()

Expand parameter list in C

I am using a C library in my Objective C project. The C library offers the following function
void processData(...);
which can be used with 1, 2 or 3 parameters, where the first parameter is mandatory and can have different types int, double, long, float and the other two arguments are optional and have int and long values and can be in whatever order.
Examples of use of this function are:
int myInt = 2;
double myDouble = 1.23;
int dataQuality = 1;
long dataTimestamp= GET_NOW();
processData(myInt);
processData(myInt, dataQuality);
processData(myDouble, dataQuality, dataTimestamp);
processData(myDouble, dataTimestamp);
I need to make an Objetive C wrapper that uses DataType class to call processDatawith the correct parameters. The Data class has getters that allows to get the data type (first argument), its value and whether the second and third arguments have value and their value.
The problem is how to make this expansion? I think it must be done at compile time, and I think the only mechanism available in C to do so is macros. But I have never used them. The implementation should be something like this (the following is pseudocode, where the arguments list is evaluated at runtime, something that I guess should be replaced by macros in order to evaluate the arguments at compile time):
-(void) objetiveCProcessData: (Data) d {
argumentList = {}
switch (d.getDataType()) {
case INT_TYPE:
append(argumentList, d.getValueAsInt()); // <-- appends a value with type `int`
break;
case DOUBLE_TYPE:
append(argumentList, d.getValueAsDouble()); // <-- appends a value with type `double`
break;
...
}
if (d.hasQuality()) {
append(argumentList, d.getQuality());
}
if (d.hasTimeStamp()) {
append(argumentList, d.getTimestamp());
}
// Call to the C function with correct number and type of arguments
processData(argumentList);
}

Objective-C: Getting the highest enum type from a collection

If I have the following enum type:
typedef enum {Type1=0, Type2, Type3} EnumType;
And the following code (which will work fine if converted to Java):
NSArray *allTypes = [NSArray arrayWithObjects:[NSNumber numberWithInt:Type1], [NSNumber numberWithInt:Type2], [NSNumber numberWithInt:Type3], nil];
EnumType aType = -1;
NSLog(#"aType is %d", aType); // I expect -1
// Trying to assign the highest type in the array to aType
for (NSNumber *typeNum in allTypes) {
EnumType type = [typeNum intValue];
NSLog(#"type is: %d", type);
if (type > aType) {
aType = type;
}
}
NSLog(#"aType is %d", aType); // I expect 2
The resulted logs are:
TestEnums[11461:b303] aType is: -1
TestEnums[11461:b303] type is: 0
TestEnums[11461:b303] type is: 1
TestEnums[11461:b303] type is: 2
TestEnums[11461:b303] aType is: -1
And when I inspect the value of aType using a breakpoint, I see:
aType = (EnumType) 4294967295
Which is according to Wikipedia the maximum unsigned long int value for 32-bit systems.
Does this mean that I cannot assign a value to enum types that is not
in the valid range of the type's values?
Why is the value of the log (-1) differ from the real value
(4294967295)? Does it have something to do with the specifier (%d)?
How can I achieve want I'm trying to do here without adding a new
type to represent an invalid value? Note that the collection may
sometimes be empty, this is why I'm using -1 at the beginning to indicate that there is no type if the collection was empty.
Note: I'm new to Objective-C/ANSI-C.
Thanks,
Mota
EDIT:
Here is something weird I've found. If I change the condition inside the loop to:
if (type > aType || aType == -1)
I get the following logs:
TestEnums[1980:b303] aType is -1
TestEnums[1980:b303] type is: 0
TestEnums[1980:b303] type is: 1
TestEnums[1980:b303] type is: 2
TestEnums[1980:b303] aType is 2
Which is exactly what I'm looking for! The weird part is how's (aType == -1) true, while (Type1 > -1), (Type2 > -1) and (Type3 > -1) are not?!
It seems like EnumType is defined to be an unsigned type. When you assign it to -1, this value actually rolls back to the highest possible value for an unsigned 32-bit integer (as you found). So, by starting the value at -1, you are ensuring that no other value that you compare it to could possibly be higher, because it is assigned to the maximum value for the data type (4294967295).
I'd suggest just starting the counter at 0 instead, as it is the lowest possible value for an EnumType.
EnumType aType = 0;
If you want to check to see if any value was chosen, you can check the count of the collection to see if there are any values first.