Whitespace and pointer cleanup.

This commit is contained in:
jmsgrogan 2021-03-29 21:31:24 +01:00
parent 6fc0b8dca8
commit a03eb9599f
32 changed files with 441 additions and 468 deletions

View file

@ -15,9 +15,9 @@ AudioDevice::~AudioDevice()
}
std::shared_ptr<AudioDevice> AudioDevice::Create()
std::unique_ptr<AudioDevice> AudioDevice::Create()
{
return std::make_shared<AudioDevice>();
return std::make_unique<AudioDevice>();
}
void AudioDevice::SetNumChannels(unsigned numChannels)
@ -35,17 +35,17 @@ void AudioDevice::SetBufferSize(std::size_t bufferSize)
mBufferSize = bufferSize;
}
unsigned AudioDevice::GetNumChannels()
unsigned AudioDevice::GetNumChannels() const
{
return mNumChannels;
}
unsigned AudioDevice::GetPeriod()
unsigned AudioDevice::GetPeriod() const
{
return mPeriod;
}
std::size_t AudioDevice::GetBufferSize()
std::size_t AudioDevice::GetBufferSize() const
{
return mBufferSize;
}
@ -55,7 +55,7 @@ void AudioDevice::SetSampleRate(unsigned rate)
mSampleRate = rate;
}
unsigned AudioDevice::GetSampleRate()
unsigned AudioDevice::GetSampleRate() const
{
return mSampleRate;
}
@ -65,7 +65,7 @@ void AudioDevice::SetName(const std::string& name)
mName = name;
}
std::string AudioDevice::GetName()
std::string AudioDevice::GetName() const
{
return mName;
}

View file

@ -22,7 +22,7 @@ public:
void SetSampleRate(unsigned rate);
unsigned GetSampleRate();
unsigned GetSampleRate() const;
void SetName(const std::string& name);
@ -32,15 +32,15 @@ public:
void SetBufferSize(std::size_t bufferSize);
unsigned GetNumChannels();
unsigned GetNumChannels() const;
unsigned GetPeriod();
unsigned GetPeriod() const;
std::size_t GetBufferSize();
std::size_t GetBufferSize() const;
std::string GetName();
std::string GetName() const;
static std::shared_ptr<AudioDevice> Create();
static std::unique_ptr<AudioDevice> Create();
};
using AudioDevicePtr = std::shared_ptr<AudioDevice>;
using AudioDevicePtr = std::unique_ptr<AudioDevice>;

View file

@ -1,10 +1,11 @@
#include "AudioManager.h"
#include "AlsaInterface.h"
AudioManager::AudioManager()
: mAudioDevices(),
mAudioInterface()
{
mAudioInterface = AudioInterface::Create();
mAudioInterface = AlsaInterface::Create();
}
AudioManager::~AudioManager()
@ -22,7 +23,7 @@ void AudioManager::AddAudioDevice(AudioDevicePtr device)
mAudioDevices.push_back(device);
}
AudioInterface* AudioManager::GetAudioInterface()
IAudioInterface* AudioManager::GetAudioInterface()
{
return mAudioInterface.get();
}

View file

@ -3,7 +3,7 @@
#include <memory>
#include <vector>
#include "AudioInterface.h"
#include "audio_interfaces/IAudioInterface.h"
#include "AudioDevice.h"
class AudioManager
@ -12,7 +12,7 @@ class AudioManager
private:
std::vector<AudioDevicePtr> mAudioDevices;
AudioInterfaceUPtr mAudioInterface;
IAudioInterfaceUPtr mAudioInterface;
public:
@ -26,7 +26,7 @@ public:
std::vector<AudioDevicePtr> GetAudioDevices();
AudioInterface* GetAudioInterface();
IAudioInterface* GetAudioInterface();
};
using AudioManagerUPtr = std::unique_ptr<AudioManager>;

View file

