Skip to content

Commit

Permalink
libcore: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent b634c78 commit 882a126
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 59 deletions.
18 changes: 11 additions & 7 deletions doomsday/libs/core/include/de/core/commandline.h
Expand Up @@ -84,7 +84,11 @@ class DE_PUBLIC CommandLine
* Returns the number of arguments. This includes the program name, which
* is the first argument in the list.
*/
dint count() const;
dsize count() const;

inline dsize size() const { return count(); }

dint sizei() const;

void clear();

Expand All @@ -106,14 +110,14 @@ class DE_PUBLIC CommandLine
* @param pos Index at which the new argument will be at.
* @param arg Argument to insert.
*/
void insert(duint pos, String const &arg);
void insert(dsize pos, String const &arg);

/**
* Removes an argument by index.
*
* @param pos Index of argument to remove.
*/
void remove(duint pos);
void remove(dsize pos);

/**
* Checks whether @a arg is in the arguments. Since the first argument is
Expand All @@ -140,7 +144,7 @@ class DE_PUBLIC CommandLine
* @return Number of parameters handled.
*/
int forAllParameters(String const &arg,
std::function<void (duint, String const &)> paramHandler) const;
std::function<void (dsize, String const &)> paramHandler) const;

/**
* Gets the parameter for an argument.
Expand All @@ -166,14 +170,14 @@ class DE_PUBLIC CommandLine
/**
* Determines whether an argument is an option, i.e., it begins with a hyphen.
*/
bool isOption(duint pos) const;
bool isOption(dsize pos) const;

/**
* Determines whether an argument is an option, i.e., it begins with a hyphen.
*/
static bool isOption(String const &arg);

String at(duint pos) const;
String at(dsize pos) const;

/**
* Returns a list of pointers to the arguments. The list contains
Expand All @@ -188,7 +192,7 @@ class DE_PUBLIC CommandLine
*
* @param pos Argument index.
*/
void makeAbsolutePath(duint pos);
void makeAbsolutePath(dsize pos);

/**
* Reads a native file and parses its contents using parse().
Expand Down
19 changes: 12 additions & 7 deletions doomsday/libs/core/include/de/data/list.h
Expand Up @@ -50,12 +50,13 @@ class List : public std::vector<T>
using Base::begin;
using Base::end;
using Base::push_back;
using Base::size;
void pop_front() { removeFirst(); } // slow...
void push_front(const T &v) { prepend(v); } // slow...

// Qt style methods:

int size() const { return int(Base::size()); }
int sizei() const { return int(Base::size()); }
void clear() { Base::clear(); }
bool isEmpty() const { return Base::size() == 0; }
inline explicit operator bool() const { return !isEmpty(); }
Expand All @@ -69,21 +70,25 @@ class List : public std::vector<T>
void append(const T &v) { push_back(v); }
void append(const List &list) { for (const T &v : list) push_back(v); }
void prepend(const T &v) { Base::insert(begin(), v); }
void insert(int pos, const T &value) { Base::insert(begin() + pos, value); }
// void insert(int pos, const T &value) { Base::insert(begin() + pos, value); }
void insert(size_t pos, const T &value) { Base::insert(begin() + pos, value); }
void insert(const const_iterator &i, const T &value) { Base::insert(i, value); }
const T &operator[](int pos) const { return Base::at(pos); }
T & operator[](int pos) { return Base::operator[](pos); }
const T &at(int pos) const { return Base::at(pos); }
// const T &operator[](int pos) const { return Base::at(pos); }
const T &operator[](size_t pos) const { return Base::at(pos); }
// T & operator[](int pos) { return Base::operator[](pos); }
T & operator[](size_t pos) { return Base::operator[](pos); }
// const T &at(int pos) const { return Base::at(pos); }
const T &at(size_t pos) const { return Base::at(pos); }
const T &first() const { return Base::front(); }
const T &last() const { return Base::back(); }
T & first() { return Base::front(); }
T & last() { return Base::back(); }
T takeFirst() { T v = first(); pop_front(); return std::move(v); }
T takeLast() { T v = last(); Base::pop_back(); return std::move(v); }
T takeAt(int pos) { T v = std::move(at(pos)); Base::erase(Base::begin() + pos); return std::move(v); }
T takeAt(size_t pos) { T v = std::move(at(pos)); Base::erase(Base::begin() + pos); return std::move(v); }
void removeFirst() { Base::erase(Base::begin()); }
void removeLast() { Base::erase(Base::begin() + size() - 1); }
void removeAt(int pos) { Base::erase(Base::begin() + pos); }
void removeAt(size_t pos) { Base::erase(Base::begin() + pos); }
void removeAll(const T &v) { Base::erase(std::remove(begin(), end(), v), end()); }
void removeOne(const T &v)
{
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libs/core/include/de/libcore.h
Expand Up @@ -229,7 +229,7 @@
* Asserts that the object really exists (not null).
*/
#define DE_SELF(Type, Var) \
DE_ASSERT(Var != 0); \
DE_ASSERT(Var != nullptr); \
de::Type *self = reinterpret_cast<de::Type *>(Var);

