LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
idpool.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include "utilconfig.h"
12#include <QByteArray>
13#include <QSet>
14#include <QIODevice>
15#include <QDataStream>
16#include <QtDebug>
17
18namespace LC::Util
19{
26 template<typename T>
27 class IDPool final
28 {
29 T CurrentID_;
30 public:
35 explicit IDPool (const T& id = T ())
36 : CurrentID_ { id }
37 {
38 }
39
44 T GetID ()
45 {
46 return ++CurrentID_;
47 }
48
53 void SetID (T id)
54 {
55 CurrentID_ = id;
56 }
57
62 void FreeID (T id)
63 {
64 Q_UNUSED (id)
65 }
66
71 QByteArray SaveState () const
72 {
73 QByteArray result;
74 {
75 QDataStream ostr (&result, QIODevice::WriteOnly);
76 quint8 ver = 1;
77 ostr << ver;
78 ostr << CurrentID_;
79 }
80 return result;
81 }
82
88 void LoadState (const QByteArray& state)
89 {
90 if (state.isEmpty ())
91 return;
92
93 QDataStream istr (state);
94 quint8 ver;
95 istr >> ver;
96 if (ver == 1)
97 istr >> CurrentID_;
98 else
99 qWarning () << Q_FUNC_INFO
100 << "unknown version"
101 << ver
102 << ", not restoring state.";
103 }
104 };
105}
QByteArray SaveState() const
Saves the state of this pool.
Definition idpool.h:71
T GetID()
Returns next ID.
Definition idpool.h:44
void FreeID(T id)
Frees the id.
Definition idpool.h:62
void SetID(T id)
Forcefully sets the current ID.
Definition idpool.h:53
void LoadState(const QByteArray &state)
Recovers the state of this pool.
Definition idpool.h:88
IDPool(const T &id=T())
Creates a pool with the given initial value.
Definition idpool.h:35