@ -5,7 +5,7 @@ list(APPEND linux_HEADERS
list(APPEND audio_HEADERS
AudioDevice.h
AudioManager.h
audio_interfaces/AudioInterface.h
audio_interfaces/IAudioInterface.h
midi/MidiReader.h
midi/MidiTrack.h
midi/MidiDocument.h
@ -20,7 +20,6 @@ list(APPEND linux_INCLUDES
list(APPEND audio_LIB_INCLUDES
AudioDevice.cpp
AudioManager.cpp
audio_interfaces/AudioInterface.cpp
midi/MidiReader.cpp
midi/MidiTrack.cpp
midi/MidiDocument.cpp
@ -28,7 +27,7 @@ list(APPEND audio_LIB_INCLUDES
midi/MetaMidiEvent.cpp
midi/MidiChannelEvent.cpp)
add_library(audio SHARED ${audio_LIB_INCLUDES} ${audio_HEADERS})
add_library(audio SHARED ${audio_LIB_INCLUDES} ${linux_INCLUDES} ${audio_HEADERS} ${linux_HEADERS})
target_include_directories(audio PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/src/core/file_utilities"
@ -41,6 +40,6 @@ list(APPEND linux_LIBS
asound
)
target_link_libraries(audio PUBLIC core)
target_link_libraries(audio PUBLIC core ${linux_LIBS})
set_target_properties( audio PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
set_property(TARGET audio PROPERTY FOLDER src)

View file

@ -19,7 +19,7 @@ std::shared_ptr<AlsaInterface> AlsaInterface::Create()
return std::make_shared<AlsaInterface>();
}
void AlsaInterface::OpenDevice(AudioDevicePtr device)
void AlsaInterface::OpenDevice(const AudioDevicePtr& device)
{
MLOG_INFO("Opening Device");
snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
@ -115,7 +115,7 @@ void AlsaInterface::SetChannelNumber(AudioDevicePtr device)
}
}
void AlsaInterface::Play(AudioDevicePtr device)
void AlsaInterface::Play(const AudioDevicePtr& device)
{
MLOG_INFO("Playing audio");
unsigned char *data = (unsigned char *)malloc(mPeriodSize);

View file

@ -3,9 +3,11 @@
#include <memory>
#include <alsa/asoundlib.h>
#include "IAudioInterface.h"
#include "AudioDevice.h"
class AlsaInterface
class AlsaInterface : public IAudioInterface
{
snd_pcm_t* mHandle;
snd_pcm_hw_params_t* mHardwareParams;
@ -19,7 +21,7 @@ public:
static std::shared_ptr<AlsaInterface> Create();
void OpenDevice(AudioDevicePtr device);
void OpenDevice(const AudioDevicePtr& device) override;
void SetAccessType(AudioDevicePtr device);
@ -33,7 +35,7 @@ public:
void SetChannelNumber(AudioDevicePtr device);
void Play(AudioDevicePtr device);
void Play(const AudioDevicePtr& device) override;
};
using AlsaInterfacePtr = std::shared_ptr<AlsaInterface>;

View file

@ -1,22 +0,0 @@
#include "AudioInterface.h"
#include "AudioDevice.h"
AudioInterface::AudioInterface()
{
}
std::unique_ptr<AudioInterface> AudioInterface::Create()
{
return std::make_unique<AudioInterface>();
}
void AudioInterface::OpenDevice(AudioDevicePtr device)
{
}
void AudioInterface::Play(AudioDevicePtr device)
{
}

View file

@ -3,19 +3,19 @@
#include <memory>
class AudioDevice;
using AudioDevicePtr = std::shared_ptr<AudioDevice>;
using AudioDevicePtr = std::unique_ptr<AudioDevice>;
class AudioInterface
class IAudioInterface
{
public:
AudioInterface();
IAudioInterface() = default;
static std::unique_ptr<AudioInterface> Create();
virtual ~IAudioInterface() = default;
void OpenDevice(AudioDevicePtr device);
virtual void OpenDevice(const AudioDevicePtr& device) = 0;
void Play(AudioDevicePtr device);
virtual void Play(const AudioDevicePtr& device) = 0;
};
using AudioInterfaceUPtr = std::unique_ptr<AudioInterface>;
using IAudioInterfaceUPtr = std::unique_ptr<IAudioInterface>;

View file

@ -20,7 +20,7 @@ MainApplication::~MainApplication()
void MainApplication::Initialize(CommandLineArgsUPtr commandLineArgs)
{
mCommandLineArgs = std::move(commandLineArgs);
std::string launch_path = mCommandLineArgs->GetLaunchPath().string();
const auto launch_path = mCommandLineArgs->GetLaunchPath().string();
FileLogger::GetInstance().SetWorkDirectory(launch_path);
FileLogger::GetInstance().Open();

View file

@ -19,12 +19,12 @@ void HttpResponse::SetBody(const std::string& body)
mBody = body;
}
unsigned HttpResponse::GetBodyLength()
unsigned HttpResponse::GetBodyLength() const
{
return unsigned(mBody.length());
}
std::string HttpResponse::GetHeaderString()
std::string HttpResponse::GetHeaderString() const
{
std::string header = "HTTP/" + mHttpVersion + " " + mResponseCode + "\n";
header += "Content-Type: " + mContentType + "\n";
@ -32,7 +32,7 @@ std::string HttpResponse::GetHeaderString()
return header;
}
std::string HttpResponse::ToString()
std::string HttpResponse::ToString() const
{
return GetHeaderString() + "\n\n" + mBody;
}

View file

@ -18,9 +18,9 @@ public:
void SetBody(const std::string& body);
unsigned GetBodyLength();
unsigned GetBodyLength() const;
std::string GetHeaderString();
std::string GetHeaderString() const;
std::string ToString();
std::string ToString() const;
};

View file

@ -11,9 +11,9 @@ Database::~Database()
}
std::shared_ptr<Database> Database::Create()
std::unique_ptr<Database> Database::Create()
{
return std::make_shared<Database>();
return std::make_unique<Database>();
}
void Database::SetPath(const std::string& path)
@ -21,7 +21,7 @@ void Database::SetPath(const std::string& path)
mPath = path;
}
std::string Database::GetPath()
std::string Database::GetPath() const
{
return mPath;
}

View file

@ -14,11 +14,11 @@ public:
~Database();
static std::shared_ptr<Database> Create();
static std::unique_ptr<Database> Create();
void SetPath(const std::string& path);
std::string GetPath();
std::string GetPath() const;
};
using DatabasePtr = std::shared_ptr<Database>;
using DatabasePtr = std::unique_ptr<Database>;

View file

@ -12,9 +12,9 @@ DatabaseManager::~DatabaseManager()
}
std::shared_ptr<DatabaseManager> DatabaseManager::Create()
std::unique_ptr<DatabaseManager> DatabaseManager::Create()
{
return std::make_shared<DatabaseManager>();
return std::make_unique<DatabaseManager>();
}

