Compare commits
3 Commits
a021e7a309
...
87fb195202
Author | SHA1 | Date | |
---|---|---|---|
87fb195202 | |||
dd29156c85 | |||
eef74e2897 |
@ -3,9 +3,9 @@
|
||||
#include <luna/Result.h>
|
||||
#include <luna/SafeArithmetic.h>
|
||||
|
||||
template <typename T> Result<T*> make()
|
||||
template <typename T, class... Args> Result<T*> make(Args... args)
|
||||
{
|
||||
T* const result = new T;
|
||||
T* const result = new T(args...);
|
||||
if (!result) return err(ENOMEM);
|
||||
return result;
|
||||
}
|
||||
|
118
luna/include/luna/LinkedList.h
Normal file
118
luna/include/luna/LinkedList.h
Normal file
@ -0,0 +1,118 @@
|
||||
#pragma once
|
||||
#include <luna/Result.h>
|
||||
|
||||
template <typename T> inline Result<T*> nonnull_or_error(T* ptr)
|
||||
{
|
||||
return ptr == nullptr ? err(ENONE) : ptr;
|
||||
}
|
||||
|
||||
template <typename T> class DoublyLinkedList;
|
||||
|
||||
template <typename T> class DoublyLinkedListNode
|
||||
{
|
||||
|
||||
private:
|
||||
DoublyLinkedListNode<T>* m_next_node;
|
||||
DoublyLinkedListNode<T>* m_last_node;
|
||||
|
||||
void detach_from_list()
|
||||
{
|
||||
m_next_node->m_last_node = m_last_node;
|
||||
m_last_node->m_next_node = m_next_node;
|
||||
}
|
||||
|
||||
void add_to_list(DoublyLinkedListNode<T>* end_node)
|
||||
{
|
||||
end_node->m_next_node = this;
|
||||
this->m_last_node = end_node;
|
||||
}
|
||||
|
||||
friend class DoublyLinkedList<T>;
|
||||
};
|
||||
|
||||
template <typename T> class DoublyLinkedList
|
||||
{
|
||||
public:
|
||||
void append(T* ptr)
|
||||
{
|
||||
DoublyLinkedListNode<T>* node = (DoublyLinkedListNode<T>*)ptr;
|
||||
if (!m_start_node) m_start_node = node;
|
||||
if (m_end_node) node->add_to_list(m_end_node);
|
||||
m_end_node = node;
|
||||
|
||||
m_count++;
|
||||
}
|
||||
|
||||
T* remove(T* ptr)
|
||||
{
|
||||
DoublyLinkedListNode<T>* node = (DoublyLinkedListNode<T>*)ptr;
|
||||
|
||||
if (node == m_end_node) m_end_node = node->m_last_node;
|
||||
if (node == m_start_node) m_start_node = node->m_next_node;
|
||||
|
||||
node->detach_from_list();
|
||||
|
||||
m_count--;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
Result<T*> first()
|
||||
{
|
||||
return nonnull_or_error((T*)m_start_node);
|
||||
}
|
||||
|
||||
Result<T*> last()
|
||||
{
|
||||
return nonnull_or_error((T*)m_end_node);
|
||||
}
|
||||
|
||||
Result<T*> next(T* item)
|
||||
{
|
||||
return nonnull_or_error(((DoublyLinkedListNode<T>*)item)->m_next_node);
|
||||
}
|
||||
|
||||
Result<T*> previous(T* item)
|
||||
{
|
||||
return nonnull_or_error(((DoublyLinkedListNode<T>*)item)->m_last_node);
|
||||
}
|
||||
|
||||
template <typename Callback> void for_each(Callback callback)
|
||||
{
|
||||
for (DoublyLinkedListNode<T>* node = m_start_node; node; node = node->m_next_node) { callback((T*)node); }
|
||||
}
|
||||
|
||||
template <typename Callback> void for_each_reversed(Callback callback)
|
||||
{
|
||||
for (DoublyLinkedListNode<T>* node = m_end_node; node; node = node->m_last_node) { callback((T*)node); }
|
||||
}
|
||||
|
||||
template <typename Callback> void for_each_after(T* start, Callback callback)
|
||||
{
|
||||
for (DoublyLinkedListNode<T>* node = ((DoublyLinkedListNode<T>*)start)->m_next_node; node;
|
||||
node = node->m_next_node)
|
||||
{
|
||||
callback((T*)node);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Callback> void for_each_before(T* end, Callback callback)
|
||||
{
|
||||
for (DoublyLinkedListNode<T>* node = ((DoublyLinkedListNode<T>*)end)->m_last_node; node;
|
||||
node = node->m_last_node)
|
||||
{
|
||||
callback((T*)node);
|
||||
}
|
||||
}
|
||||
|
||||
usize count()
|
||||
{
|
||||
return m_count;
|
||||
}
|
||||
|
||||
private:
|
||||
DoublyLinkedListNode<T>* m_start_node = nullptr;
|
||||
DoublyLinkedListNode<T>* m_end_node = nullptr;
|
||||
|
||||
usize m_count = 0;
|
||||
};
|
@ -52,7 +52,8 @@
|
||||
#define ETIMEDOUT 110 // Connection timed out
|
||||
#define EALREADY 114 // Operation already in progress
|
||||
|
||||
// This one is Luna-specific.
|
||||
// These ones are Luna-specific.
|
||||
#define EFIXME 342 // Functionality not yet implemented
|
||||
#define ENONE 343 // Internal or insignificant error
|
||||
|
||||
const char* error_string(int error);
|
@ -55,6 +55,7 @@ const char* error_string(int error)
|
||||
case ETIMEDOUT: return "Connection timed out";
|
||||
case EALREADY: return "Operation already in progress";
|
||||
case EFIXME: return "Functionality not yet implemented";
|
||||
case ENONE: return "Internal or insignificant error";
|
||||
default: return "Unknown error";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user