Is there any way to pass the value of an enum by name from the commandline? Rather what's the cleanest solution?
I tried the below,
typedef enum int unsigned { white, black, red } colour_e;
class house;
rand colour_e colour;
string n_colour = "white";
constraint c_colour {
colour.name() == n_colour;
}
endclass: house
program top;
house paradise;
initial begin
paradise = new();
void'($value$plusargs("colour=%0s", paradise.n_colour));
if (!paradise.randomize())
$display("not randomized");
else
$display("paradise.colour = %s",paradise.colour);
end
endprogram: top
I would want to pass something like this +colour=black. so that the paradise.colour is assigned black.
vcs cribbed for using enum.name() in the constraints.
below is the error.
Error-[NYI-CSTR-SYS-FTC] NYI constraint: sys function calls
T-enum_1.sv, 9 $unit, "this.colour.name" System function calls are
not yet implemented in constraints. Remove the function call or if
possible replace it with an integral state variable assigned in
pre_randomize().
while Riviera cried as below
ERROR VCP7734 "Type of 'this.colour.name()' is not allowed in a
constraint block. Only integral types are allowed in a constraint
block." "design.sv" 9 1 ERROR VCP7734 "Type of 'n_colour' is not
allowed in a constraint block. Only integral types are allowed in a
constraint block." "design.sv" 9 1 WARNING VCP7114 "STRING value
expected for format specifier %s as parameter
paradise.colour." "testbench.sv" 13 54
which brings the question to me, does everything in the contraint block has to be of integral type (just like we cannot declare a string as rand variable)?
ANyone wants to play around the code please have a look at the code at EDA playground here
Use uvm_enum_wrapper class to do the conversion from string to corresponding enum value. It is a template class wrapper defined in uvm_globals.svh (part of UVM 1.2) and you can use it as follows:
typedef enum {white, black, red} colour_e;
typedef uvm_enum_wrapper#(colour_e) colour_wrapper;
string colour_str;
void'($value$plusargs("colour=%0s", colour_str));
colour_wrapper::from_name(colour_str, paradize.n_colour);
The wrapper class uvm_enum_wrapper works by traversing the enum entries and creating an assoc array for a enum[string] map for the given enum type (supplied as template parameter). For more details take a look at the documentation.
There is another solution for the same. If you are not using UVM 1.2 version then there is like you can take string as an input argument and convert that string argument into int type. After that you can directly cast int to enum type by $cast.
Because there is no way to convert string to enum type (except UVM 1.2) so you have to add one extra step for the same.
module Test;
typedef enum {black,red,blue} color_e; //enum declaration
color_e color; //type of enum
string cmd_str; //Commandline string input
int cmd_int; //Input string is first converted into int
initial
begin
$value$plusargs("enum_c=%0s",cmd_str); //Take input argument as string it may be 0,1 or 2
cmd_int=cmd_str.atoi(); //Now convert that string argument into int type
$cast(color,cmd_int); //Casting int to enum type
$display("Value of color is --> %p",color); //Display value of enum
end
endmodule
Related
I know that in the latest version of dart we can use if else statements inside the build method. Does anyone know if we can use also if else statement when we setting class parameters? I know I can do inline statement there but inline is a bit hard to read when there are multiple conditions
const int i = 0;
class Person {
// NewClass n = NewClass(a: i == 0 ? 'a' : 'b'); //<- inline statement working
NewClass n = NewClass(a: if(i == 0) 'a' else 'b'); //<- if statement doesn't
}
class NewClass {
final String a;
const NewClass({this.a});
}
Edit:
Basically in my case I've got an TextField widget where I set its's type parameter from enum (Type.text, Type.numeric...) According to this parameter I want to set The textField parameters (textCapitalization, maxLength and so on)
As per your comment, you are already creating an enum for specifying the type of the fields.
enum Type {text, numeric}
Now for specifying the properties of that particular type, you can add an extension on this enum, as shown below:
extension TextFieldProperties on Type {
int get maxLength {
if (this == Type.text) {
return 10;
}
return 12;
}
}
So in your field class you already have a type defined, you can use that type variable to get the properties of that particular type of field.
Type type = Type.text;
print(type.maxLength); // Will print 10
type = Type.numeric;
print(type.maxLength); // Will print 12
Note: It will work only in Dart 2.7 and above
You want the conditional expression (?:), not the conditional statement or literal entry (if), as you have already discovered.
The reason if doesn't work is that if only works as a statement or as a collection literal entry. It doesn't work in arbitrary expressions.
The reason for the distinction is that the if syntax allows you to omit the else branch. That only makes sense in places where "nothing" is a valid alternative. For a statement, "doing nothing" is fine. For a collection, "adding nothing" is also fine.
In an expression context, you must evaluate to a value or throw. There is no reasonable default that we can use instead of "nothing", so an if is not allowed instead of an expression.
Doesn't work because this syntax doesn't exist in Dart. The only way to do what you would like to do is to use the ternary operator.
If you try it in the DartPad you will get an error.
I suggest you to use a function to return the right value.
I am writing a driver using iio framework. So far it goes well. All the inputs and sysfs entries are working perfectly and the values measured are fine. (It is very well documented and it is easy). But I need a small extension to be able to write on one of the channels. When I add my function in iio_info the compiler issues me an error:
drivers/iio/adc/iio-ccuss.c:197:15: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
.write_raw = ccuss_iio_write_raw,
^~~~~~~~~~~~~~~~~~~
It is very weird for me. I even can't believe I am asking shamelessly this here but I am very frustrated. I lost almost half day with that.
My structure is:
static const struct iio_info ccuss_iio_info = {
.driver_module = THIS_MODULE,
.attrs = &ccuss_iio_attribute_group,
.read_raw = ccuss_iio_read_raw,
.write_raw = ccuss_iio_write_raw,
};
my channel types are IIO_VOLTAGE, IIO_TEMP and IIO_HUMIDITYRELATIVE.
I am start thinking to make it as an device attribute :-( if I do not receive an answer in the next 12 hours.
Update:
just to be more visible, according Murphy's comment.
static int ccuss_iio_write_raw(struct iio_dev *iio,
struct iio_chan_spec const *channel, int *val1,
int *val2, long mask);
P.S. I do not want to remove this error by the most known way. The QA (and me) will be unhappy.
Thanks
According to the reference the write_raw() function is declared as follows:
int (*write_raw)(
struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int val,
int val2,
long mask);
Your implementation is declared like this:
static int ccuss_iio_write_raw(
struct iio_dev *iio,
struct iio_chan_spec const *channel,
int *val1,
int *val2,
long mask);
So you declare the two integer parameters as pointers, but they are expected to be passed by value. I think that's the mismatch that causes the "incompatible pointer type" error.
I have a C enum with around 50 values (they are error codes). Besides the first value, the numbering is kept automatic.
typedef enum {
MY_STATUS_OK = 0,
MY_STATUS_ERROR_UNKNOWN,
MY_STATUS_ERROR_A,
MY_STATUS_ERROR_B,
/* ... */
MY_STATUS_ERROR_LAST
} my_status_t;
I do get an enum description in the HTML, but it doesn't show the explicit value, only the enum values' name.
I want to document the association between hard value and enum value name. Right now when I get an error value, I need to manually count the index in the enum to find out what it means.
I have command-line plusargs that I want to map to enumerated values.
vsim foo +MY_PLUSARG=BAR
How do I get the string "BAR" to become the enum BAR?
If you are using UVM 1.2 or have access to the library, you may want to use uvm_enum_wrapper class to do the conversion. It is a template class wrapper defined in uvm_globals.svh and you can use it as follows:
typedef enum {BISTRO, COFFEE_SHOP, BAR} places_e;
typedef uvm_enum_wrapper#(places_e) places_wrapper;
places_e place;
places_wrapper::from_name("BAR", place);
Quite like the code you provided in this solution, the wrapper class works by traversing the enum entries and creating an assoc array for a enum[string] map for the given enum (supplied as template parameter). So if you are using UVM 1.2, don't repeat.
The idea behind this solution is to avoid a case statement that hardcodes the members of your enumerated type. You want to be able to change the type in one play.
Let's say you have the following enum:
typedef enum {BISTRO, COFFEE_SHOP, BAR} places_e;
You want your user to be able to type:
vsim top +MEET_PLACE=BAR
Now you want to translate the string "BAR" to the enum 'Bar'.
You can do this:
typedef enum {BISTRO, COFFEE_SHOP, BAR} places_e;
module top;
places_e place_map[string];
function void make_map;
places_e pl;
pl = pl.first();
do begin
place_map[pl.name()]=pl;
pl = pl.next();
end while (pl != pl.first());
endfunction // make_map
function string get_list;
string ss;
places_e pl;
pl = pl.first();
ss = "";
do begin
ss = {ss, " ",pl.name()};
pl = pl.next();
end while (pl != pl.first());
return ss;
endfunction // get_list
initial begin
string place_str;
places_e place;
make_map;
if (!$value$plusargs("MEET_PLACE=%s", place_str)) begin
$display("You must choose a MEET_PLACE");
$finish;
end
if (! place_map.exists(place_str)) begin
$display("You must choose from this list: %s", get_list());
$finish;
end
place = place_map[place_str];
$display("Let's meet at a %s!", place.name());
end // initial begin
endmodule // top
I wonder whether it is possible to add/append another item to an existing enum type (part of a framework)?
Something like this: We have the enum type
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
Now I want to append or add to this set an item like UIModalTransitionStyleCoverVerticalFlipped.
Can something like this be accomplished?
You can force new element to have the same type as the enum, but you can't extend it in a subclass.
header file:
extern const UIModalTransitionStyle UIModalTransitionStyleCoverVerticalFlipped;
implementation file:
const UIModalTransitionStyle UIModalTransitionStyleCoverVerticalFlipped = 10;
Make sure to give some space in case the framework is extended, so that you don't have conflicts. This is a bit of a hack, but it will get rid of compiler errors and warnings.
To do it, you have to modify the original type definition to include the new value:
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
UIModalTransitionStyleCoverVerticalFlipped
} UIModalTransitionStyle;
Otherwise, you can take a chance on its not working, and define it separately:
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
typedef enum {
UIModalTransitionStyleCoverVerticalFlipped =
UIModalTransitionStylePartialCurl + 1
} ExtendedUIModalTransitionStyle;
A variable that could hold the original enumeration will usually also work perfectly fine when/if you assign the new value as well (in a typical case, it'll just be an int) -- but it's not guaranteed. At least in theory, the implementation can/could assign few enough bits to hold that enumeration that it adding more values this way wouldn't work. It could also do range checking so assigning any out of range value wouldn't be allowed. Neither of these is at all common, so from a practical viewpoint it's probably not a problem -- but from a theoretical viewpoint, nothing really guarantees that code like this will work.
Maybe this can help you:
typedef NS_ENUM(NSInteger, BaseType) {
BaseTypeCase1,
BaseTypeCase2,
BaseTypeSize
};
typedef NS_ENUM(NSInteger, SubType) {
SubTypeCase1 = BaseTypeSize,
SubTypeCase2
};
Now you can switch on SubType knowing the values are unique.
If you don't have access to BaseType, you could set SubTypeCase1 to BaseType's last item + 1.
Downside is, you can't declare a method that takes a SubType and pass to it a BaseType without getting a compiler warning. So you need to declare your methods to take NSIntegers in order silence that warning.
Also, it feels weird when you need to declare a parameter of SubType and be able to pass in a BaseType.
To do this you have to update the Enum declaration to include UIModalTransitionStyleCoverVerticalFlipped this values as well
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
UIModalTransitionStyleCoverVerticalFlipped
} UIModalTransitionStyle;
so UIModalTransitionStyleCoverVerticalFlipped will be equivalent to integer constant 4
whereever you use any string constant from Enum dec. corresponding constant value get replaced so it is used to constraint the variable to hold only specified set of values only(i.e. 0 to 4) in above mentioned case