GithubHelp home page GithubHelp logo

suncloudsmoon / auto-store Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 21 KB

Automatically stores/retrieves variable data.

License: MIT No Attribution

C++ 100.00%
variables storage retrieve-data automatic c-plus-plus cplusplus-20 data

auto-store's Introduction

Auto-Store

Auto-Save helps you save/retrieve variables (and data structures) to/from file with minimal effort. For example, you only need to overload the << and >> operator(s) for ostream to serialize/deserialize a struct.

Examples

Simple Example:

au::auto_store<unsigned int> game_score{0, "game_score.txt"};
std::cout << "Current Game Score: " << game_score << std::endl;
std::cout << "Enter New Game Score: ";
std::cin >> game_score;

Detailed Example:

struct Game {
	int score;
	int health;
	friend std::ostream& operator<<(std::ostream& out, const Game& g) {
		out << g.score << ',' << g.health;
		return out;
	}
	friend std::istream& operator>>(std::istream& in, Game& g) {
		char nop{ NULL };
		in >> g.score >> nop >> g.health;
		return in;
	}
	// It is optional to define operator=(), but you can do it if you want to use the '=' operator.
	// Example use:
	// au::auto_save<Game> current_game{Game(), "game.txt"};
	// current_game = Game();
	
	// If you don't define '=' operator, you can always use '*' to get the object stored inside auto_save
	// Example use of '*':
	// au::auto_save<Game> current_game{Game(), "game.txt"};
	// (*current_game).score++;
	// (*current_game).health++;
	void operator=(const Game& g) {
		score = g.score;
		health = g.health;
	}
	void operator=(Game&& g) noexcept = delete; // typically used for efficiency
};

void serialize_struct_example() {
	au::auto_store<Game> g{ Game(), "game.txt" };
	(*g).health++;
	(*g).score++;
}

More examples can be found in main.cpp.

Compiling

To compile autostore.hpp (as an individual file), you need C++20.

Notes

  • Cannot save pointers to file

auto-store's People

Contributors

suncloudsmoon avatar

Stargazers

 avatar

Watchers

 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.