diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-11-24 17:28:19 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-11-24 17:46:08 +0000 |
commit | 4e640edf7642cbc7bd2cc5d308b95cb14f8e236a (patch) | |
tree | f5180878a23c12e9ddbb47008f9d9464af08b0b0 | |
parent | 81affee11ad6a020b3f6a7381c6e6cc3f7cb5123 (diff) |
Use c++11 strings instead of old c-style manual string manipulation.
-rw-r--r-- | fastos/src/vespa/fastos/file.cpp | 22 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/file.h | 6 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/process.cpp | 9 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/process.h | 10 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/unix_file.cpp | 8 | ||||
-rw-r--r-- | searchlib/src/vespa/searchlib/util/dirtraverse.cpp | 51 | ||||
-rw-r--r-- | searchlib/src/vespa/searchlib/util/dirtraverse.h | 13 | ||||
-rw-r--r-- | searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.cpp | 30 | ||||
-rw-r--r-- | searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.h | 7 | ||||
-rw-r--r-- | vespalog/src/logger/runserver.cpp | 25 | ||||
-rw-r--r-- | vespalog/src/vespa/log/control-file.cpp | 12 | ||||
-rw-r--r-- | vespalog/src/vespa/log/control-file.h | 2 | ||||
-rw-r--r-- | vespalog/src/vespa/log/internal.h | 15 |
13 files changed, 81 insertions, 129 deletions
diff --git a/fastos/src/vespa/fastos/file.cpp b/fastos/src/vespa/fastos/file.cpp index 0764c9b1b66..1382aef7386 100644 --- a/fastos/src/vespa/fastos/file.cpp +++ b/fastos/src/vespa/fastos/file.cpp @@ -39,7 +39,7 @@ static const size_t MAX_WRITE_CHUNK_SIZE = 0x4000000; // 64 MB FastOS_FileInterface::FastOS_FileInterface(const char *filename) : _fAdviseOptions(_defaultFAdviseOptions), _writeChunkSize(MAX_WRITE_CHUNK_SIZE), - _filename(nullptr), + _filename(), _openFlags(0), _directIOEnabled(false), _syncWritesEnabled(false) @@ -49,10 +49,7 @@ FastOS_FileInterface::FastOS_FileInterface(const char *filename) } -FastOS_FileInterface::~FastOS_FileInterface() -{ - free(_filename); -} +FastOS_FileInterface::~FastOS_FileInterface() = default; bool FastOS_FileInterface::InitializeClass () { @@ -358,18 +355,14 @@ FastOS_FileInterface::MakeDirIfNotPresentOrExit(const char *name) void FastOS_FileInterface::SetFileName(const char *filename) { - if (_filename != nullptr) { - free(_filename); - } - - _filename = strdup(filename); + _filename = filename; } const char * FastOS_FileInterface::GetFileName() const { - return (_filename != nullptr) ? _filename : ""; + return _filename.c_str(); } @@ -502,11 +495,8 @@ void FastOS_FileInterface::dropFromCache() const } FastOS_DirectoryScanInterface::FastOS_DirectoryScanInterface(const char *path) - : _searchPath(strdup(path)) + : _searchPath(path) { } -FastOS_DirectoryScanInterface::~FastOS_DirectoryScanInterface() -{ - free(_searchPath); -} +FastOS_DirectoryScanInterface::~FastOS_DirectoryScanInterface() = default; diff --git a/fastos/src/vespa/fastos/file.h b/fastos/src/vespa/fastos/file.h index 40b33e49b35..2d83a1766f0 100644 --- a/fastos/src/vespa/fastos/file.h +++ b/fastos/src/vespa/fastos/file.h @@ -88,7 +88,7 @@ private: void WriteBufInternal(const void *buffer, size_t length); protected: - char *_filename; + std::string _filename; unsigned int _openFlags; bool _directIOEnabled; bool _syncWritesEnabled; @@ -726,7 +726,7 @@ private: FastOS_DirectoryScanInterface& operator= (const FastOS_DirectoryScanInterface&); protected: - char *_searchPath; + std::string _searchPath; public: @@ -750,7 +750,7 @@ public: * This is an internal copy of the path specified in the constructor. * @return Search path string. */ - const char *GetSearchPath () { return _searchPath; } + const char *GetSearchPath () { return _searchPath.c_str(); } /** * Read the next entry in the directory scan. Failure indicates diff --git a/fastos/src/vespa/fastos/process.cpp b/fastos/src/vespa/fastos/process.cpp index 29c53fe9326..332d82c6aad 100644 --- a/fastos/src/vespa/fastos/process.cpp +++ b/fastos/src/vespa/fastos/process.cpp @@ -1,14 +1,13 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "process.h" -#include <cstring> FastOS_ProcessInterface::FastOS_ProcessInterface (const char *cmdLine, bool pipeStdin, FastOS_ProcessRedirectListener *stdoutListener, FastOS_ProcessRedirectListener *stderrListener, int bufferSize) : - _cmdLine(nullptr), + _cmdLine(cmdLine), _pipeStdin(pipeStdin), _stdoutListener(stdoutListener), _stderrListener(stderrListener), @@ -16,10 +15,6 @@ FastOS_ProcessInterface::FastOS_ProcessInterface (const char *cmdLine, _next(nullptr), _prev(nullptr) { - _cmdLine = strdup(cmdLine); } -FastOS_ProcessInterface::~FastOS_ProcessInterface () -{ - free (_cmdLine); -} +FastOS_ProcessInterface::~FastOS_ProcessInterface () = default; diff --git a/fastos/src/vespa/fastos/process.h b/fastos/src/vespa/fastos/process.h index 99f045d2f56..25d5224817a 100644 --- a/fastos/src/vespa/fastos/process.h +++ b/fastos/src/vespa/fastos/process.h @@ -12,6 +12,7 @@ #include "types.h" #include <cstddef> +#include <string> /** * This class serves as a sink for redirected (piped) output from @@ -52,8 +53,8 @@ private: protected: - char *_cmdLine; - bool _pipeStdin; + std::string _cmdLine; + bool _pipeStdin; FastOS_ProcessRedirectListener *_stdoutListener; FastOS_ProcessRedirectListener *_stderrListener; @@ -179,10 +180,7 @@ public: * Get command line string. * @return Command line string */ - const char *GetCommandLine () - { - return _cmdLine; - } + const char *GetCommandLine () const { return _cmdLine.c_str(); } }; #include <vespa/fastos/unix_process.h> diff --git a/fastos/src/vespa/fastos/unix_file.cpp b/fastos/src/vespa/fastos/unix_file.cpp index 2ef8c2f55ff..0fee991cac4 100644 --- a/fastos/src/vespa/fastos/unix_file.cpp +++ b/fastos/src/vespa/fastos/unix_file.cpp @@ -258,7 +258,7 @@ FastOS_UNIX_File::Open(unsigned int openFlags, const char *filename) } unsigned int accessFlags = CalcAccessFlags(openFlags); - _filedes = open(_filename, accessFlags, 0664); + _filedes = open(_filename.c_str(), accessFlags, 0664); rc = (_filedes != -1); @@ -386,10 +386,10 @@ FastOS_UNIX_File::Delete(const char *name) bool FastOS_UNIX_File::Delete(void) { - assert(!IsOpened()); - assert(_filename != nullptr); + assert( ! IsOpened()); + assert( ! _filename.empty()); - return (unlink(_filename) == 0); + return (unlink(_filename.c_str()) == 0); } bool FastOS_UNIX_File::Rename (const char *currentFileName, const char *newFileName) diff --git a/searchlib/src/vespa/searchlib/util/dirtraverse.cpp b/searchlib/src/vespa/searchlib/util/dirtraverse.cpp index 6ab5d42d350..31b39ad0d09 100644 --- a/searchlib/src/vespa/searchlib/util/dirtraverse.cpp +++ b/searchlib/src/vespa/searchlib/util/dirtraverse.cpp @@ -15,17 +15,16 @@ static int cmpname(const void *av, const void *bv) *(const DirectoryTraverse::Name *const *) av; const DirectoryTraverse::Name *const b = *(const DirectoryTraverse::Name *const *) bv; - return strcmp(a->_name, b->_name); + return strcmp(a->_name.c_str(), b->_name.c_str()); } } DirectoryTraverse::Name::Name(const char *name) - : _name(nullptr), + : _name(name), _next(nullptr) { - _name = strdup(name); } -DirectoryTraverse::Name::~Name() { free(_name); } +DirectoryTraverse::Name::~Name() = default; DirectoryTraverse::Name * DirectoryTraverse::Name::sort(Name *head, int count) @@ -132,19 +131,15 @@ DirectoryTraverse::ScanSingleDir() assert(_nameHead == nullptr); assert(_nameCount == 0); delete _curDir; - free(_fullDirName); - _fullDirName = nullptr; + _fullDirName.clear(); _curDir = UnQueueDir(); if (_curDir == nullptr) return; - _fullDirName = (char *) malloc(strlen(_baseDir) + 1 + - strlen(_curDir->_name) + 1); - strcpy(_fullDirName, _baseDir); - if (_curDir->_name[0] != '\0') { - strcat(_fullDirName, "/"); - strcat(_fullDirName, _curDir->_name); + _fullDirName = _baseDir; + if ( ! _curDir->_name.empty()) { + _fullDirName += "/" + _curDir->_name; } - FastOS_DirectoryScan *dirscan = new FastOS_DirectoryScan(_fullDirName); + FastOS_DirectoryScan *dirscan = new FastOS_DirectoryScan(_fullDirName.c_str()); while (dirscan->ReadNext()) { const char *name = dirscan->GetName(); if (strcmp(name, ".") == 0 || @@ -171,13 +166,8 @@ DirectoryTraverse::NextName() if (_nameHead == nullptr) return false; _curName = UnQueueName(); - free(_fullName); - _fullName = (char *) malloc(strlen(_fullDirName) + 1 + - strlen(_curName->_name) + 1); - strcpy(_fullName, _fullDirName); - _relName = _fullName + strlen(_baseDir) + 1; - strcat(_fullName, "/"); - strcat(_fullName, _curName->_name); + _fullName = _fullDirName + "/" + _curName->_name; + _relName = _fullName.c_str() + (_baseDir.size() + 1); return true; } @@ -193,13 +183,8 @@ DirectoryTraverse::NextRemoveDir() return false; curName = _rdirHead; _rdirHead = curName->_next; - free(_fullName); - _fullName = (char *) malloc(strlen(_baseDir) + 1 + - strlen(curName->_name) + 1); - strcpy(_fullName, _baseDir); - _relName = _fullName + strlen(_baseDir) + 1; - strcat(_fullName, "/"); - strcat(_fullName, curName->_name); + _fullName = _baseDir + "/" + curName->_name; + _relName = _fullName.c_str() + _baseDir.size() + 1; delete curName; return true; } @@ -226,7 +211,7 @@ DirectoryTraverse::RemoveTree() const char *fullname = GetFullName(); FastOS_File::RemoveDirectory(fullname); } - FastOS_File::RemoveDirectory(_baseDir); + FastOS_File::RemoveDirectory(_baseDir.c_str()); return true; } @@ -252,7 +237,7 @@ DirectoryTraverse::GetTreeSize() } DirectoryTraverse::DirectoryTraverse(const char *baseDir) - : _baseDir(nullptr), + : _baseDir(baseDir), _nameHead(nullptr), _nameCount(0), _dirHead(nullptr), @@ -261,11 +246,10 @@ DirectoryTraverse::DirectoryTraverse(const char *baseDir) _rdirHead(nullptr), _curDir(nullptr), _curName(nullptr), - _fullDirName(nullptr), - _fullName(nullptr), + _fullDirName(), + _fullName(), _relName(nullptr) { - _baseDir = strdup(baseDir); QueueDir(""); ScanSingleDir(); } @@ -273,9 +257,6 @@ DirectoryTraverse::DirectoryTraverse(const char *baseDir) DirectoryTraverse::~DirectoryTraverse() { - free(_fullDirName); - free(_fullName); - free(_baseDir); delete _curDir; delete _curName; PushPushedDirs(); diff --git a/searchlib/src/vespa/searchlib/util/dirtraverse.h b/searchlib/src/vespa/searchlib/util/dirtraverse.h index bff7aae705a..4a96ad0935d 100644 --- a/searchlib/src/vespa/searchlib/util/dirtraverse.h +++ b/searchlib/src/vespa/searchlib/util/dirtraverse.h @@ -3,6 +3,7 @@ #pragma once #include <cstdint> +#include <string> namespace search { @@ -20,14 +21,14 @@ public: Name& operator=(const Name &); public: - char *_name; + std::string _name; Name *_next; explicit Name(const char *name); ~Name(); static Name *sort(Name *head, int count); }; private: - char *_baseDir; + std::string _baseDir; Name *_nameHead; int _nameCount; Name *_dirHead; @@ -36,11 +37,11 @@ private: Name *_rdirHead; Name *_curDir; Name *_curName; - char *_fullDirName; - char *_fullName; - char *_relName; + std::string _fullDirName; + std::string _fullName; + const char *_relName; public: - const char *GetFullName() const { return _fullName; } + const char *GetFullName() const { return _fullName.c_str(); } const char *GetRelName() const { return _relName; } void QueueDir(const char *name); void PushDir(const char *name); diff --git a/searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.cpp b/searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.cpp index bd05dd4b0f5..7c702c0dae0 100644 --- a/searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.cpp +++ b/searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.cpp @@ -1,6 +1,5 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include "docsumstate.h" #include "keywordextractor.h" #include "idocsumenvironment.h" #include <vespa/searchlib/parsequery/stackdumpiterator.h> @@ -20,7 +19,7 @@ bool useful(search::ParseItem::ItemCreator creator) KeywordExtractor::KeywordExtractor(IDocsumEnvironment * env) : _env(env), - _legalPrefixes(NULL), + _legalPrefixes(nullptr), _legalIndexes() { } @@ -28,7 +27,7 @@ KeywordExtractor::KeywordExtractor(IDocsumEnvironment * env) KeywordExtractor::~KeywordExtractor() { - while (_legalPrefixes != NULL) { + while (_legalPrefixes != nullptr) { IndexPrefix *tmp = _legalPrefixes; _legalPrefixes = tmp->_next; delete tmp; @@ -42,32 +41,25 @@ KeywordExtractor::IsLegalIndexName(const char *idxName) const } KeywordExtractor::IndexPrefix::IndexPrefix(const char *prefix, IndexPrefix **list) - : _prefix(NULL), - _prefixLen(0), - _next(NULL) + : _prefix(prefix), + _next(nullptr) { - _prefix = strdup(prefix); - assert(_prefix != NULL); - _prefixLen = strlen(prefix); _next = *list; *list = this; } -KeywordExtractor::IndexPrefix::~IndexPrefix() -{ - free(_prefix); -} +KeywordExtractor::IndexPrefix::~IndexPrefix() = default; bool KeywordExtractor::IndexPrefix::Match(const char *idxName) const { - return (strncmp(idxName, _prefix, _prefixLen) == 0); + return (strncmp(idxName, _prefix.c_str(), _prefix.size()) == 0); } void KeywordExtractor::AddLegalIndexSpec(const char *spec) { - if (spec == NULL) + if (spec == nullptr) return; vespalib::string toks(spec); // tokens @@ -107,9 +99,9 @@ KeywordExtractor::GetLegalIndexSpec() { vespalib::string spec; - if (_legalPrefixes != NULL) { + if (_legalPrefixes != nullptr) { for (IndexPrefix *pt = _legalPrefixes; - pt != NULL; pt = pt->_next) { + pt != nullptr; pt = pt->_next) { if (spec.size() > 0) spec.append(';'); spec.append(pt->_prefix); @@ -131,7 +123,7 @@ KeywordExtractor::IsLegalIndex(vespalib::stringref idxS) const { vespalib::string resolvedIdxName; - if (_env != NULL) { + if (_env != nullptr) { resolvedIdxName = _env->lookupIndex(idxS); } else { @@ -238,7 +230,7 @@ KeywordExtractor::ExtractKeywords(vespalib::stringref buf) const // Must now allocate a string and copy the data from the rawbuf void *result = malloc(keywords.GetUsedLen()); - if (result != NULL) { + if (result != nullptr) { memcpy(result, keywords.GetDrainPos(), keywords.GetUsedLen()); } return static_cast<char *>(result); diff --git a/searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.h b/searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.h index 50d72f7a7d0..44c85121058 100644 --- a/searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.h +++ b/searchsummary/src/vespa/searchsummary/docsummary/keywordextractor.h @@ -24,9 +24,8 @@ public: IndexPrefix& operator=(const IndexPrefix &); public: - char *_prefix; - int _prefixLen; - IndexPrefix *_next; + vespalib::string _prefix; + IndexPrefix *_next; IndexPrefix(const char *prefix, IndexPrefix **list); ~IndexPrefix(); @@ -42,7 +41,7 @@ private: bool IsLegalIndexPrefix(const char *idxName) const { for (const IndexPrefix *pt = _legalPrefixes; - pt != NULL; + pt != nullptr; pt = pt->_next) { if (pt->Match(idxName)) diff --git a/vespalog/src/logger/runserver.cpp b/vespalog/src/logger/runserver.cpp index c74806a8b5b..68950cef4b2 100644 --- a/vespalog/src/logger/runserver.cpp +++ b/vespalog/src/logger/runserver.cpp @@ -5,7 +5,6 @@ #include <fcntl.h> #include <cerrno> #include <unistd.h> -#include <csignal> #include <sys/select.h> #include <sys/types.h> @@ -54,13 +53,13 @@ bool whole_seconds(int cnt, int secs) { class PidFile { private: - char *_pidfile; + std::string _pidfile; int _fd; PidFile(const PidFile&); PidFile& operator= (const PidFile&); public: - PidFile(const char *pidfile) : _pidfile(strdup(pidfile)), _fd(-1) {} - ~PidFile() { free(_pidfile); if (_fd >= 0) close(_fd); } + PidFile(const char *pidfile) : _pidfile(pidfile), _fd(-1) {} + ~PidFile() { if (_fd >= 0) close(_fd); } int readPid(); void writePid(); bool writeOpen(); @@ -72,7 +71,7 @@ public: void PidFile::cleanUp() { - if (!anotherRunning()) remove(_pidfile); + if (!anotherRunning()) remove(_pidfile.c_str()); if (_fd >= 0) close(_fd); _fd = -1; } @@ -82,14 +81,14 @@ PidFile::writeOpen() { if (_fd >= 0) close(_fd); int flags = O_CREAT | O_WRONLY | O_NONBLOCK; - _fd = open(_pidfile, flags, 0644); + _fd = open(_pidfile.c_str(), flags, 0644); if (_fd < 0) { - fprintf(stderr, "could not create pidfile %s: %s\n", _pidfile, + fprintf(stderr, "could not create pidfile %s: %s\n", _pidfile.c_str(), strerror(errno)); return false; } if (flock(_fd, LOCK_EX | LOCK_NB) != 0) { - fprintf(stderr, "could not lock pidfile %s: %s\n", _pidfile, + fprintf(stderr, "could not lock pidfile %s: %s\n", _pidfile.c_str(), strerror(errno)); close(_fd); _fd = -1; @@ -106,7 +105,7 @@ PidFile::writePid() int didtruncate = ftruncate(_fd, (off_t)0); if (didtruncate != 0) { fprintf(stderr, "could not truncate pid file %s: %s\n", - _pidfile, strerror(errno)); + _pidfile.c_str(), strerror(errno)); std::_Exit(1); } char buf[100]; @@ -115,16 +114,16 @@ PidFile::writePid() ssize_t didw = write(_fd, buf, l); if (didw != l) { fprintf(stderr, "could not write pid to %s: %s\n", - _pidfile, strerror(errno)); + _pidfile.c_str(), strerror(errno)); std::_Exit(1); } - LOG(debug, "wrote '%s' to %s (fd %d)", buf, _pidfile, _fd); + LOG(debug, "wrote '%s' to %s (fd %d)", buf, _pidfile.c_str(), _fd); } int PidFile::readPid() { - FILE *pf = fopen(_pidfile, "r"); + FILE *pf = fopen(_pidfile.c_str(), "r"); if (pf == NULL) return 0; char buf[100]; strcpy(buf, "0"); @@ -151,7 +150,7 @@ bool PidFile::canStealLock() { int flags = O_WRONLY | O_NONBLOCK; - int desc = open(_pidfile, flags, 0644); + int desc = open(_pidfile.c_str(), flags, 0644); if (desc < 0) { return false; } diff --git a/vespalog/src/vespa/log/control-file.cpp b/vespalog/src/vespa/log/control-file.cpp index 77ad1d0ec73..2096dd1531c 100644 --- a/vespalog/src/vespa/log/control-file.cpp +++ b/vespalog/src/vespa/log/control-file.cpp @@ -5,7 +5,6 @@ #include <ctype.h> #include <cstdio> #include <sys/mman.h> -#include <sys/stat.h> #include <errno.h> #include <unistd.h> #include <memory> @@ -28,7 +27,7 @@ ControlFile::ControlFile(const char *file, Mode mode) : (O_RDWR | O_CREAT))), _fileSize(0), _mode(mode), - _fileName(strdup(file)), + _fileName(file), _prefix(0), _mapBase(0), _mappedSize(0), @@ -43,7 +42,6 @@ ControlFile::ControlFile(const char *file, Mode mode) ControlFile::~ControlFile() { freeMapping(); - free(_fileName); } void @@ -168,7 +166,7 @@ ControlFile::extendMapping() if (fileLen == -1) { _fileBacking.unlock(); - LOG(error, "Cannot get file size of '%s': %s", _fileName, + LOG(error, "Cannot get file size of '%s': %s", _fileName.c_str(), strerror(errno)); return false; } @@ -273,14 +271,14 @@ ControlFile::getLevels(const char *name) strcat(appendedString, "\n"); int len = strlen(appendedString); - int fd = open(_fileName, O_WRONLY | O_APPEND); + int fd = open(_fileName.c_str(), O_WRONLY | O_APPEND); int wlen = write(fd, appendedString, len); oldFileLength = lseek(fd, (off_t)0, SEEK_CUR) - wlen; close(fd); if (wlen != len) { _fileBacking.unlock(); LOG(error, "Writing to control file '%s' fails (%d/%d bytes): %s", - _fileName, wlen, len, strerror(errno)); + _fileName.c_str(), wlen, len, strerror(errno)); return reinterpret_cast<unsigned int *>(inheritLevels); } else { _fileSize = _fileBacking.size(); @@ -290,7 +288,7 @@ ControlFile::getLevels(const char *name) if (!extendMapping()) { _fileBacking.unlock(); // just for sure LOG(error, "Failed to extend mapping of '%s', losing runtime " - "configurability of component '%s'", _fileName, name); + "configurability of component '%s'", _fileName.c_str(), name); return defaultLevels(); } } diff --git a/vespalog/src/vespa/log/control-file.h b/vespalog/src/vespa/log/control-file.h index 6f302c7a97c..69e725dc465 100644 --- a/vespalog/src/vespa/log/control-file.h +++ b/vespalog/src/vespa/log/control-file.h @@ -19,7 +19,7 @@ private: Lock _fileBacking; int _fileSize; enum Mode _mode; - char *_fileName; + std::string _fileName; void ensureHeader(); bool hasPrefix() { return (_prefix != NULL && _prefix[0] != '\0' && diff --git a/vespalog/src/vespa/log/internal.h b/vespalog/src/vespa/log/internal.h index c9081b72ce9..4411d9fa6e6 100644 --- a/vespalog/src/vespa/log/internal.h +++ b/vespalog/src/vespa/log/internal.h @@ -1,7 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once -#include <cstring> +#include <string> #include <cstdlib> #if !__GNUC__ && !defined(__attribute__) @@ -15,15 +15,14 @@ void throwInvalid(const char *fmt, ...) class InvalidLogException { private: - char *_what; - InvalidLogException& operator = (const InvalidLogException&); + std::string _what; public: - InvalidLogException(const InvalidLogException &x) : - _what(strdup(x._what)) {} - InvalidLogException(const char *s) : _what(strdup(s)) {} - ~InvalidLogException() { free(_what); } - const char *what() const { return _what; } + InvalidLogException& operator = (const InvalidLogException&) = delete; + InvalidLogException(const InvalidLogException &x) = default; + InvalidLogException(const char *s) : _what(s) {} + ~InvalidLogException() = default; + const char *what() const { return _what.c_str(); } }; } // end namespace ns_log |