Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
libdeng2: Imported the Id class from Hawthorn
Unique 32-bit identifiers.
- Loading branch information
Showing
4 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "core/id.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* The Doomsday Engine Project -- libdeng2 | ||
* | ||
* Copyright (c) 2009-2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef LIBDENG2_ID_H | ||
#define LIBDENG2_ID_H | ||
|
||
#include "../libdeng2.h" | ||
#include "../ISerializable" | ||
#include "../Value" | ||
#include "../Log" | ||
|
||
namespace de { | ||
|
||
class String; | ||
|
||
/** | ||
* Unique identifier number. Zero is not a valid identifier, as it reserved | ||
* for the "no identifier" special case. | ||
*/ | ||
class DENG2_PUBLIC Id : public ISerializable, public LogEntry::Arg::Base | ||
{ | ||
public: | ||
typedef duint32 Type; | ||
|
||
/// The special "no identifier". | ||
enum { NONE = 0 }; | ||
|
||
public: | ||
/** | ||
* Constructs a new identifier. It is automatically unique (until the duint32 range | ||
* is depleted). | ||
*/ | ||
Id(); | ||
|
||
Id(Type const &idValue) : _id(idValue) {} | ||
|
||
/** | ||
* Constructs an identifier from the text representation. | ||
* | ||
* @param text Text representation of an identifier, such as returned by asText(). | ||
*/ | ||
Id(String const &text); | ||
|
||
~Id(); | ||
|
||
bool operator < (Id const &other) const { return _id < other._id; } | ||
|
||
bool operator == (Id const &other) const { return _id == other._id; } | ||
|
||
bool operator != (Id const &other) const { return _id != other._id; } | ||
|
||
operator bool () const { return _id != NONE; } | ||
|
||
operator Type () const { return _id; } | ||
|
||
/// Converts the Id to a text string. | ||
operator String () const; | ||
|
||
operator Value::Number () const; | ||
|
||
/// Converts the Id to a text string. | ||
String asText() const; | ||
|
||
// Implements ISerializable. | ||
void operator >> (Writer &to) const; | ||
void operator << (Reader &from); | ||
|
||
// Implements LogEntry::Arg::Base. | ||
LogEntry::Arg::Type logEntryArgType() const { return LogEntry::Arg::STRING; } | ||
|
||
private: | ||
Type _id; | ||
static Type _generator; | ||
}; | ||
|
||
DENG2_PUBLIC QTextStream &operator << (QTextStream &os, Id const &id); | ||
|
||
} // namespace de | ||
|
||
#endif // LIBDENG2_ID_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* The Doomsday Engine Project -- libdeng2 | ||
* | ||
* Copyright (c) 2009-2013 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "de/Id" | ||
#include "de/String" | ||
#include "de/Writer" | ||
#include "de/Reader" | ||
|
||
namespace de { | ||
|
||
// The Id generator starts from one. | ||
Id::Type Id::_generator = 1; | ||
|
||
Id::Id() : _id(_generator++) | ||
{ | ||
if(_id == NONE) | ||
{ | ||
++_id; | ||
} | ||
} | ||
|
||
Id::Id(String const &text) : _id(NONE) | ||
{ | ||
if(text.beginsWith("{") && text.endsWith("}")) | ||
{ | ||
_id = text.substr(1, text.size() - 2).toUInt(); | ||
} | ||
} | ||
|
||
Id::~Id() | ||
{} | ||
|
||
Id::operator String () const | ||
{ | ||
return asText(); | ||
} | ||
|
||
Id::operator Value::Number () const | ||
{ | ||
return static_cast<Value::Number>(_id); | ||
} | ||
|
||
String Id::asText() const | ||
{ | ||
return QString("{%1}").arg(_id); | ||
} | ||
|
||
QTextStream &de::operator << (QTextStream &os, Id const &id) | ||
{ | ||
os << id.asText(); | ||
return os; | ||
} | ||
|
||
void Id::operator >> (Writer &to) const | ||
{ | ||
to << _id; | ||
} | ||
|
||
void Id::operator << (Reader &from) | ||
{ | ||
from >> _id; | ||
} | ||
|
||
} // namespace de |