R
Rob Clark
Hi,
I have an issue which is the ordinary and always recurring circular
dependency - but I do not know how to resolve it :-( The usual forward
declaration does not help...
I have a client that uses a resource. As a consequence of using the
resource, the resource later receives an asynchronous notification (from an
underlying layer, e.g. OS). This notification should then be forwarded to
the Client that owns the resource.
In code this looks something:
int main()
{
Resource resource; // Define the resource
Client client(&resource); // Configure the client to own the resource
client.Execute(); // Execute operation, which in turn uses the
resource
RunEventLoopForever();
}
class Resource
{
Client* m_client;
public:
void SetClient(Client* client)
{
m_client = client;
}
void Use()
{
// Use this resource
// DoSomethingInteresting()
}
void AsyncNotification()
{
m_client->Notify();
}
};
class Client
{
Resource* m_resource;
public:
Client(Resource* res) : m_resource(res)
{
m_resource->SetClient(this);
}
void Execute()
{
m_resource->Use();
}
void Notify()
{
// Handle notification
// m_resource->Whatever()
}
};
What should I do to resolve this issue?
Complete redesign? Rather not - I like this simple design (besides it
doesn't work...)
Introducing something in between to break the dependency? If so - what?
Thanks in advamce!
/Rob
I have an issue which is the ordinary and always recurring circular
dependency - but I do not know how to resolve it :-( The usual forward
declaration does not help...
I have a client that uses a resource. As a consequence of using the
resource, the resource later receives an asynchronous notification (from an
underlying layer, e.g. OS). This notification should then be forwarded to
the Client that owns the resource.
In code this looks something:
int main()
{
Resource resource; // Define the resource
Client client(&resource); // Configure the client to own the resource
client.Execute(); // Execute operation, which in turn uses the
resource
RunEventLoopForever();
}
class Resource
{
Client* m_client;
public:
void SetClient(Client* client)
{
m_client = client;
}
void Use()
{
// Use this resource
// DoSomethingInteresting()
}
void AsyncNotification()
{
m_client->Notify();
}
};
class Client
{
Resource* m_resource;
public:
Client(Resource* res) : m_resource(res)
{
m_resource->SetClient(this);
}
void Execute()
{
m_resource->Use();
}
void Notify()
{
// Handle notification
// m_resource->Whatever()
}
};
What should I do to resolve this issue?
Complete redesign? Rather not - I like this simple design (besides it
doesn't work...)
Introducing something in between to break the dependency? If so - what?
Thanks in advamce!
/Rob