How do you define elegant or clean code? Usually, you know it when you see it; defining it is harder. It is usually a simple, clear and well-structured code. OO programming languages (like Java, C++, System Verilog, and also e ) provide you with aids to write elegant code. This blog is about static members in e . Can you live without them? Usually, you can. What can they do for you? Help you write elegant and clean code. What if you don’t care about your code being elegant? Well, first you should care. But even if you don’t, there are problems you just cannot solve without static members. Since Specman version 15.2, the static keyword is part of e. This means that you can declare structs’ members as static (fields, methods or events) and get members that belong to the struct type, rather than to an instance of the struct. This also means that no matter how many instances of the struct are created (even none), there is only one copy of each static member which is shared by all instances of the struct type. This is actually the same idea as in the other OO programming languages mentioned above. Let’s look at a simple example. In the code below, the field max_address is declared static . This means that there will be only a single max_address field that will be created and initialized to 0x1000 before any instance of packet_s is created. extend packet_s{ static max_address : uint = 0x1000; }; You can access this field directly from every instance of packet_s (as if it is a member of it). From other places it can be accessed using the struct type name followed by “::” as in the example below. extend packet_s{ static max_address : uint = 0x1000; keep addr maximum_address ) then { maximum_address =addr; } }; }; Scenario 4: Template struct field I started by saying that using static is more elegant than adding fields to global. But you could say “I don’t much care for elegance. Defining a field in global works, and that’s good enough for me!” So what about this – consider the template of stack below. How would you handle adding a singleton max_size to a stack? Naturally you would want a different max_size for each type instantiated (as you want one max_size value for “stack of packets” and a different max_size value for “stack of instructions”). template struct stack of { }; How would you do it without a static member? Well, you can’t (at least not without lots of macros and tricks…). How do you do it with static member? Very simple, you just define a static field. template struct stack of { static max_size : uint; … }; If you think of other things you can do with static members that you cannot simply do without-we will be happy to hear. These were only simple examples to “give” you a taste; of course, there is more information in cdnshelp. We suggest you leave a place in your toolbox for static members. We encourage you also to check what you currently have under sys and global- can you find there constructs that logically belong to a specific context? Does it make sense to define them as static members in a more appropriate place? What is your opinion? Let’s say you are part of the Specman development team. Would you enable users to override a static method in a when subtype ? Take several minutes to think about it. Well, apparently, this is not a trivial question. Static method means that the method doesn't need an instance to operate on while when subtype is all about a concrete instance. So, no, this is not allowed (as C++ and Java do not allow it). If you think that it makes sense to enable it, we are happy to hear your voice! Orit Kirshenberg Specman team
↧