summaryrefslogtreecommitdiffstats
path: root/raul
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-09-16 22:11:37 +0200
committerDavid Robillard <d@drobilla.net>2018-09-16 22:11:37 +0200
commit0c6bb92f3f59b6b86f3b7b56224677e79b2e6900 (patch)
tree40e3173eec8b493f22152372cc00782f2a076f92 /raul
parent07396e8d23bb8724c5960b57aca33e08a97f4e52 (diff)
downloadraul-0c6bb92f3f59b6b86f3b7b56224677e79b2e6900.tar.gz
raul-0c6bb92f3f59b6b86f3b7b56224677e79b2e6900.tar.bz2
raul-0c6bb92f3f59b6b86f3b7b56224677e79b2e6900.zip
Lint with clang-tidy
Diffstat (limited to 'raul')
-rw-r--r--raul/Deletable.hpp5
-rw-r--r--raul/DoubleBuffer.hpp8
-rw-r--r--raul/Exception.hpp4
-rw-r--r--raul/Maid.hpp19
-rw-r--r--raul/Noncopyable.hpp10
-rw-r--r--raul/Path.hpp4
-rw-r--r--raul/Process.hpp4
-rw-r--r--raul/RingBuffer.hpp23
-rw-r--r--raul/Semaphore.hpp11
-rw-r--r--raul/Socket.hpp54
-rw-r--r--raul/Symbol.hpp8
-rw-r--r--raul/TimeSlice.hpp10
-rw-r--r--raul/TimeStamp.hpp14
13 files changed, 82 insertions, 92 deletions
diff --git a/raul/Deletable.hpp b/raul/Deletable.hpp
index 02b35c5..9b80d54 100644
--- a/raul/Deletable.hpp
+++ b/raul/Deletable.hpp
@@ -27,8 +27,9 @@ namespace Raul {
class Deletable
{
public:
- Deletable() {}
- virtual ~Deletable() {}
+ Deletable() = default;
+
+ virtual ~Deletable() = default;
};
} // namespace Raul
diff --git a/raul/DoubleBuffer.hpp b/raul/DoubleBuffer.hpp
index 3c4d934..73b5565 100644
--- a/raul/DoubleBuffer.hpp
+++ b/raul/DoubleBuffer.hpp
@@ -35,7 +35,7 @@ namespace Raul {
template<typename T>
class DoubleBuffer {
public:
- inline DoubleBuffer(T val)
+ explicit DoubleBuffer(T val)
: _state(State::READ_WRITE)
{
_vals[0] = std::move(val);
@@ -49,9 +49,11 @@ public:
case State::READ_WRITE:
case State::READ_LOCK:
return _vals[0];
- default:
- return _vals[1];
+ case State::WRITE_READ:
+ case State::LOCK_READ:
+ break;
}
+ return _vals[1];
}
inline bool set(T new_val) {
diff --git a/raul/Exception.hpp b/raul/Exception.hpp
index a1c5dfd..da4a0e0 100644
--- a/raul/Exception.hpp
+++ b/raul/Exception.hpp
@@ -26,10 +26,10 @@ namespace Raul {
/** An exception (unexpected error). */
class Exception : public std::exception {
public:
- const char* what() const noexcept { return _what.c_str(); }
+ const char* what() const noexcept override { return _what.c_str(); }
protected:
- explicit Exception(const std::string& what) : _what(what) {}
+ explicit Exception(std::string what) : _what(std::move(what)) {}
private:
const std::string _what;
diff --git a/raul/Maid.hpp b/raul/Maid.hpp
index 26845b6..fa54871 100644
--- a/raul/Maid.hpp
+++ b/raul/Maid.hpp
@@ -41,11 +41,11 @@ public:
/** An object that can be disposed via Maid::dispose(). */
class Disposable : public Deletable {
public:
- Disposable() : _maid_next(nullptr) {}
+ Disposable() = default;
private:
friend class Maid;
- Disposable* _maid_next;
+ Disposable* _maid_next{};
};
/** Disposable wrapper for any type. */
@@ -54,7 +54,7 @@ public:
{
public:
template<typename... Args>
- Managed(Args&&... args)
+ explicit Managed(Args&&... args)
: T(std::forward<Args>(args)...)
{}
};
@@ -63,14 +63,14 @@ public:
template<typename T>
class Disposer {
public:
- Disposer(Maid& maid) : _maid(&maid) {}
- Disposer() : _maid(nullptr) {}
+ explicit Disposer(Maid* maid) : _maid(maid) {}
+ Disposer() : _maid(nullptr) {}
void operator()(T* obj) {
if (_maid) { _maid->dispose(obj); }
}
- Maid* _maid;
+ Maid* _maid{nullptr};
};
/** A managed pointer that automatically disposes of its contents.
@@ -86,7 +86,7 @@ public:
/** Return false iff there is currently no garbage. */
inline bool empty() const {
- return !(bool)_disposed.load(std::memory_order_relaxed);
+ return !_disposed.load(std::memory_order_relaxed);
}
/** Enqueue an object for deletion when cleanup() is called next.
@@ -127,8 +127,9 @@ public:
/** Make a unique_ptr that will dispose its object when dropped. */
template<class T, class... Args>
managed_ptr<T> make_managed(Args&&... args) {
- T* obj = new T(std::forward<Args>(args)...);
- return std::unique_ptr<T, Disposer<T> >(obj, Disposer<T>(*this));
+ return std::unique_ptr<T, Disposer<T> >(
+ new T(std::forward<Args>(args)...),
+ Disposer<T>(this));
}
private:
diff --git a/raul/Noncopyable.hpp b/raul/Noncopyable.hpp
index 633df6e..876cee8 100644
--- a/raul/Noncopyable.hpp
+++ b/raul/Noncopyable.hpp
@@ -20,13 +20,13 @@
namespace Raul {
class Noncopyable {
-protected:
- Noncopyable() {}
- ~Noncopyable() {}
-
-private:
+public:
Noncopyable(const Noncopyable&) = delete;
const Noncopyable& operator=(const Noncopyable&) = delete;
+
+protected:
+ Noncopyable() = default;
+ ~Noncopyable() = default;
};
} // namespace Raul
diff --git a/raul/Path.hpp b/raul/Path.hpp
index d5116d2..a613e96 100644
--- a/raul/Path.hpp
+++ b/raul/Path.hpp
@@ -74,9 +74,7 @@ public:
* Note this is faster than constructing a Path from another Path's string
* since validation is unnecessary.
*/
- Path(const Path& path)
- : std::basic_string<char>(path)
- {}
+ Path(const Path& path) = default;
/** Return true iff `c` is a valid Path character. */
static inline bool is_valid_char(char c) {
diff --git a/raul/Process.hpp b/raul/Process.hpp
index 5804775..717a900 100644
--- a/raul/Process.hpp
+++ b/raul/Process.hpp
@@ -47,7 +47,7 @@ public:
// (in child)
// Close all nonstandard file descriptors
- struct rlimit max_fds;
+ struct rlimit max_fds{};
getrlimit(RLIMIT_NOFILE, &max_fds);
for (rlim_t fd = 3; fd < max_fds.rlim_cur; ++fd) {
close(static_cast<int>(fd));
@@ -79,7 +79,7 @@ public:
}
private:
- Process() {}
+ Process() = default;
};
} // namespace Raul
diff --git a/raul/RingBuffer.hpp b/raul/RingBuffer.hpp
index dfe5dd2..a7bbfb7 100644
--- a/raul/RingBuffer.hpp
+++ b/raul/RingBuffer.hpp
@@ -43,11 +43,12 @@ public:
@param size Size in bytes (note this may be rounded up).
*/
explicit RingBuffer(uint32_t size)
- : _size(next_power_of_two(size))
+ : _write_head(0)
+ , _read_head(0)
+ , _size(next_power_of_two(size))
, _size_mask(_size - 1)
- , _buf(static_cast<char*>(malloc(_size)))
+ , _buf(new char[_size])
{
- reset();
assert(read_space() == 0);
assert(write_space() == _size - 1);
}
@@ -55,9 +56,7 @@ public:
/**
Destroy a RingBuffer.
*/
- inline ~RingBuffer() {
- free(_buf);
- }
+ inline ~RingBuffer() = default;
/**
Reset (empty) the RingBuffer.
@@ -160,11 +159,11 @@ private:
static inline uint32_t next_power_of_two(uint32_t size) {
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
size--;
- size |= size >> 1;
- size |= size >> 2;
- size |= size >> 4;
- size |= size >> 8;
- size |= size >> 16;
+ size |= size >> 1U;
+ size |= size >> 2U;
+ size |= size >> 4U;
+ size |= size >> 8U;
+ size |= size >> 16U;
size++;
return size;
}
@@ -212,7 +211,7 @@ private:
const uint32_t _size; ///< Size (capacity) in bytes
const uint32_t _size_mask; ///< Mask for fast modulo
- char* const _buf; ///< Contents
+ const std::unique_ptr<char[]> _buf; ///< Contents
};
} // namespace Raul
diff --git a/raul/Semaphore.hpp b/raul/Semaphore.hpp
index e0fbf18..1dd6e39 100644
--- a/raul/Semaphore.hpp
+++ b/raul/Semaphore.hpp
@@ -52,7 +52,9 @@ public:
Chances are you want 1 wait() per 1 post(), an initial value of 0.
*/
- inline Semaphore(unsigned initial) {
+ explicit Semaphore(unsigned initial)
+ : _sem()
+ {
if (!init(initial)) {
throw std::runtime_error("Failed to create semaphore");
}
@@ -203,10 +205,7 @@ Semaphore::timed_wait(const std::chrono::duration<Rep, Period>& wait)
inline bool
Semaphore::init(unsigned initial)
{
- if (sem_init(&_sem, 0, initial)) {
- return false;
- }
- return true;
+ return !sem_init(&_sem, 0, initial);
}
inline void
@@ -247,7 +246,7 @@ Semaphore::timed_wait(const std::chrono::duration<Rep, Period>& wait)
namespace chr = std::chrono;
// Use clock_gettime to ensure sem_timedwait uses the same clock
- struct timespec time;
+ struct timespec time{};
clock_gettime(CLOCK_REALTIME, &time);
const auto now(chr::seconds(time.tv_sec) + chr::nanoseconds(time.tv_nsec));
diff --git a/raul/Socket.hpp b/raul/Socket.hpp
index d41e0dc..57279e7 100644
--- a/raul/Socket.hpp
+++ b/raul/Socket.hpp
@@ -47,11 +47,11 @@ public:
explicit Socket(Type t);
/** Wrap an existing open socket. */
- Socket(Type t,
- const std::string& uri,
- struct sockaddr* addr,
- socklen_t addr_len,
- int fd);
+ Socket(Type t,
+ std::string uri,
+ struct sockaddr* addr,
+ socklen_t addr_len,
+ int fd);
~Socket();
@@ -109,7 +109,7 @@ private:
inline
Socket::Socket(Type t)
: _uri(t == Type::UNIX ? "unix:" : "tcp:")
- , _addr(NULL)
+ , _addr(nullptr)
, _addr_len(0)
, _type(t)
, _sock(-1)
@@ -125,12 +125,12 @@ Socket::Socket(Type t)
}
inline
-Socket::Socket(Type t,
- const std::string& uri,
- struct sockaddr* addr,
- socklen_t addr_len,
- int fd)
- : _uri(uri)
+Socket::Socket(Type t,
+ std::string uri,
+ struct sockaddr* addr,
+ socklen_t addr_len,
+ int fd)
+ : _uri(std::move(uri))
, _addr(addr)
, _addr_len(addr_len)
, _type(t)
@@ -150,8 +150,8 @@ Socket::set_addr(const std::string& uri)
{
free(_addr);
if (_type == Type::UNIX && uri.substr(0, strlen("unix://")) == "unix://") {
- const std::string path = uri.substr(strlen("unix://"));
- struct sockaddr_un* uaddr = static_cast<struct sockaddr_un*>(
+ const std::string path = uri.substr(strlen("unix://"));
+ auto* uaddr = static_cast<struct sockaddr_un*>(
calloc(1, sizeof(struct sockaddr_un)));
uaddr->sun_family = AF_UNIX;
strncpy(uaddr->sun_path, path.c_str(), sizeof(uaddr->sun_path) - 1);
@@ -167,14 +167,14 @@ Socket::set_addr(const std::string& uri)
}
std::string host = authority.substr(0, port_sep);
- const std::string port = authority.substr(port_sep + 1).c_str();
+ const std::string port = authority.substr(port_sep + 1);
if (host.empty() || host == "*") {
host = "0.0.0.0"; // INADDR_ANY
}
struct addrinfo* ainfo;
int st = 0;
- if ((st = getaddrinfo(host.c_str(), port.c_str(), NULL, &ainfo))) {
+ if ((st = getaddrinfo(host.c_str(), port.c_str(), nullptr, &ainfo))) {
return false;
}
@@ -191,21 +191,13 @@ Socket::set_addr(const std::string& uri)
inline bool
Socket::bind(const std::string& uri)
{
- if (set_addr(uri) && ::bind(_sock, _addr, _addr_len) != -1) {
- return true;
- }
-
- return false;
+ return set_addr(uri) && ::bind(_sock, _addr, _addr_len) != -1;
}
inline bool
Socket::connect(const std::string& uri)
{
- if (set_addr(uri) && ::connect(_sock, _addr, _addr_len) != -1) {
- return true;
- }
-
- return false;
+ return set_addr(uri) && ::connect(_sock, _addr, _addr_len) != -1;
}
inline bool
@@ -221,9 +213,9 @@ Socket::listen()
inline std::shared_ptr<Socket>
Socket::accept()
{
- socklen_t client_addr_len = _addr_len;
- struct sockaddr* client_addr = static_cast<struct sockaddr*>(
- calloc(1, client_addr_len));
+ socklen_t client_addr_len = _addr_len;
+ auto* const client_addr =
+ static_cast<struct sockaddr*>(calloc(1, client_addr_len));
const int conn = ::accept(_sock, client_addr, &client_addr_len);
if (conn == -1) {
@@ -242,8 +234,8 @@ Socket::accept()
}
}
- return std::shared_ptr<Socket>(
- new Socket(_type, client_uri, client_addr, client_addr_len, conn));
+ return std::make_shared<Socket>(
+ _type, client_uri, client_addr, client_addr_len, conn);
}
inline void
diff --git a/raul/Symbol.hpp b/raul/Symbol.hpp
index 1189b7b..70062be 100644
--- a/raul/Symbol.hpp
+++ b/raul/Symbol.hpp
@@ -73,9 +73,7 @@ public:
* Note this is faster than constructing a Symbol from another Symbol's
* string since validation is unnecessary.
*/
- Symbol(const Symbol& symbol)
- : std::basic_string<char>(symbol)
- {}
+ Symbol(const Symbol& symbol) = default;
/** Return true iff `c` is a valid Symbol start character. */
static inline bool is_valid_start_char(char c) {
@@ -93,8 +91,8 @@ public:
return false; // Must start with a letter or underscore
}
- for (size_t i = 0; i < str.length(); ++i) {
- if (!is_valid_char(str[i])) {
+ for (auto c : str) {
+ if (!is_valid_char(c)) {
return false; // All characters must be _, a-z, A-Z, 0-9
}
}
diff --git a/raul/TimeSlice.hpp b/raul/TimeSlice.hpp
index c72ca03..3f629ea 100644
--- a/raul/TimeSlice.hpp
+++ b/raul/TimeSlice.hpp
@@ -95,19 +95,19 @@ public:
}
inline TimeStamp beats_to_seconds(TimeStamp beats) const {
- return TimeStamp(real_unit(), beats.to_double() * 1/(double)_beat_rate);
+ return TimeStamp(real_unit(), beats.to_double() * 1/_beat_rate);
}
inline TimeStamp beats_to_ticks(TimeStamp beats) const {
- return TimeStamp(ticks_unit(), beats.to_double() * (double)_beat_rate * _tick_rate);
+ return TimeStamp(ticks_unit(), beats.to_double() * _beat_rate * _tick_rate);
}
inline TimeStamp ticks_to_seconds(TimeStamp ticks) const {
- return TimeStamp(real_unit(), ticks.ticks() * 1/(double)_tick_rate);
+ return {real_unit(), ticks.ticks() * 1/_tick_rate};
}
inline TimeStamp ticks_to_beats(TimeStamp ticks) const {
- return TimeStamp(beats_unit(), ticks.ticks() * 1/(double)_tick_rate * _beat_rate);
+ return {beats_unit(), ticks.ticks() * 1/_tick_rate * _beat_rate};
}
/** Start of current sub-cycle in ticks */
@@ -130,7 +130,7 @@ public:
inline TimeUnit beats_unit() const { return _start_beats.unit(); }
inline TimeUnit ticks_unit() const { return _start_ticks.unit(); }
- inline TimeUnit real_unit() const { return TimeUnit(TimeUnit::SECONDS, 0); }
+ inline TimeUnit real_unit() const { return {TimeUnit::SECONDS, 0}; }
private:
inline void update_beat_time() {
diff --git a/raul/TimeStamp.hpp b/raul/TimeStamp.hpp
index 0c671b2..eabddec 100644
--- a/raul/TimeStamp.hpp
+++ b/raul/TimeStamp.hpp
@@ -45,9 +45,9 @@ public:
_ppt = ppt;
}
- static inline TimeUnit frames(uint32_t srate) { return TimeUnit(FRAMES, srate); }
- static inline TimeUnit beats(uint32_t ppqn) { return TimeUnit(BEATS, ppqn); }
- static inline TimeUnit seconds() { return TimeUnit(BEATS, std::numeric_limits<uint32_t>::max()); }
+ static inline TimeUnit frames(uint32_t srate) { return {FRAMES, srate}; }
+ static inline TimeUnit beats(uint32_t ppqn) { return {BEATS, ppqn}; }
+ static inline TimeUnit seconds() { return {BEATS, std::numeric_limits<uint32_t>::max()}; }
inline Type type() const { return _type; }
inline uint32_t ppt() const { return _ppt; }
@@ -76,7 +76,7 @@ private:
*/
class TimeStamp {
public:
- inline TimeStamp(TimeUnit unit, uint32_t ticks = 0, uint32_t subticks = 0)
+ explicit TimeStamp(TimeUnit unit, uint32_t ticks = 0, uint32_t subticks = 0)
: _ticks(ticks)
, _subticks(subticks)
, _unit(unit)
@@ -209,21 +209,21 @@ operator<<(std::ostream& os, const TimeStamp& t)
class FrameStamp : public TimeStamp {
public:
- inline FrameStamp(uint32_t rate, uint32_t ticks = 0, uint32_t subticks = 0)
+ explicit FrameStamp(uint32_t rate, uint32_t ticks = 0, uint32_t subticks = 0)
: TimeStamp(TimeUnit(TimeUnit::FRAMES, rate), ticks, subticks)
{}
};
class BeatStamp : public TimeStamp {
public:
- inline BeatStamp(uint32_t ppqn, uint32_t ticks = 0, uint32_t subticks = 0)
+ explicit BeatStamp(uint32_t ppqn, uint32_t ticks = 0, uint32_t subticks = 0)
: TimeStamp(TimeUnit(TimeUnit::BEATS, ppqn), ticks, subticks)
{}
};
/** Same thing as TimeStamp really, but makes code clearer and enforces
* correct semantics via type safety */
-typedef TimeStamp TimeDuration;
+using TimeDuration = TimeStamp;
} // namespace Raul