View file

@ -17,11 +17,11 @@ public:
~DatabaseManager();
static std::shared_ptr<DatabaseManager> Create();
static std::unique_ptr<DatabaseManager> Create();
void CreateDatabase(const std::string& path);
void OnShutDown();
};
using DatabaseManagerPtr = std::shared_ptr<DatabaseManager>;
using DatabaseManagerPtr = std::unique_ptr<DatabaseManager>;

View file

@ -13,12 +13,12 @@ SqliteInterface::~SqliteInterface()
}
std::shared_ptr<SqliteInterface> SqliteInterface::Create()
std::unique_ptr<SqliteInterface> SqliteInterface::Create()
{
return std::make_shared<SqliteInterface>();
return std::make_unique<SqliteInterface>();
}
void SqliteInterface::Open(DatabasePtr db)
void SqliteInterface::Open(const DatabasePtr& db)
{
int rc = sqlite3_open(db->GetPath().c_str(), &mSqliteDb);
if( rc )
@ -43,7 +43,7 @@ void SqliteInterface::Run(const std::string& statement)
{
char *zErrMsg = 0;
int rc = sqlite3_exec(mSqliteDb, statement.c_str(), callback, 0, &zErrMsg);
if( rc!=SQLITE_OK )
if (rc != SQLITE_OK)
{
std::cout << "SQL error: %s\n" << zErrMsg << std::endl;
sqlite3_free(zErrMsg);

View file

@ -14,13 +14,13 @@ public:
~SqliteInterface();
static std::shared_ptr<SqliteInterface> Create();
static std::unique_ptr<SqliteInterface> Create();
void Open(DatabasePtr db);
void Open(const DatabasePtr& db);
void Close();
void Run(const std::string& statement);
};
using SqliteInterfacePtr = std::shared_ptr<SqliteInterface>;
using SqliteInterfacePtr = std::unique_ptr<SqliteInterface>;

View file

@ -10,10 +10,9 @@ list(APPEND network_HEADERS
list(APPEND network_LIB_INCLUDES
NetworkManager.cpp
sockets/Socket.cpp
sockets/SocketInterface.cpp
)
add_library(network SHARED ${network_LIB_INCLUDES} ${network_HEADERS})
add_library(network SHARED ${network_LIB_INCLUDES} ${linux_INCLUDES} ${network_HEADERS})
target_include_directories(network PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"

View file

@ -1,4 +1,7 @@
#include "NetworkManager.h"
#ifdef __linux__
#include "UnixSocketInterface.h"
#endif
NetworkManager::NetworkManager()
: mActiveSockets(),
@ -19,13 +22,18 @@ std::unique_ptr<NetworkManager> NetworkManager::Create()
void NetworkManager::Initialize()
{
mSocketInterface = SocketInterface::Create();
mSocketInterface = UnixSocketInterface::Create();
}
void NetworkManager::RunHttpServer()
{
if (!mSocketInterface)
{
Initialize();
}
auto socket = Socket::Create();
mSocketInterface->CreateSocket(socket);
mSocketInterface->InitializeSocket(socket);
mSocketInterface->Listen(socket);
mSocketInterface->Run(socket);
}

View file

@ -1,15 +1,15 @@
#pragma once
#include <memory>
#include <vector>
#include "Socket.h"
#include "SocketInterface.h"
#include <memory>
#include <vector>
class NetworkManager
{
std::vector<SocketPtr> mActiveSockets;
SocketInterfaceUPtr mSocketInterface;
ISocketInterfaceUPtr mSocketInterface;
public:
@ -21,10 +21,6 @@ public:
void Initialize();
void OpenSocket(SocketPtr socket);
void CloseSocket(SocketPtr socket);
void RunHttpServer();
void ShutDown();

View file

@ -2,7 +2,8 @@
Socket::Socket()
: mPort(8888),
mMessage()
mMessage(),
mHandle(-1)
{
}
@ -12,12 +13,22 @@ Socket::~Socket()
}
std::shared_ptr<Socket> Socket::Create()
void Socket::SetHandle(SocketHandle handle)
{
return std::make_shared<Socket>();
mHandle = handle;
}
std::string Socket::GetMessage()
Socket::SocketHandle Socket::GetHandle() const
{
return mHandle;
}
std::unique_ptr<Socket> Socket::Create()
{
return std::make_unique<Socket>();
}
std::string Socket::GetMessage() const
{
return mMessage;
}
@ -32,7 +43,7 @@ void Socket::SetPort(unsigned port)
mPort = port;
}
unsigned Socket::GetPort()
unsigned Socket::GetPort() const
{
return mPort;
}

View file

@ -5,24 +5,32 @@
class Socket
{
unsigned mPort;
std::string mMessage;
using SocketHandle = int;
public:
Socket();
~Socket();
static std::shared_ptr<Socket> Create();
static std::unique_ptr<Socket> Create();
void SetPort(unsigned port);
unsigned GetPort();
void SetHandle(SocketHandle handle);
std::string GetMessage();
SocketHandle GetHandle() const;
unsigned GetPort() const;
std::string GetMessage() const;
void SetMessage(const std::string& message);
private:
SocketHandle mHandle;
unsigned mPort;
std::string mMessage;
};
using SocketPtr = std::shared_ptr<Socket>;
using SocketPtr = std::unique_ptr<Socket>;

View file

@ -1,27 +0,0 @@
#include "SocketInterface.h"
#include "Socket.h"
SocketInterface::SocketInterface()
{
}
std::unique_ptr<SocketInterface> SocketInterface::Create()
{
return std::make_unique<SocketInterface>();
}
void SocketInterface::CreateSocket(SocketPtr socket)
{
}
void SocketInterface::Listen(SocketPtr socket)
{
}
void SocketInterface::Run(SocketPtr socket)
{
}

View file

@ -1,20 +1,21 @@
#pragma
#pragma once
#include <memory>
class Socket;
using SocketPtr = std::shared_ptr<Socket>;
using SocketPtr = std::unique_ptr<Socket>;
class SocketInterface
class ISocketInterface
{
public:
SocketInterface();
ISocketInterface() = default;
static std::unique_ptr<SocketInterface> Create();
void CreateSocket(SocketPtr socket);
void Listen(SocketPtr socket);
void Run(SocketPtr socket);
virtual ~ISocketInterface() = default;
virtual void InitializeSocket(const SocketPtr& socket) = 0;
virtual void Listen(const SocketPtr& socket) = 0;
virtual void Run(const SocketPtr& socket) = 0;
};
using SocketInterfaceUPtr = std::unique_ptr<SocketInterface>;
using ISocketInterfaceUPtr = std::unique_ptr<ISocketInterface>;

View file

@ -1,38 +1,34 @@
#include "UnixSocketInterface.h"
#include "HttpResponse.h"
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "HttpResponse.h"
UnixSocketInterface::UnixSocketInterface()
: mOpeningHandles(),
mBufferSize(1024)
: mBufferSize(1024)
{
}
UnixSocketInterface::~UnixSocketInterface()
std::unique_ptr<UnixSocketInterface> UnixSocketInterface::Create()
{
return std::make_unique<UnixSocketInterface>();
}
std::shared_ptr<UnixSocketInterface> UnixSocketInterface::Create()
void UnixSocketInterface::InitializeSocket(const SocketPtr& socketPtr)
{
return std::make_shared<UnixSocketInterface>();
auto handle = socket(AF_INET, SOCK_STREAM, 0);
socketPtr->SetHandle(handle);
}
void UnixSocketInterface::CreateSocket(SocketPtr socketPtr)
void UnixSocketInterface::Listen(const SocketPtr& socket)
{
mOpeningHandles[socketPtr] = socket(AF_INET, SOCK_STREAM, 0);
}
void UnixSocketInterface::Listen(SocketPtr socket)
{
if(mOpeningHandles[socket] < 0)
if(socket->GetHandle() < 0)
{
std::cerr << "Error opening socket" << std::endl;
return;
@ -45,19 +41,19 @@ void UnixSocketInterface::Listen(SocketPtr socket)
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
int result = bind(mOpeningHandles[socket], (struct sockaddr *)&serv_addr, sizeof(serv_addr));
int result = bind(socket->GetHandle(), (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if(result< 0)
{
std::cerr << "Error binding socket" << std::endl;
return;
}
listen(mOpeningHandles[socket], 5);
listen(socket->GetHandle(), 5);
}
void UnixSocketInterface::Run(SocketPtr socket)
void UnixSocketInterface::Run(const SocketPtr& socket)
{
if(mOpeningHandles[socket] < 0)
if(socket->GetHandle() < 0)
{
std::cerr << "Error opening socket" << std::endl;
return;
@ -67,7 +63,7 @@ void UnixSocketInterface::Run(SocketPtr socket)
socklen_t clilen = sizeof(cli_addr);
while(true)
{
SocketHandle new_socket_handle = accept(mOpeningHandles[socket],
auto new_socket_handle = accept(socket->GetHandle(),
(struct sockaddr *) &cli_addr, &clilen);
if (new_socket_handle < 0)
{
@ -88,7 +84,7 @@ void UnixSocketInterface::Run(SocketPtr socket)
HttpResponse response;
response.SetBody("Hello world!");
std::string response_message = response.ToString();
auto response_message = response.ToString();
n = write(new_socket_handle, response_message.c_str(), response_message.length());
if (n < 0)
{

View file

@ -1,30 +1,31 @@
#pragma once
#include "Socket.h"
#include "SocketInterface.h"
#include <memory>
#include <map>
#include <cstddef>
#include "Socket.h"
class UnixSocketInterface
class UnixSocketInterface : public ISocketInterface
{
using SocketHandle = int;
std::map<SocketPtr, SocketHandle> mOpeningHandles;
std::size_t mBufferSize;
public:
UnixSocketInterface();
~UnixSocketInterface();
virtual ~UnixSocketInterface() = default;
static std::shared_ptr<UnixSocketInterface> Create();
static std::unique_ptr<UnixSocketInterface> Create();
void CreateSocket(SocketPtr socket);
void InitializeSocket(const SocketPtr& socket) override;
void Listen(SocketPtr socket);
void Listen(const SocketPtr& socket) override;
void Run(SocketPtr socket);
void Run(const SocketPtr& socket) override;
private:
std::size_t mBufferSize { 0 };
};
using UnixSocketInterfacePtr = std::shared_ptr<UnixSocketInterface>;
using UnixSocketInterfacePtr = std::unique_ptr<UnixSocketInterface>;