pgator

Module handles functions and templates that we lazied to factor out to separate module.

Function categories:

  • JSON de/serialization based on annotations
  • Exception handling functions
  • Cheat casting functions
  • String processing functions (the only one fromStringz)
  • Introspecting templates that cannot be found in Phobos
  • Functional styled utilities for optional values and lazy ranges

License
Subject to the terms of the MIT license, as written in the included LICENSE file.
Authors
Zaramzan , NCrashed

T  deserializeFromJson(T)(Json src) if (is(T == struct));

Deserializes from Json to type T

Supported only structs yet

Example:
struct S
{
   @required
int a; //get value from Json. Throws RequiredFieldException

   @possible
int b; //tries to get value from Json

int c; //will be ignored
}

auto s = deserializeFromJson!S(json);
Authors
Zaramzan

Json  serializeRequiredToJson(T)(T val);

Serializes struct with @required attributes fields to Json

Example

struct S
{
   @required
int a = 1; //will be used

   @possible
int b = 2; //will be ignored

int c; //will be ignored
}

writeln(serializeRequiredToJson(S())); // { "a":1 }


template  tryEx(Ex, alias func)

Tries to call function. On exception throws Ex, otherwise return func() result

Authors
Zaramzan

T  tryEx(Ex, T)(lazy T par);

Tries to evaluate par. On exception throws Ex, otherwise return par

Authors
Zaramzan

shared(P)  toShared(T)(T par);

cast to shared type T

Warning:
Don't use this, if you want send object to another thread. It just dirty hack.

P  toUnqual(T)(T par);

cast to unqual type T


template  getMemberType(Class, string name)

 getMemberType

Retrieves member type with name of class Class. If member is agregate type declaration or simply doesn't exist, retrieves no type. You can check it with is operator.

Example:
class A
{
    int aField;
    string b;
    bool c;

    class B {}
    struct C {}
    union D {}
    interface E {}
}

static assert(is(getMemberType!(A, "aField") == int));
static assert(is(getMemberType!(A, "b") == string));
static assert(is(getMemberType!(A, "c") == bool));

static assert(!is(getMemberType!(A, "B")));
static assert(!is(getMemberType!(A, "C")));
static assert(!is(getMemberType!(A, "D")));
static assert(!is(getMemberType!(A, "E")));

template  FieldNameTuple(Class)

 FieldNameTuple

Retrieves names of all class/struct/union Class fields excluding technical ones like this, Monitor.

Example:
class A
{
    int aField;

    void func1() {}
    static void func2() {}

    string b;

    final func3() {}
    abstract void func4();

    bool c;
}

static assert(FieldNameTuple!A == ["aField","b","c"]);

void  removeOne(T)(ref DList!T list, T elem);

Removes one element from the list

NEVER use while iterating the list.


struct  Maybe(T) if (is(T == class) || is(T == interface) || isPointer!T || isArray!T);

Struct-wrapper to handle result of computations, that can fail.

Example:
class A {}

auto a = new A();
auto ma = Maybe!A(a);
auto mb = Maybe!A(null);

assert(!ma.isNothing);
assert(mb.isNothing);

assert(ma.get == a);
assertThrown!Error(mb.get);

bool ncase = false, jcase = false;
ma.map(() {ncase = true;}, (v) {jcase = true;});
assert(jcase && !ncase);

ncase = jcase = false;
mb.map(() {ncase = true;}, (v) {jcase = true;});
assert(!jcase && ncase);

alias  StoredType = T;

Alias to stored type


pure this(T value);

Constructing Maybe from value. If pointer is null methods: isNothing returns true and get throws Error.


static Maybe!T  nothing();

Constructing empty Maybe. If Maybe is created with the method, it is considred empty and isNothing returns false.


const bool  isNothing();

Returns true if stored value is null


T  get();

Unwrap value from Maybe. If stored value is null, Error is thrown.


const const(T)  get();

Unwrap value from Maybe. If stored value is null, Error is thrown.


U  map(U)(U delegate() nothingCase, U delegate(T) justCase);

If struct holds null, then nothingCase result is returned. If struct holds not null value, then result is returned. justCase is fed with unwrapped value.


const U  map(U)(U delegate() nothingCase, U delegate(const T) justCase);

If struct holds null, then nothingCase result is returned. If struct holds not null value, then result is returned. justCase is fed with unwrapped value.


struct  Maybe(T) if (is(T == struct) || isAssociativeArray!T || isBasicType!T);

Struct-wrapper to handle result of computations, that can fail.

Example:
struct A {}

auto ma = Maybe!A(A());
auto mb = Maybe!A.nothing;

assert(!ma.isNothing);
assert(mb.isNothing);

assert(ma.get == A());
assertThrown!Error(mb.get);

bool ncase = false, jcase = false;
ma.map(() {ncase = true;}, (v) {jcase = true;});
assert(jcase && !ncase);

ncase = jcase = false;
mb.map(() {ncase = true;}, (v) {jcase = true;});
assert(!jcase && ncase);

alias  StoredType = T;

Alias to stored type


static Maybe!T  nothing();

Constructing empty Maybe. If Maybe is created with the method, it is considred empty and isNothing returns false.


pure this(T value);

Constructing Maybe from value. If Maybe is created with the constructor, it is considered non empty and isNothing returns false.


const bool  isNothing();

Returns true if stored value is null


T  get();

Unwrap value from Maybe. If the Maybe is empty, Error is thrown.


const const(T)  get();

Unwrap value from Maybe. If the Maybe is empty, Error is thrown.


U  map(U)(U delegate() nothingCase, U delegate(T) justCase);

If struct holds null, then nothingCase result is returned. If struct holds not null value, then result is returned. justCase is fed with unwrapped value.


const U  map(U)(U delegate() nothingCase, U delegate(const T) justCase);

If struct holds null, then nothingCase result is returned. If struct holds not null value, then result is returned. justCase is fed with unwrapped value.


auto  generator(T)(Maybe!T delegate() genfunc);

Transforms delegate into lazy range. Generation is stopped, when genfunc returns Maybe!T.nothing.

Example:
assert( (() => Maybe!int(1)).generator.take(10).equal(1.repeat.take(10)) );
assert( (() => Maybe!int.nothing).generator.empty);
assert( (()
        {
            static size_t i = 0;
            return i++ < 10 ? Maybe!int(1) : Maybe!int.nothing;
        }
        ).generator.equal(1.repeat.take(10)));

class A {}
auto a = new A();

assert( (() => Maybe!A(a)).generator.take(10).equal(a.repeat.take(10)) );
assert( (() => Maybe!A.nothing).generator.empty);
assert( (()
        {
            static size_t i = 0;
            return i++ < 10 ? Maybe!A(a) : Maybe!A.nothing;
        }
        ).generator.equal(a.repeat.take(10)));

@property void  tie(T...)(Tuple!(TypesOf!T) t);

Allows to fast retreiving results from functions that returns a tuple.

Examples
Example
Tuple!(int, string) foo()
{
    return tuple(1, "a");
}

int x;
string y;

tie!(x,y) = foo();
assert(x == 1 && y == "a");