Skip to content

Commit

Permalink
Add applications.
Browse files Browse the repository at this point in the history
  • Loading branch information
eeyribas committed Dec 8, 2024
1 parent 3b20175 commit 588d75d
Show file tree
Hide file tree
Showing 16 changed files with 932 additions and 0 deletions.
8 changes: 8 additions & 0 deletions GetDiskUsage/GetDiskUsage.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
QT -= gui

CONFIG -= app_bundle

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
main.cpp
323 changes: 323 additions & 0 deletions GetDiskUsage/GetDiskUsage.pro.user

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions GetDiskUsage/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <QCoreApplication>
#include <QDebug>
#include <stdint.h>
#include <string>
#include <stdexcept>
#include <sys/statvfs.h>
#include <errno.h>

uint64_t GetDiskUsage(const char* path)
{
struct statvfs info = {};
if (statvfs(path, &info)) {
int err_num = errno;
qDebug() << "Error in statvfs : " << strerror(err_num);

return 0;
}

uint64_t available = info.f_bavail * info.f_frsize / 1000000000;

return available;
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Available Disk Memory : " << GetDiskUsage("/home");

return a.exec();
}
19 changes: 19 additions & 0 deletions PolymorphismBasedApp/PolymorphismBasedApp.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
QT -= gui

CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
main.cpp \
datapacket.cpp \
base.cpp \
derived1.cpp \
derived2.cpp

HEADERS += \
parameters.h \
customqueue.h \
datapacket.h \
base.h \
derived1.h \
derived2.h
323 changes: 323 additions & 0 deletions PolymorphismBasedApp/PolymorphismBasedApp.pro.user

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions PolymorphismBasedApp/base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "base.h"

void Base::Method1(int value)
{
std::cout << "Base : Method-1 = " << value << std::endl;
}

void Base::Method2(bool value)
{
std::cout << "Base : Method-2 = " << value << std::endl;
}
13 changes: 13 additions & 0 deletions PolymorphismBasedApp/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef BASE_H
#define BASE_H

#include <iostream>

class Base
{
public:
virtual void Method1(int value);
virtual void Method2(bool value);
};

#endif // BASE_H
73 changes: 73 additions & 0 deletions PolymorphismBasedApp/customqueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef CUSTOMQUEUE_H
#define CUSTOMQUEUE_H

#include <QByteArray>
#include <QSemaphore>
#include <QMutex>
#include <QQueue>

template <class T, int SIZE = 1024>
class CustomQueue
{
public:
CustomQueue(int size = SIZE);
void putQueue(const T &item);
T getQueue();
T peek();
bool isEmpty() const { return m_queue.count() == 0; }
int count() const { return m_queue.count(); }
void clear() { m_queue.clear(); }

private:
QQueue<T> m_queue;
QSemaphore m_semProducer;
QSemaphore m_semConsumer;
QMutex m_mutex;
int m_size;
};

template <class T, int SIZE>
CustomQueue<T, SIZE>::CustomQueue(int size)
: m_semProducer(size), m_semConsumer(0), m_size(size)
{
}

template <class T, int SIZE>
void CustomQueue<T, SIZE>::putQueue(const T &item)
{
m_semProducer.acquire();
m_mutex.lock();
m_queue.enqueue(item);
m_mutex.unlock();
m_semConsumer.release();
}

template <class T, int SIZE>
T CustomQueue<T, SIZE>::getQueue()
{
T item;

m_semConsumer.acquire();
m_mutex.lock();
item = m_queue.dequeue();
m_mutex.unlock();
m_semProducer.release();

return item;
}

template <class T, int SIZE>
T CustomQueue<T, SIZE>::peek()
{
T item;

m_semConsumer.acquire();
m_mutex.lock();
item = m_queue.head();
m_mutex.unlock();
m_semProducer.release();

return item;
}

#endif // CUSTOMQUEUE_H
21 changes: 21 additions & 0 deletions PolymorphismBasedApp/datapacket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "datapacket.h"

void DataPacket::SetValue1(int value)
{
value_1 = value;
}

int DataPacket::GetValue1()
{
return value_1;
}

void DataPacket::SetValue2(int value)
{
value_2 = value;
}

int DataPacket::GetValue2()
{
return value_2;
}
19 changes: 19 additions & 0 deletions PolymorphismBasedApp/datapacket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef DATAPACKET_H
#define DATAPACKET_H

#include "customqueue.h"

class DataPacket
{
public:
void SetValue1(int value);
int GetValue1();
void SetValue2(int value);
int GetValue2();

private:
int value_1;
int value_2;
};

#endif // DATAPACKET_H
6 changes: 6 additions & 0 deletions PolymorphismBasedApp/derived1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "derived1.h"

void Derived1::Method1(int value)
{
std::cout << "Derived-1 : Method-1 = " << value << std::endl;
}
13 changes: 13 additions & 0 deletions PolymorphismBasedApp/derived1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DERIVED1_H
#define DERIVED1_H

#include <iostream>
#include "base.h"

class Derived1 : public Base
{
public:
void Method1(int value);
};

#endif // DERIVED1_H
7 changes: 7 additions & 0 deletions PolymorphismBasedApp/derived2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "derived2.h"

void Derived2::Method2(bool value)
{
std::cout << "Derived-2 : Method-2 = " << value << std::endl;
}

13 changes: 13 additions & 0 deletions PolymorphismBasedApp/derived2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DERIVED2_H
#define DERIVED2_H

#include <iostream>
#include "base.h"

class Derived2 : public Base
{
public:
void Method2(bool value);
};

#endif // DERIVED2_H
37 changes: 37 additions & 0 deletions PolymorphismBasedApp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <QCoreApplication>
#include "base.h"
#include "derived1.h"
#include "derived2.h"
#include "parameters.h"
#include "datapacket.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

Base *base;
if (Parameters::type == 0) {
Derived1 derived_1;
base = &derived_1;
base->Method1(5);
base->Method2(true);
} else if (Parameters::type == 1) {
Derived2 derived_2;
base = &derived_2;
base->Method1(3);
base->Method2(false);
}

Parameters::data_packet.SetValue1(9);
std::cout << "Value 1 = " << Parameters::data_packet.GetValue1() << std::endl;
Parameters::data_packet.SetValue2(6);
std::cout << "Value 2 = " << Parameters::data_packet.GetValue2() << std::endl;

DataPacket data_packet;
data_packet.SetValue1(7);
data_packet.SetValue2(8);
Parameters::custom_queue.putQueue(data_packet);
std::cout << "Queue Size = " << Parameters::custom_queue.count() << std::endl;

return a.exec();
}
16 changes: 16 additions & 0 deletions PolymorphismBasedApp/parameters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PARAMETERS_H
#define PARAMETERS_H

#include <iostream>
#include "datapacket.h"
#include "customqueue.h"

class Parameters
{
public:
inline static int type = 1;
inline static DataPacket data_packet;
inline static CustomQueue<DataPacket> custom_queue;
};

#endif // PARAMETERS_H

0 comments on commit 588d75d

Please sign in to comment.