LeechCraft 0.6.70-14794-g33744ae6ce
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 <QDataStream>
15#include <QtDebug>
16
17namespace LC::Util
18{
25 template<typename T>
26 class IDPool final
27 {
28 T CurrentID_;
29 public:
34 explicit IDPool (const T& id = T ())
35 : CurrentID_ { id }
36 {
37 }
38
43 T GetID ()
44 {
45 return ++CurrentID_;
46 }
47
52 void SetID (T id)
53 {
54 CurrentID_ = id;
55 }
56
61 void FreeID (T id)
62 {
63 Q_UNUSED (id)
64 }
65
70 QByteArray SaveState () const
71 {
72 QByteArray result;
73 {
74 QDataStream ostr (&result, QIODevice::WriteOnly);
75 quint8 ver = 1;
76 ostr << ver;
77 ostr << CurrentID_;
78 }
79 return result;
80 }
81
87 void LoadState (const QByteArray& state)
88 {
89 if (state.isEmpty ())
90 return;
91
92 QDataStream istr (state);
93 quint8 ver;
94 istr >> ver;
95 if (ver == 1)
96 istr >> CurrentID_;
97 else
98 qWarning () << Q_FUNC_INFO
99 << "unknown version"
100 << ver
101 << ", not restoring state.";
102 }
103 };
104}
A simple pool of identificators of the given type.
Definition: idpool.h:27
QByteArray SaveState() const
Saves the state of this pool.
Definition: idpool.h:70
T GetID()
Returns next ID.
Definition: idpool.h:43
void FreeID(T id)
Frees the id.
Definition: idpool.h:61
void SetID(T id)
Forcefully sets the current ID.
Definition: idpool.h:52
void LoadState(const QByteArray &state)
Recovers the state of this pool.
Definition: idpool.h:87
IDPool(const T &id=T())
Creates a pool with the given initial value.
Definition: idpool.h:34