Best Practice: Alternatives to Void Pointers

2018-01-30 Thread jsako via Digitalmars-d-learn
The common C way to get a blob of generic data at runtime is to 
use void pointers like so:


struct Structo {
  int type;
  void* data;
}

Then cast the void pointer to whatever data you needed based on 
the type. I imagine D has a better mechanism for this sort of 
thing, but after some searching I couldn't find what is 
considered "best practice" for this application.


The obvious solution is to use objects and have polymorphism 
solve the problem, but if you want to avoid as much memory 
dereferencing as possible (say, to avoid cache misses), objects 
seem a poor choice since they are always reference variables.


So what's considered the best alternative to void pointers in D 
if you don't want to use objects? Make a tagged Union of all 
possible datatypes in the struct? Have a Byte array and cast that 
instead of a void pointer? Some sort of magic involving templates 
or other metaprogramming mechanisms?


Re: Filter a Range Based on a Passed In Variable

2018-01-20 Thread jsako via Digitalmars-d-learn

On Saturday, 20 January 2018 at 19:09:28 UTC, Adam D. Ruppe wrote:

On Saturday, 20 January 2018 at 19:04:06 UTC, jsako wrote:
I want to be able to filter a range based on a variable known 
at runtime. Something like this:


[code]
int id = getFilterID();

auto filteredRange = filter!(a => a.id == 
id)(rangeToBeFiltered);


[/code]

This doesn't seem to be possible, however as .filter only 
takes unary predicates. I tried:


That should actually work. What exactly happened when you ran 
that literal code above?


... Huh. The code I posted was a simplified case of what I 
actually have and like a fool I didn't test it first. You're 
absolutely right, the above code does work. Color me embarrassed.


In the actual code, I kept getting DMD saying "...does not match 
template declaration filter(alias predicate) if 
(is(typeof(unaryFun!predicate)))".


I think it may be a scoping issue? I'll have to look closer at it.

Thanks for the help! Sorry I wasted your time. At least this 
pointed me in the right direction to find out what is really 
going on.


Filter a Range Based on a Passed In Variable

2018-01-20 Thread jsako via Digitalmars-d-learn
I want to be able to filter a range based on a variable known at 
runtime. Something like this:


[code]
int id = getFilterID();

auto filteredRange = filter!(a => a.id == id)(rangeToBeFiltered);

[/code]

This doesn't seem to be possible, however as .filter only takes 
unary predicates. I tried:


[code]

filterString = "a.id == " ~ to!string(id);
filter!filterString(rangeToBeFiltered);

[/code]

But that also doesn't work. Is there another range algorithm that 
should be used in this case? Do I roll my own?


Re: Getting a String Literal From an Aliased Template Mixin Parameter

2016-09-14 Thread jsako via Digitalmars-d-learn

On Thursday, 15 September 2016 at 01:40:50 UTC, Anonymouse wrote:

You mean, the literal string name of the original symbol you 
passed as an alias? If so then you want the .stringof property.


void main () {
int first;
bool second;
string third;

mixin Foo!(first, second, third);
}

mixin template Foo(alias abc, alias def, alias ghi)
{
static assert(abc.stringof == "first");
static assert(def.stringof == "second");
static assert(ghi.stringof == "third");
}


Aha! Yes, that's exactly what I was looking for! Thanks!


Getting a String Literal From an Aliased Template Mixin Parameter

2016-09-14 Thread jsako via Digitalmars-d-learn
I was making a quick mocking infrastructure for internal mocks so 
I wouldn't have to pass in objects to constructors all the time 
and came up with this:


[code]
mixin template internalMockRig(alias _var, string _varName, alias 
_varStandard, alias _varMock) {


version(unittest) {

@property bool internalMockSetting() {
return _useInternalMocks;
}
@property useInternalMocks(bool uim) {

// Are we changing the mock type?
if (uim != _useInternalMocks) {

_useInternalMocks = uim;

if(_useInternalMocks) {
_var = new _varMock;
}
else {
_var = new _varStandard;
}
}
}

private: bool _useInternalMocks = false;
}
}
[/code]

Then in the object:

[code]
class usingTestThing {

testThing thing;

mixin internalMockRig!(thing, testThing, testThingMock);

this() {

thing = new testThing;
}

}
[/code]

This works great as long as there is only one mock object. I 
thought about using string mixins to generate the properties on a 
per-variable basis, but the problem is that I can't figure out 
how to get the string of the variable name from the aliased 
template mixin parameter. Is there a way to do this?


If not, I'll have to pass in the strings of the variables 
seperately (as in: mixin internalMockRig!(thing, "thing", 
testThing, "testThing", testThingMock, "testThingMock"); ), but 
that seems inelegant (and a bit error prone).