r/C_Programming • • 40m ago

Question Need help avoiding circular dependency

[Solved but I'll gladly accept new solutions]

I am making a small clone of asteroids on C and plan to use the observer pattern described in Game Programming Patterns by Bob Nystrom. (Adapted to C) To do so, I currently have 2 files, a stats system (stats.h/c) and the actual observer pattern code (event.h/c). (Relevant Source code below)

My problem is that stats.h/c requires access to structs and enums defined in event.h, but event.h has a struct that contains a union with a struct defined in stats.h, and I don't know how to solve the circular dependency.

stats.h and stats.c (implementation of statsOnNotify lies in C file)

struct Stats { ... };

// statsOnNotify requires struct Observer and enum EventType, defined in event.h 
void statsOnNotify(struct Observer \*observer, enum EventType event, void \*data) {
  switch (event) { 
    case ...: { 
      //Access Observer union to access the struct Stats data 
    } 
  break; 
  }
}

event.h

enum EventType { ... };

struct Observer;

// Requires struct Observer and enum EventType. 
typedef void (\*NotificationHandler)(struct Observer \*observer, enum EventType event, void \*data);

enum ObserverType { ... };

// struct Observer requires struct Stats, defined in stats.h 
struct Observer { 
  enum ObserverType type; 
  union { 
    struct Stats stats; 
  } data; 
  NotificationHandler onNotify; 
};
1 Upvotes

9 comments sorted by

3

u/lfdfq 36m ago edited 26m ago

Why does the statsOnNotify function definition need to be in the header file? The function declaration should be fine, you can forward declare struct types when used as pointers, you just can't have the implementation there, too.

EDIT: the EventType enumeration cannot be forward declared however, so that would probably have to move into stats.h or into a third file e.g. event_types.h

1

u/Zeta_ggwp 31m ago

Oh well the implementation itself is in the C file, I just trimmed the fat when posting.

The problem still remains because I can't access the struct observer fields in the C file either if I forward declare it

1

u/lfdfq 29m ago

But you don't need the fields at all in the header if the implementation is in the C file and not the header.

1

u/Zeta_ggwp 26m ago

The problem I have is that stats.c cannot access fields from struct observer defined in event.h if I don't include event.h's struct Observer implementation, not just the declaration.

But the implementation of struct Observer is a tagged union that has struct Stats inside, which is defined in stats.h so I need to include event.h in stats.h and stats.h in event.h

1

u/lfdfq 21m ago

You don't have to include event.h in stats.h just to declare the prototype of the function that takes a pointer to a struct Observer. The following is totally valid:

struct C;
void foo(struct C *p);

the type struct C is incomplete until it is defined, and thus doesn't have a size or fields yet, but you can still use it in declarations of other types that don't need either of those things. Then you can include stats.h in event.h without there being a cycle.

You can then just include both event.h and stats.h in stats.c, if you need both sets of types. That's also fine.

1

u/Zeta_ggwp 19m ago edited 15m ago

Ok that's a good solution. Thank you.

I'll use a pointer to stats inside observer instead tho because I feel it conveys the meaning better to keep all the code related to each system separate.

1

u/WittyStick 25m ago

Because struct Stats is an incomplete type in event.h, we can't put it directly into the Observer type - we would have to lift the Stats into a separate header that both stats.h and event.h include if this were necessary.

A common solution here is to give the Observer a pointer to Stats rather than trying to put the stats as a nested structure. We can have a pointer to an incomplete type no problem.

struct Observer { 
  enum ObserverType type; 
  union { 
    struct Stats *stats; 
  } data; 
  NotificationHandler onNotify; 
};

1

u/Zeta_ggwp 22m ago

I did think about this. I was trying to make it so observer keeps the systems inside so it can be a stack variable. But I guess in hindsight it doesn't make much sense to have things like an audiosystem be so deeply tied to the observer object it is contained in.

Is the union still worth keeping if I have a pointer or would it be better to change it to a simple void pointer instead? Since the type enum is already there.

1

u/WittyStick 16m ago

The union can still give type safety that void* may not - though it would need updating for every new observable type if you have a pointer to the specific type.

If observables have something in common, you could extract that into its own structure and have a pointer to that instead.

struct Observable { ... };

struct Stats {
    struct Observable base;
    ...
};

struct Observer {
    enum ObserverType type;
    struct Observable *data;
    NotificationHandler onNotify;
};

C permits casting of pointers to struct which share the same initial members. We can cast struct Stats * to struct Observable *. When casting the other way - from struct Observable * to struct Stats *, if we attempted to dereference a field that was not in the original it would be UB - but provided the pointer was create as a pointer to struct Stats* there is no problem. This is basically how OOP is implemented in C.