1 #include "datasource.qh"
4 CLASS(DataSource, Object)
5 entity DataSource_true;
6 entity DataSource_false;
7 INIT_STATIC(DataSource) {
8 DataSource_true = NEW(Object);
9 DataSource_false = NULL;
12 * get entry `i` passing `name` and `icon` through `returns` if it is not null
13 * returns `DataSource_false` if out of bounds
14 * otherwise returns an entity or `DataSource_true`
16 METHOD(DataSource, getEntry, entity(entity this, int i, void(string name, string icon) returns)) { return DataSource_false; }
17 /** return the index of the first match for `find`. optional */
18 METHOD(DataSource, indexOf, int(entity this, string find)) { return -1; }
19 /** reload all entries matching `filter` returning how many matches were found */
20 METHOD(DataSource, reload, int(entity this, string filter)) { return 0; }
21 /** cleanup on shutdown. optional */
22 METHOD(DataSource, destroy, void(entity this)) { }
26 CLASS(StringSource, DataSource)
27 ATTRIB(StringSource, StringSource_str, string, string_null)
28 ATTRIB(StringSource, StringSource_sep, string, string_null)
29 CONSTRUCTOR(StringSource, string str, string sep)
31 CONSTRUCT(StringSource);
32 this.StringSource_str = str;
33 this.StringSource_sep = sep;
35 METHOD(StringSource, getEntry, entity(entity this, int i, void(string name, string icon) returns))
37 int n = tokenizebyseparator(this.StringSource_str, this.StringSource_sep);
38 if (i < 0 || i >= n) return DataSource_false;
40 if (returns) returns(s, string_null);
41 return DataSource_true;
43 METHOD(StringSource, reload, int(entity this, string filter))
45 return tokenizebyseparator(this.StringSource_str, this.StringSource_sep);
47 ENDCLASS(StringSource)
49 CLASS(CvarStringSource, StringSource)
50 ATTRIB(CvarStringSource, CvarStringSource_cvar, string, string_null)
51 CONSTRUCTOR(CvarStringSource, string cv, string sep)
53 CONSTRUCT(CvarStringSource);
54 this.CvarStringSource_cvar = cv;
55 this.StringSource_sep = sep;
57 METHOD(CvarStringSource, getEntry, entity(entity this, int i, void(string name, string icon) returns))
59 string s = this.CvarStringSource_cvar;
60 this.StringSource_str = s ? cvar_string(s) : string_null;
61 return SUPER(CvarStringSource).getEntry(this, i, returns);
63 METHOD(CvarStringSource, reload, int(entity this, string filter))
65 string s = this.CvarStringSource_cvar;
66 this.StringSource_str = s ? cvar_string(s) : string_null;
67 return SUPER(CvarStringSource).reload(this, filter);
69 ENDCLASS(CvarStringSource)