Split the lib in smaller libs to ease unit tests

This commit is contained in:
emeric
2020-02-13 18:04:35 +01:00
parent 1e2c1caeed
commit 15e53caa2d
131 changed files with 382 additions and 138 deletions
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdexcept>
#include <string>
class LmsException : public std::runtime_error
{
public:
LmsException(const std::string& error = "") : std::runtime_error {error} {}
};
+40
View File
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2016 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <filesystem>
#include <memory>
#include <unordered_set>
// Used to get config values from configuration files
class IConfig
{
public:
// Default values are returned in case of setting not found
virtual std::string getString(const std::string& setting, const std::string& def = "", const std::unordered_set<std::string>& allowedValues = {}) = 0;
virtual std::filesystem::path getPath(const std::string& setting, const std::filesystem::path& def = std::filesystem::path()) = 0;
virtual unsigned long getULong(const std::string& setting, unsigned long def = 0) = 0;
virtual long getLong(const std::string& setting, long def = 0) = 0;
virtual bool getBool(const std::string& setting, bool def = false) = 0;
};
std::unique_ptr<IConfig> createConfig(const std::filesystem::path& p);
+84
View File
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2013 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <sstream>
#include "Service.hpp"
enum class Severity
{
FATAL,
ERROR,
WARNING,
INFO,
DEBUG,
};
enum class Module
{
API_SUBSONIC,
AUTH,
AV,
COVER,
DB,
DBUPDATER,
FEATURE,
MAIN,
METADATA,
REMOTE,
SERVICE,
SIMILARITY,
TRANSCODE,
UI,
};
const char* getModuleName(Module mod);
const char* getSeverityName(Severity sev);
class Logger;
class Log
{
public:
Log(Logger* logger, Module module, Severity severity);
~Log();
Module getModule() const { return _module; }
Severity getSeverity() const { return _severity; }
std::string getMessage() const;
std::ostringstream& getOstream() { return _oss; }
private:
Module _module;
Severity _severity;
std::ostringstream _oss;
Logger* _logger {};
};
class Logger
{
public:
virtual void processLog(const Log& log) = 0;
};
#define LMS_LOG(module, severity) Log(ServiceProvider<Logger>::get(), Module::module, Severity::severity).getOstream()
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/asio/ip/address.hpp>
namespace std
{
template<> struct hash<boost::asio::ip::address>
{
std::size_t operator()(const boost::asio::ip::address& ipAddr) const;
};
}
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2016 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <filesystem>
#include <string>
#include <vector>
#include <Wt/WDateTime.h>
void computeCrc(const std::filesystem::path& p, std::vector<unsigned char>& checksum);
// Make sure the given path is a directory
// Create it if needed
bool ensureDirectory(const std::filesystem::path& dir);
// Get the last write time since Epoch
Wt::WDateTime getLastWriteTime(const std::filesystem::path& dir);
+64
View File
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <algorithm>
#include <random>
namespace Random {
using RandGenerator = std::mt19937;
RandGenerator& getRandGenerator();
template <typename T>
T
getRandom(T min, T max)
{
std::uniform_int_distribution<> dist {min, max};
return dist (getRandGenerator());
}
template <typename T>
T
getRealRandom(T min, T max)
{
std::uniform_real_distribution<> dist {min, max};
return dist (getRandGenerator());
}
template <typename Container>
void
shuffleContainer(Container& container)
{
std::shuffle(std::begin(container), std::end(container), getRandGenerator());
}
template <typename Container>
typename Container::const_iterator
pickRandom(const Container& container)
{
if (container.empty())
return std::end(container);
return std::next(std::begin(container), getRandom(0, static_cast<int>(container.size() - 1)));
}
}
+64
View File
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <type_traits>
template <typename Class>
class ServiceProvider
{
public:
template <class DerivedClass, class ...Args>
static
Class&
create(Args&&... args)
{
static_assert(std::is_base_of<Class, DerivedClass>::value);
assign(std::make_unique<DerivedClass>(std::forward<Args>(args)...));
return *get();
}
template <class ...Args>
static
Class&
create(Args&&... args)
{
assign(std::make_unique<Class>(std::forward<Args>(args)...));
return *get();
}
static
Class&
assign(std::unique_ptr<Class> service)
{
_service = std::move(service);
return *get();
}
static void clear() { _service.reset(); }
static Class* get() { return _service.get(); }
private:
static inline std::unique_ptr<Class> _service;
};
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Logger.hpp"
class StreamLogger final : public Logger
{
public:
StreamLogger(std::ostream& oss);
void processLog(const Log& log);
private:
std::ostream& _os;
};
+81
View File
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <optional>
#include <string>
#include <sstream>
#include <vector>
namespace StringUtils {
std::vector<std::string>
splitString(const std::string& string, const std::string& separators);
std::string
joinStrings(const std::vector<std::string>& strings, const std::string& delimiter);
std::string
stringTrim(const std::string& str, const std::string& whitespaces = " \t");
std::string
stringTrimEnd(const std::string& str, const std::string& whitespaces = " \t");
std::string
stringToLower(const std::string& str);
std::string
stringToUpper(const std::string& str);
std::string
bufferToString(const std::vector<unsigned char>& data);
template<typename T>
std::optional<T> readAs(const std::string& str)
{
T res;
std::istringstream iss ( str );
iss >> res;
if (iss.fail())
return std::nullopt;
return res;
}
template<>
std::optional<std::string>
readAs(const std::string& str);
std::string
replaceInString(const std::string& str, const std::string& from, const std::string& to);
std::string
jsEscape(const std::string& str);
bool
stringEndsWith(const std::string& str, const std::string& ending);
std::optional<std::string>
stringFromHex(const std::string& str);
} // StringUtils
+47
View File
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <optional>
#include <string>
#include <string_view>
#include "utils/String.hpp"
class UUID
{
public:
static std::optional<UUID> fromString(std::string_view str);
std::string_view getAsString() const { return _value; }
private:
UUID(std::string_view value) : _value {value} {}
std::string _value;
};
namespace StringUtils
{
template <>
std::optional<UUID>
readAs(const std::string& str);
}
+30
View File
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2015 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <functional>
template<class T, class Compare = std::less<>>
constexpr T clamp(T v, T lo, T hi, Compare comp = {})
{
assert(!comp(hi, lo));
return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}
+29
View File
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Logger.hpp"
class WtLogger final : public Logger
{
public:
void processLog(const Log& log) override;
};