Added a http session class.

This commit is contained in:
Relintai 2021-08-04 20:32:51 +02:00
parent 3f6752ed20
commit 2521c28a9a
2 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,51 @@
#include "http_session.h"
void HTTPSession::add_object(const std::string &key, Object *obj) {
std::lock_guard<std::mutex> lock(_mutex);
_data[key] = obj;
}
void HTTPSession::remove_object(const std::string &key) {
std::lock_guard<std::mutex> lock(_mutex);
_data.erase(key);
}
Object *HTTPSession::get_object(const std::string &key) {
//don't lock here
return _data[key];
}
void HTTPSession::add_int(const std::string &key, const int val) {
std::lock_guard<std::mutex> lock(_mutex);
_int_data[key] = val;
}
void HTTPSession::remove_int(const std::string &key) {
std::lock_guard<std::mutex> lock(_mutex);
_int_data.erase(key);
}
int HTTPSession::get_int(const std::string &key) {
//don't lock here
return _int_data[key];
}
void HTTPSession::clear() {
_data.clear();
_int_data.clear();
}
void HTTPSession::reset() {
clear();
session_id = "";
}
HTTPSession::HTTPSession() {
}
HTTPSession::~HTTPSession() {
clear();
}

35
core/http/http_session.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef HTTP_SESSION_H
#define HTTP_SESSION_H
#include "core/object.h"
#include <map>
#include <mutex>
#include <string>
class HTTPSession : public Object {
public:
void add_object(const std::string &key,Object *obj);
void remove_object(const std::string &key);
Object *get_object(const std::string &key);
void add_int(const std::string &key, const int val);
void remove_int(const std::string &key);
int get_int(const std::string &key);
std::string session_id;
void clear();
void reset();
HTTPSession();
~HTTPSession();
protected:
std::mutex _mutex;
//todo make something similar to godot's variant. (Or get godot's variant lol)
std::map<std::string, Object *> _data;
std::map<std::string, int> _int_data;
};
#endif