Source

glLoadGen / modules / StylePointerCPP.lua

  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
local common = require "CommonStyle"

local my_style = {}
my_style.header = {}
my_style.source = {}

----------------------------------------------------
-- Global styling functions.
function my_style.WriteLargeHeading(hFile, headingName)
	hFile:write(string.rep("/", 6 + #headingName), "\n")
	hFile:write("// ", headingName, "\n")
	hFile:write(string.rep("/", 6 + #headingName), "\n")
end

function my_style.WriteSmallHeading(hFile, headingName)
	hFile:write("// ", headingName, "\n")
end

------------------------------------------------------
-- Header styling functions

function my_style.header.CreateFile(basename, options)
	local filename = basename .. ".hpp"
	return common.CreateFile(filename, options.indent), filename
end

local function GenIncludeGuardName(hFile, spec, options)
	local str = "POINTER_CPP_GENERATED_HEADER" ..
		spec.GetIncludeGuardString() .. "_HPP"

	if(#options.prefix > 0) then
		return options.prefix:upper() .. "_" .. str
	end
	
	return str
end

function my_style.header.WriteBeginIncludeGuard(hFile, spec, options)
	local inclGuard = GenIncludeGuardName(hFile, spec, options)
	
	hFile:fmt("#ifndef %s\n", inclGuard)
	hFile:fmt("#define %s\n", inclGuard)
end

function my_style.header.WriteEndIncludeGuard(hFile, spec, options)
	hFile:fmt("#endif //%s\n", GenIncludeGuardName(hFile, spec, options))
end

function my_style.header.WriteStdTypedefs(hFile, specData, spec, options)
	local defArray = common.GetStdTypedefs()
	hFile:write("#ifndef GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS\n")
	hFile:write("#define GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS\n")
	hFile:write("\n")
	hFile:inc()
	for _, def in ipairs(defArray) do
		hFile:write(def)
	end
	hFile:dec()
	hFile:write("\n")
	hFile:write("#endif /*GL_LOAD_GEN_BASIC_OPENGL_TYPEDEFS*/\n")
end

function my_style.header.WriteSpecTypedefs(hFile, specData, spec, options)
	hFile:push()
	common.WritePassthruData(hFile, specData.funcData.passthru)
	hFile:pop()
end

local function StartNamespace(hFile, namespaceName)
	hFile:fmt("namespace %s\n", namespaceName)
	hFile:write("{\n")
	hFile:inc()
end

local function EndNamespace(hFile, namespaceName)
	hFile:dec()
	hFile:fmt("} //namespace %s\n", namespaceName)
end

function my_style.header.WriteBeginDecl(hFile, spec, options)
	if(#options.prefix > 0) then
		StartNamespace(hFile, options.prefix)
	end
	StartNamespace(hFile, spec.FuncNamePrefix())
end

function my_style.header.WriteEndDecl(hFile, spec, options)
	EndNamespace(hFile, spec.FuncNamePrefix())
	if(#options.prefix > 0) then
		EndNamespace(hFile, options.prefix)
	end
end

local extBlockNamespace = "exts"
local extVariableTypeDefinition = [[
class LoadTest
{
private:
	//Safe bool idiom. Joy!
	typedef void (LoadTest::*bool_type)() const;
	void big_long_name_that_really_doesnt_matter() const {}
	
public:
	operator bool_type() const
	{
		return m_isLoaded ? &LoadTest::big_long_name_that_really_doesnt_matter : 0;
	}
	
	int GetNumMissing() const {return m_numMissing;}
	
	void IgnoreThis(bool isLoaded, int numMissing)
	{
		m_isLoaded = isLoaded;
		m_numMissing = numMissing;
	}
	
	LoadTest() : m_isLoaded(false), m_numMissing(0) {}
private:
	bool m_isLoaded;
	int m_numMissing;
};
]]

function my_style.header.WriteBeginExtVarDeclBlock(hFile, spec, options)
	StartNamespace(hFile, extBlockNamespace)
	hFile:writeblock(extVariableTypeDefinition)
	hFile:write("\n")
end

function my_style.header.WriteEndExtVarDeclBlock(hFile, spec, options)
	EndNamespace(hFile, extBlockNamespace)
end

local function GenExtensionVarName(extName, spec, options)
	return "var_" .. extName;
end

function my_style.header.WriteExtVariableDecl(hFile, extName,
	specData, spec, options)
	hFile:fmt("extern LoadTest %s;\n",
		GenExtensionVarName(extName, spec, options));
end

function my_style.header.WriteBeginEnumDeclBlock(hFile, spec, options)
	hFile:write("enum\n")
	hFile:write("{\n")
	hFile:inc()
end

function my_style.header.WriteEndEnumDeclBlock(hFile, spec, options)
	hFile:dec()
	hFile:write("}\n")
end

local function GenEnumName(enum)
	--Note: some enumerators start with characters C++ forbids as initial
	--identifiers. If we detect such an enum, prefix it with `_`.
	local enumName = enum.name
	if(not enumName:match("^[a-zA-Z_]")) then
		enumName = "_" .. enumName
	end
	
	return enumName
end

function my_style.header.WriteEnumDecl(hFile, enum, enumTable, spec, options)
	local enumName = GenEnumName(enum)
	local lenEnum = #enumName
	local numIndent = 33
	
	local numSpaces = numIndent - lenEnum
	if(numSpaces < 1) then
		numSpaces = 1
	end

	hFile:fmt("%s%s= %s,\n",
		enumName,
		string.rep(" ", numSpaces),
		common.ResolveEnumValue(enum, enumTable))
end

function my_style.header.WriteEnumPrevDecl(hFile, enum, enumTable, spec, options, extName)
	hFile:fmt("//%s taken from ext: %s\n", enum.name, extName)
end

function my_style.header.WriteBeginFuncDeclBlock(hFile, spec, options)
	--Block containing all spec function declarations.
end

function my_style.header.WriteEndFuncDeclBlock(hFile, spec, options)
	--Block containing all spec function declarations.
end

function my_style.header.WriteBeginExtFuncDeclBlock(hFile, extName, spec, options)
	--Block containing all spec function declarations for a particular extension.
	--Useful for include-guards around extension function pointers.
end

function my_style.header.WriteEndExtFuncDeclBlock(hFile, extName, spec, options)
	--Block containing all spec function declarations for a particular extension.
end

local function GenFuncPtrName(func, spec, options)
	return func.name
end

local function GenFuncPtrTypedefName(func, spec, options)
	return "PFN" .. GenFuncPtrName(func, spec, options):upper()
end

local function WriteFuncPtrTypedefStmt(hFile, func, typemap, spec, options)
	hFile:fmt("typedef %s (%s *%s)(%s);\n",
		common.GetFuncReturnType(func, typemap),
		spec.GetCodegenPtrType(),
		GenFuncPtrTypedefName(func, spec, options),
		common.GetFuncParamList(func, typemap))
end

local function GenFuncPtrDefDirect(func, typemap, spec, options)
	return string.format("%s (%s *%s)(%s)",
		common.GetFuncReturnType(func, typemap),
		spec.GetCodegenPtrType(),
		GenFuncPtrName(func, spec, options),
		common.GetFuncParamList(func, typemap))
end

local function GenFuncPtrDefTypedef(func, typemap, spec, options)
	return string.format("%s %s",
		GenFuncPtrTypedefName(func, spec, options),
		GenFuncPtrName(func, spec, options))
end

function my_style.header.WriteFuncDecl(hFile, func, typemap, spec, options)
	hFile:write("extern ",
		GenFuncPtrDefDirect(func, typemap, spec, options),
		";\n")
end

function my_style.header.WriteBeginSysDeclBlock(hFile, spec, options)
	StartNamespace(hFile, "sys")
end

function my_style.header.WriteEndSysDeclBlock(hFile, spec, options)
	EndNamespace(hFile, "sys")
end

function my_style.header.WriteUtilityDecls(hFile, spec, options)
	--Write declarations for public utility stuff. Enums for return values, etc.
end

function my_style.header.WriteMainLoaderFuncDecl(hFile, spec, options)
	hFile:write("LoadTest LoadFunctions(%s);\n", spec.GetLoaderParams())
end

function my_style.header.WriteVersioningFuncDecls(hFile, spec, options)
	hFile:writeblock([[
int GetMinorVersion();
int GetMajorVersion();
bool IsVersionGEQ(int majorVersion, int minorVersion);
]])
end

--------------------------------------------------
-- Source code styling functions.
function my_style.source.CreateFile(basename, options)
	--Create a source file. Use:
	--common.CreateFile(filename, options.indent)
	--To create it. You don't need to return the filename, just the hFile.
	return common.CreateFile(basename .. ".cpp", options.indent)
end

function my_style.source.WriteIncludes(hFile, spec, options)
	--Write the system include files for things you wish to use.
end

function my_style.source.WriteBeginDef(hFile, spec, options)
	--Block containing the entire set of definitions.
end

function my_style.source.WriteEndDef(hFile, spec, options)
	--Block containing the entire set of definitions.
end

function my_style.source.WriteBeginExtVarDefBlock(hFile, spec, options)
	--Block containing the extension variable definitions.
end

function my_style.source.WriteEndExtVarDefBlock(hFile, spec, options)
	--Block containing the extension variable definitions.
end

function my_style.source.WriteExtVariableDef(hFile, extName,
	specData, spec, options)
	--Writes the definition for the extension variable `extName`.
end

function my_style.source.WriteBeginExtFuncDefBlock(hFile, extName, spec, options)
	--Block containing the extension function definitions and load function
	--for the functions in the extension `extName`.
end

function my_style.source.WriteEndExtFuncDefBlock(hFile, extName, spec, options)
	--Block containing the extension function definitions and load function
	--for the functions in the extension `extName`.
end

function my_style.source.WriteFuncDef(hFile, func, typemap, spec, options)
	--Writes the definition of an extension function.
	--Function pointers should be declared with spec.GetCodegenPtrType().
	--common.GetFuncReturnType(func, typemap) and
	--common.GetFuncParamList(func, typemap) can get the function types.
	--A third parameter passed to GetFuncParamList can tell it to provide
	--parameter names.
	--common.GetOpenGLFuncName(func, spec) can be used to get the
	--proper OpenGL name of the function.
end

function my_style.source.WriteBeginExtLoaderBlock(hFile, extName, spec, options)
	--The start of the loader function for the extension `extName`.
	--The general idea is that this function should write the beginning of
	--a file-static function that will contain the results of later
	--WriteExtFuncLoader calls.
end

function my_style.source.WriteEndExtLoaderBlock(hFile, extName, spec, options)
	--The end of the loader function for the extension `extName`.
end

function my_style.source.WriteExtFuncLoader(hFile, func, typemap, spec, options)
	--Writes the code to load the function `func` into a function pointer.
	--It should also write test code to check if the pointer is null
	--and possibly deal with that in some way.
	--To get the name for the function that you should call to load a
	--pointer (which takes the string name of the function), use:
	--common.GetProcAddressName(spec)
end

function my_style.source.WriteBeginCoreFuncDefBlock(hFile, version, spec, options)
	--Block containing the core functions for `version`.
	--The block also contains the loading function for this version.
end

function my_style.source.WriteEndCoreFuncDefBlock(hFile, version, spec, options)
	--Block containing the core functions for `version`.
end

function my_style.source.WriteGetExtStringFuncDef(hFile, func, typemap,
	spec, options)
	--This should write a *file static* function definition for the
	--given function.
	--This is used to ensure that the function needed to get the extension
	--string will be available. Will only ever be called in WGL, because
	--wglGetExtensionStringARB is an extension function. In GL, glGetString
	--will always be there; even in post-3.0, glGetStringi is required to
	--be there. and glXQueryExtensionString is core GLX 1.4.
end

function my_style.source.WriteBeginCoreLoaderBlock(hFile, version, spec, options)
	--The start of the loader function for the version `version`.
	--The general idea is that this function should write the beginning of
	--a file-static function that will contain the results of later
	--WriteCoreFuncLoader calls.
end

function my_style.source.WriteEndCoreLoaderBlock(hFile, version, spec, options)
	--The end of the loader function for the version `version`.
end

function my_style.source.WriteCoreFuncLoader(hFile, func, typemap, spec, options)
	--Writes the code to load the function `func` into a function pointer.
	--It should also write test code to check if the pointer is null
	--and possibly deal with that in some way.
	--To get the name for the function that you should call to load a
	--pointer (which takes the string name of the function), use:
	--common.GetProcAddressName(spec)
	--This is separate from WriteExtFuncLoader because certain core extension
	--functions, like glTextureStorage2DEXT are from EXT_DSA, but not really.
	--So this is a way to not erronously report the absence of EXT_DSA
	--functions. If func.name ends in "EXT", then it's a DSA function,
	--so don't count it if it's not there.
end

function my_style.source.WriteBeginSysDefBlock(hFile, spec, options)
	--Block containing the definitions of the system functions.
	--IE: the functions (and other definitions) used by the user
	--to actually load things. Useful for wrapping it in a namespace.
end

function my_style.source.WriteEndSysDefBlock(hFile, spec, options)
	--Block containing the definitions of the system functions.
end

function my_style.source.WriteUtilityDefs(hFile, specData, spec, options)
	--Writes utility function definitions.
end

function my_style.source.WriteMainLoaderFunc(hFile, specData, spec, options)
	--Writes the actual loader function. Possibly also some utilities used
	--by it.
end

function my_style.source.WriteVersioningFuncs(hFile, specData, spec, options)
	--Write definitions for versioning functions.
	--Will only be called if using a spec that has a version (ie: OpenGL).
end


--------------------------------------------------
-- Style retrieval machinery

local function Create()
	return common.DeepCopyTable(my_style)
end

return { Create = Create }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.