Giter Site home page Giter Site logo

cnroom's Introduction

CNRoom 1.2

CNRoom (Chats Noirs Room) is a header-only key-value database library using C++17 (std::filesystem and std::variant) and based on local files. It is actually in development by Fatih#6810 ([email protected]) from Moonlit Black Cats.


How to use

Installation

CNRoom is header-only, you just need a compiler that supports C++17 (std::filesystem and std::variant) then to include CNRoom/Room.hpp after downloading files in the include directory. You may also need to link libstdc++fs to your project in order to compile the std::filesystem library that CNRoom uses.

Library vocabulary

I use the arrangement metaphor, you can imagine a room (base directory) with several drawers (file), inside these drawers you can tidy up papers (key) with informations written on them (values). CNRoom lets you assign several values of different types (std::string, int, double, bool) to a string called key and will write these values on a file of your choice (values are indexed in the order you write them). You can access to these values by the key.

Example code

A tutorial example code showing how easy CNRoom is to use!

//CNRoom
#include "CNRoom/Room.hpp"

//Standard
#include <iostream>

int main()
{
    /// Stream class
    ////////////////////////////////////////////////////////////
    CNRoom::Stream stream("database/lebgdu92.hkn", true); ///< Open a file, create new if doesn't exist

    stream << CNRoom::Key{"mail", {"[email protected]", true}}; ///< Write using <<
    stream >> "mail"; ///< Read using >>

    //Show method 1: std::get<Type>
    std::cout << std::get<std::string>(stream()[0]) << std::endl; ///< Retreive key using () and value using []

    stream.remove("mail"); ///< Remove a key

    /// Room helper class
    ////////////////////////////////////////////////////////////
    CNRoom::Room room;

    room.connect("database", true); ///< Base directory (current path by default)

    room.open("lebgdu92.hkn", [](auto& stream) ///< Open a file
    {
        stream.write({"sword", {"Sword of the Warrior", false, 4.85, 0}}); ///< Write using function write()

        for(const auto& it: stream.read("sword").values) ///< Read using function read()
        {
            //Show method 2: Visit
            std::visit([](auto const& value){ std::cout << value << ' '; }, it.standard());
        }
    }, true);

    room.destroy("lebgdu92.hkn"); ///< Delete a file

    /// Shortcut functions
    ////////////////////////////////////////////////////////////
    room.quick_write("lebgdu92.hkn", {"sword", {"Sword of the Warrior", false, 4.85, 0}});

    //Show method 3: Convert to string using Key::string()
    std::cout << CNRoom::Key::string(room.quick_read("lebgdu92.hkn", "sword")[2]) << std::endl;

    return 0;
}

Output:

[email protected]
Sword of the Warrior 0 4.85 0 4.850000

Help

Documentation

Struct and classes provided in CNRoom/Room.hpp.

Class & members Description
Variant Class inheriting from std::variant and internally converting const char* to std::string if needed.
Variant::standard() Convert to standard std::variant.
Struct & members Description
Key Struct that represents a key with a name and a vector of values.
static Key::string(value) Convert value to std::string.
Key::name Name of the key.
Key::values Vector of variant<string, int, double, bool>, access values with std::get, std::visit or Key::string.
Key::operator[] Access value by index.
Class & members Description
Stream Stream class to operate on files.
Stream::operator() Returns a reference to the internal key.
Stream::write(key) Write a key.
Stream::operator<< Write a key.
Stream::read(name) Read a key by name.
Stream::operator>> Read a key by name.
Stream::remove(name) Remove a key.

Some helper functions are provided in the Room class.

Class & members Description
Room Stream wrapper class.
Room::connect(path) Set the base directory. Optional, current path by default.
Room::open(file, function) Opens a file and call the given function.
Room::exists(file) Check if the given file exists.
Room::destroy(file) Delete a file.
Room::quick_write(file, key) Short way to write a key.
Room::quick_read(file, name) Short way to read a key.

Performances

Can write and read keys of 5 values in a rate of 120 keys per second with my poor Toshiba DT01ACA100.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.