Skip to content

Commit

Permalink
vendor in jar
Browse files Browse the repository at this point in the history
  • Loading branch information
redthing1 committed Jul 13, 2024
1 parent bbd3fd2 commit a346263
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
1 change: 0 additions & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ dependency "witchcraft" version="~>0.1.9"
dependency "colorize" version="~>1.0.5"
dependency "typetips" version="~>0.1.4"
dependency "minlog" version="~>2.0.0"
dependency "jar" version="~>0.1.0"
dependency "dray" version=">=5.0.0-r1 <5.1.0-0"
dependency "optional" version="~>1.3.0"

Expand Down
1 change: 0 additions & 1 deletion dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"ddmp": "0.0.1-0.dev.3",
"dray": "5.0.0-r1",
"fluent-asserts": "0.13.3",
"jar": "0.1.0",
"libdparse": "0.14.0",
"minlog": "2.0.0",
"optional": "1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion source/re/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import re.math;
import re.util.logger;
import re.util.tweens.tween_manager;
import re.util.env;
import jar;
import re.util.jar;
static import raylib;

/**
Expand Down
61 changes: 61 additions & 0 deletions source/re/util/jar.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module re.util.jar;

import std.array;
import std.typecons;

/// a container for registered types
class Jar {
private Object[][TypeInfo] _instance_registry;

/// register a type instance
public T register(T)(T instance) {
auto type = typeid(instance);
if (type !in _instance_registry) {
_instance_registry[type] = new Object[0];
}
_instance_registry[type] ~= instance;
return instance;
}

/// resolve all matching type instances
public T[] resolve_all(T)() {
auto type = typeid(T);
if (type in _instance_registry) {
Appender!(T[]) resolved;
foreach (item; _instance_registry[type]) {
resolved ~= cast(T) item;
}
return resolved.data;
}
return new T[0];
}

/// resolve first matching type instance
public Nullable!T resolve(T)() {
auto items = resolve_all!T();
if (items.length > 0) {
return Nullable!T(items[0]);
}
return Nullable!T.init;
}
}

unittest {
class Cookie {
bool delicious = true;
}

auto jar = new Jar();

auto c1 = new Cookie();
jar.register(c1);
auto r1 = jar.resolve!Cookie();
assert(!r1.isNull, "resolved item was null");
assert(r1.get() == c1, "resolved item did not match");

auto c2 = new Cookie();
jar.register(c2);

const auto cookies = jar.resolve_all!Cookie();
assert(cookies.length == 2, "mismatch in number of registered items");
}

0 comments on commit a346263

Please sign in to comment.