mirror of
https://github.com/ondra-novak/gates_of_skeldal.git
synced 2025-07-04 21:50:38 -04:00
windows release build, fix book crash, fix lock, fix finale
This commit is contained in:
parent
3d8ee275e4
commit
d13297e4f4
24 changed files with 158 additions and 63 deletions
|
@ -3,6 +3,7 @@
|
|||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
|
|
@ -764,7 +764,7 @@ void draw_medium_map(void)
|
|||
{
|
||||
int xr, yr;
|
||||
int xp, yp;
|
||||
int xc,yc,x,y;
|
||||
int xc=0,yc=0,x=0,y=0;
|
||||
int j,i,k,layer;
|
||||
//char c=" ";
|
||||
|
||||
|
|
|
@ -1363,7 +1363,7 @@ extern char att_player;
|
|||
void mob_hit(TMOB *mm,int dostal)
|
||||
{
|
||||
int ch;
|
||||
int mob_dostal,mob_dostal_pocet;
|
||||
int mob_dostal=0,mob_dostal_pocet=0;
|
||||
|
||||
if (mm->vlajky & MOB_PASSABLE) return;
|
||||
if (dostal>mm->vlastnosti[VLS_MAXHIT]) dostal=mm->vlastnosti[VLS_MAXHIT];
|
||||
|
|
|
@ -592,7 +592,7 @@ void global_map_point(EVENT_MSG *msg,void **_)
|
|||
}
|
||||
}
|
||||
|
||||
void unwire_global_map()
|
||||
void unwire_global_map(void)
|
||||
{
|
||||
purge_index_tab();
|
||||
send_message(E_DONE,E_MOUSE,global_map_point);
|
||||
|
@ -601,7 +601,7 @@ void unwire_global_map()
|
|||
}
|
||||
|
||||
|
||||
void wire_global_map()
|
||||
void wire_global_map(void)
|
||||
{
|
||||
unwire_proc();
|
||||
schovej_mysku();
|
||||
|
@ -614,14 +614,14 @@ void wire_global_map()
|
|||
change_click_map(NULL,0);
|
||||
}
|
||||
|
||||
static void *old_wire_save;
|
||||
static void (*old_wire_save)(void);
|
||||
static int old_viewsector;
|
||||
static void empty_unwire()
|
||||
static void empty_unwire(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void unwire_automap_file()
|
||||
static void unwire_automap_file(void)
|
||||
{
|
||||
load_map_automap(level_fname);
|
||||
wire_proc=old_wire_save;
|
||||
|
|
|
@ -1207,7 +1207,7 @@ static void play_wav(int wav,int sector)
|
|||
static void play_random_sound(int sector,int dir,int pos)
|
||||
{
|
||||
int seed;
|
||||
int v;
|
||||
int v=0;
|
||||
|
||||
seed=rand();
|
||||
srand(sector+dir);
|
||||
|
@ -1264,7 +1264,7 @@ void fletna_glob_add_note(uint8_t note)
|
|||
if (strlen(globFletnaStr)<250) strcat(globFletnaStr,globNotes[note]);
|
||||
}
|
||||
|
||||
static char compareMelody(const char *m1,const char *m2)
|
||||
static int compareMelody(const char *m1,const char *m2)
|
||||
{
|
||||
while (*m1 && *m2)
|
||||
{
|
||||
|
|
19
game/inv.c
19
game/inv.c
|
@ -803,7 +803,7 @@ char uloz_sip(int id,int xa,int ya,int xr,int yr);
|
|||
|
||||
char info_box_drawed=0;
|
||||
void *info_box_below=NULL;
|
||||
void *inv_keyboard(EVENT_MSG *msg,void **usr);
|
||||
void inv_keyboard(EVENT_MSG *msg,void **usr);
|
||||
|
||||
T_CLK_MAP clk_inv_view[]=
|
||||
{
|
||||
|
@ -1742,7 +1742,7 @@ void inv_item_info_box(EVENT_MSG *msg,void **data)
|
|||
}
|
||||
}
|
||||
|
||||
void unwire_inv_mode()
|
||||
void unwire_inv_mode(void)
|
||||
{
|
||||
send_message(E_DONE,E_KEYBOARD,inv_keyboard);
|
||||
send_message(E_DONE,E_MOUSE,inv_item_info_box);
|
||||
|
@ -2189,7 +2189,7 @@ char human_click(int id,int xa,int ya,int xr,int yr)
|
|||
}
|
||||
|
||||
|
||||
void *inv_keyboard(EVENT_MSG *msg,void **usr)
|
||||
void inv_keyboard(EVENT_MSG *msg,void **usr)
|
||||
{
|
||||
char c;
|
||||
|
||||
|
@ -2215,7 +2215,6 @@ void *inv_keyboard(EVENT_MSG *msg,void **usr)
|
|||
break;
|
||||
}
|
||||
}
|
||||
return &inv_keyboard;
|
||||
}
|
||||
|
||||
|
||||
|
@ -2241,9 +2240,9 @@ static int fly_count; //vyuziti mapy
|
|||
void draw_fly_items(int celx,int cely,int sector,int side)
|
||||
{
|
||||
LETICI_VEC *p;
|
||||
int xpos,ypos;
|
||||
int xpos=0,ypos=0;
|
||||
const short *pic;
|
||||
short picnum;char turn,smr;
|
||||
short picnum;char turn=0,smr=0;
|
||||
TITEM *it;
|
||||
int i;
|
||||
|
||||
|
@ -2385,8 +2384,8 @@ char shop_keeper_click(int id, int xa, int ya,int xr,int yr);
|
|||
char shop_block_click(int id, int xa, int ya,int xr,int yr);
|
||||
char shop_change_player(int id, int xa, int ya,int xr,int yr);
|
||||
char _exit_shop(int id, int xa, int ya,int xr,int yr);
|
||||
void unwire_shop();
|
||||
void wire_shop();
|
||||
void unwire_shop(void);
|
||||
void wire_shop(void);
|
||||
|
||||
|
||||
|
||||
|
@ -2960,7 +2959,7 @@ static void shop_keyboard_proc(EVENT_MSG *msg, void **_) {
|
|||
|
||||
static int old_inv_view_mode;
|
||||
|
||||
void unwire_shop()
|
||||
void unwire_shop(void)
|
||||
{
|
||||
send_message(E_DONE,E_MOUSE,shop_mouse_event);
|
||||
send_message(E_DONE,E_KEYBOARD, shop_keyboard_proc);
|
||||
|
@ -2969,7 +2968,7 @@ void unwire_shop()
|
|||
inv_view_mode=old_inv_view_mode;
|
||||
}
|
||||
|
||||
void wire_shop()
|
||||
void wire_shop(void)
|
||||
{
|
||||
int32_t size;
|
||||
static TSHOP *last_shop=NULL;
|
||||
|
|
10
game/kniha.c
10
game/kniha.c
|
@ -283,12 +283,12 @@ static char read_set(TMPFILE_RD *txt,char *var,char *set)
|
|||
do
|
||||
c=temp_storage_getc(txt);
|
||||
while (c<33);
|
||||
if (c=='"') temp_storage_scanf(txt,"%[^\"]%c%c",set,&d,&d);
|
||||
else if (c=='\'') temp_storage_scanf(txt,"%[^']%c%c",set,&d,&d);
|
||||
if (c=='"') temp_storage_scanf(txt,"%[^\"]%c%c",set,&d,&c);
|
||||
else if (c=='\'') temp_storage_scanf(txt,"%[^']%c%c",set,&d,&c);
|
||||
else
|
||||
{
|
||||
temp_storage_ungetc(txt);
|
||||
temp_storage_scanf(txt,"%[^> ]%c",set,&d);
|
||||
temp_storage_scanf(txt,"%[^> ]%c",set,&c);
|
||||
}
|
||||
while(c<33 && c!=EOF) c=temp_storage_getc(txt);
|
||||
if (c!='>') temp_storage_ungetc(txt);
|
||||
|
@ -308,7 +308,7 @@ static char read_set(TMPFILE_RD *txt,char *var,char *set)
|
|||
return c;
|
||||
}
|
||||
|
||||
static int get_data_handle(char *filename,void *dec)
|
||||
static int get_data_handle(char *filename,ABLOCK_DECODEPROC dec)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
@ -492,7 +492,7 @@ static void read_text(TMPFILE_RD *txt)
|
|||
|
||||
static void seek_section(TMPFILE_RD *txt,int sect_number)
|
||||
{
|
||||
int c=0,i;
|
||||
int c=0,i=0;
|
||||
|
||||
winconv=0;
|
||||
do
|
||||
|
|
|
@ -739,7 +739,7 @@ void call_macro_ex(int side, int flags, int runatside) {
|
|||
z = mrec.action_list;
|
||||
if (z->general.flags & flags) {
|
||||
int jmp_to = -1;
|
||||
char force_cancel = 0;
|
||||
char cancel_enabled = 1;
|
||||
int stindex = z - first_macro;
|
||||
if (!z->general.once || !macro_state_block.states[stindex]) {
|
||||
macro_state_block.states[stindex] = 1;
|
||||
|
@ -789,14 +789,14 @@ void call_macro_ex(int side, int flags, int runatside) {
|
|||
enter_shop(z->text.textindex);
|
||||
break;
|
||||
case MA_CLOCK:
|
||||
force_cancel = decode_lock(z->clock.znak, z->clock.string,
|
||||
cancel_enabled = decode_lock(z->clock.znak, z->clock.string,
|
||||
z->clock.codenum);
|
||||
break;
|
||||
case MA_CACTN:
|
||||
cancel_action(z->cactn.sector, z->cactn.dir);
|
||||
break;
|
||||
case MA_LOCK:
|
||||
force_cancel = if_lock(side, z->lock.key_id,
|
||||
cancel_enabled = if_lock(side, z->lock.key_id,
|
||||
z->lock.thieflevel, &z->lock);
|
||||
break;
|
||||
case MA_SWAPS:
|
||||
|
@ -879,7 +879,7 @@ void call_macro_ex(int side, int flags, int runatside) {
|
|||
if (jmp_to != -1) {
|
||||
mrec = go_macro(runatside, jmp_to);
|
||||
program_counter = jmp_to;
|
||||
} else if (z->general.cancel || force_cancel) {
|
||||
} else if (z->general.cancel && cancel_enabled) {
|
||||
break;
|
||||
} else {
|
||||
program_counter++;
|
||||
|
|
|
@ -672,7 +672,7 @@ static void kill_timer(void)
|
|||
timer_tree.next=NULL;
|
||||
}
|
||||
|
||||
void *user_timer(EVENT_MSG *msg,void **usr)
|
||||
void user_timer(EVENT_MSG *msg,void **usr)
|
||||
{
|
||||
int x;
|
||||
static int lastvalue=0;
|
||||
|
@ -685,7 +685,6 @@ void *user_timer(EVENT_MSG *msg,void **usr)
|
|||
lastvalue+=x;
|
||||
if (x) send_message(E_TIMER,x);
|
||||
}
|
||||
return &user_timer;
|
||||
}
|
||||
|
||||
void do_timer(void)
|
||||
|
@ -1305,7 +1304,7 @@ static void game_big_circle(char enforced)
|
|||
char s[13];
|
||||
|
||||
purge_playlist();
|
||||
s[12]=0;strcopy_n(s,loadlevel.name,12);
|
||||
strcopy_n(s,loadlevel.name,sizeof(s));
|
||||
err=load_map(s);
|
||||
if (!enforced)
|
||||
{
|
||||
|
@ -1363,7 +1362,7 @@ static void game_big_circle(char enforced)
|
|||
enter_game();
|
||||
|
||||
leave_current_map();
|
||||
s[12]=0;strcopy_n(s,loadlevel.name,12);
|
||||
strcopy_n(s,loadlevel.name,sizeof(s));
|
||||
if (s[0]!=0)err=load_map(s);
|
||||
memset(GlobEventList,0,sizeof(GlobEventList));
|
||||
|
||||
|
@ -1384,9 +1383,9 @@ static void new_game(int argc, char *argv[])
|
|||
load_shops();
|
||||
open_story_file();
|
||||
if (argc<2)
|
||||
strcopy_n(loadlevel.name,default_map,12);
|
||||
strcopy_n(loadlevel.name,default_map,sizeof(loadlevel.name));
|
||||
else
|
||||
strcopy_n(loadlevel.name,argv[1],12);
|
||||
strcopy_n(loadlevel.name,argv[1],sizeof(loadlevel.name));
|
||||
if (argc>2)
|
||||
{
|
||||
sscanf(argv[2],"%d",§);
|
||||
|
|
|
@ -733,7 +733,7 @@ void wire_end_game()
|
|||
if (mname != NULL) {
|
||||
cur_group = postavy[i].groupnum;
|
||||
TMA_LOADLEV lv;
|
||||
strcopy_n(lv.name,mname, sizeof(lv.name)-1);
|
||||
strcopy_n(lv.name,mname, sizeof(lv.name));
|
||||
lv.start_pos = -postavy[i].sektor;
|
||||
lv.dir = postavy[i].direction;
|
||||
macro_load_another_map(&lv);
|
||||
|
@ -1968,7 +1968,7 @@ void fix_group_direction()
|
|||
if (postavy[i].used && postavy[i].groupnum==g && !postavy[i].programovano) postavy[i].direction=viewdir;
|
||||
}
|
||||
|
||||
void souboje_turn(char smer)
|
||||
void souboje_turn(int smer)
|
||||
{
|
||||
if (pass_zavora) return;
|
||||
norefresh=1;
|
||||
|
|
|
@ -49,15 +49,15 @@ void temp_storage_store(const char *name, const void *data, int32_t size) {
|
|||
int32_t temp_storage_find(const char *name) {
|
||||
auto iter = temp_fsystem.find(std::string_view(name));
|
||||
if (iter == temp_fsystem.end()) return -1;
|
||||
return iter->second.size();
|
||||
return static_cast<int32_t>(iter->second.size());
|
||||
}
|
||||
|
||||
int32_t temp_storage_retrieve(const char *name, void *data, int32_t size) {
|
||||
auto iter = temp_fsystem.find(std::string_view(name));
|
||||
if (iter == temp_fsystem.end()) return -1;
|
||||
size = std::min<int32_t>(size, iter->second.size());
|
||||
size = std::min<int32_t>(size, static_cast<int32_t>(iter->second.size()));
|
||||
std::copy(iter->second.begin(), iter->second.end(), reinterpret_cast<uint8_t *>(data));
|
||||
return iter->second.size();
|
||||
return static_cast<int32_t>(iter->second.size());
|
||||
|
||||
}
|
||||
|
||||
|
@ -109,7 +109,7 @@ uint32_t temp_storage_read(void *data, uint32_t size, TMPFILE_RD *f) {
|
|||
d = d.substr(p.size());
|
||||
auto b = reinterpret_cast<uint8_t *>(data);
|
||||
std::copy(p.begin(), p.end(), b);
|
||||
return p.size();
|
||||
return static_cast<uint32_t>(p.size());
|
||||
}
|
||||
|
||||
void temp_storage_skip(TMPFILE_RD *f, int bytes) {
|
||||
|
@ -136,7 +136,7 @@ char *temp_storage_gets(char *buff, size_t sz, TMPFILE_RD *f) {
|
|||
if (pos > d.size()) pos = d.size(); else ++pos;
|
||||
if (pos == 0) return NULL;
|
||||
if (pos > sz - 1) pos = sz - 1;
|
||||
temp_storage_read(buff, pos, f);
|
||||
temp_storage_read(buff, static_cast<int32_t>(pos), f);
|
||||
buff[pos] = 0;
|
||||
return buff;
|
||||
}
|
||||
|
|
|
@ -359,7 +359,7 @@ static reload_mobs()
|
|||
{
|
||||
extern char reset_mobiles;
|
||||
reset_mobiles=1;
|
||||
strcopy_n(loadlevel.name,level_fname,12);
|
||||
strcopy_n(loadlevel.name,level_fname,sizeof(loadlevel.name));
|
||||
loadlevel.start_pos=viewsector;
|
||||
loadlevel.name[12]=0;
|
||||
loadlevel.dir=viewdir;
|
||||
|
|
|
@ -78,7 +78,7 @@ int build_tables()
|
|||
case 128:while (fgetc(source)!='\n');
|
||||
fgetc(source);
|
||||
i=fscanf(source,"%[^\n]",global_name);
|
||||
strcopy_n(kouzla_tab[cur_spell].spellname,global_name,29);
|
||||
strcopy_n(kouzla_tab[cur_spell].spellname,global_name,sizeof(kouzla_tab[cur_spell].spellname));
|
||||
printf("(%3d, 0x%05X) %s\n",cur_spell, kouzla_tab[cur_spell].start,global_name);
|
||||
break;
|
||||
case 129:add_prog_command(0xff);
|
||||
|
|
|
@ -352,7 +352,7 @@ inline bool CSVReader<Source>::readRow(const CSVFieldIndexMapping<T> &mapping,T
|
|||
} else {
|
||||
static_assert(std::is_same_v<TVal, bool>);
|
||||
CSVState st = read(buff);
|
||||
std::transform(buff.begin(), buff.end(), buff.begin(), [](char c) -> char { return std::tolower(c); });
|
||||
std::transform(buff.begin(), buff.end(), buff.begin(), [](char c) -> char { return static_cast<char>(std::tolower(c)); });
|
||||
if (buff == "y" || buff == "t" || buff =="true" || buff == "yes" || buff == "on") {
|
||||
target.*ptr = true;
|
||||
} else if (buff == "n" || buff == "f" || buff =="false" || buff == "no" || buff == "off") {
|
||||
|
|
|
@ -40,10 +40,9 @@ TSTR_LIST merge_configs(TSTR_LIST target, TSTR_LIST source)
|
|||
int i;
|
||||
char *c;
|
||||
|
||||
buff[255]=0;
|
||||
for (i=0;i<str_count(source);i++) if (source[i])
|
||||
{
|
||||
strcopy_n(buff,source[i],255);
|
||||
strcopy_n(buff,source[i],sizeof(buff));
|
||||
c=strchr(buff,' ');
|
||||
if (c!=NULL)
|
||||
{
|
||||
|
|
|
@ -666,7 +666,7 @@ void display_status()
|
|||
int k;
|
||||
|
||||
for(k=0;k<5;k++) copys[k]=h->flags & (1<<k)?flags[k]:'.';
|
||||
if (h->src_file[0]) strcopy_n(nname,h->src_file,12);else strcpy(nname,"<local>");
|
||||
if (h->src_file[0]) strcopy_n(nname,h->src_file,sizeof(nname));else strcpy(nname,"<local>");
|
||||
printf("%04Xh ... %12s %s %s %08lXh %6d %10d %6d \n",i*BK_MINOR_HANDLES+j,
|
||||
nname,names[h->status-1],
|
||||
copys,(unsigned long)(uintptr_t)h->blockdata,h->size,h->counter,h->lockcount);
|
||||
|
|
|
@ -363,7 +363,7 @@ char mgif_play(const void *mgif) //dekoduje a zobrazi frame
|
|||
const char *pc;
|
||||
char *ff;
|
||||
// int acts,size,act,csize;
|
||||
const void *scr_sav;
|
||||
const void *scr_sav = 0;
|
||||
int scr_act=-1;
|
||||
|
||||
|
||||
|
|
|
@ -17,10 +17,10 @@ class MUSStreamParser: public IMusicStream {
|
|||
public:
|
||||
|
||||
MUSStreamParser(const char *name) {
|
||||
data = map_file_to_memory(file_icase_find(name), &sz);
|
||||
if (data) {
|
||||
_data = map_file_to_memory(file_icase_find(name), &sz);
|
||||
if (_data) {
|
||||
_loaded = true;
|
||||
iter = reinterpret_cast<const uint8_t *>(data);
|
||||
iter = reinterpret_cast<const uint8_t *>(_data);
|
||||
chans = read_short();
|
||||
freq = read_int();
|
||||
iter += 4;
|
||||
|
@ -32,7 +32,7 @@ public:
|
|||
}
|
||||
|
||||
~MUSStreamParser() {
|
||||
if (data) unmap_file(data, sz);
|
||||
if (_data) unmap_file(_data, sz);
|
||||
}
|
||||
MUSStreamParser(const MUSStreamParser &) = delete;
|
||||
MUSStreamParser& operator=(const MUSStreamParser &) = delete;
|
||||
|
@ -60,7 +60,7 @@ public:
|
|||
|
||||
protected:
|
||||
bool _loaded = false;
|
||||
void *data;
|
||||
void *_data;
|
||||
std::size_t sz;
|
||||
|
||||
std::int16_t chans;
|
||||
|
|
|
@ -30,7 +30,7 @@ TSTRINGTABLE *stringtable_load(const char *filename) {
|
|||
CSVRecord rec;
|
||||
std::unique_ptr<TSTRINGTABLE> tbl = std::make_unique<TSTRINGTABLE>();
|
||||
while (reader.readRow(mapping, rec)) {
|
||||
windows2kamenik(rec.string.data(), rec.string.size(), rec.string.data());
|
||||
windows2kamenik(rec.string.data(), (int)rec.string.size(), rec.string.data());
|
||||
tbl->_strings[rec.index] = rec.string;
|
||||
}
|
||||
return tbl.release();
|
||||
|
|
|
@ -173,7 +173,7 @@ EVENT_MSG *task_wait_event(int32_t event_number) {
|
|||
return cur_message;
|
||||
}
|
||||
int q_any_task() {
|
||||
return task_list.size();
|
||||
return (int)task_list.size();
|
||||
}
|
||||
char task_quitmsg() {
|
||||
if (current_task_inst == NULL) return 0;
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#pragma warning(disable: 4456)
|
||||
#pragma warning(disable: 4457)
|
||||
#pragma warning(disable: 4702)
|
||||
#pragma warning(disable: 4100)
|
||||
#define CASE_FALLTHROUGH
|
||||
#else
|
||||
#define CASE_FALLTHROUGH [[fallthrough]]
|
||||
|
|
|
@ -26,9 +26,8 @@ void SDLContext::SDL_Deleter::operator ()(SDL_Texture* texture) {
|
|||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
void SDLContext::SDL_Audio_Deleter::operator()(void *x) {
|
||||
SDL_AudioDeviceID id = reinterpret_cast<std::uintptr_t>(x);
|
||||
SDL_CloseAudioDevice(id);
|
||||
void SDLContext::SDL_Audio_Deleter::operator()(SDL_AudioDeviceID x) {
|
||||
SDL_CloseAudioDevice(x);
|
||||
}
|
||||
|
||||
struct SDL_INIT_Context {
|
||||
|
@ -602,12 +601,12 @@ SDLContext::AudioInfo SDLContext::init_audio(const AudioConfig &config, SDL_Audi
|
|||
}
|
||||
throw std::runtime_error(err.str());
|
||||
}
|
||||
_audio.reset(reinterpret_cast<void *>(id));
|
||||
_audio.reset(id);
|
||||
|
||||
return {obtaied.freq};
|
||||
}
|
||||
void SDLContext::pause_audio(bool pause) {
|
||||
SDL_AudioDeviceID id = reinterpret_cast<std::intptr_t>(_audio.get());
|
||||
SDL_AudioDeviceID id = _audio.get();
|
||||
SDL_PauseAudioDevice(id, pause?1:0);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <vector>
|
||||
#include <libs/mouse.h>
|
||||
#include <functional>
|
||||
#include "unique_value.h"
|
||||
|
||||
#include <queue>
|
||||
|
||||
|
@ -124,7 +125,7 @@ protected:
|
|||
};
|
||||
|
||||
struct SDL_Audio_Deleter {
|
||||
void operator()(void *x);
|
||||
void operator()(SDL_AudioDeviceID x);
|
||||
};
|
||||
|
||||
|
||||
|
@ -142,7 +143,7 @@ protected:
|
|||
std::unique_ptr<SDL_Texture, SDL_Deleter> _texture;
|
||||
std::unique_ptr<SDL_Texture, SDL_Deleter> _texture2;
|
||||
std::unique_ptr<SDL_Texture, SDL_Deleter> _crt_effect;
|
||||
std::unique_ptr<void, SDL_Audio_Deleter> _audio;
|
||||
unique_value<SDL_AudioDeviceID, SDL_Audio_Deleter> _audio;
|
||||
SDL_Texture *_visible_texture;
|
||||
SDL_Texture *_hidden_texture;
|
||||
|
||||
|
|
96
platform/sdl/unique_value.h
Normal file
96
platform/sdl/unique_value.h
Normal file
|
@ -0,0 +1,96 @@
|
|||
#include <optional>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
template<typename T, typename Deleter>
|
||||
class unique_value {
|
||||
public:
|
||||
using value_type = T;
|
||||
using deleter_type = Deleter;
|
||||
|
||||
private:
|
||||
std::optional<T> value;
|
||||
deleter_type deleter;
|
||||
|
||||
public:
|
||||
unique_value() noexcept(std::is_nothrow_default_constructible_v<Deleter>)
|
||||
: value(std::nullopt), deleter{} { }
|
||||
|
||||
explicit unique_value(T v, Deleter d = Deleter{}) noexcept
|
||||
: value(std::move(v)), deleter(std::move(d)) { }
|
||||
|
||||
// Zakázání kopírování
|
||||
unique_value(const unique_value&) = delete;
|
||||
unique_value& operator=(const unique_value&) = delete;
|
||||
|
||||
unique_value(unique_value&& other) noexcept
|
||||
: value(std::move(other.value)), deleter(std::move(other.deleter))
|
||||
{
|
||||
other.value.reset();
|
||||
}
|
||||
|
||||
unique_value& operator=(unique_value&& other) noexcept {
|
||||
if (this != &other) {
|
||||
reset(); // Uvolní aktuální hodnotu (volá deleter)
|
||||
value = std::move(other.value);
|
||||
deleter = std::move(other.deleter);
|
||||
other.value.reset();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
~unique_value() {
|
||||
reset();
|
||||
}
|
||||
|
||||
T* operator->() {
|
||||
return &(*value);
|
||||
}
|
||||
const T* operator->() const {
|
||||
return &(*value);
|
||||
}
|
||||
|
||||
T& operator*() {
|
||||
return *value;
|
||||
}
|
||||
const T& operator*() const {
|
||||
return *value;
|
||||
}
|
||||
|
||||
T& get() {
|
||||
return *value;
|
||||
}
|
||||
const T& get() const {
|
||||
return *value;
|
||||
}
|
||||
|
||||
bool has_value() const noexcept {
|
||||
return value.has_value();
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
return value.has_value();
|
||||
}
|
||||
|
||||
std::optional<T> release() noexcept {
|
||||
auto temp = std::move(value);
|
||||
value.reset();
|
||||
return temp;
|
||||
}
|
||||
|
||||
void reset() noexcept {
|
||||
if (value.has_value()) {
|
||||
deleter(*value);
|
||||
value.reset();
|
||||
}
|
||||
}
|
||||
void reset(T &&v) noexcept {
|
||||
reset();
|
||||
value = std::move(v);
|
||||
}
|
||||
void reset(const T &v) noexcept {
|
||||
reset();
|
||||
value = v;
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue