/usr/include/d/gtkd-3/gtkd/Implement.d is in libgtkd-3-dev 3.7.5-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | /*
* This file is part of gtkD.
*
* gtkD is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version, with
* some exceptions, please read the COPYING file.
*
* gtkD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with gtkD; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/
module gtkd.Implement;
import std.algorithm;
import std.traits;
import std.meta;
import std.range;
import std.string;
import std.uni;
import std.conv;
import gobject.c.types;
/**
* This template generates the boilerplate needed to override
* GTK functions from D.
*
* Example:
* --------------------------
* class MyApplication : Application
* {
* import gtkd.Implement;
* import gobject.c.functions : g_object_newv;
*
* mixin ImplementClass!GtkApplication;
*
* this()
* {
* //TODO: sort out the constructor.
* super(cast(GtkApplication*)g_object_newv(getType(), 0, null), true);
*
* setApplicationId("org.gtkd.demo.popupmenu");
* setFlags(GApplicationFlags.FLAGS_NONE);
* }
*
* override void activate()
* {
* new PopupMenuDemo(this);
* }
* }
* --------------------------
*/
mixin template ImplementClass(Class)
{
mixin(ImplementClassImpl!(Class, typeof(this))());
}
/**
* This template generates the boilerplate needed to implement a
* GTK interface in D.
*
* Base is the Gtk struct for the base class, and Iface is the
* Gtk Iface struct for the interface.
*
* In your constructor you will need to instantiate the Gtk class
* by calling the ObjectG costructor: `super(getType(), null);`
*
* If you are using ImplementInterface in conjunction with ImplementClass
* you will need to mixin ImplementClass before mixing in any interfaces.
*/
mixin template ImplementInterface(Base, Iface)
{
mixin(ImplementInterfaceImpl!(Base, Iface, typeof(this))());
}
template ImplementClassImpl(Klass, Impl)
{
string ImplementClassImpl()
{
string result;
result ~= "import glib.Str;\n"~
"import gobject.Type : Type;\n"~
"import gobject.c.functions : g_type_class_peek_parent, g_object_get_data;\n";
if ( !is(Klass == gobject.c.types.GObject) )
result ~= "import "~ getTypeImport!Klass() ~": "~ getTypeFunction!Klass()[0..$-2] ~";\n";
if ( !hasMember!(Impl, toCamelCase!Impl()) )
{
result ~= "\nstruct "~ toPascalCase!Impl() ~"\n"~
"{\n"~
"\t"~ Klass.stringof ~" parentInstance;\n"~
"}\n\n";
result ~= "struct "~ toPascalCase!Impl() ~"Class\n"~
"{\n"~
"\t"~ Klass.stringof ~"Class parentClass;\n"~
"}\n\n";
result ~= "protected "~ toPascalCase!Impl() ~"* "~ toCamelCase!Impl() ~";\n"~
"protected static "~ Klass.stringof ~"Class* parentClass = null;\n\n";
result ~= "protected override void* getStruct()\n"~
"{\n"~
"\treturn cast(void*)gObject;\n"~
"}\n\n";
}
if ( !implements!Impl("getType") )
{
result ~= "public static GType getType()\n"~
"{\n"~
"\timport std.algorithm : startsWith;\n\n"~
"\tGType "~ toCamelCase!Impl() ~"Type = Type.fromName(\""~ toPascalCase!Impl() ~"\");\n\n"~
"\tif ("~ toCamelCase!Impl() ~"Type == GType.INVALID)\n"~
"\t{\n"~
"\t\t"~ toCamelCase!Impl() ~"Type = Type.registerStaticSimple(\n"~
"\t\t\t"~ getTypeFunction!Klass() ~",\n"~
"\t\t\t\""~ toPascalCase!Impl() ~"\",\n"~
"\t\t\tcast(uint)"~ toPascalCase!Impl() ~"Class.sizeof,\n"~
"\t\t\tcast(GClassInitFunc) &"~ toCamelCase!Impl() ~"ClassInit,\n"~
"\t\t\tcast(uint)"~ toPascalCase!Impl() ~".sizeof, null, cast(GTypeFlags)0);\n\n"~
"\t\tforeach ( member; __traits(derivedMembers, "~ Impl.stringof ~") )\n"~
"\t\t{\n"~
"\t\t\tstatic if ( member.startsWith(\"_implementInterface\") )\n"~
"\t\t\t\t__traits(getMember, "~ Impl.stringof ~", member)("~ toCamelCase!Impl() ~"Type);\n"~
"\t\t}\n"~
"\t}\n\n"~
"\treturn "~ toCamelCase!Impl() ~"Type;\n"~
"}\n\n";
}
result ~= "extern(C)\n{\n";
if ( !implements!Impl(toCamelCase!Impl() ~"ClassInit") )
{
result ~= "static void "~ toCamelCase!Impl() ~"ClassInit (void* klass)\n"~
"{\n"~
"\tparentClass = cast("~ Klass.stringof ~"Class*) g_type_class_peek_parent(klass);\n";
result ~= setFunctionPointers!(getClass!Klass)();
result ~= "}\n\n";
}
result ~= getWrapFunctions!(getClass!Klass)();
result ~= "}";
return result;
}
string setFunctionPointers(GtkClass)()
{
string result;
alias names = FieldNameTuple!GtkClass;
foreach ( i, member; Fields!GtkClass )
{
static if ( names[i] == "parentClass" )
{
result ~= "\t"~ fullyQualifiedName!member ~"* "~ toCamelCase!member() ~" = cast("~ fullyQualifiedName!member ~"*)klass;\n";
result ~= setFunctionPointers!member();
}
else if ( isCallable!member &&
implements!Impl(names[i]) &&
!implements!Impl(toCamelCase!Impl() ~ names[i].capitalizeFirst) )
//TODO: __traits(isOverrideFunction, Foo.foo) ?
{
result ~= "\t"~ toCamelCase!GtkClass() ~"."~ names[i] ~" = &"~ toCamelCase!Impl() ~ names[i].capitalizeFirst ~";\n";
}
}
result ~= "\n";
return result;
}
string getWrapFunctions(GtkClass)()
{
string result;
alias names = FieldNameTuple!GtkClass;
foreach ( i, member; Fields!GtkClass )
{
static if ( names[i] == "parentClass" )
{
result ~= getWrapFunctions!member();
}
else static if ( isCallable!member &&
implements!Impl(names[i]) &&
!implements!Impl(toCamelCase!Impl() ~ names[i].capitalizeFirst) )
//TODO: __traits(isOverrideFunction, Foo.foo) ?
{
result ~= getWrapFunction!(Impl, member, names[i]);
}
}
return result;
}
template getClass(Instance)
{
mixin("import "~ getClassImport!Instance() ~"; alias getClass = "~ Instance.stringof ~"Class;");
}
private string getClassImport(Klass)()
{
return fullyQualifiedName!Klass.replace("."~ Klass.stringof, "");
}
}
template ImplementInterfaceImpl(Base, Klass, Impl)
{
string ImplementInterfaceImpl()
{
string result;
result ~= "import glib.Str;\n"~
"import gobject.Type : Type;\n"~
"import gobject.c.functions : g_type_class_peek_parent, g_object_get_data;\n";
if ( !is(Base == gobject.c.types.GObject) )
result ~= "import "~ getTypeImport!Base() ~": "~ getTypeFunction!Base()[0..$-2] ~";\n";
result ~= "import "~ getTypeImport!Klass() ~" : "~ getTypeFunction!Klass()[0..$-2] ~";\n\n";
if ( !hasMember!(Impl, toCamelCase!Impl()) )
{
result ~= "\nstruct "~ toPascalCase!Impl() ~"\n"~
"{\n"~
"\t"~ Base.stringof ~" parentInstance;\n"~
"}\n\n";
result ~= "struct "~ toPascalCase!Impl() ~"Class\n"~
"{\n"~
"\t"~ Base.stringof ~"Class parentClass;\n"~
"}\n\n";
result ~= "protected "~ toPascalCase!Impl() ~"* "~ toCamelCase!Impl() ~";\n"~
"protected static "~ Base.stringof ~"Class* parentClass = null;\n\n";
result ~= "protected override void* getStruct()\n"~
"{\n"~
"\treturn cast(void*)gObject;\n"~
"}\n\n";
if ( is(Base == gobject.c.types.GObject) )
{
result ~= "public this()\n"~
"{\n"~
"\tauto p = super(getType(), null);\n"~
"\t"~ toCamelCase!Impl() ~" = cast("~ toPascalCase!Impl() ~"*) p.getObjectGStruct();\n"~
"}\n\n";
}
}
if ( !implements!Impl("getType") )
{
result ~= "public static GType getType()\n"~
"{\n"~
"\tGType "~ toCamelCase!Impl() ~"Type = Type.fromName(\""~ toPascalCase!Impl() ~"\");\n\n"~
"\tif ("~ toCamelCase!Impl() ~"Type == GType.INVALID)\n"~
"\t{\n"~
"\t\t"~ toCamelCase!Impl() ~"Type = Type.registerStaticSimple(\n"~
"\t\t\t"~ getTypeFunction!Base() ~",\n"~
"\t\t\t\""~ toPascalCase!Impl() ~"\",\n"~
"\t\t\tcast(uint)"~ toPascalCase!Impl() ~"Class.sizeof,\n"~
"\t\t\tcast(GClassInitFunc) &"~ toCamelCase!Impl() ~"ClassInit,\n"~
"\t\t\tcast(uint)"~ toPascalCase!Impl() ~".sizeof, null, cast(GTypeFlags)0);\n\n"~
"\t\tforeach ( member; __traits(derivedMembers, "~ Impl.stringof ~") )\n"~
"\t\t{\n"~
"\t\t\tstatic if ( member.startsWith(\"_implementInterface\") )\n"~
"\t\t\t\t__traits(getMember, "~ Impl.stringof ~", member)("~ toCamelCase!Impl() ~"Type);\n"~
"\t\t}\n"~
"\t}\n\n"~
"\treturn "~ toCamelCase!Impl() ~"Type;\n"~
"}\n\n";
}
result ~= "static void _implementInterface"~ Klass.stringof ~"(GType type)\n"~
"{\n"~
"\tGInterfaceInfo "~ Klass.stringof ~"Info =\n"~
"\t{\n"~
"\t\tcast(GInterfaceInitFunc) &"~ toCamelCase!Klass() ~"Init,\n"~
"\t\tnull,\n"~
"\t\tnull\n"~
"\t};\n"~
"\tType.addInterfaceStatic(type, "~ getTypeFunction!Klass() ~", &"~ Klass.stringof ~"Info);\n"~
"};\n\n";
result ~= "extern(C)\n{\n";
if ( !implements!Impl(toCamelCase!Impl() ~"ClassInit") )
{
result ~= "static void "~ toCamelCase!Impl() ~"ClassInit (void* klass)\n"~
"{\n"~
"\tparentClass = cast("~ Base.stringof ~"Class*) g_type_class_peek_parent(klass);\n"~
"}\n\n";
}
if ( !implements!Impl(toCamelCase!Klass() ~"Init") )
{
result ~= "static void "~ toCamelCase!Klass() ~"Init ("~ Klass.stringof ~" *iface)\n"~
"{\n";
auto names = FieldNameTuple!Klass;
foreach ( i, member; Fields!Klass )
{
if ( isCallable!member && implements!Impl(names[i]) && (!implements!Impl("addOn"~ names[i].capitalizeFirst) || implements!Impl(toCamelCase!Impl() ~ names[i].capitalizeFirst) ) )
{
result ~= "\tiface."~ names[i] ~" = &"~ toCamelCase!Impl() ~ names[i].capitalizeFirst ~";\n";
}
}
result ~= "}\n\n";
}
alias names = FieldNameTuple!Klass;
foreach ( i, member; Fields!Klass )
{
if ( isCallable!member &&
implements!Impl(names[i]) &&
!implements!Impl(toCamelCase!Impl() ~ names[i].capitalizeFirst) &&
!implements!Impl("addOn"~ names[i].capitalizeFirst) )
{
result ~= getWrapFunction!(Impl, member, names[i]);
}
}
result ~= "}";
return result;
}
}
private string getTypeFunction(Iface)()
{
string result;
if ( is(Iface == gobject.c.types.GObject) )
return "GType.OBJECT";
else
{
foreach ( i, char c; Iface.stringof )
{
if ( c.isUpper && i > 0 )
result ~= "_"~c;
else
result ~= c;
}
return result.toLower.replace("_iface", "")~ "_get_type()";
}
}
private string getTypeImport(Iface)()
{
return fullyQualifiedName!Iface.replace("types."~ Iface.stringof, "functions");
}
private string getWrapFunction(Impl, Member, string name)()
{
string result;
static if ( isCallable!Member )
{
alias Params = Parameters!Member;
alias STC = ParameterStorageClass;
auto ParamStorage = [STC.none, ParameterStorageClassTuple!(__traits(getMember, Impl, name))];
auto ParamNames = ["iface", ParameterIdentifierTuple!(__traits(getMember, Impl, name))];
alias DParamTypes = AliasSeq!(void, Parameters!(__traits(getMember, Impl, name)));
result ~= "static "~ ReturnType!Member.stringof ~" "~ toCamelCase!Impl() ~ name.capitalizeFirst ~"(";
foreach ( i, param; Params )
{
if ( i > 0 )
result ~= ", ";
result ~= param.stringof ~" "~ ParamNames[i];
}
result ~= ")\n"~
"{\n";
if ( implements!Impl("get"~ Impl.stringof ~"Struct") && implements!Impl("getStruct") )
result ~= "\tauto impl = ObjectG.getDObject!("~ Impl.stringof ~")(cast("~ toPascalCase!Impl() ~"*)iface);\n";
else
result ~= "\tauto impl = cast("~ Impl.stringof ~")g_object_get_data(cast(GObject*)iface, \"GObject\".ptr);\n";
foreach ( i, param; Params )
{
if ( ParamStorage[i] == STC.out_ && isGtkdType!(DParamTypes[i]) )
result ~= "\t"~ DParamTypes[i].stringof ~" d_"~ ParamNames[i] ~";\n";
else if ( ParamStorage[i] == STC.ref_ && isGtkdType!(DParamTypes[i]) )
result ~= "\t"~ DParamTypes[i].stringof ~" d_"~ ParamNames[i] ~" = "~ ParamNames[i] ~".get"~ DParamTypes[i].stringof ~"Struct();\n";
}
if ( is(ReturnType!Member == void) )
result ~= "\n\timpl."~ name ~"(";
else
result ~= "\n\tauto ret = impl."~ name ~"(";
foreach ( i, param; Params )
{
if ( i == 0 )
continue;
else
{
if ( i > 1 )
result ~= ", ";
if ( (ParamStorage[i] == STC.out_ || ParamStorage[i] == STC.ref_) && isGtkdType!(DParamTypes[i]) )
result ~= "d_"~ ParamNames[i];
else if ( isGtkdType!(DParamTypes[i]) )
result ~= "ObjectG.getDObject!("~ DParamTypes[i].stringof ~")("~ ParamNames[i] ~")";
else
result ~= ParamNames[i];
}
}
result ~= ");\n\n";
foreach ( i, param; Params )
{
if ( (ParamStorage[i] == STC.out_ || ParamStorage[i] == STC.ref_) && isGtkdType!(DParamTypes[i]) )
{
result ~= "\tif ( d_"~ ParamNames[i] ~" !is null )\n"~
"\t\t*"~ ParamNames[i] ~" = *d_"~ ParamNames[i] ~".get"~ DParamTypes[i].stringof ~"Struct();\n";
}
}
if ( isGtkdType!(ReturnType!(__traits(getMember, Impl, name))) && isPointer!(ReturnType!Member) )
result ~= "\treturn ret ? ret.get"~ (ReturnType!(__traits(getMember, Impl, name))).stringof ~"Struct() : null;\n";
else if ( !is(ReturnType!Member == void) )
result ~= "\treturn ret;\n";
result ~= "}\n\n";
}
return result;
}
private string toCamelCase(Type)()
{
string result;
foreach (i, word; to!string(fullyQualifiedName!Type).split("."))
{
if ( i == 0 )
word = word[0 .. 1].toLower ~ word[1 .. $];
else
word = word.capitalizeFirst;
result ~= word;
}
return result;
}
private string toPascalCase(Type)()
{
string result;
foreach (word; to!string(fullyQualifiedName!Type).split("."))
{
result ~= word.capitalizeFirst;
}
return result;
}
private template isGtkdType(T)
{
static if ( __traits(compiles, new T(cast(typeof(T.tupleof[0]))null, true)) )
enum bool isGtkdType = hasMember!(T, "get"~ T.stringof ~"Struct");
else
enum bool isGtkdType = false;
}
private bool implements(Impl)(string member)
{
return (cast(string[])[__traits(derivedMembers, Impl)]).canFind(member);
}
private string capitalizeFirst(string str)
{
if ( str.empty )
return str;
else if ( str.length == 1 )
return str.toUpper;
else
return str[0 .. 1].toUpper ~ str[1 .. $];
}
|