Usermode/libc++ - Starting on a hacky STL implementation
authorJohn Hodge <[email protected]>
Sun, 18 May 2014 14:01:17 +0000 (22:01 +0800)
committerJohn Hodge <[email protected]>
Sun, 18 May 2014 14:01:17 +0000 (22:01 +0800)
- String is the most complete, list is hack central

Usermode/Libraries/libc++.so_src/include_exp/algorithm [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/allocator [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/cstddef [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/cstdint [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/exception [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/list [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/memory [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/stdexcept [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/string [new file with mode: 0644]
Usermode/Libraries/libc++.so_src/include_exp/vector [new file with mode: 0644]

diff --git a/Usermode/Libraries/libc++.so_src/include_exp/algorithm b/Usermode/Libraries/libc++.so_src/include_exp/algorithm
new file mode 100644 (file)
index 0000000..b02f5b0
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * string (header)
+ * - C++'s String type
+ */
+#ifndef _LIBCXX_ALGORITHM_
+#define _LIBCXX_ALGORITHM_
+
+namespace std {
+
+// Maximum
+template <class T>
+const T& max(const T& a, const T& b)
+{
+       return (a<b) ? b : a;
+}
+
+template <class T, class Compare>
+const T& max(const T& a, const T& b, Compare comp)
+{
+       return comp(a, b) ? b : a;
+}
+
+template <class T>
+const T& min(const T& a, const T& b)
+{
+       return (a<b) ? a : b;
+}
+
+template <class T, class Compare>
+const T& min(const T& a, const T& b, Compare comp)
+{
+       return comp(a, b) ? a : b;
+}
+
+};     // namespace std
+
+#endif
+
+// vim: ft=cpp
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/allocator b/Usermode/Libraries/libc++.so_src/include_exp/allocator
new file mode 100644 (file)
index 0000000..0bb12ea
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * string (header)
+ * - C++'s String type
+ */
+#ifndef _LIBCXX_ALLOCATOR_
+#define _LIBCXX_ALLOCATOR_
+
+#include "cstddef"
+
+namespace std {
+
+template <class T> class allocator
+{
+public:
+       typedef T       value_type;
+       typedef T*      pointer;
+       typedef T&      reference;
+       typedef const T* const_pointer;
+       typedef const T&        const_reference;
+       typedef size_t  size_type;
+       typedef ptrdiff_t       difference_type;
+       
+       template <class Type>
+       struct rebind
+       {
+               typedef allocator<Type> other;
+       };
+       
+       allocator() throw() {
+       }
+       allocator(const allocator& alloc) throw() {
+       }
+       template <class U>
+       allocator(const allocator<U>& alloc) throw() {
+       }
+       ~allocator() throw() {
+       }
+       
+       pointer address(reference x) const {
+               return &x;
+       }
+       const_pointer address(const_reference x) const {
+               return &x;
+       }
+       pointer allocate(size_type n, const void* hint=0) {
+               ::operator new (n * sizeof(value_type));
+       }
+       void deallocate(pointer p, size_type n) {
+               ::operator delete(p);
+       }
+};
+
+};
+
+#endif
+
+// vim: ft=cpp
+
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/cstddef b/Usermode/Libraries/libc++.so_src/include_exp/cstddef
new file mode 100644 (file)
index 0000000..b7b0c13
--- /dev/null
@@ -0,0 +1,15 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * cstddef (header)
+ * - C++ wrapper around stddef.h
+ */
+#ifndef _LIBCXX_CSTDDEF_
+#define _LIBCXX_CSTDDEF_
+
+extern "C" {
+#include <stddef.h>
+};
+
+#endif
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/cstdint b/Usermode/Libraries/libc++.so_src/include_exp/cstdint
new file mode 100644 (file)
index 0000000..c5575d9
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * cstdint (header)
+ * - C++ wrapper around stdint.h
+ */
+#ifndef _LIBCXX_CSTDINT_
+#define _LIBCXX_CSTDINT_
+
+extern "C" {
+#include <stdint.h>
+};
+
+#endif
+
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/exception b/Usermode/Libraries/libc++.so_src/include_exp/exception
new file mode 100644 (file)
index 0000000..6edf82b
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * exception (header)
+ * - C++'s base exception type
+ */
+#ifndef _LIBCXX_EXCEPTION_
+#define _LIBCXX_EXCEPTION_
+
+#define noexcept       throw()
+
+namespace std {
+
+class exception
+{
+public:
+       exception() noexcept;
+       exception(const exception& e) noexcept;
+       exception& operator= (const exception& e) noexcept;
+       virtual ~exception() noexcept;
+       virtual const char* what() const noexcept;
+};
+
+};     // namespace std
+
+#endif
+// vim: ft=cpp
+
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/list b/Usermode/Libraries/libc++.so_src/include_exp/list
new file mode 100644 (file)
index 0000000..602d2ae
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * list (header)
+ * - List container
+ */
+#ifndef _LIBCXX_LIST_
+#define _LIBCXX_LIST_
+
+#include <cstddef>
+
+namespace std {
+
+namespace _bits {
+template <class T> class list_iterator;
+template <class T> class list_item;
+}
+
+template <class T>
+class list
+{
+       friend class ::std::_bits::list_iterator<T>;
+       
+       typedef ::std::_bits::list_item<T>      item_type;
+       item_type       *m_start;
+       item_type       *m_end;
+public:
+       typedef T value_type;
+       typedef value_type& reference;
+       typedef const value_type& const_reference;
+       typedef _bits::list_iterator<value_type>        iterator;
+       typedef _bits::list_iterator<const value_type>  const_iterator;
+       
+       list():
+               m_start(0), m_end(0)
+       {
+       }
+       list(size_t n, const value_type& val = value_type());
+       list(const list& x);
+       ~list() {
+               clear();
+       }
+       
+       list& operator =(const list& x);
+       
+       iterator begin() {
+               return iterator(m_start);
+       }
+       const_iterator begin() const {
+               return const_iterator(m_start);
+       }
+
+       iterator end() {
+               return iterator(0);
+       }
+       const_iterator end() const {
+               return const_iterator(0);
+       }
+       
+       bool empty() const {
+               return !m_start;
+       }
+       size_t size() const;
+       size_t max_size() const;
+       
+       T& front();
+       const T& front() const;
+       T& back();
+       const T& back() const;
+       
+       void clear() {
+               while( m_start ) {
+                       item_type* item = m_start;
+                       m_start = m_start->next;
+                       delete item;
+               }
+       }
+};
+
+
+namespace _bits {
+
+template <class T>
+struct list_item
+{
+       list_item<T>    *next;
+       list_item<T>    *prev;
+       T       value;
+};
+
+template <class T>
+class list_iterator//:
+       //public bidirectional_iterator_tag;
+{
+       friend class list<T>;
+
+       list_item<T>    *cur;
+public:
+       bool operator == (const list_iterator& other) const {
+               return cur == other.cur;
+       }
+       bool operator != (const list_iterator& other) const {
+               return cur != other.cur;
+       }
+       
+       T& operator * () {
+               return cur->value;
+       }
+       T& operator -> () {
+               return cur->value;
+       }
+       list_iterator& operator ++ () {
+               cur = cur->next;
+       }
+       list_iterator& operator -- () {
+               cur = cur->prev;
+       }
+
+private:
+       list_iterator(list_item<T> *item):
+               cur(item)
+       {
+       }
+};
+
+};     // namespace _bits
+
+};     // namespace std
+
+#endif
+
+// vim: ft=cpp
+
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/memory b/Usermode/Libraries/libc++.so_src/include_exp/memory
new file mode 100644 (file)
index 0000000..b604fbe
--- /dev/null
@@ -0,0 +1,20 @@
+
+#ifndef _LIBCXX_MEMORY_
+#define _LIBCXX_MEMORY_
+
+namespace std {
+
+template <>
+class allocator<void>
+{
+public:
+  typedef void* pointer;
+  typedef const void* const_pointer;
+  typedef void value_type;
+  template <class U> struct rebind { typedef allocator<U> other; };
+};
+
+}
+
+#endif
+
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/stdexcept b/Usermode/Libraries/libc++.so_src/include_exp/stdexcept
new file mode 100644 (file)
index 0000000..f9e3e3e
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * string (header)
+ * - C++'s String type
+ */
+#ifndef _LIBCXX_STDEXCEPT_
+#define _LIBCXX_STDEXCEPT_
+
+#include "exception"
+
+namespace std {
+
+class logic_error:
+       public exception
+{
+public:
+       explicit logic_error(const basic_string<char>& what_arg);
+};
+
+class runtime_error:
+       public exception
+{
+public:
+       explicit runtime_error(const basic_string<char>& what_arg);
+};
+
+class out_of_range:
+       public logic_error
+{
+public:
+       explicit out_of_range(const string& what_arg);
+};
+
+}      // namespace std
+
+#endif
+
+// vim: ft=cpp
+
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/string b/Usermode/Libraries/libc++.so_src/include_exp/string
new file mode 100644 (file)
index 0000000..7326217
--- /dev/null
@@ -0,0 +1,219 @@
+/*
+ * Acess2 C++ Library
+ * - By John Hodge (thePowersGang)
+ *
+ * string (header)
+ * - C++'s String type
+ */
+#ifndef _LIBCXX_STRING_
+#define _LIBCXX_STRING_
+
+#include <allocator>
+
+namespace std {
+
+template <class charT>
+struct char_traits
+{
+};
+
+template <>
+struct char_traits<char>
+{
+       typedef char    char_type;
+       typedef int     int_type;
+       //typedef streamoff     off_type;
+       //typedef streampos     pos_type;
+       //typedef mbstate_t     state_type;
+       
+       static bool eq(const char_type& c, const char_type& d) {
+               return c == d;
+       }
+       static bool lt(const char_type& c, const char_type& d) {
+               return c < d;
+       }
+       static size_t length(const char_type* s) {
+               size_t  ret = 0;
+               while(*s++)     ret ++;
+               return ret;
+       }
+};
+
+template <>
+struct char_traits<wchar_t>
+{
+       typedef wchar_t char_type;
+       typedef int     int_type;
+       //typedef streamoff     off_type;
+       //typedef streampos     pos_type;
+       //typedef mbstate_t     state_type;
+       
+       static size_t length(const char_type* s) {
+               size_t  ret = 0;
+               while(*s++)     ret ++;
+               return ret;
+       }
+       static bool eq(const char_type& c, const char_type& d) {
+               return c == d;
+       }
+       static bool lt(const char_type& c, const char_type& d) {
+               return c < d;
+       }
+};
+
+template < class charT, class traits=char_traits<charT>, class Alloc=allocator<charT> >
+class basic_string;
+
+typedef basic_string<char>     string;
+
+};     // namespace std
+
+#include <stdexcept>
+
+namespace std {
+
+template < class charT, class traits, class Alloc >
+class basic_string
+{
+public:
+       typedef traits  traits_type;
+       typedef Alloc   allocator_type;
+       typedef charT   value_type;
+       typedef typename allocator_type::reference      reference;
+       typedef typename allocator_type::const_reference        const_reference;
+       typedef size_t  size_type;
+
+       basic_string(const allocator_type& alloc = allocator_type()):
+               m_allocator(alloc),
+               m_capacity(0),
+               m_length(0),
+               m_data(0)
+       {
+       }
+       basic_string(const basic_string& str):
+               basic_string(str, allocator_type())
+       {
+       }
+       basic_string(const basic_string& str, const allocator_type& alloc):
+               basic_string(str, 0, str.length(), alloc)
+       {
+       }
+       basic_string(const basic_string& str, size_type pos, size_type len = npos, const allocator_type& alloc = allocator_type()):
+               basic_string(alloc)
+       {
+               if( pos < str.length() )
+               {
+                       if( len > str.length() - pos )
+                               len = str.length() - pos;
+                       reserve(len);
+                       for( size_type i = 0; i < len; i ++ )
+                               m_data[i] = str.m_data[pos+i];
+                       m_length = len;
+               }
+       }
+       basic_string(const charT *s, const allocator_type& alloc = allocator_type()):
+               basic_string(s, traits::length(s), alloc)
+       {
+       }
+       basic_string(const charT *s, size_type n, const allocator_type& alloc = allocator_type()):
+               basic_string(alloc)
+       {
+               if( n > 0 )
+               {
+                       reserve(n);
+                       for( size_type i = 0; i < n; i ++ )
+                               m_data[i] = s[i];
+                       m_length = n;
+               }
+       }
+       basic_string(size_type n, charT c, const allocator_type& alloc = allocator_type()):
+               basic_string(alloc)
+       {
+               if( n > 0 )
+               {
+                       reserve(n);
+                       for( size_type i = 0; i < n; i ++ )
+                               m_data[i] = c;
+                       m_length = n;
+               }
+       }
+       
+       // iterators
+       
+       // capacity
+       size_type size() const {
+               return m_size;
+       }
+       size_type length() const {
+               return size();
+       }
+       size_type max_size() const {
+               return -1;
+       }
+       void resize(size_type size, charT c = 0) {
+               reserve(size);
+               if( m_size < size ) {
+                       for( size_type ofs = m_size; ofs < size; ofs ++ )
+                               m_data[ofs] = c;
+               }
+               m_size = size;
+               m_data[m_size] = 0;
+       }
+       size_type capacity() const {
+               return m_capacity;
+       }
+       void reserve(size_type size) {
+               size = (size+1 + 31) & ~31;
+               if( size > m_capacity ) {
+                       auto new_area = m_allocator.allocate(size);
+                       for( size_type i = 0; i < m_length; i ++ )
+                               new_area[i] = m_data[i];
+                       m_allocator.deallocate(m_data, m_capacity);
+                       m_data = new_area;
+                       m_capacity = size;
+               }
+       }
+       void clear() {
+               m_size = 0;
+       }
+       bool empty() const {
+               return length() == 0;
+       }
+       
+       // Access
+       reference operator[] (size_type pos) {
+               return m_data[pos];
+       }
+       const_reference operator[] (size_type pos) const {
+               return m_data[pos];
+       }
+       reference at(size_type pos) {
+               if(pos >= m_size)       throw ::std::out_of_range("basic_string - at");
+               return m_data[pos];
+       }
+       const_reference at(size_type pos) const {
+               if(pos >= m_size)       throw ::std::out_of_range("basic_string - at");
+               return m_data[pos];
+       }
+       
+       // Modifiers
+       basic_string& operator +=(const basic_string& str);
+       basic_string& operator +=(const charT* s);
+       basic_string& operator +=(charT c);
+       
+       static const size_type npos = -1;
+private:
+       allocator_type  m_allocator;
+       size_type       m_capacity;
+       size_type       m_size;
+       size_type       m_length;
+       typename allocator_type::pointer m_data;
+};
+
+};
+
+#include <stdexcept>
+
+#endif
+
+// vim: ft=cpp
diff --git a/Usermode/Libraries/libc++.so_src/include_exp/vector b/Usermode/Libraries/libc++.so_src/include_exp/vector
new file mode 100644 (file)
index 0000000..e69de29

UCC git Repository :: git.ucc.asn.au