Skip to content

SetOnce is a header ony library that allows you to initialize the value of an object only once i.e. single-assignment.

License

Notifications You must be signed in to change notification settings

IshaanKarnik/SetOnce

Repository files navigation

SetOnce

SetOnce is a header ony library that allows you to initialize the value of an object only once i.e. single-assignment. Once assigned a value, a final variable's value cannot be changed.

Build enviroment must support C++11 standard.

Features

  • Ultralight (less than 100 LOC)
  • Easy to use
  • Headers only
  • No 3rd-party dependencies
  • Cross-platform (Windows, Linux, vxWorks, FreeBSD, macOS, Android)
  • Thread and type safe
  • Uses modern CMake; alternatively can be used wothout CMake by just adding the header file to source-code
  • Only C++11 support required

Use Cases

  • Store Configuration Data
  • Store Calibration Data
  • Store Compensation Data
  • Any Data that needs to be stored/written only once but read overtime throughout the lifetime of program

Usage

  1. Import Library Header
#include <SetOnce.hxx>
#include <iostream> //Not needed by SetOnce library but is needed for std::cout [Step 3, 5, 6]
  1. Create an object of SetOnce class.
SetOnce<int> a; //Craete an instance of SetOnce object holding integer data
  1. Check if data has been set i.e. the data has been initialized in SetOnce object
std::cout << "Is the value of 'a' set : " << std::boolalpha << a.is_set() << std::endl; //Check if the value of a has been set
  1. Set the value (data) to the SetOnce object
a.set(66); //Set the value of 66 to a
  1. Check if data has been set i.e. the data has been initialized in SetOnce object
std::cout << "Is the value of 'a' set : " << std::boolalpha << a.is_set() << std::endl; //Check if the value of a has been set
  1. Read the value stored in the SetOnce object (return by value)
std::cout << "a : " << a.get() << std::endl; //Get the value of a
  1. Alternatively read the value stored in the SetOnce object (return by constant reference)
const int& b = a.get_const_reference();
std::cout << "a : " << b << std::endl; //Get the value of a

License

This version of SetOnce is licensed under the Apache2 license. You can freely use it in your commercial or opensource software.

Version history

Version 2.0.0 (20-08-2023)

Initial public release

About

SetOnce is a header ony library that allows you to initialize the value of an object only once i.e. single-assignment.

Resources

License

Stars

Watchers

Forks

Packages

No packages published