What're these used for?

Recently I was going through a bunch of old game's source code and I found some things I don't quite understand about how it works. I am aware this is used in a lot of projects, but for the longest time I've been trying to figure it out with no luck.

I see things like

 
CS_LEAKGUARD_IMPLEMENT (csBaseRenderStepLoader);


 
extern CS_IMPLEMENTATION void cs_baseRegisterMode(char* arguments)


They're just inside the .CPP file not within a function or class.

I know quite a bit about C++ programming, pointers, syntax, etc and have been working on a 2D Game Engine using SFML and a combination of other things.

I understand methods, classes, callbacks, static functions. Using preprocessor directives, etc.. but this is rather strange because it doesn't seem to be a method.

Can someone give me an Explanation on how these are used and implemented? The more details the better it will help me understand. Thank you!
Last edited on
CS_LEAKGUARD_IMPLEMENT (csBaseRenderStepLoader);

CS_LEAKGUARD_IMPLEMENT is probably a macro (#define) that expands to a class definition or something. Of course I can't say for sure without seeing the actual macro definition.

Macros were used like this in older C++ before templates became more accepted. This line here might be similar to a template instantiation.

extern CS_IMPLEMENTATION void cs_baseRegisterMode(char* arguments)

CS_IMPLEMENTATION is another macro... but this time it's either defined as nothing (ie just:
#define CS_IMPLEMENTATION ) or is defined as a calling convention... similar to WinAPI's 'CALLBACK'.

Calling conventions tell the compiler how the parameters for the function should be arranged on the assembly level. This can be important for linking with other libraries/programs since if the parameters are arranged differently, calling the functions will not work as expected across the lib boundary. Explicitly specifying the calling convention ensures that the parameters will be arranged consistently.


The rest of that (sans the CS_IMPLEMENTATION) is just a normal function prototype/definition.
Last edited on
Well, I took a look at the leakguard and it comes out as this.

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
#ifndef __CS_LEAKGUARD_H__
#define __CS_LEAKGUARD_H__

#ifdef CS_USE_LEAK_GUARD
#define CS_LEAKGUARD_DECLARE(m) \
struct csLeakGuard \
{ \
  int counter; \
  csLeakGuard () : counter (0) { } \
  ~csLeakGuard () \
  { \
    if (counter != 0) \
    { \
      printf ("%d leaking instance(s) of %s detected!\n", counter, #m); \
      fflush (stdout); \
    } \
  } \
}; \
static csLeakGuard leakguard; \
struct csLeakGuardInstance \
{ \
  csLeakGuardInstance () \
  { \
    leakguard.counter++; \
  } \
  ~csLeakGuardInstance () \
  { \
    leakguard.counter--; \
  } \
}; \
csLeakGuardInstance leakguardinstance

#define CS_LEAKGUARD_IMPLEMENT(m) \
  m::csLeakGuard m::leakguard

#else
#define CS_LEAKGUARD_DECLARE(m) \
  struct csLeakGuard /* ignored; pacify -ansi -pedantic */
#define CS_LEAKGUARD_IMPLEMENT(m) \
  struct csLeakGuard /* ignored; pacify -ansi -pedantic */
#endif

#endif // __CS_LEAKGUARD_H__ 


Sort of looks like a template, but I'm not too sure. Many other source files seem to use both templates(Using <>) and this.
Last edited on
yup that's exactly what I thought. It's the macro version of what now would be written as a template.


EDIT:

C still does this kind of stuff because it doesn't have templates... but this has ctors/dtors so it's clearly C++. Either this code is old or the developers just didn't know how to use templates properly (or couldn't use them due to environment restraints).
Last edited on
That'd make perfect sense then. This game's workspace has over a dozen different projects that use C++ or C code.

Anyway I understand the concept of it now. Thanks for the information
Topic archived. No new replies allowed.