-
Notifications
You must be signed in to change notification settings - Fork 0
Nested Containers
Last time we used Nested Arrays to store classes, now we are going to do the same thing with Nested Containers witch are containers inside another containers.
We use the declare()
method to create arrays, containers and nested containers.
- Creating a container
// Structure
database.declare("container_name", lan::Container);
// Example - Creating "colors"
database.declare("colors", lan::Container);
- Creating a nested container
// Structure
database.declare("container_name", "nested_container_name", lan::Container);
// Example - Creating a "colors.red"
database.declare("colors", "red", lan::Container);
As we saw in the Nested Arrays's chapter, anchors are a good way to avoid big and confusing addresses. This time we are going to use them again, in Containers to avoid addresses like home.people.Muala.pets.cat
every time that i need get my little cat's name!
// Usage
database.set_anchor("container_address");
// Example - Setting the anchor to "home.cats"
database.set_anchor("home.cats");
To set variables inside of Nested Containers we also use the set()
method
// Usage
database.set<data_type>("container_address", "variable_name", variable_data, landb_type);
// Example - Setting food.fruits.favourite
database.set< std::string >("food.fruits", "favourite", "Mango", lan::String);
(Example program to recreate the first class of our classes example from Nested Arrays)
First we create the container to store our classes
// Creating the classes container
database.declare("classes", lan::Container);
Then we create the nested container to store the class_a
// Creating a classes.class_a nested container
database.declare("classes", "class_a", lan::Container);
Now it's time to add student_1 .
// Creating the first student's container
database.declare("classes.class_a", "student_1", lan::Container);
Here we set the anchor to "classes.class_1.student_1" because we are going to use that address many times to set the first student's data
// Setting the anchor to reuse the address "classes.class_a.student_1"
database.set_anchor("classes.class_a.student_1");
Adding the first student's data in "@"
, with is a synonym to "classes.class_a.student_1"
// Adding student_1.name
database.set< std::string >("@", "name", "Anita", lan::String);
// Adding student_1.average
database.set< float >("@", "average", 16, lan::Float);
// Adding student_1.has_passed
database.set< bool >("@", "has_passed", true, lan::Bool);
Setting the 2nd and 3rd students' data
// Second student
// Creating the second student's container
database.declare("classes.class_a", "student_2", lan::Container);
// Setting the anchor to reuse the address "classes.class_a.student_2"
database.set_anchor("classes.class_a.student_2");
// Adding student_2.name
database.set< std::string >("@", "name", "Carlos", lan::String);
// Adding student_2.average
database.set< float >("@", "average", 12, lan::Float);
// Adding student_2.has_passed
database.set< bool >("@", "has_passed", true, lan::Bool);
// Third student
// Creating the third student's container
database.declare("classes.class_a", "student_3", lan::Container);
// Setting the anchor to reuse the address "classes.class_a.student_3"
database.set_anchor("classes.class_a.student_3");
// Adding student_3.name
database.set< std::string >("@", "name", "Zinha", lan::String);
// Adding student_3.average
database.set< float >("@", "average", 8, lan::Float);
// Adding student_3.has_passed
database.set< bool >("@", "has_passed", false, lan::Bool);
// Printing everything
for (int i = 0 ; i < 3 ; i++ ){
// setting anchor to classes.class_a.student_i
database.set_anchor("classes.class_a.student_"+std::to_string(i+1));
std::cout << "Name: " << database.get< std::string >("@", "name", lan::String) << " - "
<< "Average: " << database.get< float >("@", "average", lan::Float) << " - "
<< "Passed: " << database.get< bool >("@", "has_passed", lan::Bool) << std::endl;
}
return 0;
}
#include <iostream>
#include "landb.hpp"
int main(int argc, const char * argv[]) {
// Database
lan::db database;
// Creating the classes container
database.declare("classes", lan::Container);
// Creating a classes.class_a nested container
database.declare("classes", "class_a", lan::Container);
// Creating the first student's container
database.declare("classes.class_a", "student_1", lan::Container);
// Setting the anchor to reuse the address "classes.class_a.student_1"
database.set_anchor("classes.class_a.student_1");
// Adding student_1.name
database.set< std::string >("@", "name", "Anita", lan::String);
// Adding student_1.average
database.set< float >("@", "average", 16, lan::Float);
// Adding student_1.has_passed
database.set< bool >("@", "has_passed", true, lan::Bool);
// Second student
// Creating the second student's container
database.declare("classes.class_a", "student_2", lan::Container);
// Setting the anchor to reuse the address "classes.class_a.student_2"
database.set_anchor("classes.class_a.student_2");
// Adding student_2.name
database.set< std::string >("@", "name", "Carlos", lan::String);
// Adding student_2.average
database.set< float >("@", "average", 12, lan::Float);
// Adding student_2.has_passed
database.set< bool >("@", "has_passed", true, lan::Bool);
// Third student
// Creating the third student's container
database.declare("classes.class_a", "student_3", lan::Container);
// Setting the anchor to reuse the address "classes.class_a.student_3"
database.set_anchor("classes.class_a.student_3");
// Adding student_3.name
database.set< std::string >("@", "name", "Zinha", lan::String);
// Adding student_3.average
database.set< float >("@", "average", 8, lan::Float);
// Adding student_3.has_passed
database.set< bool >("@", "has_passed", false, lan::Bool);
// Printing everything
for (int i = 0 ; i < 3 ; i++ ){
// setting anchor to classes.class_a.student_i
database.set_anchor("classes.class_a.student_"+std::to_string(i+1));
std::cout << "Name: " << database.get< std::string >("@", "name", lan::String) << " - "
<< "Average: " << database.get< float >("@", "average", lan::Float) << " - "
<< "Passed: " << database.get< bool >("@", "has_passed", lan::Bool) << std::endl;
}
return 0;
}
Go back to Home.