Skip to content

Commit

Permalink
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
skyjake committed Feb 19, 2013
1 parent ff66582 commit adaa0db
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
1 change: 1 addition & 0 deletions doomsday/libdeng2/include/de/Id
@@ -0,0 +1 @@
#include "core/id.h"
96 changes: 96 additions & 0 deletions doomsday/libdeng2/include/de/core/id.h
@@ -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
3 changes: 3 additions & 0 deletions doomsday/libdeng2/libdeng2.pro
Expand Up @@ -72,6 +72,7 @@ HEADERS += \
include/de/Error \
include/de/Event \
include/de/FileLogSink \
include/de/Id \
include/de/Library \
include/de/Log \
include/de/LogBuffer \
Expand Down Expand Up @@ -100,6 +101,7 @@ HEADERS += \
include/de/core/debuglogsink.h \
include/de/core/event.h \
include/de/core/filelogsink.h \
include/de/core/id.h \
include/de/core/library.h \
include/de/core/log.h \
include/de/core/logbuffer.h \
Expand All @@ -126,6 +128,7 @@ SOURCES += \
src/core/config.cpp \
src/core/debuglogsink.cpp \
src/core/filelogsink.cpp \
src/core/id.cpp \
src/core/library.cpp \
src/core/log.cpp \
src/core/logbuffer.cpp \
Expand Down
80 changes: 80 additions & 0 deletions doomsday/libdeng2/src/core/id.cpp
@@ -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

0 comments on commit adaa0db

Please sign in to comment.