DIP62: Volatile type qualifier for unoptimizable variables in embedded programming

safety0ff via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 15 15:16:37 PDT 2014


On Tuesday, 15 July 2014 at 20:45:20 UTC, Johannes Pfau wrote:
> Am Tue, 15 Jul 2014 12:48:21 -0700
> schrieb Walter Bright <newshound2 at digitalmars.com>:
>> 
>> 3. if you really hate peek/poke calls appearing in the code, 
>> you can
>> use UFCS to make them look like variables
>
> But you cant do REGISTER.peek() |= 0b1;
>

You can create a function which returns a struct that wraps your 
desired read-modify-write semantics, e.g.:

auto myvolatile(size_t* addr)
{
   static struct VolatileWrapper
   {
      size_t* addr;
      // read-modify-write
      auto opOpAssign(string op)(size_t rhs) { /*...*/ }
      // write
      auto opAssign(size_t rhs) { addr.poke(rhs); return this; }
      // read
      auto rval() { return addr.peek(); }
      alias rval this;
   }
   return VolatileWrapper(addr);
}

REGISTER.myvolatile() |= 0b1;

Taking it further, I think you could implement something that 
looked like this:

enum : VolatileWrapper
{
   TIMER0 = VolatileWrapper(REGISTER),
   // etc
}

Don't get me wrong, I'm in favor of volatile, it's just that that 
counter-argument isn't extremely convincing.


More information about the Digitalmars-d mailing list