/**
Expand Down
12 changes: 6 additions & 6 deletions doomsday/libs/core/src/c_wrapper.cpp
Expand Up @@ -122,13 +122,13 @@ void CommandLine_Alias(char const *longname, char const *shortname)

int CommandLine_Count(void)
{
return DE_COMMANDLINE().count();
return DE_COMMANDLINE().sizei();
}

char const *CommandLine_At(int i)
{
DE_ASSERT(i >= 0);
DE_ASSERT(i < DE_COMMANDLINE().count());
DE_ASSERT(i < DE_COMMANDLINE().sizei());
return *(DE_COMMANDLINE().argv() + i);
}

Expand All @@ -145,7 +145,7 @@ char const *CommandLine_Next(void)
if (!argLastMatch || argLastMatch >= CommandLine_Count() - 1)
{
// No more arguments following the last match.
return 0;
return nullptr;
}
return CommandLine_At(++argLastMatch);
}
Expand All @@ -155,7 +155,7 @@ char const *CommandLine_NextAsPath(void)
if (!argLastMatch || argLastMatch >= CommandLine_Count() - 1)
{
// No more arguments following the last match.
return 0;
return nullptr;
}
DE_COMMANDLINE().makeAbsolutePath(argLastMatch + 1);
return CommandLine_Next();
Expand Down Expand Up @@ -228,7 +228,7 @@ de_Info *Info_NewFromString(char const *utf8text)
catch (de::Error const &er)
{
LOG_WARNING(er.asText());
return 0;
return nullptr;
}
}

Expand All @@ -243,7 +243,7 @@ de_Info *Info_NewFromFile(char const *nativePath)
catch (de::Error const &er)
{
LOG_WARNING(er.asText());
return 0;
return nullptr;
}
}

Expand Down
39 changes: 22 additions & 17 deletions doomsday/libs/core/src/core/commandline.cpp
Expand Up @@ -95,7 +95,7 @@ DE_PIMPL(CommandLine)
DE_ASSERT(pointers.back() == nullptr);
}

void insert(int pos, String const &arg)
void insert(dsize pos, String const &arg)
{
if (pos > arguments.size())
{
Expand All @@ -106,12 +106,12 @@ DE_PIMPL(CommandLine)
arguments.insert(pos, arg);

pointers.insert(pointers.begin() + pos, duplicateStringAsUtf8(arg));
DE_ASSERT(pointers.back() == 0);
DE_ASSERT(pointers.back() == nullptr);
}

void remove(duint pos)
void remove(dsize pos)
{
if (pos >= (duint) arguments.size())
if (pos >= arguments.size())
{
/// @throw OutOfRangeError @a pos is out of range.
throw OutOfRangeError("CommandLine::remove", "Index out of range");
Expand All @@ -130,7 +130,7 @@ CommandLine::CommandLine() : d(new Impl(*this))

CommandLine::CommandLine(const StringList &args) : d(new Impl(*this))
{
for (int i = 0; i < args.size(); ++i)
for (dsize i = 0; i < args.size(); ++i)
{
mb_iterator arg = args.at(i);
if (*arg == '@')
Expand All @@ -147,9 +147,9 @@ CommandLine::CommandLine(const StringList &args) : d(new Impl(*this))

CommandLine::CommandLine(const CommandLine &other) : d(new Impl(*this))
{
DE_FOR_EACH_CONST(Impl::Arguments, i, other.d->arguments)
for (const auto &i : other.d->arguments)
{
d->appendArg(*i);
d->appendArg(i);
}
}

Expand All @@ -158,11 +158,16 @@ NativePath CommandLine::startupPath()
return d->initialDir;
}

dint CommandLine::count() const
dsize CommandLine::count() const
{
return d->arguments.size();
}

dint CommandLine::sizei() const
{
return dint(count());
}

void CommandLine::clear()
{
d->clear();
Expand All @@ -173,12 +178,12 @@ void CommandLine::append(String const &arg)
d->appendArg(arg);
}

void CommandLine::insert(duint pos, String const &arg)
void CommandLine::insert(dsize pos, String const &arg)
{
d->insert(pos, arg);
}

void CommandLine::remove(duint pos)
void CommandLine::remove(dsize pos)
{
d->remove(pos);
}
Expand Down Expand Up @@ -214,7 +219,7 @@ CommandLine::ArgWithParams CommandLine::check(String const &arg, dint numParams)
}

int CommandLine::forAllParameters(String const &arg,
std::function<void (duint, String const &)> paramHandler) const
std::function<void (dsize, String const &)> paramHandler) const
{
int total = 0;
bool inside = false;
Expand Down Expand Up @@ -267,9 +272,9 @@ dint CommandLine::has(String const &arg) const
return howMany;
}

bool CommandLine::isOption(duint pos) const
bool CommandLine::isOption(dsize pos) const
{
if (pos >= (duint) d->arguments.size())
if (pos >= d->arguments.size())
{
/// @throw OutOfRangeError @a pos is out of range.
throw OutOfRangeError("CommandLine::isOption", "Index out of range");
Expand All @@ -283,20 +288,20 @@ bool CommandLine::isOption(String const &arg)
return !(arg.empty() || arg.first() != '-');
}

String CommandLine::at(duint pos) const
String CommandLine::at(dsize pos) const
{
return d->arguments.at(pos);
}

char const *const *CommandLine::argv() const
{
DE_ASSERT(*d->pointers.rbegin() == 0); // the list itself must be null-terminated
DE_ASSERT(*d->pointers.rbegin() == nullptr); // the list itself must be null-terminated
return &d->pointers[0];
}

void CommandLine::makeAbsolutePath(duint pos)
void CommandLine::makeAbsolutePath(dsize pos)
{
if (pos >= (duint) d->arguments.size())
if (pos >= d->arguments.size())
{
/// @throw OutOfRangeError @a pos is out of range.
throw OutOfRangeError("CommandLine::makeAbsolutePath", "Index out of range");
Expand Down
16 changes: 8 additions & 8 deletions doomsday/libs/core/src/core/logbuffer.cpp
Expand Up @@ -66,7 +66,7 @@ DE_PIMPL(LogBuffer)
, maxEntryCount(maxEntryCount)
, useStandardOutput(true)
, flushingEnabled(true)
, fileLogSink(0)
, fileLogSink(nullptr)
//#ifndef WIN32
, outSink(std::cout)
, errSink(std::cerr)
Expand Down Expand Up @@ -162,7 +162,7 @@ LogBuffer::~LogBuffer()
setOutputFile("");
clear();

if (_appBuffer == this) _appBuffer = 0;
if (_appBuffer == this) _appBuffer = nullptr;
}

void LogBuffer::clear()
Expand All @@ -189,10 +189,10 @@ void LogBuffer::latestEntries(Entries &entries, int count) const
{
DE_GUARD(this);
entries.clear();
for (int i = d->entries.size() - 1; i >= 0; --i)
for (int i = d->entries.sizei() - 1; i >= 0; --i)
{
entries.append(d->entries[i]);
if (count && entries.size() >= count)
if (count && entries.sizei() >= count)
{
return;
}
Expand All @@ -213,7 +213,7 @@ void LogBuffer::setEntryFilter(IFilter const *entryFilter)

bool LogBuffer::isEnabled(duint32 entryMetadata) const
{
DE_ASSERT(d->entryFilter != 0);
DE_ASSERT(d->entryFilter != nullptr);
DE_ASSERT(entryMetadata & LogEntry::DomainMask); // must have a domain
if (entryMetadata & LogEntry::Privileged)
{
Expand Down Expand Up @@ -338,7 +338,7 @@ void LogBuffer::flush()
// d->lastFlushedAt = Time();

// Too many entries? Now they can be destroyed since we have flushed everything.
while (d->entries.size() > d->maxEntryCount)
while (d->entries.sizei() > d->maxEntryCount)
{
LogEntry *old = d->entries.front();
d->entries.pop_front();
Expand All @@ -353,13 +353,13 @@ void LogBuffer::setAppBuffer(LogBuffer &appBuffer)

LogBuffer &LogBuffer::get()
{
DE_ASSERT(_appBuffer != 0);
DE_ASSERT(_appBuffer != nullptr);
return *_appBuffer;
}

bool LogBuffer::appBufferExists()
{
return _appBuffer != 0;
return _appBuffer != nullptr;
}

} // namespace de
4 changes: 2 additions & 2 deletions doomsday/libs/core/src/core/memorylogsink.cpp
Expand Up @@ -73,14 +73,14 @@ int MemoryLogSink::entryCount() const
{
DE_GUARD(this);

return _entries.size();
return _entries.sizei();
}

LogEntry const &MemoryLogSink::entry(int index) const
{
DE_GUARD(this);
DE_ASSERT(index >= 0);
DE_ASSERT(index < _entries.size());
DE_ASSERT(index < _entries.sizei());

return *_entries.at(index);
}
Expand Down

0 comments on commit 882a126

Please sign in to comment.