Skip to content

Containers

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

Containers πŸ“¦

Containers are contexts that allows us to manage variables without worrying about positions or indexes. In a container each variable needs to be fully declared with a name, type and data.

Here is an example in LDS:

(person:
	name = s: "Muala"
	age = i : 17
	height = f : 1.66
)

That way person.name has the value "Muala", person.age has 17, etc...

The set() method again πŸ˜„

In order to create a variable inside of a container, we can use set() method again!

    // Usage
    database.set< data_type >("container_name", "variable_name", variable_data, landb_type);
    // Example - Creating person.name
    database.set< std::string >("person", "name", "Muala", lan::String);

The get() method again πŸ˜„

When we want to get a variable from a container, we can also use the get() method again!

    // Usage
    database.get< data_type >("container_name", "variable_name", landb_type);
  
    // Example - Getting person.name
    database.get< std::string >("person", "name", lan::String);

Creating a Container πŸ–‹

The declare() method that we used to declare arrays can also be used to declare Containers. Example program:

    // Declaring a container person
    database.declare("person", lan::Container);

creating variables inside of the Container πŸ–Œ

Using the set() method to create variables inside of person

    // Creating person.name
    database.set< std::string >("person", "name", "Muala", lan::String);
    
    // Creating person.age
    database.set< int >("person", "age", 17, lan::Int);
    
    // Creating person.height
    database.set< float >("person", "height", 1.66, lan::Float);

printing everything πŸ“Ί

Using the get() method to get variables inside of person

    // Getting person.name
    std::cout << "person.name: " << database.get< std::string >("person", "name", lan::String) << std::endl;
    
    // Getting person.age
    std::cout << "person.age: " << database.get< int >("person", "age", lan::Int) << std::endl;
    
    // Getting person.height
    std::cout << "person.height: " << database.get< float >("person", "height", lan::Float) << std::endl;

Here is the entire code {✨}

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

int main(int argc, const char * argv[]) {
    
    // Database
    lan::db database;
        
    // Declaring a container person
    database.declare("person", lan::Container);
    
    // Creating person.name
    database.set< std::string >("person", "name", "Muala", lan::String);
    
    // Creating person.age
    database.set< int >("person", "age", 17, lan::Int);
    
    // Creating person.height
    database.set< float >("person", "height", 1.66, lan::Float);
        
    // Getting person.name
    std::cout << "person.name: " << database.get< std::string >("person", "name", lan::String) << std::endl;
    
    // Getting person.age
    std::cout << "person.age: " << database.get< int >("person", "age", lan::Int) << std::endl;
    
    // Getting person.height
    std::cout << "person.height: " << database.get< float >("person", "height", lan::Float) << std::endl;
    
    return 0;
}

Go back to Home.