Skip to content

Commit

Permalink
removed some unnecessary getters and added more comments
Browse files Browse the repository at this point in the history
Took 1 hour 37 minutes
  • Loading branch information
kagurazaka-ayano committed Jan 31, 2024
1 parent 40f5cf8 commit 8749a8b
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 10 deletions.
8 changes: 8 additions & 0 deletions include/KawaiiMQ/Consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ namespace KawaiiMQ {
*/
std::vector<std::shared_ptr<MessageData>> fetchSingleTopic(const Topic& topic);

/**
* get the name of the consumer
* @return name of the consumer
*/
std::string getName() const;

/**
* get all subscribed topics
* @return all subscribed topic, in a vector
*/
std::vector<Topic> getSubscribedTopics() const;

private:
Expand Down
9 changes: 9 additions & 0 deletions include/KawaiiMQ/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace KawaiiMQ {

/**
* queue related exceptions
*/
class QueueException : public std::exception {
public:
explicit QueueException(const std::string& message);
Expand All @@ -21,6 +24,9 @@ namespace KawaiiMQ {
std::string message;
};

/**
* topic related exceptions
*/
class TopicException : public std::exception {
public:
explicit TopicException(const std::string& message);
Expand All @@ -29,6 +35,9 @@ namespace KawaiiMQ {
std::string message;
};

/**
* type related exceptions
*/
class TypeException : public std::exception {
public:
explicit TypeException(const std::string& message);
Expand Down
28 changes: 27 additions & 1 deletion include/KawaiiMQ/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,29 @@
#include "Exceptions.h"

namespace KawaiiMQ {

/**
* base class of message
*/
class MessageData {
public:
MessageData() = default;
virtual ~MessageData() = default;
};

/**
* generic message wrapper
* @tparam T type of message content
*/
template<typename T>
requires std::is_convertible_v<T, std::byte>
class Message : public MessageData {
public:
explicit Message(T content) : content(std::move(content)) {}

/**
* set content of the message
* @param content content
*/
void setContent(T content) {
this->content = std::move(content);
}
Expand All @@ -35,7 +46,15 @@ namespace KawaiiMQ {
T content;
};

/**
* Get the message content from a message
* @tparam T type of message you are expecting
* @param in shared ptr of message
* @return message content
* @exception TypeException Will throw if the type of the message is not the same as the type you are expecting
*/
template<typename T>
requires std::is_convertible_v<T, std::byte>
T getMessage(std::shared_ptr<MessageData> in) {
std::shared_ptr<Message<T>> tmp = std::dynamic_pointer_cast<Message<T>>(in);
if (tmp != nullptr) {
Expand All @@ -45,7 +64,14 @@ namespace KawaiiMQ {
"Expected type " + std::string(typeid(T).name()) + ", got " + std::string(typeid(in).name()));
}

/**
* construct a message shared ptr
* @tparam T type of content
* @param content message content
* @return message shared ptr
*/
template<typename T>
requires std::is_convertible_v<T, std::byte>
std::shared_ptr<Message<T>> makeMessage(T content) {
auto msg = std::make_shared<Message<T>>(content);
return msg;
Expand Down
12 changes: 9 additions & 3 deletions include/KawaiiMQ/Producer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ namespace KawaiiMQ {
}
}

std::string getName() const {
return name;
}
/**
* get the name of the producer
* @return name of the producer
*/
std::string getName() const;

/**
* get all subscribed topics
* @return all subscribed topic, in a vector
*/
std::vector<Topic> getSubscribedTopics() const;
private:
std::vector<Topic> subscribed;
Expand Down
21 changes: 20 additions & 1 deletion include/KawaiiMQ/Queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,36 @@ namespace KawaiiMQ {
*/
void setTimeout(int timeout_ms) noexcept;

/**
* Get timeout for wait()
* @return timeout in milliseconds
*/
int getTimeout() const noexcept;

/**
* Set the timeout time of the safely unrelate queue
* @param timeout_ms timeout time in milliseconds
*/
void setSafeTimeout(int timeout_ms) noexcept;

/**
* Get the timeout time of the safely unrelate queue
* @return timeout time in milliseconds
*/
int getSafeTimeout() const noexcept;

/**
* Get the condition variable for safe timeout
* @return condition variable for safe timeout
*/
std::condition_variable_any& getSafeCond() noexcept;

/**
* Get the name of the queue
* @return name of the queue
*/
std::string getName() const;

std::queue<std::shared_ptr<MessageData>> getElements() const;

private:
static std::shared_mutex mtx;
Expand Down
6 changes: 4 additions & 2 deletions include/KawaiiMQ/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@

namespace KawaiiMQ{

/**
* A concept that checks if a type is a message
* @tparam C
*/
template <class C>
concept MessageType = requires(C c) {
// IILE, that only binds to A<...> specialisations
// Including classes derived from them
[]<typename X>(Message<X>&){}(c);
};

Expand Down
4 changes: 4 additions & 0 deletions src/Producer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ namespace KawaiiMQ {
std::vector<Topic> Producer::getSubscribedTopics() const {
return subscribed;
}

std::string Producer::getName() const {
return name;
}
}
3 changes: 0 additions & 3 deletions src/Queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,5 @@ namespace KawaiiMQ {
return name;
}

std::queue<std::shared_ptr<MessageData>> Queue::getElements() const {
return queue;
}

}

0 comments on commit 8749a8b

Please sign in to comment.