volatile struct definition for peripheral control registers
Salih Dincer
salihdb at hotmail.com
Mon Dec 2 21:21:28 UTC 2024
On Monday, 2 December 2024 at 20:27:23 UTC, Josh Holtrop wrote:
>
> If I use `alias this` I don't know how I can get
> `volatileLoad()` to be called.
Please allow me to illustrate the point with a simple example so
that you can visualize it:
```d
struct FixedStr(size_t capacity)
{
char[capacity] data = ' ';
char[] buff;
size_t titleLength;
this(string _data)
{
buff = data[];
this ~= _data;
titleLength = _data.length;
}
alias opReturn this;
@property auto opReturn() inout
=> data[titleLength..$]; // cuts off the title
auto ref opOpAssign(string op)(string arr) if(op == "~")
=> buff.put(arr); // add in the data
}
import std.range : chunks, put;
import std.string : format;
import std.stdio : writeln;
void main()
{
enum text = "sixtwoone";
auto test = FixedStr!22("Nums : ");
assert(is(typeof(test) == struct));
// not a string ------------^
foreach(num; text.chunks(3))
{
test ~= num.format!"%s, ";
}
test.writeln; // six, two, one,
test[5..8].writeln; // two
}
```
Let's have a 2-function struct as above. In fact, the functions
do not have a feature that is open to direct use. There's not
even an overload like `opReturn()`, it's my made :)
To rotate a fixed string (char[]) by cropping it from the
beginning (open it to the outside world) and to add data
sequentially (via `std.range.put`) to its limit. But we also want
to use it like the familiar string (e.g. line 2 output: "two").
And that's what we do easily with `alias ... this`. However, in
this example, it is necessary to use @property and inout; keep in
mind...
It's also a good idea to keep this in mind:
https://dlang.org/changelog/2.100.0.html#alias_this_assignment
SDB at 79
More information about the Digitalmars-d-learn
mailing list