Code Monkey home page Code Monkey logo

cpputils's Introduction

CppUtils

Utilities (e.g. Parser, Logger, ...) for C++

Prerequisites

You need to install the boost library on your system.

Simple example program

Build with make and run bin/cpp-utils to get help on usage:

[INFO ] Running some utility code snippets in C++
Usage:
  cpp-utils [OPTION...]

  -T, --timer          Timing is key
  -P, --progress-bar   Make some progress
  -C, --config-parser  Parse the config file real hard
  -h, --help           Show this help

The code serves as reference for the usage of all the utils in this repository.

Content

Configuration file parser built on boost::property_tree::ptree.

Supports .json, .info, .ini and .xml.

Usage

Example configuration file: example-json

    #include "ConfigParser.h"

    // parse the configuration file
    ConfigParser confP {ConfigParser("ConfigParser/resources/config.json")};
    
    // get simple value by key
    int version { confP.getVal<int>("info.version");
    
    // get object holding multiple keys
    auto cp_mode = confP.getObj("modes.config-parser");
    // get value in obj by key
    std::string optFlag = cp_mode.getVal<std::string>("opt");
    
    // accessing list of objects
    for(auto module: confP.getObjList("utils")){
        // access field
        std::cout << module.get<std::string>("description") << std::endl;
    }

Class for logging in dependece of typelog:

  • DEBUG
  • INFO
  • WARN
  • ERROR

with coloring of printouts.

Usage

structlog LOGCFG = {};

LOGCFG.headers = true;
LOGCFG.level = DEBUG;

Logger(DEBUG) << "Debug message";
Logger(INFO)  << "Info message";
Logger(WARN)  << "Warning message";
Logger(ERROR) << "Error message";

// printing variables
int a = 0;
Logger(INFO) << "a = " << a;

Class for timing events.

Dependencies

  • <chrono>

Usage

Timer timer;
timer.reset();
// do something
double elapsedTime = timer.elapsed();
std::cout << "Elapsed time: " << elapsedTime << " s" << std::endl;

Visualize progress using a progress bar within the print-outs/shell.

Progress Bar example

Usage

// initialize colors (if wanted)
Color::Modifier green(Color::FG_GREEN);
Color::Modifier default(Color::FG_DEFAULT);
    
// initialize progress bar
int width = 80;
ProgressBar progressBar(width);

for (int i = 0; i < 1500; i++) {

	// calculate something
	
	//change color to green
	std::cout << green;
	//calculate progress
	float progress = i/1500.0;
	// visualize progress using the progress bar
	progressBar.show_progress(progress);
	// change color back to default
	std::cout << default;
}

Used libraries

  • cxxopts: For parsing command line options in GNU style cxxopts is utilized.

cpputils's People

Contributors

michaelst98 avatar jammartin avatar

Watchers

 avatar  avatar

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.