From 65e74db123c9aa8a2cb86fe9299c256a910ebac1 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Tue, 28 Apr 2020 10:39:10 +0200 Subject: [PATCH 1/2] devel -> master : removing unstable --- include/bitops.hpp | 2 - include/gp/flat_tree.hpp | 10 - include/gp/function.hpp | 1 - include/gp/memory.hpp | 56 ----- include/gp/optional.hpp | 1 - include/gp/subtree_iterator.hpp | 26 --- include/indexed_array.hpp | 2 - include/rc6_generic.hpp | 142 ------------ include/region_locker.hpp | 2 - include/shared_fd.hpp | 381 ------------------------------- include/shared_mmap.hpp | 2 - include/stored_array.hpp | 1 - include/stored_clog.hpp | 2 - include/stored_hmap.hpp | 2 - include/stored_indexed_array.hpp | 1 - tests.cpp | 2 - tests/rc6_generic.cpp | 88 ------- tests/shared_fd.cpp | 286 ----------------------- 18 files changed, 1007 deletions(-) delete mode 100644 include/bitops.hpp delete mode 100644 include/gp/flat_tree.hpp delete mode 100644 include/gp/memory.hpp delete mode 100644 include/gp/subtree_iterator.hpp delete mode 100644 include/indexed_array.hpp delete mode 100644 include/rc6_generic.hpp delete mode 100644 include/region_locker.hpp delete mode 100644 include/shared_fd.hpp delete mode 100644 include/shared_mmap.hpp delete mode 100644 include/stored_array.hpp delete mode 100644 include/stored_clog.hpp delete mode 100644 include/stored_hmap.hpp delete mode 100644 include/stored_indexed_array.hpp delete mode 100644 tests/rc6_generic.cpp delete mode 100644 tests/shared_fd.cpp diff --git a/include/bitops.hpp b/include/bitops.hpp deleted file mode 100644 index 3f59c93..0000000 --- a/include/bitops.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once - diff --git a/include/gp/flat_tree.hpp b/include/gp/flat_tree.hpp deleted file mode 100644 index 0e3632e..0000000 --- a/include/gp/flat_tree.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma one -#include -#include -#include - -template -class flat_tree { - gp::array data_; - -}; diff --git a/include/gp/function.hpp b/include/gp/function.hpp index 63beaeb..059f068 100644 --- a/include/gp/function.hpp +++ b/include/gp/function.hpp @@ -1,5 +1,4 @@ #pragma once -#include "gp/memory.hpp" #include "gp/exception.hpp" namespace gp{ diff --git a/include/gp/memory.hpp b/include/gp/memory.hpp deleted file mode 100644 index 90220bd..0000000 --- a/include/gp/memory.hpp +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once -#include "gp_config.hpp" -#include "gp/exception.hpp" -#include - -namespace gp{ - template> - class default_memory_allocator - { - public: - using pointer_type = T*; - using reference_type = T&; - using const_pointer_type = const T*; - using const_reference_type = const T&; - - pointer_type allocate(size_t sz) - { - return reinterpret_cast (gp_config::memory_module::memory_allocator(sizeof(T) * sz)); - } - - void deallocate(pointer_type ptr) - { - gp_config::memory_module::memory_deallocator(ptr); - } - - template - pointer_type construct(pointer_type ptr, params... args) - { - new(ptr) T(args...); - return ptr; - } - - void destroy(pointer_type v) - { - v->~T(); - } - }; -} - -void* operator new(size_t sz) -{ - auto ptr = gp_config::memory_module::memory_allocator(sz); - if constexpr (gp_config::has_exceptions) - { - if(!ptr) - { - throw gp::bad_alloc{}; - } - } - return ptr; -} - -void operator delete (void* ptr) noexcept -{ - gp_config::memory_module::memory_deallocator(ptr); -} \ No newline at end of file diff --git a/include/gp/optional.hpp b/include/gp/optional.hpp index 6080629..552e436 100644 --- a/include/gp/optional.hpp +++ b/include/gp/optional.hpp @@ -2,7 +2,6 @@ #include #include "gp_config.hpp" #include "gp/exception.hpp" -#include "gp/memory.hpp" namespace gp{ struct nullopt_t{}; diff --git a/include/gp/subtree_iterator.hpp b/include/gp/subtree_iterator.hpp deleted file mode 100644 index d2b432b..0000000 --- a/include/gp/subtree_iterator.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include -#include -#include - -template -struct subtree_iterator final -{ - gp::buffer target; - size_t idx; -public: - subtree_iterator() - : target{} - , idx{0} - {} - - subtree_iterator(gp::buffer data, size_t itr_idx) - : target{data} - , idx{itr_idx} - {} - - template - void climb_traversal(func& traverser) { - - } -}; \ No newline at end of file diff --git a/include/indexed_array.hpp b/include/indexed_array.hpp deleted file mode 100644 index 3f59c93..0000000 --- a/include/indexed_array.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once - diff --git a/include/rc6_generic.hpp b/include/rc6_generic.hpp deleted file mode 100644 index 38825a1..0000000 --- a/include/rc6_generic.hpp +++ /dev/null @@ -1,142 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - - - -template -class RC6 { - static constexpr size_t word_size = 8*sizeof(word_t); - - constexpr static word_t r_l(const word_t& w, size_t v) { - return (w << v) | ( w >> (word_size-v)); - } - - constexpr static word_t r_r(const word_t& w, size_t v) { - return (w >> v) | ( w << (word_size-v)); - } - - - class RC6_KeySched { - using sched_t = std::array; - public: - static constexpr size_t c = (b+word_size-1)/word_size; - static constexpr size_t v_3 = std::max(c, 2*r+4); - static constexpr size_t v = v_3*3; - private: - sched_t S; - public: - constexpr RC6_KeySched(std::array L) - { - assert(r_l(r_r(13,13),13) == 13); - - auto it = S.begin(); - *(it++) = P; - for(; it != S.end(); ++it) - { - *it = *(it-1) + Q; - } - word_t A = 0; - word_t B = 0; - word_t i = 0; - word_t j = 0; - - for(size_t s = 0; s < v; ++s) - { - A = S[i] = r_l( S[i] + A + B, 3 ); - B = L[j] = r_l( L[j] + A + B, (A + B)%(word_size)); - - i = s % S.size(); - j = s % L.size(); - } - } - - const auto cbegin() - { - return S.cbegin(); - } - - const auto cend() - { - return S.cend(); - } - - const auto crbegin() - { - return S.crbegin(); - } - - const auto crend() - { - return S.crend(); - } - }; - - RC6_KeySched S; - -public: - - typedef std::array key_type; - typedef std::array block_type; - - constexpr RC6(const key_type& key) - : S(key) - {} - - constexpr block_type encrypt(block_type plaintext) { - using namespace gp::math; - auto& A = plaintext[0]; - auto& B = plaintext[1]; - auto& C = plaintext[2]; - auto& D = plaintext[3]; - - auto it = S.cbegin(); - - B += *(it++); - D += *(it++); - - for(size_t i = 0; i < r; ++i) - { - auto u = r_l( D * ( 2 * D + 1 ), msb(word_size)); - auto t = r_l( B * ( 2 * B + 1 ), msb(word_size)); - A = r_l((A ^ t), u % word_size) + *(it++); - C = r_l((C ^ u), t % word_size) + *(it++); - std::rotate(plaintext.begin(), plaintext.begin()+1, plaintext.end()); - } - - A += *(it++); - C += *(it++); - assert(it == S.cend()); - return plaintext; - } - - constexpr block_type decrypt(block_type plaintext) { - using namespace gp::math; - auto& A = plaintext[0]; - auto& B = plaintext[1]; - auto& C = plaintext[2]; - auto& D = plaintext[3]; - auto it = S.crbegin(); - - C -= *(it++); - A -= *(it++); - - for(size_t i = 0; i < r; ++i) - { - std::rotate(plaintext.begin(), plaintext.end()-1, plaintext.end()); - auto u = r_l( D * ( 2 * D + 1 ), msb(word_size)); - auto t = r_l( B * ( 2 * B + 1 ), msb(word_size)); - C = r_r( (C - *(it++)) , t % word_size) ^ u ; - A = r_r( (A - *(it++)) , u % word_size) ^ t ; - } - - D -= *(it++); - B -= *(it++); - assert(it == S.crend()); - return plaintext; - } - -}; \ No newline at end of file diff --git a/include/region_locker.hpp b/include/region_locker.hpp deleted file mode 100644 index 3f59c93..0000000 --- a/include/region_locker.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once - diff --git a/include/shared_fd.hpp b/include/shared_fd.hpp deleted file mode 100644 index e585161..0000000 --- a/include/shared_fd.hpp +++ /dev/null @@ -1,381 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace gp{ - using open_opt_flags = int; - - enum class open_options : open_opt_flags { - append = O_APPEND, - create = O_CREAT, - async = O_ASYNC, - direct = O_DIRECT, - data_sync = O_DSYNC, - file_sync = O_SYNC, - exclusive = O_EXCL, - no_access_time = O_NOATIME, - no_follow = O_NOFOLLOW, - read = O_RDONLY, - write = O_WRONLY, -#ifdef O_TEMP - temporary = O_TEMP, -#endif -#ifdef O_TRUC - truncate = O_TRUC, -#endif - }; - - using open_opt_mode = int; - - enum class open_modes : open_opt_mode { - user_read = S_IRUSR, - user_write = S_IWUSR, - user_exec = S_IXUSR, - group_read = S_IRGRP, - group_write = S_IWGRP, - group_exec = S_IXGRP, - other_read = S_IROTH, - other_write = S_IWOTH, - other_exec = S_IXOTH, - set_uid = S_ISUID, - set_gid = S_ISGID, - sticky_bit = S_ISVTX, - }; - - enum class socket_domain : int { - ip4 = AF_INET, - ip6 = AF_INET6, - unix = AF_UNIX - }; - - enum class socket_protocol : int { - tcp_like = SOCK_STREAM, - udp_like = SOCK_DGRAM - }; - - - using socket_opt_flags = int; - - enum class net_socket_opt_flags : socket_opt_flags { - non_blocking = SOCK_NONBLOCK, - close_on_exec = SOCK_CLOEXEC, - }; - - using socket_opt_flags = int; - - enum class unix_socket_opt_flags : socket_opt_flags { - non_blocking = SOCK_NONBLOCK, - close_on_exec = SOCK_CLOEXEC, - }; - - using address = std::variant; - - template - struct stream_expect{ - const T v; - stream_expect(T value) - : v(value) - {} - }; - - template - std::istream& operator>>(std::istream& in, const stream_expect& expector) - { - T vin; - in >> vin; - if(vin != expector.v) - throw std::runtime_error("Expector failed"); - return in; - } - - address make_ipv4(const std::string_view addr, const uint16_t port) - { - std::stringstream din; - din< address{0}; - int ex; - din>>ex - >>stream_expect('.'); - address[0]=ex; - din>>ex - >>stream_expect('.'); - address[1]=ex; - din>>ex - >>stream_expect('.'); - address[2]=ex; - din>>ex; - address[3]=ex; - - sockaddr_in ret; - in_addr ret_addr; - ret_addr.s_addr = *((uint32_t*)&address); - ret.sin_family = AF_INET; - ret.sin_addr = ret_addr; - ret.sin_port = htons(port); - return ret; - } - - // TODO: make an IPv6 parser - //address make_ipv6(const std::string_view addr, const uint16_t port){} - - address make_unix(const std::string_view filename) - { - sockaddr_un ret; - if(filename.size()>(sizeof(ret.sun_path)-1)) - throw std::runtime_error("Filename too long"); - - ret.sun_family = AF_UNIX; - *std::copy(filename.begin(), filename.end(), ret.sun_path) = '\0'; - return ret; - } - - class shared_fd { - int fd; - std::atomic_int* guard; - int last_error; - shared_fd(int fd_v) - : fd(fd_v) - , guard(new std::atomic_int{1}) - , last_error(0) - {} - - public: - shared_fd() - : fd(-1) - , guard(nullptr) - , last_error(0) - {} - - shared_fd(const shared_fd& oth) - : fd(oth.fd) - , guard(oth.guard) - , last_error(0) - { - if(guard) - guard->fetch_add(1, std::memory_order_acq_rel); - } - - shared_fd(shared_fd&& oth) - : last_error(0) - { - fd=oth.fd; - guard=oth.guard; - oth.fd=-1; - oth.guard=nullptr; - } - - const int get() const { - return fd; - } - - void operator=(const shared_fd& oth) - { - this->~shared_fd(); - fd = oth.fd; - guard = oth.guard; - if(guard) - guard->fetch_add(1, std::memory_order_acq_rel); - } - - void operator=(shared_fd&& oth) - { - std::swap(fd,oth.fd); - std::swap(guard,oth.guard); - } - - static shared_fd open(const std::string& filename, const open_opt_flags& flags, const open_opt_mode& mode = 0) - { - shared_fd ret{::open(filename.c_str(), flags, mode)}; - return ret; - } - - static shared_fd create(const std::string& filename, const open_opt_mode& mode) - { - shared_fd ret{::creat(filename.c_str(), mode)}; - return ret; - } - - static shared_fd tcp_socket() - { - shared_fd ret; - - auto res = ::socket(AF_INET, SOCK_STREAM, 0); - if(res >= 0) - { - ret = shared_fd{res}; - fcntl(res, F_SETFL, O_NONBLOCK); - } - else - ret.last_error = errno; - return ret; - } - - static shared_fd socket(const socket_domain& dom, const socket_protocol& proto)//, const net_socket_opt_flags& flags) - { - shared_fd ret; - - auto res = ::socket((int)dom, (int)proto, (int)0); - if(res >= 0) - { - ret = shared_fd{res}; - } - else - ret.last_error = errno; - return ret; - } - - static shared_fd unix_socket(const socket_protocol& proto, const socket_opt_flags flags) { - shared_fd ret; - - auto res = ::socket((int)AF_UNIX, (int)proto, (int)flags); - if(res >= 0) - ret = shared_fd{res}; - - return ret; - } - - static std::pair unix_socket_pair(const socket_protocol& proto, const net_socket_opt_flags flags) { - std::pair ret; - int fds[2]; - auto result = ::socketpair((int)AF_UNIX, (int)proto, (int)flags, fds); - if(result != 0) - return ret; - ret.first = shared_fd(fds[0]); - ret.second = shared_fd(fds[1]); - - return ret; - } - - bool is_valid() const { - return guard && (fd>=0); - } - - bool has_failed() const { - return last_error; - } - - bool was_connection_refused() const { - return last_error==ECONNREFUSED; - } - - bool in_progress() const { - return last_error==EINPROGRESS; - } - - bool must_retry() const { - return last_error==EAGAIN; - } - - int error() const { - return last_error; - } - - void bind(const address& addr) - { - int ret; - std::visit([&](const auto& v){ - ret = ::bind(fd, (struct sockaddr*)&v, sizeof(v)); - }, addr); - if(ret==0) - { - last_error = 0; - return; - } - else - last_error = errno; - } - - void connect(const address& addr) - { - int ret; - std::visit([&](const auto& v){ - ret = ::connect(fd, (struct sockaddr*)&v, sizeof(v)); - }, addr); - if(ret!=0) - { - last_error = errno; - return; - } - last_error = 0; - } - - void listen(const int& backlog = 16) - { - int ret = 0; - std::visit([&](){ - ret = ::listen(fd, backlog); - }); - if(ret!=0) - { - last_error = errno; - return; - } - last_error = 0; - } - - shared_fd accept() - { - int ret = 0; - std::array buffer; - socklen_t len; - ret = ::accept(fd,(sockaddr*)buffer.begin(), &len); - if(ret!=0) - { - last_error = errno; - return shared_fd{}; - } - last_error = 0; - return shared_fd{ret}; - } - - std::string_view read(const std::string_view buffer) - { - int sz = ::read(fd, (void*)(buffer.begin()), buffer.size()); - if(sz<0) - last_error = errno; - else - last_error = 0; - sz = sz<0?0:sz; - return std::string_view(buffer.begin(), sz); - } - - std::string_view write(const std::string_view buffer) - { - int sz = ::write(fd, (void*)(buffer.begin()), buffer.size()); - if(sz<0) - last_error = errno; - else - last_error = 0; - sz = sz<0?0:sz; - return std::string_view(buffer.begin()+sz, buffer.size()-sz); - } - - ~shared_fd() - { - if(guard) - if(guard->fetch_sub(1, std::memory_order_acq_rel) == 1) - { - assert(guard->load() == 0); - assert(fd >= 0); - delete guard; - guard = nullptr; - if(fd >= 0) - { - ::close(fd); - } - } - } - }; -} \ No newline at end of file diff --git a/include/shared_mmap.hpp b/include/shared_mmap.hpp deleted file mode 100644 index 3f59c93..0000000 --- a/include/shared_mmap.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once - diff --git a/include/stored_array.hpp b/include/stored_array.hpp deleted file mode 100644 index 7b9637e..0000000 --- a/include/stored_array.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once \ No newline at end of file diff --git a/include/stored_clog.hpp b/include/stored_clog.hpp deleted file mode 100644 index 3f59c93..0000000 --- a/include/stored_clog.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once - diff --git a/include/stored_hmap.hpp b/include/stored_hmap.hpp deleted file mode 100644 index 3f59c93..0000000 --- a/include/stored_hmap.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once - diff --git a/include/stored_indexed_array.hpp b/include/stored_indexed_array.hpp deleted file mode 100644 index 7b9637e..0000000 --- a/include/stored_indexed_array.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once \ No newline at end of file diff --git a/tests.cpp b/tests.cpp index e039d15..4930d58 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1,8 +1,6 @@ #include "test_scaffold.h" #include "gp_config.hpp" #include "meta_test.cpp" -#include "shared_fd.cpp" -#include "rc6_generic.cpp" #include "gp_test.cpp" #include "bloomfilter.cpp" #include "quotient_filter.cpp" diff --git a/tests/rc6_generic.cpp b/tests/rc6_generic.cpp deleted file mode 100644 index f25b53e..0000000 --- a/tests/rc6_generic.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "rc6_generic.hpp" -#include "test_scaffold.h" -#include -#include - -struct RC6test : public test_scaffold { - RC6test() { - name = __FILE__ ":1"; - } - - virtual int run() { - using rc = RC6<>; - - rc::key_type key = {0,0,0,0}; - rc::block_type plaintext = {0,0,0,0}; - rc::block_type expected = {0x8fc3a536,0x56b1f778,0xc129df4e,0x9848a41e}; - - /* - std::cout<<"plain:"; - for(auto a : plaintext) - std::cout << std::hex << a; - */ - auto cipher = rc{key}; - plaintext = cipher.encrypt(plaintext); - - /*std::cout<<"\nkey__:"; - for(auto a : key) - std::cout << std::hex << a; - - std::cout<<"\nciphe:"; - for(auto a : plaintext) - std::cout << std::hex << a; - - std::cout<<"\nexpec:"; - for(auto a : expected) - std::cout << std::hex << a; - std::cout << std::endl; - */ - return plaintext != expected; - } -}; - -append_test dummy_szfhu5463(new RC6test{}); - -struct RC6test2 : public test_scaffold { - RC6test2() { - name = __FILE__ ":2"; - } - - virtual int run() { - using rc = RC6<>; - - rc::key_type key = {0,0,0,0}; - rc::block_type plaintext = {0,0,0,0}; - rc::block_type expected = {0,0,0,0}; - - /* - std::cout<<"plain:"; - for(auto a : plaintext) - std::cout << std::hex << a; - */ - auto cipher = rc{key}; - plaintext = cipher.encrypt(plaintext); - - /*std::cout<<"\nkey__:"; - for(auto a : key) - std::cout << std::hex << a; - - std::cout<<"\nciphe:"; - for(auto a : plaintext) - std::cout << std::hex << a; - */ - plaintext = cipher.decrypt(plaintext); - - /*std::cout<<"\ncidec:"; - for(auto a : plaintext) - std::cout << std::hex << a; - - std::cout<<"\nexpec:"; - for(auto a : expected) - std::cout << std::hex << a; - std::cout << std::endl; - */ - return plaintext != expected; - } -}; - -append_test dummy_szmltz63(new RC6test2{}); \ No newline at end of file diff --git a/tests/shared_fd.cpp b/tests/shared_fd.cpp deleted file mode 100644 index f7134c6..0000000 --- a/tests/shared_fd.cpp +++ /dev/null @@ -1,286 +0,0 @@ -#include "shared_fd.hpp" -#include "test_scaffold.h" -#include -#include -#include -#include - - -struct create_test : public test_scaffold { - create_test() { - name = __FILE__ ":1"; - } - - virtual int run() { - auto fd = gp::shared_fd::create("./bin/test_n", int(gp::open_modes::user_read) | int(gp::open_modes::user_write)); - - return fd.is_valid()?0:1; - } -}; - -append_test dummy_sdfhuisd3(new create_test{}); - - -struct open_test : public test_scaffold { - open_test() { - name = __FILE__ ":2"; - } - - virtual int run() { - auto fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::append)); - - return fd.is_valid()?0:1; - } -}; - -append_test dummy_sdf564dd3(new open_test{}); - - -struct manip_test : public test_scaffold { - manip_test() { - name = __FILE__ ":3"; - } - - virtual int run() { - auto fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::append)); - - int error = fd.is_valid()?0:1; - - error += !(fd.get()>=0); - - auto fd_cpy = fd; - error += fd_cpy.is_valid()?0:1; - - gp::shared_fd constr_cpy(fd_cpy); - error += constr_cpy.is_valid()?0:1; - - gp::shared_fd constr_mv(std::move(constr_cpy)); - error += constr_mv.is_valid()?0:1; - - gp::shared_fd assign_cpy; - assign_cpy.operator=(fd_cpy); - error += assign_cpy.is_valid()?0:1; - - gp::shared_fd assign_mv; - assign_mv.operator=(std::move(assign_cpy)); - error += assign_mv.is_valid()?0:1; - error += (!assign_cpy.is_valid())?0:1; - return error; - } -}; - -append_test dummy_lkjs64dd3(new manip_test{}); - - -struct rw_test : public test_scaffold { - rw_test() { - name = __FILE__ ":4"; - } - - virtual int run() { - auto fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::write)); - - int error = fd.is_valid()?0:1; - - fd.write("potatoes"); - error += fd.has_failed(); - - fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::read)); - - std::array buffer; - - auto str = fd.read(std::string_view(buffer.begin(), buffer.size())); - error += fd.has_failed(); - - error += (str != "potatoes"); - - return error; - } -}; - -append_test dummy_l6z5e4rdd3(new rw_test{}); - - -struct rw_err_test : public test_scaffold { - rw_err_test() { - name = __FILE__ ":5"; - } - - virtual int run() { - auto fd = gp::shared_fd::create("./bin/test_n", int(gp::open_modes::user_read) | int(gp::open_modes::user_write)); - fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::read)); - - int error = fd.is_valid()?0:1; - - fd.write("potatoes"); - error += fd.has_failed(); - - fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::write)); - error += fd.is_valid()?0:1; - - std::array buffer; - - auto str = fd.read(std::string_view(buffer.begin(), buffer.size())); - error += fd.has_failed(); - - return error == 2 ? 0 : 1; - } -}; - -append_test dummy_l6987erd3(new rw_err_test{}); - -/* -struct make_address_test : public test_scaffold { - make_address_test() { - name = __FILE__ ":6"; - } - - virtual int run() { - int error = 0; - - gp::address ipv4 = gp::make_ipv4("127.0.0.1", 0x1234); - auto p = std::get(ipv4); - error += (p.sin_family != AF_INET); - error += (p.sin_addr.s_addr != htonl(0x7F000001)); - error += (p.sin_port != htons(0x1234)); - - try{ - gp::make_ipv4("not an IP", 1234); - error += 1; - }catch(...){} - - std::string filename = "/tmp/my_socket"; - gp::address unix = gp::make_unix(filename); - auto q = std::get(unix); - error += (q.sun_family != AF_UNIX); - error += strcmp(filename.c_str(), q.sun_path); - - try{ - std::string long_str(1024, 'p'); - gp::make_unix(long_str); - error += 1; - }catch(...){} - - return error; - } -}; - -append_test dummy_l6923ml3(new make_address_test{}); - -struct sockets_test : public test_scaffold { - sockets_test() { - name = __FILE__ ":7"; - } - - virtual int run() { - int error = 0; - - auto v = gp::shared_fd::socket(gp::socket_domain::ip4, gp::socket_protocol::tcp_like); - error += !(v.is_valid()); - - v = gp::shared_fd::socket(gp::socket_domain::ip4, gp::socket_protocol::tcp_like); - error += !(v.is_valid()); - - auto pair_v = gp::shared_fd::unix_socket_pair(gp::socket_protocol::tcp_like, (gp::net_socket_opt_flags)0); - error += !(pair_v.first.is_valid()); - error += !(pair_v.second.is_valid()); - - pair_v = gp::shared_fd::unix_socket_pair(gp::socket_protocol::tcp_like, (gp::net_socket_opt_flags)-1); - error += pair_v.first.is_valid(); - error += pair_v.second.is_valid(); - - auto u_v = gp::shared_fd::unix_socket(gp::socket_protocol::tcp_like, (gp::socket_opt_flags)0); - error += !(u_v.is_valid()); - - u_v = gp::shared_fd::unix_socket(gp::socket_protocol::tcp_like, (gp::socket_opt_flags)-1); - error += u_v.is_valid(); - - return error; - } -}; - -append_test dummy_r3321443(new sockets_test{}); - -using namespace std::chrono_literals; -struct sockets_co_test : public test_scaffold { - - sockets_co_test() { - name = __FILE__ ":8"; - } - - virtual int run() { - std::atomic_int error = 0; - { - - auto v2 = gp::shared_fd::tcp_socket(); - error += !(v2.is_valid()); - - std::thread sside([&](){ - auto v1 = gp::shared_fd::tcp_socket(); - - v1.bind(gp::make_ipv4("0.0.0.0",1235)); - error += v1.has_failed(); - - v1.listen(); - std::cout << "listens?:" << v1.is_valid()< buffer; - std::string_view retstr; - do - { - retstr = v2.read(std::string_view(buffer.begin(), buffer.size())); - std::this_thread::sleep_for(1ms); - } - while(v2.must_retry()); - puts("received"); - std::cout<< "out:" << std::quoted(retstr) << std::endl; - error += !(v2.has_failed()); - error += retstr != "potatoes"; - if(sside.joinable()) - sside.join(); - } - return error; - - } - return error; - } -}; - -// append_test dummy_polmdf43(new sockets_co_test{}); -*/ \ No newline at end of file From 9bf5404f14bd4cbce676c42dda30f3b452cf6eb9 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Tue, 28 Apr 2020 12:09:47 +0200 Subject: [PATCH 2/2] optional hotfix --- include/gp/optional.hpp | 130 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 123 insertions(+), 7 deletions(-) diff --git a/include/gp/optional.hpp b/include/gp/optional.hpp index 552e436..8864d29 100644 --- a/include/gp/optional.hpp +++ b/include/gp/optional.hpp @@ -2,12 +2,14 @@ #include #include "gp_config.hpp" #include "gp/exception.hpp" +#include "gp/algorithm/move.hpp" namespace gp{ struct nullopt_t{}; constexpr nullopt_t nullopt; + // TODO: Add allocators to the template template::value || std::is_fundamental::value> class optional; @@ -33,9 +35,38 @@ namespace gp{ constexpr optional(T&& value) : ready{true} { - new(buffer) T(std::move(value)); + new(buffer) T(gp::move(value)); } + optional& operator=(nullopt_t) { + if(ready) { + ((T*)buffer)->~T(); + ready = false; + } + return *this; + } + + optional& operator=(T& value) { + if(ready) { + *(T*)buffer = value; + } else { + ready = true; + new(buffer) T(value); + } + return *this; + } + + optional& operator=(T&& value) { + if(ready) { + *(T*)buffer = gp::move(value); + } else { + ready = true; + new(buffer) T(gp::move(value)); + } + return *this; + } + + constexpr bool has_value() { return ready; @@ -49,15 +80,18 @@ namespace gp{ { throw bad_optional{}; } + } else { + gp_config::assertion(ready, "bad optional access"); } return *reinterpret_cast(buffer); } }; + // TODO: Add allocators to the template template class optional{ bool ready = false; - void* ptr; + T* ptr; public: constexpr optional() : ready{false} @@ -67,16 +101,90 @@ namespace gp{ : ready{false} {} - constexpr optional(T& value) + template + constexpr optional(U& value) : ready{true} { - ptr = (void*)new T(value); + ptr = new U(value); // TODO: Use allocators } - constexpr optional(T&& value) + template + constexpr optional(U&& value) : ready{true} { - ptr = (void*)new T(std::move(value)); + ptr = new U(gp::move(value)); // TODO: Use allocators + } + + optional& operator=(nullopt_t) { + if(ready) { + delete ptr; + ready = false; + } + return *this; + } + + template + optional& operator=(U& value) { + if(ready) { + if constexpr (std::is_same_v) { + *ptr = value; + } else { + delete ptr; // TODO: Use allocators + ptr = new U(value); // TODO: Use allocators + } + } else { + ready = true; + ptr = new U(value); // TODO: Use allocators + } + return *this; + } + + template + optional& operator=(U&& value) { + if(ready) { + if constexpr (std::is_same_v) { + *ptr = gp::move(value); + } else { + delete ptr; // TODO: Use allocators + ptr = new U(gp::move(value)); // TODO: Use allocators + } + } else { + ready = true; + ptr = new U(gp::move(value)); // TODO: Use allocators + } + return *this; + } + + template<> + optional& operator=(optional& value) { + if(ready) { + *ptr = value; + } else { + ready = true; + ptr = new optional(value.value()); // TODO: Use allocators + } + return *this; + } + + template<> + optional& operator=(optional&& value){ + if(ready) { + delete ptr; // TODO: Use allocators + } + if(value.ready) { + ptr = value.ptr; + value.ready = false; + ready = true; + return *this; + } else { + ready = false; + return *this; + } + } + + operator T&() { + gp_config::assertion(ready, "bad optional access"); + return *ptr; } constexpr bool has_value() @@ -92,8 +200,16 @@ namespace gp{ { throw bad_optional{}; } + } else { + gp_config::assertion(ready, "bad optional access"); + } + return *ptr; + } + + ~optional() { + if(ready) { + delete ptr; // TODO: Use allocators } - return *reinterpret_cast(ptr); } }; } \ No newline at end of file