Skip to content

Manual variable handling

René Descartes Muala edited this page May 1, 2021 · 1 revision

Manual variable handing ⚙️

If you love performance (like me) probably you didn't like the idea of using the get() method every time that you need to access a variable and set() every time that you need to change it. For those people landb offers a way to manually handle the variables, as c++ variables.

The get_p() method *📍

One easy way to manually handle variables in landb, is by getting their data pointers, that's where the get_p() method comes to our code! It works exactly as get(), but instead of returning values, it returns pointes.

Implementation 🔨

First we create a landb variable (we can also pull it from a file)

    // Creating a variable
    database.set< std::string >("myFruit", "mango", lan::String);

Creating a c++ pointer to handle the variable

    // Creating a C++ string pointer
    std::string (*myFruit);

Linking the pointer to landb variable

    // Getting our landb variable to handle manually
    myFruit = database.get_p< std::string >("myFruit", lan::String);

Right now, every change we made to the cpp version will change the landb version and vice-versa.

Printing the variable 📺

    // Printing our C++ variable witch is linked to our landb variable
    std::cout << (*myFruit) << std::endl;
    return 0;
}

Here is the entire code {✨}

#include <iostream>
#include "landb.hpp"

int main(int argc, const char * argv[]) {
    
    // Database
    lan::db database;
    
    // Creating a variable
    database.set< std::string >("myFruit", "mango", lan::String);
    
    // Creating a C++ string pointer
    std::string (*myFruit);
    
    // Getting our landb variable to handle manually
    myFruit = database.get_p< std::string >("myFruit", lan::String);
    
    // Printing our C++ variable witch is linked to our landb variable
    std::cout << (*myFruit) << std::endl;
    return 0;
}

Go back to Home.