[Question] Using pokitto library in other library

Hello everyone
I’m trying to write library for pokitto
I’m used Pokitto functions constructed at main.cpp in my library functions. For example library uses display class like “pok.display.something” but pok constructed in main.cpp.
What is the best way using Pokitto functions in library for best memory usage?
Is “Pokitto::Display::something” is good?
Am i need the reconstruct Pokitto library in my library?
Am i need to pass constructed Pokitto class with pointer to library class? For example:
In library

class Library{
    private:
        Pokitto *pokittoLibrary;
    public:
        void Library(Pokitto *);
        uint8_t buttonA();
}

void Library::Library(Pokitto *_pok){
    pokittoLibrary = _pok;
}

uint8_t Library::buttonA(){
    return &pokittoLibrary->buttons->pressed(BTN_A);
}

In main.cpp

Pokitto pok;
Library library(&pok);
uint8_t buttonA_state;
void main(void){
    buttonA_state = library.buttonA();
}

I appreciate any suggestion.

It depends on what your library is supposed to do.

The PokittoLib is designed so that all functions are static and you don’t actually need a Pokitto object (though I don’t know if that will ever change), so for now you can just use e.g. Pokitto::Buttons::pressed(BTN_A) all the time, you don’t need to keep a Pokitto::Core object.


A few notes on your example code:

  • there is no Pokitto type, Pokitto is a namespace and the actual class is Pokitto::Core
  • constructors shouldn’t be marked void, they shouldn’t have any return type whatsoever.
  • if you didn’t separate the function definitions into a separate .cpp file then you’d need to add the word inline before the definition.
  • your buttonA implementation is incorrect, the & isn’t needed and the second -> should be a ..
class Library {
    private:
        Pokitto *pokittoLibrary;
    public:
        // Not void
        Library(Pokitto *);
        uint8_t buttonA();
}

// Not void
// Marked as inline
inline Library::Library(Pokitto *_pok) {
    pokittoLibrary = _pok;
}

// Marked as inline
inline uint8_t Library::buttonA() {
    return pokittoLibrary->buttons.pressed(BTN_A);
}

As for the stylistic complaints:

Avoid naming things with a leading underscore.
Names that start with a leading underscore (followed by an uppercase letter) are reserved for use by the standard library.
(See here for more details.)

And lastly, prefer to pass references to constructors (rather than pointers).

i.e.

class Library
{
    private:
        Pokitto * pokitto;
    public:
        // Not void
        Library(Pokitto &);
        uint8_t buttonA();
}

// Not void
// Marked as inline
// Uses reference
// Uses new-style construction
inline Library::Library(Pokitto & pokitto)
  : pokitto(&pokitto)
{
}

// Marked as inline
inline uint8_t Library::buttonA() {
    return pokitto->buttons.pressed(BTN_A);
}

The reason for this is that pointers can be nullptr, but requiring a reference means that a valid object has to exist.

Although neither approach will guarantee that the object won’t be destroyed before Library is destroyed.
Not even std::shared_ptr could guarantee that (though it might help).

(Also your code isn’t const correct, but that might not matter.)

1 Like