Compare commits
No commits in common. "87fb19520268842b09caa5094dca441ffa488769" and "a021e7a309a0c22538eff58730e8a8c816e5c84e" have entirely different histories.
87fb195202
...
a021e7a309
@ -3,9 +3,9 @@
|
|||||||
#include <luna/Result.h>
|
#include <luna/Result.h>
|
||||||
#include <luna/SafeArithmetic.h>
|
#include <luna/SafeArithmetic.h>
|
||||||
|
|
||||||
template <typename T, class... Args> Result<T*> make(Args... args)
|
template <typename T> Result<T*> make()
|
||||||
{
|
{
|
||||||
T* const result = new T(args...);
|
T* const result = new T;
|
||||||
if (!result) return err(ENOMEM);
|
if (!result) return err(ENOMEM);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1,118 +0,0 @@
|
|||||||
#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,8 +52,7 @@
|
|||||||
#define ETIMEDOUT 110 // Connection timed out
|
#define ETIMEDOUT 110 // Connection timed out
|
||||||
#define EALREADY 114 // Operation already in progress
|
#define EALREADY 114 // Operation already in progress
|
||||||
|
|
||||||
// These ones are Luna-specific.
|
// This one is Luna-specific.
|
||||||
#define EFIXME 342 // Functionality not yet implemented
|
#define EFIXME 342 // Functionality not yet implemented
|
||||||
#define ENONE 343 // Internal or insignificant error
|
|
||||||
|
|
||||||
const char* error_string(int error);
|
const char* error_string(int error);
|
@ -55,7 +55,6 @@ const char* error_string(int error)
|
|||||||
case ETIMEDOUT: return "Connection timed out";
|
case ETIMEDOUT: return "Connection timed out";
|
||||||
case EALREADY: return "Operation already in progress";
|
case EALREADY: return "Operation already in progress";
|
||||||
case EFIXME: return "Functionality not yet implemented";
|
case EFIXME: return "Functionality not yet implemented";
|
||||||
case ENONE: return "Internal or insignificant error";
|
|
||||||
default: return "Unknown error";
|
default: return "Unknown error";
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user