samedi 11 novembre 2023

Moving third party library from header file

I have the following code snippet:

#include <nats/nats.h>

class MyClass
{
public:
    // some functions here
private:
template<typename T, void (*DestroyFn)(T*)>
decltype(DestroyFn) makeDeleter()
{
    return [](T* ptr)
    {
        DestroyFn(ptr);
    };
}

std::unique_ptr<natsOptions, std::function<void(natsOptions*)>> m_options{
    nullptr,
    makeDeleter<natsOptions, natsOptions_Destroy>()};
std::unique_ptr<natsConnection, std::function<void(natsConnection*)>> m_connection{
    nullptr,
    makeDeleter<natsConnection, natsConnection_Destroy>()};
std::unique_ptr<natsSubscription, std::function<void(natsSubscription*)>> m_subscription{
    nullptr,
    makeDeleter<natsSubscription, natsSubscription_Destroy>()};
};

If I wouldn't have required custom deleters I could easily forward declare used types from NATS library. But is it possible to do same if I have makeDeleter function template deleter which calls actual functions from the library (e.g., natsOptions_Destroy)? The thing is that the reviewers of the code require to not include 3rd party library in the current header file, but on the other hand I don't see the issue in my case, because current header is included only in two .cpp file, hence only two translation units will be recompiled if something changes in the nats.h.

Aucun commentaire:

Enregistrer un commentaire