• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

C++ベースのLightweightなHTTPサーバー


Commit MetaInfo

Revision5ba1330a29fca6ed22b118cf787bd6cfe94e3ecf (tree)
Time2013-01-20 21:01:02
AuthorMichio Hirai <smg_ykz@user...>
CommiterMichio Hirai

Log Message

[Function] New support of mt::AutoPtr copy syntax to/from convertible types.

Change Summary

Incremental Difference

--- a/cm/cm_vector_socket.cpp
+++ b/cm/cm_vector_socket.cpp
@@ -25,5 +25,11 @@ bool VectorSocket::writev(size_t& bytes_written, const IOVectorBase& vector, siz
2525 return true;
2626 }
2727
28+int VectorSocket::release()
29+{
30+ int fd_to_return = fd_;
31+ fd_ = -1;
32+ return fd_to_return;
33+}
2834
2935 } // namespace cm
--- a/cm/test/cm_vector_socket_test.cpp
+++ b/cm/test/cm_vector_socket_test.cpp
@@ -3,6 +3,7 @@
33
44 #include "gmock/gmock.h"
55
6+#include "mt_auto_ptr.h"
67 #include "mt_typelist_algo_utility.h"
78 #include "mt_shim_clear_memory.h"
89 #include "cm_vector_socket.h"
@@ -76,4 +77,41 @@ TEST(CmVectorSocketTest, simple)
7677 EXPECT_EQ(bytes_read, total_bytes_written);
7778 }
7879
80+TEST(CmVectorSocketTest, ownership_move_from_socket)
81+{
82+ cm::Socket original_sock(STDOUT_FILENO);
83+
84+ cm::VectorSocket vec_sock(original_sock);
85+
86+ const cm::IOVectorBase& iov = cm::IOVec()(string1, sizeof(string1) - 1)
87+ (string2, sizeof(string2) - 1)
88+ (string3, sizeof(string3) - 1);
89+
90+ size_t bytes_written = 0u;
91+ vec_sock.writev(bytes_written, iov);
92+}
93+
94+TEST(CmVectorSocketTest, ownership_move_from_socket_via_auto_ptr)
95+{
96+ mt::AutoPtr<cm::SocketIf> original_socket(new cm::Socket(STDOUT_FILENO));
97+
98+ mt::AutoPtr<cm::VectorSocket> vec_sock(original_socket);
99+
100+ const cm::IOVectorBase& iov = cm::IOVec()(string1, sizeof(string1) - 1)
101+ (string2, sizeof(string2) - 1)
102+ (string3, sizeof(string3) - 1);
103+
104+ size_t bytes_written = 0u;
105+ vec_sock->writev(bytes_written, iov);
106+
107+ mt::AutoPtr<cm::Socket> renewed_socket(vec_sock);
108+
109+ std::cout << std::endl;
110+ renewed_socket->write(bytes_written, "hello world(2)!!!", sizeof("hello world(2)!!!") - 1);
111+
112+ vec_sock = renewed_socket;
113+
114+ vec_sock->writev(bytes_written, iov);
115+}
116+
79117 } // namespace
--- a/inc/cm_vector_socket.h
+++ b/inc/cm_vector_socket.h
@@ -27,6 +27,8 @@ public:
2727 bool writev(size_t& bytes_written, const IOVectorBase& vector, size_t skip_bytes = 0u);
2828 // bool readv(size_t& bytes_read, IOVector<T>& vector);
2929
30+ int release();
31+
3032 private:
3133 int fd_;
3234 };
--- a/inc/mt_auto_ptr.h
+++ b/inc/mt_auto_ptr.h
@@ -31,8 +31,12 @@ public:
3131
3232 AutoPtr(AutoPtr<T>& rhs)
3333 : raw_ptr_(rhs.release())
34- {
35- }
34+ {}
35+
36+ template <typename X>
37+ AutoPtr(AutoPtr<X>& rhs)
38+ : raw_ptr_(rhs.get() ? new T(*(rhs.release())) : 0)
39+ {}
3640
3741 AutoPtr(AutoPtrRef<T> ref)
3842 : raw_ptr_(ref.raw_ptr_)
@@ -89,9 +93,19 @@ public:
8993
9094 AutoPtr& operator=(AutoPtr& rhs)
9195 {
92- this->reset();
93- this->set(rhs.get());
94- rhs.release();
96+ if (this != &rhs) {
97+ this->reset();
98+ this->set(rhs.get());
99+ rhs.release();
100+ }
101+ return *this;
102+ }
103+
104+ template <typename X>
105+ AutoPtr& operator=(AutoPtr<X>& rhs)
106+ {
107+ X* ptr = rhs.get();
108+ raw_ptr_ = ptr ? new T(*ptr) : 0;
95109 return *this;
96110 }
97111