r/C_Programming • • 1h 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

View all comments

Show parent comment

1

u/Zeta_ggwp 54m 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 49m ago edited 25m 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.


In regards to it being a stack variable, you can still do this even with a pointer.

void foo() {
    struct Stats stats = { ... };
    struct Observer obsever = { OBSERVER_TYPE_STATS, &stats, ... };
    ...
}