0

I am working on a legacy C++ code with two libraries. Library1 is the main and Library2 uses classes from Library1 (so there are some #includes "HeaderFromLibrary1" in Library2).

Deep in the code of Library1, there is some work done and when something happens I need to set a boolean variable. Library2 has to be able to get this value and reset it.

All code is object-oriented, so there are no global variables nor C-functions, but only classes objects.

What would be the best way to be able to modify this boolean variable from these libraries?

What I did is, I created two new files in Library1: SharedData.cpp and SharedData.h.

SharedData.h

#ifndef SHARED_DATA_H_
#define SHARED_DATA_H_

namespace MyNameSpace
{
    /*
    * Get the Boolean and reset it to FALSE.
    */
    boolean GetAndResetBoolean(void);

    /*
    * Set the Boolean.
    */
    void SetBoolean(void);
}

#endif

SharedData.cpp

#include "util/SharedData.h"
#include "Mutex.h"

namespace MyNameSpace
{
    static Mutex mutexBoolean;
    static boolean myBoolean;

    /**
    * Method used to set the boolean.
    */
    void SetBoolean(void)
    {
        ScopedLock lock(mutexBoolean);
        myBoolean= TRUE;
    }

    /**
    * Method used to get and reset the Boolean.
    */
    boolean GetAndResetBoolean(void)
    {
        ScopedLock lock(mutexBoolean);
        boolean currentBoolean = myBoolean;
        myBoolean = FALSE;
        return currentBoolean;
    }
}

So I include the SharedData.h in the file I need to set the boolean (Library1) and also in the file I need to get the value and reset it.

I do not like that I have to create 2 "global" variables. I would prefer to store the boolean in an object, but the lifetime of the object that sets the boolean is not very long. And I do not want to include any HeaderFromLibrary2.h in Library1 because Library2 uses Library1 and not the other way around.

Do you have any recommendation?

4

Deep in the code of Library1, there is some work done and when something happens I need to set a boolean variable. Library2 has to be able to get this value and reset it.

The libraries are no longer independent of each other. For all practical purposes, they are no longer separate libraries. Because library1 now depends on library2 for clearning thea flag.

All code is object-oriented, so there are no global variables

Yes, there are! The variable you are talking about is global. And no, wrapping it in getter an setter won't change that, nor will wrapping it in a singleton. It's global state and it's global state no matter how you access it.

I would prefer to store the boolean in an object, but the lifetime of the object that sets the boolean is not very long.

The boolean signifies something. Perhaps you need an object representing that something.

Do you have any recommendation?

Without more information about the problem and the design, it's not really possible. Because it is the fact that you “need to modify variable from different libraries” itself that is the issue, not any technical details of how to do it.

Depending on those details, you may need to get a handle object from the first library that will reset the boolean when released, or a callback to notify of completion of some process. But what is appropriate can't really be said without knowing what the boolean represents.

| improve this answer | |
  • Thanks for your answer! The boolean represents a disconnection of the client. Another process will poll the value and reset it. I opted to make the Get and Set functions as well as the variables, static member functions of the class that is using the boolean. So I do not need to use global functions/variables. I guess is a better solution because of the encapsulation. – J.PG Sep 21 '17 at 10:27
  • 4
    And what happens if you ever need to extend this code to support more than one client? Whether a client is connected or not should surely be associated with the individual class instance that represents that client... – Sean Burton Sep 21 '17 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.