www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - bitfields - Default values?

reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
  The documentation for bitfields doesn't go into detail if you 
can put any default values into it. Can you? It makes sense if 
you can that it would convert the whole thing into a single 
number (or or-ing the results into a single expression with 
shifting and all).

  From what I can tell it all defaults to 0...
Jun 04 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Era Scarecrow:
  The documentation for bitfields doesn't go into detail if you 
 can put any default values into it. Can you?
I think you can't. See: http://d.puremagic.com/issues/show_bug.cgi?id=4425 Bye, bearophile
Jun 04 2012
parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Tuesday, 5 June 2012 at 00:17:08 UTC, bearophile wrote:
 Era Scarecrow:
 The documentation for bitfields doesn't go into detail if you 
 can put any default values into it. Can you?
I think you can't. See: http://d.puremagic.com/issues/show_bug.cgi?id=4425 Bye, bearophile
K, I think I have a fix for this done. Now if I bother to get GitHub to work with me at all... Here's a sample, thoughts of naming or other changes before I try and add this back in? (I'm not the best with naming things) //T is a array of strings of name=value. //returns a replacement string from bitfields with the defaults in place string bitfields_D(string bitf, T...)(); struct defs { mixin(bitfields_D!( bitfields!( //borrowed from std.bitmanip bool, "b", 1, uint, "i", 3, short, "s", 4), "i=2", "s=5")); } defs s; assert(!s.b); assert(s.i == 2); assert(s.s == 5);
Jun 12 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Era Scarecrow:

 struct defs {
   mixin(bitfields_D!(
     bitfields!( //borrowed from std.bitmanip
       bool, "b", 1,
       uint, "i", 3,
       short, "s", 4),
     "i=2",
     "s=5"));
 }
Are you able to support a syntax like:
 struct defs {
   mixin(bitfields!(
       bool, "b", 1,
       uint, "i=2", 3,
       short, "s=5", 4));
 }
Bye, bearophile
Jun 12 2012
next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 12.06.2012 17:05, bearophile wrote:
 Era Scarecrow:
 Are you able to support a syntax like:

 struct defs {
 mixin(bitfields!(
 bool, "b", 1,
 uint, "i=2", 3,
 short, "s=5", 4));
 }
Or iff bitfields is a mixin template: struct defs { mixin bitfields!( bool, "b", 1, uint, "i=2", 3, short, "s=5", 4); } -- Dmitry Olshansky
Jun 12 2012
prev sibling parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Tuesday, 12 June 2012 at 13:05:26 UTC, bearophile wrote:
 Era Scarecrow:

 struct defs {
  mixin(bitfields_D!(
    bitfields!( //borrowed from std.bitmanip
      bool, "b", 1,
      uint, "i", 3,
      short, "s", 4),
    "i=2",
    "s=5"));
 }
Are you able to support a syntax like:
 struct defs {
  mixin(bitfields!(
      bool, "b", 1,
      uint, "i=2", 3,
      short, "s=5", 4));
 }
That does look cleaner and better IMO. At this second I'm not sure. I was trying not to interfere with the original bitfield generator. I'm not as competent with templates as I'd like to be so having it go through and separate the data out seems a lot more difficult than simply replacing the end of the string as I currently have it. The bitfields as it is creates enums (min/max values), the get, the set, and then at the end has a 'private xxx valname;'. My little function is just a wrapper that builds on it by making the calculations as specified and then modifying the very end of the bitfields string with the answer, in my example that was '84'. I can try and make it as you have it, but I'm not sure how much harder that will be compared to what it is now; would probably end up as 'private xxx valname = (2<<1) | (5<<4);, which makes sense without adding too much complexity.. unless you go negative numbers, then you have to add a mask... Hmmm.... 'private xxx valname = ((2 & 7) << 1) | ((5 & 15) << 4);'
Jun 12 2012
parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Tuesday, 12 June 2012 at 15:51:31 UTC, Era Scarecrow wrote:
 On Tuesday, 12 June 2012 at 13:05:26 UTC, bearophile wrote:
 Are you able to support a syntax like:

 struct defs {
 mixin(bitfields!(
     bool, "b", 1,
     uint, "i=2", 3,
     short, "s=5", 4));
 }
That does look cleaner and better IMO. At this second I'm not sure. I was trying not to interfere with the original bitfield generator.
Good news, I _can_ do that very format. Not too much complexity and it seems to work okay, and it's definitely cleaner. Got some testing yet to do, but it looks good to me..
Jun 12 2012