├── readme ├── fifo_spinlock ├── Lock.h └── test.cpp ├── inline_guard ├── a.out ├── main.cc ├── test.cpp ├── test.ipp └── test.h ├── MemoryPool ├── memory_pool.cpp ├── pool.h └── pool.cpp ├── protobuf ├── pb │ ├── CMakeLists.txt │ ├── im.helloworld.pb.h │ └── im.helloworld.pb.cc ├── im.helloworld.proto ├── server │ ├── CMakeLists.txt │ └── server.cpp ├── client │ ├── CMakeLists.txt │ └── client.cpp ├── CMakeLists.txt ├── test.cc └── serialization.hpp ├── LockFreeQueue ├── LockFreeQueue.cpp └── Queue.h ├── LockFreeMemoryPool ├── memory_pool.cpp ├── LockFreeMemPool.cpp └── memory_pool.h ├── compiler_micro.h ├── no-free-store.cc ├── switch_use.cc ├── spinlock ├── Lock.h └── test.cpp ├── is_palindrome.cpp ├── getline.cc ├── thread_once_singleton.cc ├── continue_timer.cc ├── type_erasure.cc ├── concat_string.cc ├── ScopeGuard.cpp ├── shared_ptr_copyonwrite.cc ├── aligned_test.cc ├── siglongjmp.c ├── spawn_say_love_fxp.cc ├── best-sellers-books.cc ├── async_say_love_fxp.cc ├── circular_buffer.cc ├── say.cc └── ScopeGuard.h /readme: -------------------------------------------------------------------------------- 1 | @auther fengxp 2 | just some ideas while programming with c++ 3 | -------------------------------------------------------------------------------- /fifo_spinlock/Lock.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaopeifeng/CodeTricks/HEAD/fifo_spinlock/Lock.h -------------------------------------------------------------------------------- /inline_guard/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaopeifeng/CodeTricks/HEAD/inline_guard/a.out -------------------------------------------------------------------------------- /fifo_spinlock/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaopeifeng/CodeTricks/HEAD/fifo_spinlock/test.cpp -------------------------------------------------------------------------------- /MemoryPool/memory_pool.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaopeifeng/CodeTricks/HEAD/MemoryPool/memory_pool.cpp -------------------------------------------------------------------------------- /protobuf/pb/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | aux_source_directory(. DIR_LIB_SRCS) 2 | 3 | add_library(helloworld ${DIR_LIB_SRCS}) 4 | -------------------------------------------------------------------------------- /LockFreeQueue/LockFreeQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaopeifeng/CodeTricks/HEAD/LockFreeQueue/LockFreeQueue.cpp -------------------------------------------------------------------------------- /inline_guard/main.cc: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | int main(int , char**) 4 | { 5 | Test t; 6 | t.xxxx(); 7 | } 8 | -------------------------------------------------------------------------------- /LockFreeMemoryPool/memory_pool.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaopeifeng/CodeTricks/HEAD/LockFreeMemoryPool/memory_pool.cpp -------------------------------------------------------------------------------- /inline_guard/test.cpp: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | #ifndef _INLINE_ 4 | #define INLINE 5 | #include "test.ipp" 6 | #endif 7 | 8 | -------------------------------------------------------------------------------- /inline_guard/test.ipp: -------------------------------------------------------------------------------- 1 | 2 | INLINE void Test::xxxx() 3 | { 4 | std::cout << "function:" << __PRETTY_FUNCTION__ << "\n"; 5 | } 6 | -------------------------------------------------------------------------------- /LockFreeMemoryPool/LockFreeMemPool.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xiaopeifeng/CodeTricks/HEAD/LockFreeMemoryPool/LockFreeMemPool.cpp -------------------------------------------------------------------------------- /protobuf/im.helloworld.proto: -------------------------------------------------------------------------------- 1 | package im; 2 | message helloworld 3 | { 4 | required string usrname = 1; 5 | required string passwd = 2; 6 | optional string email = 3; 7 | } 8 | -------------------------------------------------------------------------------- /compiler_micro.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _COMPILER_MICRO_H_ 3 | #define _COMPILER_MICRO_H_ 4 | 5 | #define CACHELINE_SIZE 64 6 | #define CACHELINE_ALIGNMENT __attribute__((aligned(CACHELINE_SIZE))) 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /no-free-store.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class NoFreeStore { 4 | void* operator new(std::size_t sz); 5 | }; 6 | 7 | NoFreeStore global; 8 | 9 | int main() 10 | { 11 | NoFreeStore local; 12 | NoFreeStore* freestore = new NoFreeStore; 13 | } 14 | -------------------------------------------------------------------------------- /inline_guard/test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H_ 2 | #define TEST_H_ 3 | 4 | #include 5 | 6 | class Test { 7 | public: 8 | void xxxx(); 9 | }; 10 | 11 | #ifdef _INLINE_ 12 | #define INLINE inline 13 | #include "test.ipp" 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /protobuf/server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | find_package(Boost 1.55 COMPONENTS system filesystem REQUIRED) 2 | include_directories(${Boost_INCLUDE_DIR}) 3 | aux_source_directory(. SERVER_SRCS) 4 | add_executable(server ${SERVER_SRCS}) 5 | 6 | target_link_libraries(server helloworld ${Boost_LIBRARIES} ${PROTOBUF_LIBRARY}) 7 | -------------------------------------------------------------------------------- /protobuf/client/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | find_package(Boost 1.55 COMPONENTS system filesystem REQUIRED) 2 | include_directories(${Boost_INCLUDE_DIR}) 3 | aux_source_directory(. CLIENT_SRCS) 4 | 5 | add_executable(client ${CLIENT_SRCS}) 6 | 7 | target_link_libraries(client helloworld ${Boost_LIBRARIES} ${PROTOBUF_LIBRARY}) 8 | -------------------------------------------------------------------------------- /MemoryPool/pool.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class memory_pool 5 | { 6 | public: 7 | explicit memory_pool(unsigned int num, unsigned int size); 8 | ~memory_pool(); 9 | 10 | public: 11 | void* Allocate(); 12 | void Release(void* ptr); 13 | 14 | private: 15 | unsigned int _total_blocks; 16 | unsigned int _block_size; 17 | std::vector _free_blocks; 18 | std::vector _busy_blocks; 19 | }; -------------------------------------------------------------------------------- /switch_use.cc: -------------------------------------------------------------------------------- 1 | #include 2 | /// a better use of switch which can decrease the number 3 | /// of 'if' occurence 4 | /// 5 | 6 | bool is_tspecial(int c) 7 | { 8 | switch(c) 9 | { 10 | case ' ': case '{' : case '}' : case '^' : case '|': 11 | return true; 12 | default: 13 | return false; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | char c = ' '; 20 | if(is_tspecial(c)){ 21 | std::cout << "yes" << std::endl; 22 | }else{ 23 | std::cout << "no" << std::endl; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spinlock/Lock.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class SpinLock 5 | { 6 | public: 7 | SpinLock() 8 | { 9 | m_status = 0; 10 | } 11 | 12 | ~SpinLock() 13 | {} 14 | 15 | public: 16 | 17 | bool try_lock() 18 | { 19 | long l = InterlockedExchangeAdd((long*)(&m_status), 1); 20 | return l == 0; 21 | } 22 | 23 | void lock() 24 | { 25 | for (unsigned u = 0; !try_lock(); ++u); 26 | } 27 | 28 | void unlock() 29 | { 30 | m_status = 0; 31 | } 32 | 33 | private: 34 | volatile long m_status; 35 | }; 36 | -------------------------------------------------------------------------------- /protobuf/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project (google_protobuf) 2 | 3 | include(FindProtobuf) 4 | find_package(Protobuf REQUIRED) 5 | find_package(Boost REQUIRED) 6 | include_directories(${PROTOBUF_INCLUDE_DIR}) 7 | include_directories(${Boost_INCLUDE_DIR}) 8 | aux_source_directory(. DIR_SRCS) 9 | 10 | add_definitions(-std=c++11) 11 | add_subdirectory(pb) 12 | 13 | add_subdirectory(client) 14 | 15 | add_subdirectory(server) 16 | 17 | add_executable(googleproto ${DIR_SRCS}) 18 | 19 | target_link_libraries(googleproto helloworld ${PROTOBUF_LIBRARY}) 20 | -------------------------------------------------------------------------------- /is_palindrome.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // so many algorithms in the std, so why not use it, no need to rewrite again. 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | bool is_palindrome(const std::string& str) 9 | { 10 | return std::equal(str.begin(), str.begin() + str.size() / 2, str.rbegin()); 11 | } 12 | 13 | void test(const std::string str) 14 | { 15 | std::cout << "\"" << str << "\"" << (is_palindrome(str) ? "is" : "is not") << " palindrome" << std::endl; 16 | } 17 | 18 | int main(int argc, char* argv[]) 19 | { 20 | test("fxp"); 21 | test("fxf"); 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /getline.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::fstream fs; 9 | fs.open("/home/f_x_p/test_code/c++/testfile"); 10 | if(!fs.is_open()){ 11 | std::cerr << "file open failed!" << std::endl; 12 | return -1; 13 | } 14 | int i = 0; 15 | while(!fs.eof()) 16 | { 17 | std::string str; 18 | if(!std::getline(fs, str)){ 19 | std::cerr << "getline failed!" << std::endl; 20 | if((fs.rdstate() & std::fstream::eofbit) != 0) 21 | { 22 | continue; 23 | }else{ 24 | fs.clear(); 25 | } 26 | } 27 | std::cout << str << std::endl; 28 | if(i == 0){ 29 | fs.setstate(std::fstream::badbit); 30 | ++i; 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /protobuf/test.cc: -------------------------------------------------------------------------------- 1 | #include "serialization.hpp" 2 | #include "pb/im.helloworld.pb.h" 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | im::helloworld msg; 10 | msg.set_usrname("fxp"); 11 | msg.set_passwd("fengxiaopei"); 12 | msg.set_email("xiaopeifenng@gmail.com"); 13 | 14 | std::string encode_str = fxp::encode(msg); 15 | 16 | im::helloworld* msg2; 17 | google::protobuf::Message* ptr; 18 | ptr = fxp::decode(encode_str); 19 | msg2 = dynamic_cast(ptr); 20 | std::cout << ptr->GetTypeName() << std::endl; 21 | std::cout << msg2->usrname() << std::endl; 22 | std::cout << msg2->passwd() << std::endl; 23 | std::cout << msg2->email() << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /LockFreeMemoryPool/memory_pool.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class MemoryPool 4 | { 5 | private: 6 | enum 7 | { 8 | head_size = 8, 9 | }; 10 | public: 11 | explicit MemoryPool(size_t num, size_t size); 12 | ~MemoryPool(); 13 | 14 | public: 15 | void* Alloc(); 16 | void Delete(void* ptr); 17 | int GetFreeNum(); 18 | 19 | private: 20 | void Initial(); 21 | bool IsFree(size_t i); // first check m_free_blocks > 0, if true decrease with 1, then set the block busy state. 22 | 23 | unsigned int AtomSetBusy(size_t i); 24 | unsigned int AtomSetFree(size_t i); 25 | unsigned int AtomIncreaseBlockNum(); 26 | unsigned int AtomDecreaseBlockNum(); 27 | 28 | private: 29 | int m_free_blocks; 30 | size_t m_total; 31 | size_t m_block_size; 32 | unsigned char* m_buf; 33 | }; -------------------------------------------------------------------------------- /thread_once_singleton.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class Singleton 6 | { 7 | public: 8 | static Singleton* getInstance() 9 | { 10 | pthread_once(&onceFlag_, init); 11 | return obj_; 12 | } 13 | 14 | std::string function() 15 | { 16 | return "hi, there.\n"; 17 | } 18 | 19 | private: 20 | Singleton() {} 21 | ~Singleton(){} 22 | Singleton(const Singleton&); 23 | void operator=(const Singleton&); 24 | 25 | static void init() 26 | { 27 | obj_ = new Singleton(); 28 | } 29 | 30 | private: 31 | static pthread_once_t onceFlag_; 32 | static Singleton* obj_; 33 | }; 34 | 35 | pthread_once_t Singleton::onceFlag_ = PTHREAD_ONCE_INIT; 36 | Singleton* Singleton::obj_ = NULL; 37 | 38 | int main() 39 | { 40 | Singleton* ptr = Singleton::getInstance(); 41 | std::cout << ptr->function(); 42 | } 43 | -------------------------------------------------------------------------------- /continue_timer.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | boost::asio::io_service ios; 6 | void continue_timer(); 7 | // how to use it 8 | // just push the continue_timer into where you want do something every some seconds 9 | void something_to_do(void) 10 | { 11 | // things to do every 5 seconds 12 | std::cout << "i love U..." << std::endl; 13 | std::cout << "see u 5 seconds later..." << std::endl; 14 | continue_timer(); 15 | } 16 | 17 | boost::asio::deadline_timer timer(ios); 18 | 19 | void continue_timer() 20 | { 21 | static boost::asio::deadline_timer timer(ios); 22 | timer.expires_from_now(boost::posix_time::seconds(5)); 23 | timer.async_wait(boost::bind(&something_to_do)); 24 | } 25 | 26 | int main() 27 | { 28 | continue_timer(); 29 | ios.run(); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /type_erasure.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class loader 4 | { 5 | public: 6 | virtual int size() = 0; 7 | }; 8 | 9 | template 10 | class loader_imp : public loader 11 | { 12 | public: 13 | loader_imp(T* obj) : _real_obj(obj) 14 | {} 15 | 16 | int size() 17 | { 18 | return _real_obj->size(); 19 | } 20 | private: 21 | T* _real_obj; 22 | }; 23 | 24 | class anything 25 | { 26 | public: 27 | template 28 | anything(T* ptr) : _imp_ptr( new loader_imp(ptr)) 29 | {} 30 | int size() 31 | { 32 | _imp_ptr->size(); 33 | } 34 | private: 35 | loader* _imp_ptr; 36 | }; 37 | 38 | int tell_me_the_size(anything obj) 39 | { 40 | return obj.size(); 41 | } 42 | 43 | class anything_1 44 | { 45 | public: 46 | int size() 47 | { 48 | return 100; 49 | } 50 | }; 51 | 52 | int main() 53 | { 54 | anything_1* ptr = new anything_1(); 55 | std::cout << tell_me_the_size(ptr); 56 | } 57 | -------------------------------------------------------------------------------- /concat_string.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // 作用: 利用concat_string接口可以随便不分类型的将他们作为字符串叠加在一起 5 | // 方便了不同类型序列化的繁杂代码书写过程 6 | // 用法:  直接在concat_string(arg1, arg2, arg3, ...);这样就直接将args串在一块了 7 | // 代码来源: http://www.reddit.com/r/cpp/comments/21pxx2/what_is_your_favorite_little_c_snippet_of_code 8 | // 注: 这可是reddit上面最受欢迎的c++代码片段哟 9 | 10 | inline void build_string(std::ostream& o) {} 11 | 12 | template 13 | inline void build_string(std::ostream& o, const First& value, const Rest&... rest) 14 | { 15 | o << value; 16 | build_string(o, rest...); 17 | } 18 | 19 | template 20 | std::string concat_string(const T&... value) 21 | { 22 | std::ostringstream o; 23 | build_string(o, value...); 24 | return o.str(); 25 | } 26 | 27 | int main() 28 | { 29 | int year = 2014; 30 | int month = 11; 31 | int day = 19; 32 | std::cout << concat_string(year, '-', month, '-', day) << std::endl; 33 | } 34 | -------------------------------------------------------------------------------- /ScopeGuard.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Facebook, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "ScopeGuard.h" 18 | 19 | #include 20 | 21 | /*static*/ void folly::ScopeGuardImplBase::warnAboutToCrash() noexcept { 22 | // Ensure the availability of std::cerr 23 | std::ios_base::Init ioInit; 24 | std::cerr 25 | << "This program will now terminate because a folly::ScopeGuard callback " 26 | "threw an \nexception.\n"; 27 | } 28 | -------------------------------------------------------------------------------- /spinlock/test.cpp: -------------------------------------------------------------------------------- 1 | #include "Lock.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define SPIN_LOCK 8 | int sum = 0; 9 | #ifdef SPIN_LOCK 10 | SpinLock g_lock; 11 | #else 12 | std::mutex g_lock; 13 | #endif 14 | 15 | void func() 16 | { 17 | for (int i = 0; i < 1000000; i++) 18 | { 19 | g_lock.lock(); 20 | sum = sum + 1; 21 | g_lock.unlock(); 22 | } 23 | std::cout << "thread: " << std::this_thread::get_id() << "finished..." << std::endl; 24 | } 25 | 26 | int main(int argc, char* argv[]) 27 | { 28 | auto start = std::chrono::steady_clock::now(); 29 | std::thread t1(func); 30 | std::thread t2(func); 31 | std::thread t3(func); 32 | 33 | t1.join(); 34 | t2.join(); 35 | t3.join(); 36 | auto end = std::chrono::steady_clock::now(); 37 | 38 | std::cout << "hi, " << std::endl; 39 | std::cout << "After computer, the result is : "; 40 | std::cout << sum << std::endl; 41 | 42 | using namespace std::chrono; 43 | duration time_span = \ 44 | duration_cast>(end - start); 45 | std::cout << "time elapsed " << time_span.count() << "seconds" << std::endl; 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /MemoryPool/pool.cpp: -------------------------------------------------------------------------------- 1 | #include "pool.h" 2 | #include 3 | 4 | memory_pool::memory_pool(unsigned int num, unsigned int size) 5 | : _total_blocks(num) 6 | , _block_size(size) 7 | { 8 | for (auto i = 0; i < num; ++i) 9 | { 10 | _free_blocks.push_back(new unsigned char[size]); 11 | } 12 | } 13 | 14 | memory_pool::~memory_pool() 15 | { 16 | if (_free_blocks.size() != 0) 17 | { 18 | for (auto iter = _free_blocks.begin(); iter != _free_blocks.end(); ++iter) 19 | { 20 | delete *iter; 21 | } 22 | } 23 | if (_busy_blocks.size() != 0) 24 | { 25 | for (auto iter = _busy_blocks.begin(); iter != _busy_blocks.end(); ++iter) 26 | { 27 | delete *iter; 28 | } 29 | } 30 | } 31 | 32 | void* memory_pool::Allocate() 33 | { 34 | if (_free_blocks.size()) 35 | { 36 | void* ptr = _free_blocks[_free_blocks.size() - 1]; 37 | _busy_blocks.push_back(ptr); 38 | _free_blocks.erase(_free_blocks.end() - 1, _free_blocks.end()); 39 | return ptr; 40 | } 41 | } 42 | 43 | void memory_pool::Release(void* ptr) 44 | { 45 | memset(ptr, 0, _block_size); 46 | _free_blocks.push_back(ptr); 47 | _busy_blocks.erase( 48 | std::remove(_busy_blocks.begin(), _busy_blocks.end(), ptr), _busy_blocks.end()); 49 | } -------------------------------------------------------------------------------- /LockFreeQueue/Queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | template 4 | class Queue 5 | { 6 | private: 7 | struct Node 8 | { 9 | Node() 10 | { 11 | ptr = nullptr; 12 | next = nullptr; 13 | } 14 | 15 | T* ptr; 16 | Node* next; 17 | }; 18 | 19 | public: 20 | Queue() 21 | { 22 | m_head = new Node; 23 | m_tail = m_head; 24 | m_size = 0; 25 | } 26 | 27 | public: 28 | void Push(T* obj) 29 | { 30 | Node* p = new Node; 31 | p->ptr = obj; 32 | 33 | Node* old_tail; 34 | do 35 | { 36 | old_tail = m_tail; 37 | } while (InterlockedCompareExchange(m_tail->next, NULL, p) != old_tail); 38 | 39 | m_tail->next = p; 40 | m_tail = p; 41 | ++m_size; 42 | } 43 | 44 | bool Pop(T*& obj) 45 | { 46 | if (m_size > 0) 47 | { 48 | obj = m_head->next->ptr; 49 | Node* temp = m_head->next->next; 50 | delete m_head->next; 51 | m_head->next = temp; 52 | --m_size; 53 | return true; 54 | } 55 | else 56 | { 57 | return false; 58 | } 59 | } 60 | 61 | void print() 62 | { 63 | Node* p = m_head->next; 64 | while (p) 65 | { 66 | std::cout << *(p->ptr) << std::endl; 67 | p = p->next; 68 | } 69 | } 70 | 71 | size_t Size() 72 | { 73 | return m_size; 74 | } 75 | 76 | private: 77 | Node* m_head; 78 | Node* m_tail; 79 | size_t m_size; 80 | }; -------------------------------------------------------------------------------- /shared_ptr_copyonwrite.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | std::shared_ptr kNumPtr(new int(0)); 8 | std::mutex kmtx; 9 | 10 | std::shared_ptr getSharedPtr() 11 | { 12 | kmtx.lock(); 13 | std::shared_ptr ptr = kNumPtr; 14 | kmtx.unlock(); 15 | return ptr; 16 | } 17 | 18 | void dosomething(std::shared_ptr ptr) 19 | { 20 | std::cout << "value: " << *ptr << std::endl; 21 | } 22 | 23 | int main() 24 | { 25 | 26 | auto threadProc = [&](){ 27 | for(size_t i = 0; i < 100; ++i) 28 | { 29 | kmtx.lock(); 30 | if(!kNumPtr.unique()){ 31 | kNumPtr.reset(new int(*kNumPtr)); 32 | } 33 | assert(kNumPtr.unique()); 34 | *kNumPtr = *kNumPtr + 1; 35 | kmtx.unlock(); 36 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); 37 | } 38 | }; 39 | 40 | std::thread t1(threadProc); 41 | std::thread t2(threadProc); 42 | std::thread t3( 43 | [&](){ 44 | for(size_t i = 0; i < 100000; ++i) 45 | { 46 | std::shared_ptr ptr = getSharedPtr(); 47 | dosomething(ptr); 48 | } 49 | } 50 | ); 51 | 52 | t1.join(); 53 | t2.join(); 54 | t3.join(); 55 | 56 | std::cout << "kNumPtr's value: " << *kNumPtr << std::endl; 57 | assert(*kNumPtr = 200); 58 | assert(kNumPtr.unique()); 59 | } 60 | -------------------------------------------------------------------------------- /aligned_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "compiler_micro.h" 6 | 7 | #define TEST_TIMES 100000000 8 | 9 | struct CACHELINE_ALIGNMENT Foo { 10 | long a; 11 | long b; 12 | }; 13 | 14 | struct Foo2 { 15 | long x; 16 | long y; 17 | }; 18 | 19 | long a; 20 | Foo fc; 21 | 22 | long b; 23 | Foo2 fc2; 24 | 25 | void func_read(long *ptr) 26 | { 27 | long x = 0; 28 | for (auto i = 0; i < TEST_TIMES; ++i) { 29 | x += *ptr; 30 | } 31 | } 32 | 33 | void func_write(long *ptr) 34 | { 35 | for (auto i = 0; i < TEST_TIMES; ++i) { 36 | *ptr += 1; 37 | } 38 | } 39 | 40 | int main(int argc, char** argv) { 41 | typedef std::chrono::high_resolution_clock Time; 42 | typedef std::chrono::milliseconds ms; 43 | typedef std::chrono::duration fsec; 44 | 45 | auto start = Time::now(); 46 | if (std::atoi(argv[1]) == 0) { 47 | std::thread t1(std::bind(func_read, &(fc.a))); 48 | std::thread t2(std::bind(func_write, &a)); 49 | t1.join();t2.join(); 50 | } else { 51 | std::thread t1(std::bind(func_read, &(fc2.x))); 52 | std::thread t2(std::bind(func_write, &b)); 53 | t1.join();t2.join(); 54 | } 55 | 56 | auto end = Time::now(); 57 | fsec fs = end - start; 58 | auto fs2 = std::chrono::duration_cast(fs); 59 | std::cout << "finished\n"; 60 | std::cout << fs.count() << "s\n"; 61 | std::cout << fs2.count() << "ms\n"; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /protobuf/client/client.cpp: -------------------------------------------------------------------------------- 1 | #include "../pb/im.helloworld.pb.h" 2 | #include "../serialization.hpp" 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace boost::asio; 8 | boost::asio::io_service ioservice; 9 | ip::tcp::socket sock(ioservice); 10 | 11 | void send_handle(const boost::system::error_code& err, std::size_t bytes_transferred) 12 | { 13 | if(err) 14 | { 15 | std::cout << "send_handle error" << std::endl; 16 | } 17 | std::cout << bytes_transferred << "has been transferted" << std::endl; 18 | std::cout << "send sucess" << std::endl; 19 | } 20 | 21 | void connect_handle(const boost::system::error_code& err, const std::string sendbuf) 22 | { 23 | if(err) 24 | { 25 | std::cout << "connect_handle error" << std::endl; 26 | return; 27 | } 28 | static ip::tcp::no_delay option(true); 29 | sock.set_option(option); 30 | int packet_length = sendbuf.length(); 31 | boost::asio::streambuf tempbuf; 32 | boost::asio::write(sock, boost::asio::buffer(&packet_length, sizeof(int))); 33 | async_write(sock, buffer(sendbuf,sendbuf.length()), boost::asio::transfer_exactly(packet_length), 34 | boost::bind(&send_handle, boost::asio::placeholders::error, 35 | boost::asio::placeholders::bytes_transferred)); 36 | } 37 | 38 | int main() 39 | { 40 | ip::tcp::endpoint peer(ip::address_v4::from_string("127.0.0.1"),27015); 41 | im::helloworld msg; 42 | msg.set_usrname("fxp"); 43 | msg.set_passwd("fengxiaopei"); 44 | msg.set_email("xiaopeifenng@gmail.com"); 45 | std::string encode_message = fxp::encode(msg); 46 | sock.async_connect(peer, boost::bind(connect_handle, 47 | boost::asio::placeholders::error, 48 | encode_message)); 49 | ioservice.run(); 50 | } 51 | -------------------------------------------------------------------------------- /siglongjmp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | #define gettid syscall(SYS_gettid) 13 | 14 | sigjmp_buf jmp_ctx; 15 | static int number = 10; 16 | 17 | void 18 | alarm_func(int n) { 19 | printf("%ld enter alarm_func\n", gettid); 20 | number++; 21 | siglongjmp(jmp_ctx, 0); 22 | } 23 | 24 | void 25 | longtimetask(int time_used) { 26 | printf("%ld long time task running\n", gettid); 27 | sleep(time_used); 28 | alarm(0); 29 | } 30 | 31 | void* 32 | worker(void* args) { 33 | printf("enter worker thread: %ld\n", gettid); 34 | signal(SIGALRM, alarm_func); 35 | alarm(5); 36 | 37 | int res = sigsetjmp(jmp_ctx, 0); 38 | printf("tid: %ld sigsetjmp return value: %d\n", gettid, res); 39 | printf("tid: %ld number: %d\n", gettid, number); 40 | if (res) { 41 | goto fail; 42 | } 43 | 44 | longtimetask(atoi((char*)args)); 45 | goto ok; 46 | 47 | fail: 48 | printf("tid: %ld worker fail\n", gettid); 49 | return NULL; 50 | 51 | ok: 52 | printf("tid: %ld worker ok\n", gettid); 53 | return; 54 | } 55 | 56 | void 57 | main_jobs() { 58 | long count = 30; 59 | while (count--) { 60 | printf("tid: %ld, count %ld\n", gettid, count); 61 | sleep(1); 62 | } 63 | } 64 | 65 | int main(int argc, char **argv) { 66 | if (argc != 2) { 67 | printf("usage: %s time\n", argv[0]); 68 | exit(0); 69 | } 70 | 71 | printf("start worker thread!\n"); 72 | 73 | pthread_t th; 74 | pthread_create(&th, NULL, worker, argv[1]); 75 | 76 | main_jobs(); 77 | 78 | pthread_join(th, NULL); 79 | 80 | printf("main thread exit!\n"); 81 | } 82 | 83 | -------------------------------------------------------------------------------- /spawn_say_love_fxp.cc: -------------------------------------------------------------------------------- 1 | #include "boost/bind.hpp" 2 | #include "boost/asio.hpp" 3 | #include "boost/asio/spawn.hpp" 4 | #include 5 | #include 6 | #include 7 | 8 | #define MAX_BUF_LEN 1024 9 | 10 | namespace posix = boost::asio::posix; 11 | 12 | class say_you_love_fxp 13 | { 14 | public: 15 | say_you_love_fxp(boost::asio::io_service& io_service) 16 | : _input(io_service, ::dup(STDIN_FILENO)) 17 | , _output(io_service, ::dup(STDOUT_FILENO)) 18 | , _input_buffer(MAX_BUF_LEN) 19 | {} 20 | 21 | void begin(boost::asio::yield_context yield) 22 | { 23 | std::cout << "please say you love fxp !!!" << std::endl; 24 | boost::asio::async_read_until(_input, _input_buffer, '\n', yield); 25 | 26 | char read_buf[MAX_BUF_LEN] = { 0 }; 27 | _input_buffer.sgetn(read_buf, _input_buffer.size() -1); 28 | _input_buffer.consume(_input_buffer.size()); 29 | std::string str(read_buf); 30 | if (str.find("fxp") != std::string::npos && str.find("love") != std::string::npos){ 31 | boost::asio::async_write(_output, boost::asio::buffer("what? Are u sure, you just said, you love me? \n"), yield); 32 | } 33 | std::cout << "yes ? or no" << std::endl; 34 | _input_buffer.consume(_input_buffer.size()); 35 | memset(read_buf, 0, sizeof(read_buf)); 36 | boost::asio::async_read_until(_input, _input_buffer, '\n', yield); 37 | _input_buffer.sgetn(read_buf, _input_buffer.size() - 1); 38 | str = std::string(read_buf); 39 | if (!str.compare("yes")){ 40 | std::cout << "thanks, I love you too!!!" << std::endl; 41 | } 42 | else if (!str.compare("no")){ 43 | std::cout << "no ? I never know you!!!" << std::endl; 44 | } 45 | else{ 46 | std::cout << "wrong input" << std::endl; 47 | } 48 | } 49 | 50 | private: 51 | posix::stream_descriptor _input; 52 | posix::stream_descriptor _output; 53 | boost::asio::streambuf _input_buffer; 54 | }; 55 | int main(int argc, char** argv) 56 | { 57 | boost::asio::io_service io_service; 58 | say_you_love_fxp say(io_service); 59 | boost::asio::spawn(io_service, boost::bind(&say_you_love_fxp::begin, &say, _1)); 60 | 61 | io_service.run(); 62 | return 0; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /best-sellers-books.cc: -------------------------------------------------------------------------------- 1 | #include "avhttp.hpp" 2 | #include "boost/algorithm/string.hpp" 3 | #include 4 | #include 5 | 6 | class best_seller 7 | { 8 | public: 9 | explicit best_seller(boost::asio::io_service& io) 10 | : _io(io) 11 | , _h(io) 12 | {} 13 | 14 | ~best_seller() 15 | {} 16 | 17 | public: 18 | void start(void) 19 | { 20 | boost::system::error_code ec; 21 | std::string query_url = "http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-recent7-0-0-1-1"; 22 | _h.open(query_url, ec); 23 | if(ec){ 24 | std::cerr << "cannot open the url" << std::endl; 25 | return; 26 | } 27 | handle_process(); 28 | } 29 | 30 | private: 31 | void handle_process() 32 | { 33 | std::istream is(&_h); 34 | std::string str; 35 | while(std::getline(is, str)) { 36 | if(str.find("bang_list clearfix bang_list_mode")) break; 37 | } 38 | 39 | while(is.good()) 40 | { 41 | str.clear(); 42 | std::getline(is, str); 43 | switch(_state) 44 | { 45 | case state_unknown: 46 | boost::trim(str); 47 | if(str.find("div class=\"list_num red\"")){ 48 | _state = state_skip_1; 49 | } 50 | break; 51 | case state_skip_1: _state = state_found;break; 52 | case state_found: 53 | std::cout << str << std::endl; 54 | _state = state_unknown; 55 | default: break; 56 | } 57 | } 58 | } 59 | 60 | private: 61 | enum { 62 | state_unknown, 63 | state_skip_1, 64 | state_found 65 | }_state; 66 | 67 | private: 68 | std::string _url; 69 | boost::asio::io_service& _io; 70 | avhttp::http_stream _h; 71 | }; 72 | 73 | int main(int argc, char** argv) 74 | { 75 | boost::asio::io_service io; 76 | /* 77 | boost::system::error_code ec; 78 | avhttp::http_stream h(io); 79 | h.open(query_url, ec); 80 | if(ec){ 81 | std::cerr << "cannot open the url" << std::endl; 82 | return -1; 83 | } 84 | std::string result; 85 | std::istream is(&h); 86 | 87 | try 88 | { 89 | while(is.good()) 90 | { 91 | std::string str; 92 | std::getline(is, str); 93 | result += str; 94 | } 95 | std::cout << result; 96 | } 97 | catch(std::exception& e) 98 | { 99 | std::cerr << e.what() << std::endl; 100 | return -1; 101 | } 102 | */ 103 | best_seller book(io); 104 | book.start(); 105 | 106 | io.run(); 107 | } 108 | 109 | -------------------------------------------------------------------------------- /async_say_love_fxp.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | #define MAX_BUF_LEN 1024 8 | 9 | namespace posix = boost::asio::posix; 10 | 11 | class say_you_love_fxp 12 | { 13 | public: 14 | say_you_love_fxp(boost::asio::io_service& io_service) 15 | : _input(io_service, ::dup(STDIN_FILENO)) 16 | , _output(io_service, ::dup(STDOUT_FILENO)) 17 | , _input_buffer(MAX_BUF_LEN) 18 | { 19 | } 20 | 21 | void begin() 22 | { 23 | boost::asio::async_read_until(_input, _input_buffer, '\n', 24 | boost::bind(&say_you_love_fxp::handle_read_from_cin, this, 25 | boost::asio::placeholders::error, 26 | boost::asio::placeholders::bytes_transferred)); 27 | } 28 | 29 | private: 30 | void handle_read_from_cin(const boost::system::error_code& error, std::size_t length) 31 | { 32 | if(!error) 33 | { 34 | char read_buf[MAX_BUF_LEN] = {0}; 35 | _input_buffer.sgetn(read_buf, length - 1); 36 | _input_buffer.consume(1); 37 | std::string str(read_buf); 38 | if(str.find("fxp") != std::string::npos && str.find("love") != std::string::npos){ 39 | boost::asio::async_write(_output, boost::asio::buffer("What? Are u sure, you just said, you love me? ...\n"), 40 | boost::bind(&say_you_love_fxp::handle_write_to_console, this, 41 | boost::asio::placeholders::error, 42 | boost::asio::placeholders::bytes_transferred)); 43 | } 44 | } 45 | } 46 | 47 | void handle_write_to_console(const boost::system::error_code& error, std::size_t length) 48 | { 49 | if(!error) 50 | { 51 | std::cout << "yes? or no" << std::endl; 52 | boost::asio::async_read_until(_input, _input_buffer, '\n', 53 | boost::bind(&say_you_love_fxp::handle_input_yes_no, this, 54 | boost::asio::placeholders::error, 55 | boost::asio::placeholders::bytes_transferred)); 56 | } 57 | } 58 | 59 | void handle_input_yes_no(const boost::system::error_code& error, std::size_t length) 60 | { 61 | if(!error) 62 | { 63 | char read_buf[MAX_BUF_LEN] = {0}; 64 | _input_buffer.sgetn(read_buf, length - 1); 65 | _input_buffer.consume(1); 66 | std::string str(read_buf); 67 | if(!str.compare("yes")){ 68 | std::cout << "i love u too..." << std::endl; 69 | }else if(!str.compare("no")){ 70 | std::cout << "i never know you..." << std::endl; 71 | }else{ 72 | std::cout << "yes or no?" << std::endl; 73 | } 74 | } 75 | } 76 | 77 | private: 78 | posix::stream_descriptor _input; 79 | posix::stream_descriptor _output; 80 | boost::asio::streambuf _input_buffer; 81 | }; 82 | 83 | int main() 84 | { 85 | boost::asio::io_service io_service; 86 | say_you_love_fxp say_love(io_service); 87 | say_love.begin(); 88 | 89 | io_service.run(); 90 | } 91 | -------------------------------------------------------------------------------- /protobuf/serialization.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // create message base on message type name 12 | namespace fxp{ 13 | static google::protobuf::Message* create_message(const std::string& type_name) 14 | { 15 | google::protobuf::Message* message = NULL; 16 | const google::protobuf::Descriptor* descriptor = \ 17 | google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(type_name); 18 | if(descriptor) 19 | { 20 | const google::protobuf::Message* prototype = 21 | google::protobuf::MessageFactory::generated_factory()->GetPrototype(descriptor); 22 | if(prototype) 23 | { 24 | message = prototype->New(); 25 | } 26 | } 27 | return message; 28 | } 29 | 30 | static std::string encode(const google::protobuf::Message& message) 31 | { 32 | std::string res; 33 | const std::string& type_name = message.GetTypeName(); 34 | size_t type_len = type_name.length(); 35 | size_t net_len = htonl(type_len); 36 | res.resize(sizeof(size_t)); 37 | res.append(reinterpret_cast(&net_len), sizeof(size_t)); // add type's length 38 | res.append(type_name.c_str(), type_len); // add type's type 39 | bool success = message.AppendToString(&res); 40 | if(success) 41 | { 42 | size_t len = htonl(res.length()); 43 | std::copy(reinterpret_cast(&len), reinterpret_cast(&len) + sizeof(len), 44 | res.begin()); 45 | } 46 | else 47 | { 48 | std::cout << "encode error" << std::endl; 49 | res.clear(); 50 | } 51 | return res; 52 | } 53 | 54 | static google::protobuf::Message* decode(const std::string& buf) 55 | { 56 | if(buf.empty()) return NULL; 57 | const char* ptr = buf.data(); 58 | size_t packet_len = ntohl(*( (size_t*)ptr )); 59 | std::cout << "buf.length: " << buf.length() << std::endl; 60 | std::cout << "packet_len: " << packet_len << std::endl; 61 | if(buf.length() != packet_len) // check the packet's length 62 | { 63 | std::cout << "receved error buf" << std::endl; 64 | return NULL; 65 | } 66 | size_t type_name_len = ntohl(*(size_t*)(ptr + sizeof(size_t))); 67 | const char* type_begin = ptr + 2 * sizeof(size_t); 68 | const char* type_end = type_begin + type_name_len; 69 | std::string type_name(type_begin, type_end); // get the typename 70 | std::cout << "type_name_len: " << type_name_len << std::endl; 71 | std::cout << "type_name: " << type_name << std::endl; 72 | google::protobuf::Message* result = create_message(type_name); // create the protobuf message 73 | if(!result) 74 | { 75 | std::cout << "create_message error, typename is not right" << std::endl; 76 | return result; 77 | } 78 | std::string packet(type_end, buf.length() - 2 * sizeof(size_t) - type_name_len); 79 | if(!result->ParseFromString(packet)) // serialized from string 80 | { 81 | delete result; 82 | std::cout << "ParseFromString error" << std::endl; 83 | return NULL; 84 | } 85 | return result; 86 | } 87 | }; 88 | 89 | -------------------------------------------------------------------------------- /circular_buffer.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #ifndef min 8 | #define min(a,b) (((a) < (b)) ? (a) : (b)) 9 | #endif 10 | 11 | #ifndef max 12 | #define max(a,b) (((a) > (b)) ? (a) : (b)) 13 | #endif 14 | 15 | class circular_buffer 16 | { 17 | public: 18 | circular_buffer(int buf_size) 19 | : m_buffer_size(buf_size) 20 | , m_circle_buffer(NULL) 21 | , m_write_p(0) 22 | , m_read_p(0) 23 | { 24 | m_circle_buffer = new char[m_buffer_size]; 25 | } 26 | 27 | ~circular_buffer() 28 | { 29 | if (m_circle_buffer) 30 | delete[] m_circle_buffer; 31 | m_circle_buffer = NULL; 32 | } 33 | 34 | void clear() 35 | { 36 | m_write_p = 0; 37 | m_read_p = 0; 38 | } 39 | 40 | unsigned int available() 41 | { 42 | return m_buffer_size - (m_write_p - m_read_p); 43 | } 44 | 45 | unsigned int used() 46 | { 47 | return m_write_p - m_read_p; 48 | } 49 | 50 | unsigned int put_data(const char* buffer, unsigned int len) 51 | { 52 | unsigned int l; 53 | len = _min(len, m_buffer_size - m_write_p + m_read_p); 54 | /* first put the data starting from fifo->in to buffer end */ 55 | l = _min(len, m_buffer_size - (m_write_p & (m_buffer_size - 1))); 56 | memcpy(m_circle_buffer + (m_write_p & (m_buffer_size - 1)), buffer, l); 57 | /* then put the rest (if any) at the beginning of the buffer */ 58 | memcpy(m_circle_buffer, buffer + l, len - l); 59 | m_write_p += len; 60 | return len; 61 | } 62 | 63 | unsigned int get_data(char* buffer, unsigned int len) 64 | { 65 | unsigned int l; 66 | len = _min(len, m_write_p - m_read_p); 67 | /* first get the data from fifo->out until the end of the buffer */ 68 | l = _min(len, m_buffer_size - (m_read_p & (m_buffer_size - 1))); 69 | memcpy(buffer, m_circle_buffer + (m_read_p & (m_buffer_size - 1)), l); 70 | /* then get the rest (if any) from the beginning of the buffer */ 71 | memcpy(buffer + l, m_circle_buffer, len - l); 72 | m_read_p += len; 73 | return len; 74 | } 75 | 76 | protected: 77 | inline unsigned int _max(unsigned int a, unsigned int b) 78 | { 79 | return max(a, b); 80 | } 81 | 82 | inline unsigned int _min(unsigned int a, unsigned int b) 83 | { 84 | return min(a, b); 85 | } 86 | 87 | private: 88 | int m_buffer_size; 89 | char* m_circle_buffer; 90 | unsigned int m_write_p; 91 | unsigned int m_read_p; 92 | }; 93 | 94 | int main() 95 | { 96 | circular_buffer buf(1024); 97 | auto producer = [&buf]() 98 | { 99 | std::string str("abcdefg"); 100 | int i = 0; 101 | while(true) 102 | { 103 | if(!buf.put_data(str.c_str() + i, 1)) { 104 | usleep(1); 105 | continue; 106 | } 107 | i = (i + 1) % str.length(); 108 | } 109 | }; 110 | auto consumer = [&buf]() 111 | { 112 | while(true) 113 | { 114 | char c; 115 | if(!buf.get_data(&c, 1)){ 116 | usleep(1); 117 | continue; 118 | } 119 | std::cout << c << std::endl; 120 | } 121 | }; 122 | std::thread thread_producer(producer); 123 | std::thread thread_consumer(consumer); 124 | 125 | thread_producer.join(); 126 | thread_consumer.join(); 127 | } 128 | -------------------------------------------------------------------------------- /protobuf/server/server.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "../serialization.hpp" 11 | #include "../pb/im.helloworld.pb.h" 12 | 13 | using namespace boost::asio; 14 | io_service ioservice; 15 | 16 | class server : public boost::enable_shared_from_this 17 | { 18 | typedef boost::function message_callback_t; 19 | public: 20 | server(short port) 21 | : _acceptor(ioservice, ip::tcp::endpoint(ip::tcp::v4(), port)) 22 | {} 23 | public: 24 | void start() 25 | { 26 | ip::tcp::socket* sock = new ip::tcp::socket(ioservice); 27 | // boost::shared_ptr sock = boost::make_shared(boost::ref(ioservice)); 28 | _acceptor.async_accept(*sock, boost::bind(&server::handle_accept, this, 29 | sock, 30 | boost::asio::placeholders::error)); 31 | } 32 | 33 | void add_message_process_mode(const std::string& name, message_callback_t cb) 34 | { 35 | _message_callbacks[name] = cb; 36 | } 37 | private: 38 | void handle_accept(ip::tcp::socket* sock, const boost::system::error_code& err) 39 | { 40 | if(err) return; 41 | start(); 42 | std::cout << "client: " << sock->remote_endpoint().address().to_string() << " is connected..." << std::endl; 43 | int packet_length; 44 | boost::asio::read(*sock, boost::asio::buffer(&packet_length, sizeof(int))); 45 | std::cout << "length : " << packet_length << std::endl; 46 | 47 | boost::asio::async_read(*sock, _request_buf, boost::asio::transfer_exactly(packet_length), 48 | boost::bind(&server::handle_read, this, 49 | boost::asio::placeholders::error, 50 | boost::asio::placeholders::bytes_transferred)); 51 | } 52 | 53 | void handle_read(const boost::system::error_code& err, size_t bytes_transferred) 54 | { 55 | if(err) return; 56 | std::string tmp_str; tmp_str.resize(bytes_transferred); 57 | _request_buf.sgetn(&tmp_str[0], bytes_transferred); 58 | _request_buf.consume(bytes_transferred); 59 | google::protobuf::Message* message = fxp::decode(tmp_str); 60 | deal_message(message); 61 | } 62 | 63 | void deal_message(google::protobuf::Message* msg) 64 | { 65 | auto iter_map = _message_callbacks.find(msg->GetTypeName()); 66 | if(iter_map != _message_callbacks.end()) 67 | { 68 | iter_map->second(msg); 69 | } 70 | else 71 | { 72 | std::cout << "message dealer no found" << std::endl; 73 | } 74 | } 75 | private: 76 | ip::tcp::acceptor _acceptor; 77 | std::map _message_callbacks; 78 | boost::asio::streambuf _request_buf; 79 | }; 80 | 81 | void deal_with_helloworld(google::protobuf::Message* msg) 82 | { 83 | im::helloworld* msg_deal = dynamic_cast(msg); 84 | if(msg_deal) 85 | { 86 | std::cout << msg_deal->GetTypeName() << std::endl; 87 | std::cout << msg_deal->usrname() << std::endl; 88 | std::cout << msg_deal->passwd() << std::endl; 89 | std::cout << msg_deal->email() << std::endl; 90 | } 91 | delete msg_deal; 92 | } 93 | 94 | int main() 95 | { 96 | short port = 27015; 97 | server s(port); 98 | s.add_message_process_mode("im.helloworld", 99 | boost::bind(&deal_with_helloworld, 100 | _1)); 101 | s.start(); 102 | 103 | ioservice.run(); 104 | } 105 | -------------------------------------------------------------------------------- /say.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #define GIRL_NUM 5 12 | 13 | class foo; 14 | struct Girl; 15 | 16 | void DumpFriendList(); 17 | std::vector > girls; 18 | boost::circular_buffer > > circular_buf(5); 19 | boost::mutex mtx; 20 | 21 | struct Girl 22 | { 23 | Girl(int i) : m_id(i) {} 24 | 25 | void Say() 26 | { 27 | std::cout << "id: " << m_id << " say: " << "i love u, fxp!" << std::endl; 28 | } 29 | 30 | void SaySorry() 31 | { 32 | std::cout << "id: " << m_id << " say: " << "i am sorry, i am late..." << std::endl; 33 | } 34 | 35 | int m_id; 36 | boost::weak_ptr m_ref; 37 | }; 38 | 39 | // some girl say "i love u, fxp" , then she will live more 5 seconds 40 | // if you do not say, you will be deleted after 5 seconds 41 | void Saylove() 42 | { 43 | srand(NULL); 44 | int times = 0; 45 | for (;;) 46 | { 47 | int i = rand() % GIRL_NUM; 48 | girls[i].lock()->Say(); 49 | boost::weak_ptr w_ptr = girls[i].lock()->m_ref; 50 | boost::shared_ptr ptr(w_ptr.lock()); 51 | { 52 | boost::lock_guard guard(mtx); 53 | circular_buf.back().insert(ptr); 54 | } 55 | sleep(1); 56 | times++; 57 | if(times == 5) break; 58 | } 59 | } 60 | 61 | class foo 62 | { 63 | public: 64 | foo( const boost::weak_ptr &ptr ) 65 | : m_friend(ptr) 66 | {} 67 | 68 | ~foo() 69 | { 70 | boost::shared_ptr ptr = m_friend.lock(); 71 | if (ptr) 72 | { 73 | ptr->SaySorry(); 74 | } 75 | } 76 | 77 | bool operator < (const foo &rfs) const 78 | { 79 | return true; 80 | } 81 | 82 | private: 83 | boost::weak_ptr m_friend; 84 | }; 85 | 86 | //this is the judger, he can judge if you have said "i love u, fxp" in 5 seconds 87 | //if not, then you will be move out. 88 | void Judger() 89 | { 90 | while (true) 91 | { 92 | boost::unordered_set > s; 93 | { 94 | boost::lock_guard guard(mtx); 95 | // DumpFriendList(); 96 | circular_buf.push_back(s); 97 | } 98 | 99 | sleep(1); 100 | } 101 | } 102 | 103 | void DumpFriendList() 104 | { 105 | std::cout << "size = " << circular_buf.size() << std::endl; 106 | typedef boost::circular_buffer > > BUFF; 107 | int i = 0; 108 | for(BUFF::iterator iter = circular_buf.begin(); iter != circular_buf.end(); ++iter,++i) 109 | { 110 | boost::unordered_set > &ref = *iter; 111 | std::cout << i << " len : " << ref.size() << std::endl; 112 | boost::unordered_set >::iterator iterj = ref.begin(); 113 | for(; iterj != ref.end(); ++iterj) 114 | { 115 | std::cout << "usecount: " << iterj->use_count() << std::endl; 116 | } 117 | } 118 | } 119 | 120 | int main(int argc, char* argv[]) 121 | { 122 | //test five girl 123 | boost::shared_ptr ptr1(new Girl(1)); 124 | boost::shared_ptr ptr2(new Girl(2)); 125 | boost::shared_ptr ptr3(new Girl(3)); 126 | boost::shared_ptr ptr4(new Girl(4)); 127 | boost::shared_ptr ptr5(new Girl(5)); 128 | 129 | boost::weak_ptr w1(ptr1); 130 | boost::weak_ptr w2(ptr2); 131 | boost::weak_ptr w3(ptr3); 132 | boost::weak_ptr w4(ptr4); 133 | boost::weak_ptr w5(ptr5); 134 | 135 | girls.push_back(w1); 136 | girls.push_back(w2); 137 | girls.push_back(w3); 138 | girls.push_back(w4); 139 | girls.push_back(w5); 140 | 141 | { 142 | boost::shared_ptr foo1(new foo(w1)); 143 | boost::weak_ptr w_ptr1(foo1); 144 | girls[0].lock()->m_ref = w_ptr1; 145 | std::cout << "foo1.usecount: " << foo1.use_count() << std::endl; 146 | boost::shared_ptr foo2(new foo(w2)); 147 | boost::weak_ptr w_ptr2(foo2); 148 | girls[1].lock()->m_ref = w_ptr2; 149 | 150 | boost::shared_ptr foo3(new foo(w3)); 151 | boost::weak_ptr w_ptr3(foo3); 152 | girls[2].lock()->m_ref = w_ptr3; 153 | 154 | boost::shared_ptr foo4(new foo(w4)); 155 | boost::weak_ptr w_ptr4(foo4); 156 | girls[3].lock()->m_ref = w_ptr4; 157 | 158 | boost::shared_ptr foo5(new foo(w5)); 159 | boost::weak_ptr w_ptr5(foo5); 160 | girls[4].lock()->m_ref = w_ptr5; 161 | 162 | circular_buf.resize(5); 163 | 164 | std::cout << "foo1.usecount: " << foo1.use_count() << std::endl; 165 | circular_buf.back().insert(foo1); 166 | circular_buf.back().insert(foo2); 167 | circular_buf.back().insert(foo3); 168 | circular_buf.back().insert(foo4); 169 | circular_buf.back().insert(foo5); 170 | } 171 | 172 | 173 | boost::thread t(Judger); 174 | boost::thread t2(Saylove); 175 | 176 | t.join(); 177 | t2.join(); 178 | 179 | return 0; 180 | } 181 | -------------------------------------------------------------------------------- /ScopeGuard.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Facebook, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | namespace folly { 30 | 31 | /** 32 | * ScopeGuard is a general implementation of the "Initialization is 33 | * Resource Acquisition" idiom. Basically, it guarantees that a function 34 | * is executed upon leaving the currrent scope unless otherwise told. 35 | * 36 | * The makeGuard() function is used to create a new ScopeGuard object. 37 | * It can be instantiated with a lambda function, a std::function, 38 | * a functor, or a void(*)() function pointer. 39 | * 40 | * 41 | * Usage example: Add a friend to memory if and only if it is also added 42 | * to the db. 43 | * 44 | * void User::addFriend(User& newFriend) { 45 | * // add the friend to memory 46 | * friends_.push_back(&newFriend); 47 | * 48 | * // If the db insertion that follows fails, we should 49 | * // remove it from memory. 50 | * // (You could also declare this as "auto guard = makeGuard(...)") 51 | * ScopeGuard guard = makeGuard([&] { friends_.pop_back(); }); 52 | * 53 | * // this will throw an exception upon error, which 54 | * // makes the ScopeGuard execute UserCont::pop_back() 55 | * // once the Guard's destructor is called. 56 | * db_->addFriend(GetName(), newFriend.GetName()); 57 | * 58 | * // an exception was not thrown, so don't execute 59 | * // the Guard. 60 | * guard.dismiss(); 61 | * } 62 | * 63 | * Examine ScopeGuardTest.cpp for some more sample usage. 64 | * 65 | * Stolen from: 66 | * Andrei's and Petru Marginean's CUJ article: 67 | * http://drdobbs.com/184403758 68 | * and the loki library: 69 | * http://loki-lib.sourceforge.net/index.php?n=Idioms.ScopeGuardPointer 70 | * and triendl.kj article: 71 | * http://www.codeproject.com/KB/cpp/scope_guard.aspx 72 | */ 73 | class ScopeGuardImplBase { 74 | public: 75 | void dismiss() noexcept { 76 | dismissed_ = true; 77 | } 78 | 79 | template 80 | FOLLY_ALWAYS_INLINE static void runAndWarnAboutToCrashOnException( 81 | T& function) noexcept { 82 | try { 83 | function(); 84 | } catch (...) { 85 | warnAboutToCrash(); 86 | std::terminate(); 87 | } 88 | } 89 | 90 | protected: 91 | ScopeGuardImplBase() noexcept : dismissed_(false) {} 92 | 93 | static ScopeGuardImplBase makeEmptyScopeGuard() noexcept { 94 | return ScopeGuardImplBase{}; 95 | } 96 | 97 | template 98 | static const T& asConst(const T& t) noexcept { 99 | return t; 100 | } 101 | 102 | bool dismissed_; 103 | 104 | private: 105 | static void warnAboutToCrash() noexcept; 106 | }; 107 | 108 | template 109 | class ScopeGuardImpl : public ScopeGuardImplBase { 110 | public: 111 | explicit ScopeGuardImpl(FunctionType& fn) noexcept( 112 | std::is_nothrow_copy_constructible::value) 113 | : ScopeGuardImpl( 114 | asConst(fn), 115 | makeFailsafe(std::is_nothrow_copy_constructible{}, 116 | &fn)) {} 117 | 118 | explicit ScopeGuardImpl(const FunctionType& fn) noexcept( 119 | std::is_nothrow_copy_constructible::value) 120 | : ScopeGuardImpl( 121 | fn, 122 | makeFailsafe(std::is_nothrow_copy_constructible{}, 123 | &fn)) {} 124 | 125 | explicit ScopeGuardImpl(FunctionType&& fn) noexcept( 126 | std::is_nothrow_move_constructible::value) 127 | : ScopeGuardImpl( 128 | std::move_if_noexcept(fn), 129 | makeFailsafe(std::is_nothrow_move_constructible{}, 130 | &fn)) {} 131 | 132 | ScopeGuardImpl(ScopeGuardImpl&& other) noexcept( 133 | std::is_nothrow_move_constructible::value) 134 | : function_(std::move_if_noexcept(other.function_)) { 135 | // If the above line attempts a copy and the copy throws, other is 136 | // left owning the cleanup action and will execute it (or not) depending 137 | // on the value of other.dismissed_. The following lines only execute 138 | // if the move/copy succeeded, in which case *this assumes ownership of 139 | // the cleanup action and dismisses other. 140 | dismissed_ = other.dismissed_; 141 | other.dismissed_ = true; 142 | } 143 | 144 | ~ScopeGuardImpl() noexcept { 145 | if (!dismissed_) { 146 | execute(); 147 | } 148 | } 149 | 150 | private: 151 | static ScopeGuardImplBase makeFailsafe(std::true_type, const void*) noexcept { 152 | return makeEmptyScopeGuard(); 153 | } 154 | 155 | template 156 | static auto makeFailsafe(std::false_type, Fn* fn) noexcept 157 | -> ScopeGuardImpl { 158 | return ScopeGuardImpl{std::ref(*fn)}; 159 | } 160 | 161 | template 162 | explicit ScopeGuardImpl(Fn&& fn, ScopeGuardImplBase&& failsafe) 163 | : ScopeGuardImplBase{}, function_(std::forward(fn)) { 164 | failsafe.dismiss(); 165 | } 166 | 167 | void* operator new(std::size_t) = delete; 168 | 169 | void execute() noexcept { 170 | runAndWarnAboutToCrashOnException(function_); 171 | } 172 | 173 | FunctionType function_; 174 | }; 175 | 176 | template 177 | ScopeGuardImpl::type> 178 | makeGuard(FunctionType&& fn) noexcept( 179 | std::is_nothrow_constructible::type, 180 | FunctionType>::value) { 181 | return ScopeGuardImpl::type>( 182 | std::forward(fn)); 183 | } 184 | 185 | /** 186 | * This is largely unneeded if you just use auto for your guards. 187 | */ 188 | typedef ScopeGuardImplBase&& ScopeGuard; 189 | 190 | namespace detail { 191 | 192 | #if defined(FOLLY_EXCEPTION_COUNT_USE_CXA_GET_GLOBALS) || \ 193 | defined(FOLLY_EXCEPTION_COUNT_USE_GETPTD) || \ 194 | defined(FOLLY_EXCEPTION_COUNT_USE_STD) 195 | 196 | /** 197 | * ScopeGuard used for executing a function when leaving the current scope 198 | * depending on the presence of a new uncaught exception. 199 | * 200 | * If the executeOnException template parameter is true, the function is 201 | * executed if a new uncaught exception is present at the end of the scope. 202 | * If the parameter is false, then the function is executed if no new uncaught 203 | * exceptions are present at the end of the scope. 204 | * 205 | * Used to implement SCOPE_FAIL and SCOPE_SUCCESS below. 206 | */ 207 | template 208 | class ScopeGuardForNewException { 209 | public: 210 | explicit ScopeGuardForNewException(const FunctionType& fn) 211 | : function_(fn) { 212 | } 213 | 214 | explicit ScopeGuardForNewException(FunctionType&& fn) 215 | : function_(std::move(fn)) { 216 | } 217 | 218 | ScopeGuardForNewException(ScopeGuardForNewException&& other) 219 | : function_(std::move(other.function_)) 220 | , exceptionCounter_(std::move(other.exceptionCounter_)) { 221 | } 222 | 223 | ~ScopeGuardForNewException() noexcept(executeOnException) { 224 | if (executeOnException == exceptionCounter_.isNewUncaughtException()) { 225 | if (executeOnException) { 226 | ScopeGuardImplBase::runAndWarnAboutToCrashOnException(function_); 227 | } else { 228 | function_(); 229 | } 230 | } 231 | } 232 | 233 | private: 234 | ScopeGuardForNewException(const ScopeGuardForNewException& other) = delete; 235 | 236 | void* operator new(std::size_t) = delete; 237 | 238 | FunctionType function_; 239 | UncaughtExceptionCounter exceptionCounter_; 240 | }; 241 | 242 | /** 243 | * Internal use for the macro SCOPE_FAIL below 244 | */ 245 | enum class ScopeGuardOnFail {}; 246 | 247 | template 248 | ScopeGuardForNewException::type, true> 249 | operator+(detail::ScopeGuardOnFail, FunctionType&& fn) { 250 | return 251 | ScopeGuardForNewException::type, true>( 252 | std::forward(fn)); 253 | } 254 | 255 | /** 256 | * Internal use for the macro SCOPE_SUCCESS below 257 | */ 258 | enum class ScopeGuardOnSuccess {}; 259 | 260 | template 261 | ScopeGuardForNewException::type, false> 262 | operator+(ScopeGuardOnSuccess, FunctionType&& fn) { 263 | return 264 | ScopeGuardForNewException::type, false>( 265 | std::forward(fn)); 266 | } 267 | 268 | #endif // native uncaught_exception() supported 269 | 270 | /** 271 | * Internal use for the macro SCOPE_EXIT below 272 | */ 273 | enum class ScopeGuardOnExit {}; 274 | 275 | template 276 | ScopeGuardImpl::type> 277 | operator+(detail::ScopeGuardOnExit, FunctionType&& fn) { 278 | return ScopeGuardImpl::type>( 279 | std::forward(fn)); 280 | } 281 | } // namespace detail 282 | 283 | } // folly 284 | 285 | #define SCOPE_EXIT \ 286 | auto FB_ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE) \ 287 | = ::folly::detail::ScopeGuardOnExit() + [&]() noexcept 288 | 289 | #if defined(FOLLY_EXCEPTION_COUNT_USE_CXA_GET_GLOBALS) || \ 290 | defined(FOLLY_EXCEPTION_COUNT_USE_GETPTD) || \ 291 | defined(FOLLY_EXCEPTION_COUNT_USE_STD) 292 | #define SCOPE_FAIL \ 293 | auto FB_ANONYMOUS_VARIABLE(SCOPE_FAIL_STATE) \ 294 | = ::folly::detail::ScopeGuardOnFail() + [&]() noexcept 295 | 296 | #define SCOPE_SUCCESS \ 297 | auto FB_ANONYMOUS_VARIABLE(SCOPE_SUCCESS_STATE) \ 298 | = ::folly::detail::ScopeGuardOnSuccess() + [&]() 299 | #endif // native uncaught_exception() supported 300 | -------------------------------------------------------------------------------- /protobuf/pb/im.helloworld.pb.h: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: im.helloworld.proto 3 | 4 | #ifndef PROTOBUF_im_2ehelloworld_2eproto__INCLUDED 5 | #define PROTOBUF_im_2ehelloworld_2eproto__INCLUDED 6 | 7 | #include 8 | 9 | #include 10 | 11 | #if GOOGLE_PROTOBUF_VERSION < 2005000 12 | #error This file was generated by a newer version of protoc which is 13 | #error incompatible with your Protocol Buffer headers. Please update 14 | #error your headers. 15 | #endif 16 | #if 2005000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 17 | #error This file was generated by an older version of protoc which is 18 | #error incompatible with your Protocol Buffer headers. Please 19 | #error regenerate this file with a newer version of protoc. 20 | #endif 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | // @@protoc_insertion_point(includes) 28 | 29 | namespace im { 30 | 31 | // Internal implementation detail -- do not call these. 32 | void protobuf_AddDesc_im_2ehelloworld_2eproto(); 33 | void protobuf_AssignDesc_im_2ehelloworld_2eproto(); 34 | void protobuf_ShutdownFile_im_2ehelloworld_2eproto(); 35 | 36 | class helloworld; 37 | 38 | // =================================================================== 39 | 40 | class helloworld : public ::google::protobuf::Message { 41 | public: 42 | helloworld(); 43 | virtual ~helloworld(); 44 | 45 | helloworld(const helloworld& from); 46 | 47 | inline helloworld& operator=(const helloworld& from) { 48 | CopyFrom(from); 49 | return *this; 50 | } 51 | 52 | inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { 53 | return _unknown_fields_; 54 | } 55 | 56 | inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { 57 | return &_unknown_fields_; 58 | } 59 | 60 | static const ::google::protobuf::Descriptor* descriptor(); 61 | static const helloworld& default_instance(); 62 | 63 | void Swap(helloworld* other); 64 | 65 | // implements Message ---------------------------------------------- 66 | 67 | helloworld* New() const; 68 | void CopyFrom(const ::google::protobuf::Message& from); 69 | void MergeFrom(const ::google::protobuf::Message& from); 70 | void CopyFrom(const helloworld& from); 71 | void MergeFrom(const helloworld& from); 72 | void Clear(); 73 | bool IsInitialized() const; 74 | 75 | int ByteSize() const; 76 | bool MergePartialFromCodedStream( 77 | ::google::protobuf::io::CodedInputStream* input); 78 | void SerializeWithCachedSizes( 79 | ::google::protobuf::io::CodedOutputStream* output) const; 80 | ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; 81 | int GetCachedSize() const { return _cached_size_; } 82 | private: 83 | void SharedCtor(); 84 | void SharedDtor(); 85 | void SetCachedSize(int size) const; 86 | public: 87 | 88 | ::google::protobuf::Metadata GetMetadata() const; 89 | 90 | // nested types ---------------------------------------------------- 91 | 92 | // accessors ------------------------------------------------------- 93 | 94 | // required string usrname = 1; 95 | inline bool has_usrname() const; 96 | inline void clear_usrname(); 97 | static const int kUsrnameFieldNumber = 1; 98 | inline const ::std::string& usrname() const; 99 | inline void set_usrname(const ::std::string& value); 100 | inline void set_usrname(const char* value); 101 | inline void set_usrname(const char* value, size_t size); 102 | inline ::std::string* mutable_usrname(); 103 | inline ::std::string* release_usrname(); 104 | inline void set_allocated_usrname(::std::string* usrname); 105 | 106 | // required string passwd = 2; 107 | inline bool has_passwd() const; 108 | inline void clear_passwd(); 109 | static const int kPasswdFieldNumber = 2; 110 | inline const ::std::string& passwd() const; 111 | inline void set_passwd(const ::std::string& value); 112 | inline void set_passwd(const char* value); 113 | inline void set_passwd(const char* value, size_t size); 114 | inline ::std::string* mutable_passwd(); 115 | inline ::std::string* release_passwd(); 116 | inline void set_allocated_passwd(::std::string* passwd); 117 | 118 | // optional string email = 3; 119 | inline bool has_email() const; 120 | inline void clear_email(); 121 | static const int kEmailFieldNumber = 3; 122 | inline const ::std::string& email() const; 123 | inline void set_email(const ::std::string& value); 124 | inline void set_email(const char* value); 125 | inline void set_email(const char* value, size_t size); 126 | inline ::std::string* mutable_email(); 127 | inline ::std::string* release_email(); 128 | inline void set_allocated_email(::std::string* email); 129 | 130 | // @@protoc_insertion_point(class_scope:im.helloworld) 131 | private: 132 | inline void set_has_usrname(); 133 | inline void clear_has_usrname(); 134 | inline void set_has_passwd(); 135 | inline void clear_has_passwd(); 136 | inline void set_has_email(); 137 | inline void clear_has_email(); 138 | 139 | ::google::protobuf::UnknownFieldSet _unknown_fields_; 140 | 141 | ::std::string* usrname_; 142 | ::std::string* passwd_; 143 | ::std::string* email_; 144 | 145 | mutable int _cached_size_; 146 | ::google::protobuf::uint32 _has_bits_[(3 + 31) / 32]; 147 | 148 | friend void protobuf_AddDesc_im_2ehelloworld_2eproto(); 149 | friend void protobuf_AssignDesc_im_2ehelloworld_2eproto(); 150 | friend void protobuf_ShutdownFile_im_2ehelloworld_2eproto(); 151 | 152 | void InitAsDefaultInstance(); 153 | static helloworld* default_instance_; 154 | }; 155 | // =================================================================== 156 | 157 | 158 | // =================================================================== 159 | 160 | // helloworld 161 | 162 | // required string usrname = 1; 163 | inline bool helloworld::has_usrname() const { 164 | return (_has_bits_[0] & 0x00000001u) != 0; 165 | } 166 | inline void helloworld::set_has_usrname() { 167 | _has_bits_[0] |= 0x00000001u; 168 | } 169 | inline void helloworld::clear_has_usrname() { 170 | _has_bits_[0] &= ~0x00000001u; 171 | } 172 | inline void helloworld::clear_usrname() { 173 | if (usrname_ != &::google::protobuf::internal::kEmptyString) { 174 | usrname_->clear(); 175 | } 176 | clear_has_usrname(); 177 | } 178 | inline const ::std::string& helloworld::usrname() const { 179 | return *usrname_; 180 | } 181 | inline void helloworld::set_usrname(const ::std::string& value) { 182 | set_has_usrname(); 183 | if (usrname_ == &::google::protobuf::internal::kEmptyString) { 184 | usrname_ = new ::std::string; 185 | } 186 | usrname_->assign(value); 187 | } 188 | inline void helloworld::set_usrname(const char* value) { 189 | set_has_usrname(); 190 | if (usrname_ == &::google::protobuf::internal::kEmptyString) { 191 | usrname_ = new ::std::string; 192 | } 193 | usrname_->assign(value); 194 | } 195 | inline void helloworld::set_usrname(const char* value, size_t size) { 196 | set_has_usrname(); 197 | if (usrname_ == &::google::protobuf::internal::kEmptyString) { 198 | usrname_ = new ::std::string; 199 | } 200 | usrname_->assign(reinterpret_cast(value), size); 201 | } 202 | inline ::std::string* helloworld::mutable_usrname() { 203 | set_has_usrname(); 204 | if (usrname_ == &::google::protobuf::internal::kEmptyString) { 205 | usrname_ = new ::std::string; 206 | } 207 | return usrname_; 208 | } 209 | inline ::std::string* helloworld::release_usrname() { 210 | clear_has_usrname(); 211 | if (usrname_ == &::google::protobuf::internal::kEmptyString) { 212 | return NULL; 213 | } else { 214 | ::std::string* temp = usrname_; 215 | usrname_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 216 | return temp; 217 | } 218 | } 219 | inline void helloworld::set_allocated_usrname(::std::string* usrname) { 220 | if (usrname_ != &::google::protobuf::internal::kEmptyString) { 221 | delete usrname_; 222 | } 223 | if (usrname) { 224 | set_has_usrname(); 225 | usrname_ = usrname; 226 | } else { 227 | clear_has_usrname(); 228 | usrname_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 229 | } 230 | } 231 | 232 | // required string passwd = 2; 233 | inline bool helloworld::has_passwd() const { 234 | return (_has_bits_[0] & 0x00000002u) != 0; 235 | } 236 | inline void helloworld::set_has_passwd() { 237 | _has_bits_[0] |= 0x00000002u; 238 | } 239 | inline void helloworld::clear_has_passwd() { 240 | _has_bits_[0] &= ~0x00000002u; 241 | } 242 | inline void helloworld::clear_passwd() { 243 | if (passwd_ != &::google::protobuf::internal::kEmptyString) { 244 | passwd_->clear(); 245 | } 246 | clear_has_passwd(); 247 | } 248 | inline const ::std::string& helloworld::passwd() const { 249 | return *passwd_; 250 | } 251 | inline void helloworld::set_passwd(const ::std::string& value) { 252 | set_has_passwd(); 253 | if (passwd_ == &::google::protobuf::internal::kEmptyString) { 254 | passwd_ = new ::std::string; 255 | } 256 | passwd_->assign(value); 257 | } 258 | inline void helloworld::set_passwd(const char* value) { 259 | set_has_passwd(); 260 | if (passwd_ == &::google::protobuf::internal::kEmptyString) { 261 | passwd_ = new ::std::string; 262 | } 263 | passwd_->assign(value); 264 | } 265 | inline void helloworld::set_passwd(const char* value, size_t size) { 266 | set_has_passwd(); 267 | if (passwd_ == &::google::protobuf::internal::kEmptyString) { 268 | passwd_ = new ::std::string; 269 | } 270 | passwd_->assign(reinterpret_cast(value), size); 271 | } 272 | inline ::std::string* helloworld::mutable_passwd() { 273 | set_has_passwd(); 274 | if (passwd_ == &::google::protobuf::internal::kEmptyString) { 275 | passwd_ = new ::std::string; 276 | } 277 | return passwd_; 278 | } 279 | inline ::std::string* helloworld::release_passwd() { 280 | clear_has_passwd(); 281 | if (passwd_ == &::google::protobuf::internal::kEmptyString) { 282 | return NULL; 283 | } else { 284 | ::std::string* temp = passwd_; 285 | passwd_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 286 | return temp; 287 | } 288 | } 289 | inline void helloworld::set_allocated_passwd(::std::string* passwd) { 290 | if (passwd_ != &::google::protobuf::internal::kEmptyString) { 291 | delete passwd_; 292 | } 293 | if (passwd) { 294 | set_has_passwd(); 295 | passwd_ = passwd; 296 | } else { 297 | clear_has_passwd(); 298 | passwd_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 299 | } 300 | } 301 | 302 | // optional string email = 3; 303 | inline bool helloworld::has_email() const { 304 | return (_has_bits_[0] & 0x00000004u) != 0; 305 | } 306 | inline void helloworld::set_has_email() { 307 | _has_bits_[0] |= 0x00000004u; 308 | } 309 | inline void helloworld::clear_has_email() { 310 | _has_bits_[0] &= ~0x00000004u; 311 | } 312 | inline void helloworld::clear_email() { 313 | if (email_ != &::google::protobuf::internal::kEmptyString) { 314 | email_->clear(); 315 | } 316 | clear_has_email(); 317 | } 318 | inline const ::std::string& helloworld::email() const { 319 | return *email_; 320 | } 321 | inline void helloworld::set_email(const ::std::string& value) { 322 | set_has_email(); 323 | if (email_ == &::google::protobuf::internal::kEmptyString) { 324 | email_ = new ::std::string; 325 | } 326 | email_->assign(value); 327 | } 328 | inline void helloworld::set_email(const char* value) { 329 | set_has_email(); 330 | if (email_ == &::google::protobuf::internal::kEmptyString) { 331 | email_ = new ::std::string; 332 | } 333 | email_->assign(value); 334 | } 335 | inline void helloworld::set_email(const char* value, size_t size) { 336 | set_has_email(); 337 | if (email_ == &::google::protobuf::internal::kEmptyString) { 338 | email_ = new ::std::string; 339 | } 340 | email_->assign(reinterpret_cast(value), size); 341 | } 342 | inline ::std::string* helloworld::mutable_email() { 343 | set_has_email(); 344 | if (email_ == &::google::protobuf::internal::kEmptyString) { 345 | email_ = new ::std::string; 346 | } 347 | return email_; 348 | } 349 | inline ::std::string* helloworld::release_email() { 350 | clear_has_email(); 351 | if (email_ == &::google::protobuf::internal::kEmptyString) { 352 | return NULL; 353 | } else { 354 | ::std::string* temp = email_; 355 | email_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 356 | return temp; 357 | } 358 | } 359 | inline void helloworld::set_allocated_email(::std::string* email) { 360 | if (email_ != &::google::protobuf::internal::kEmptyString) { 361 | delete email_; 362 | } 363 | if (email) { 364 | set_has_email(); 365 | email_ = email; 366 | } else { 367 | clear_has_email(); 368 | email_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 369 | } 370 | } 371 | 372 | 373 | // @@protoc_insertion_point(namespace_scope) 374 | 375 | } // namespace im 376 | 377 | #ifndef SWIG 378 | namespace google { 379 | namespace protobuf { 380 | 381 | 382 | } // namespace google 383 | } // namespace protobuf 384 | #endif // SWIG 385 | 386 | // @@protoc_insertion_point(global_scope) 387 | 388 | #endif // PROTOBUF_im_2ehelloworld_2eproto__INCLUDED 389 | -------------------------------------------------------------------------------- /protobuf/pb/im.helloworld.pb.cc: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: im.helloworld.proto 3 | 4 | #define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION 5 | #include "im.helloworld.pb.h" 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | // @@protoc_insertion_point(includes) 18 | 19 | namespace im { 20 | 21 | namespace { 22 | 23 | const ::google::protobuf::Descriptor* helloworld_descriptor_ = NULL; 24 | const ::google::protobuf::internal::GeneratedMessageReflection* 25 | helloworld_reflection_ = NULL; 26 | 27 | } // namespace 28 | 29 | 30 | void protobuf_AssignDesc_im_2ehelloworld_2eproto() { 31 | protobuf_AddDesc_im_2ehelloworld_2eproto(); 32 | const ::google::protobuf::FileDescriptor* file = 33 | ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName( 34 | "im.helloworld.proto"); 35 | GOOGLE_CHECK(file != NULL); 36 | helloworld_descriptor_ = file->message_type(0); 37 | static const int helloworld_offsets_[3] = { 38 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, usrname_), 39 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, passwd_), 40 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, email_), 41 | }; 42 | helloworld_reflection_ = 43 | new ::google::protobuf::internal::GeneratedMessageReflection( 44 | helloworld_descriptor_, 45 | helloworld::default_instance_, 46 | helloworld_offsets_, 47 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, _has_bits_[0]), 48 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, _unknown_fields_), 49 | -1, 50 | ::google::protobuf::DescriptorPool::generated_pool(), 51 | ::google::protobuf::MessageFactory::generated_factory(), 52 | sizeof(helloworld)); 53 | } 54 | 55 | namespace { 56 | 57 | GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_); 58 | inline void protobuf_AssignDescriptorsOnce() { 59 | ::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_, 60 | &protobuf_AssignDesc_im_2ehelloworld_2eproto); 61 | } 62 | 63 | void protobuf_RegisterTypes(const ::std::string&) { 64 | protobuf_AssignDescriptorsOnce(); 65 | ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( 66 | helloworld_descriptor_, &helloworld::default_instance()); 67 | } 68 | 69 | } // namespace 70 | 71 | void protobuf_ShutdownFile_im_2ehelloworld_2eproto() { 72 | delete helloworld::default_instance_; 73 | delete helloworld_reflection_; 74 | } 75 | 76 | void protobuf_AddDesc_im_2ehelloworld_2eproto() { 77 | static bool already_here = false; 78 | if (already_here) return; 79 | already_here = true; 80 | GOOGLE_PROTOBUF_VERIFY_VERSION; 81 | 82 | ::google::protobuf::DescriptorPool::InternalAddGeneratedFile( 83 | "\n\023im.helloworld.proto\022\002im\"<\n\nhelloworld\022" 84 | "\017\n\007usrname\030\001 \002(\t\022\016\n\006passwd\030\002 \002(\t\022\r\n\005emai" 85 | "l\030\003 \001(\t", 87); 86 | ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile( 87 | "im.helloworld.proto", &protobuf_RegisterTypes); 88 | helloworld::default_instance_ = new helloworld(); 89 | helloworld::default_instance_->InitAsDefaultInstance(); 90 | ::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_im_2ehelloworld_2eproto); 91 | } 92 | 93 | // Force AddDescriptors() to be called at static initialization time. 94 | struct StaticDescriptorInitializer_im_2ehelloworld_2eproto { 95 | StaticDescriptorInitializer_im_2ehelloworld_2eproto() { 96 | protobuf_AddDesc_im_2ehelloworld_2eproto(); 97 | } 98 | } static_descriptor_initializer_im_2ehelloworld_2eproto_; 99 | 100 | // =================================================================== 101 | 102 | #ifndef _MSC_VER 103 | const int helloworld::kUsrnameFieldNumber; 104 | const int helloworld::kPasswdFieldNumber; 105 | const int helloworld::kEmailFieldNumber; 106 | #endif // !_MSC_VER 107 | 108 | helloworld::helloworld() 109 | : ::google::protobuf::Message() { 110 | SharedCtor(); 111 | } 112 | 113 | void helloworld::InitAsDefaultInstance() { 114 | } 115 | 116 | helloworld::helloworld(const helloworld& from) 117 | : ::google::protobuf::Message() { 118 | SharedCtor(); 119 | MergeFrom(from); 120 | } 121 | 122 | void helloworld::SharedCtor() { 123 | _cached_size_ = 0; 124 | usrname_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 125 | passwd_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 126 | email_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 127 | ::memset(_has_bits_, 0, sizeof(_has_bits_)); 128 | } 129 | 130 | helloworld::~helloworld() { 131 | SharedDtor(); 132 | } 133 | 134 | void helloworld::SharedDtor() { 135 | if (usrname_ != &::google::protobuf::internal::kEmptyString) { 136 | delete usrname_; 137 | } 138 | if (passwd_ != &::google::protobuf::internal::kEmptyString) { 139 | delete passwd_; 140 | } 141 | if (email_ != &::google::protobuf::internal::kEmptyString) { 142 | delete email_; 143 | } 144 | if (this != default_instance_) { 145 | } 146 | } 147 | 148 | void helloworld::SetCachedSize(int size) const { 149 | GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); 150 | _cached_size_ = size; 151 | GOOGLE_SAFE_CONCURRENT_WRITES_END(); 152 | } 153 | const ::google::protobuf::Descriptor* helloworld::descriptor() { 154 | protobuf_AssignDescriptorsOnce(); 155 | return helloworld_descriptor_; 156 | } 157 | 158 | const helloworld& helloworld::default_instance() { 159 | if (default_instance_ == NULL) protobuf_AddDesc_im_2ehelloworld_2eproto(); 160 | return *default_instance_; 161 | } 162 | 163 | helloworld* helloworld::default_instance_ = NULL; 164 | 165 | helloworld* helloworld::New() const { 166 | return new helloworld; 167 | } 168 | 169 | void helloworld::Clear() { 170 | if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { 171 | if (has_usrname()) { 172 | if (usrname_ != &::google::protobuf::internal::kEmptyString) { 173 | usrname_->clear(); 174 | } 175 | } 176 | if (has_passwd()) { 177 | if (passwd_ != &::google::protobuf::internal::kEmptyString) { 178 | passwd_->clear(); 179 | } 180 | } 181 | if (has_email()) { 182 | if (email_ != &::google::protobuf::internal::kEmptyString) { 183 | email_->clear(); 184 | } 185 | } 186 | } 187 | ::memset(_has_bits_, 0, sizeof(_has_bits_)); 188 | mutable_unknown_fields()->Clear(); 189 | } 190 | 191 | bool helloworld::MergePartialFromCodedStream( 192 | ::google::protobuf::io::CodedInputStream* input) { 193 | #define DO_(EXPRESSION) if (!(EXPRESSION)) return false 194 | ::google::protobuf::uint32 tag; 195 | while ((tag = input->ReadTag()) != 0) { 196 | switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { 197 | // required string usrname = 1; 198 | case 1: { 199 | if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 200 | ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { 201 | DO_(::google::protobuf::internal::WireFormatLite::ReadString( 202 | input, this->mutable_usrname())); 203 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 204 | this->usrname().data(), this->usrname().length(), 205 | ::google::protobuf::internal::WireFormat::PARSE); 206 | } else { 207 | goto handle_uninterpreted; 208 | } 209 | if (input->ExpectTag(18)) goto parse_passwd; 210 | break; 211 | } 212 | 213 | // required string passwd = 2; 214 | case 2: { 215 | if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 216 | ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { 217 | parse_passwd: 218 | DO_(::google::protobuf::internal::WireFormatLite::ReadString( 219 | input, this->mutable_passwd())); 220 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 221 | this->passwd().data(), this->passwd().length(), 222 | ::google::protobuf::internal::WireFormat::PARSE); 223 | } else { 224 | goto handle_uninterpreted; 225 | } 226 | if (input->ExpectTag(26)) goto parse_email; 227 | break; 228 | } 229 | 230 | // optional string email = 3; 231 | case 3: { 232 | if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 233 | ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { 234 | parse_email: 235 | DO_(::google::protobuf::internal::WireFormatLite::ReadString( 236 | input, this->mutable_email())); 237 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 238 | this->email().data(), this->email().length(), 239 | ::google::protobuf::internal::WireFormat::PARSE); 240 | } else { 241 | goto handle_uninterpreted; 242 | } 243 | if (input->ExpectAtEnd()) return true; 244 | break; 245 | } 246 | 247 | default: { 248 | handle_uninterpreted: 249 | if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 250 | ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { 251 | return true; 252 | } 253 | DO_(::google::protobuf::internal::WireFormat::SkipField( 254 | input, tag, mutable_unknown_fields())); 255 | break; 256 | } 257 | } 258 | } 259 | return true; 260 | #undef DO_ 261 | } 262 | 263 | void helloworld::SerializeWithCachedSizes( 264 | ::google::protobuf::io::CodedOutputStream* output) const { 265 | // required string usrname = 1; 266 | if (has_usrname()) { 267 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 268 | this->usrname().data(), this->usrname().length(), 269 | ::google::protobuf::internal::WireFormat::SERIALIZE); 270 | ::google::protobuf::internal::WireFormatLite::WriteString( 271 | 1, this->usrname(), output); 272 | } 273 | 274 | // required string passwd = 2; 275 | if (has_passwd()) { 276 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 277 | this->passwd().data(), this->passwd().length(), 278 | ::google::protobuf::internal::WireFormat::SERIALIZE); 279 | ::google::protobuf::internal::WireFormatLite::WriteString( 280 | 2, this->passwd(), output); 281 | } 282 | 283 | // optional string email = 3; 284 | if (has_email()) { 285 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 286 | this->email().data(), this->email().length(), 287 | ::google::protobuf::internal::WireFormat::SERIALIZE); 288 | ::google::protobuf::internal::WireFormatLite::WriteString( 289 | 3, this->email(), output); 290 | } 291 | 292 | if (!unknown_fields().empty()) { 293 | ::google::protobuf::internal::WireFormat::SerializeUnknownFields( 294 | unknown_fields(), output); 295 | } 296 | } 297 | 298 | ::google::protobuf::uint8* helloworld::SerializeWithCachedSizesToArray( 299 | ::google::protobuf::uint8* target) const { 300 | // required string usrname = 1; 301 | if (has_usrname()) { 302 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 303 | this->usrname().data(), this->usrname().length(), 304 | ::google::protobuf::internal::WireFormat::SERIALIZE); 305 | target = 306 | ::google::protobuf::internal::WireFormatLite::WriteStringToArray( 307 | 1, this->usrname(), target); 308 | } 309 | 310 | // required string passwd = 2; 311 | if (has_passwd()) { 312 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 313 | this->passwd().data(), this->passwd().length(), 314 | ::google::protobuf::internal::WireFormat::SERIALIZE); 315 | target = 316 | ::google::protobuf::internal::WireFormatLite::WriteStringToArray( 317 | 2, this->passwd(), target); 318 | } 319 | 320 | // optional string email = 3; 321 | if (has_email()) { 322 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 323 | this->email().data(), this->email().length(), 324 | ::google::protobuf::internal::WireFormat::SERIALIZE); 325 | target = 326 | ::google::protobuf::internal::WireFormatLite::WriteStringToArray( 327 | 3, this->email(), target); 328 | } 329 | 330 | if (!unknown_fields().empty()) { 331 | target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( 332 | unknown_fields(), target); 333 | } 334 | return target; 335 | } 336 | 337 | int helloworld::ByteSize() const { 338 | int total_size = 0; 339 | 340 | if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { 341 | // required string usrname = 1; 342 | if (has_usrname()) { 343 | total_size += 1 + 344 | ::google::protobuf::internal::WireFormatLite::StringSize( 345 | this->usrname()); 346 | } 347 | 348 | // required string passwd = 2; 349 | if (has_passwd()) { 350 | total_size += 1 + 351 | ::google::protobuf::internal::WireFormatLite::StringSize( 352 | this->passwd()); 353 | } 354 | 355 | // optional string email = 3; 356 | if (has_email()) { 357 | total_size += 1 + 358 | ::google::protobuf::internal::WireFormatLite::StringSize( 359 | this->email()); 360 | } 361 | 362 | } 363 | if (!unknown_fields().empty()) { 364 | total_size += 365 | ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( 366 | unknown_fields()); 367 | } 368 | GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); 369 | _cached_size_ = total_size; 370 | GOOGLE_SAFE_CONCURRENT_WRITES_END(); 371 | return total_size; 372 | } 373 | 374 | void helloworld::MergeFrom(const ::google::protobuf::Message& from) { 375 | GOOGLE_CHECK_NE(&from, this); 376 | const helloworld* source = 377 | ::google::protobuf::internal::dynamic_cast_if_available( 378 | &from); 379 | if (source == NULL) { 380 | ::google::protobuf::internal::ReflectionOps::Merge(from, this); 381 | } else { 382 | MergeFrom(*source); 383 | } 384 | } 385 | 386 | void helloworld::MergeFrom(const helloworld& from) { 387 | GOOGLE_CHECK_NE(&from, this); 388 | if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { 389 | if (from.has_usrname()) { 390 | set_usrname(from.usrname()); 391 | } 392 | if (from.has_passwd()) { 393 | set_passwd(from.passwd()); 394 | } 395 | if (from.has_email()) { 396 | set_email(from.email()); 397 | } 398 | } 399 | mutable_unknown_fields()->MergeFrom(from.unknown_fields()); 400 | } 401 | 402 | void helloworld::CopyFrom(const ::google::protobuf::Message& from) { 403 | if (&from == this) return; 404 | Clear(); 405 | MergeFrom(from); 406 | } 407 | 408 | void helloworld::CopyFrom(const helloworld& from) { 409 | if (&from == this) return; 410 | Clear(); 411 | MergeFrom(from); 412 | } 413 | 414 | bool helloworld::IsInitialized() const { 415 | if ((_has_bits_[0] & 0x00000003) != 0x00000003) return false; 416 | 417 | return true; 418 | } 419 | 420 | void helloworld::Swap(helloworld* other) { 421 | if (other != this) { 422 | std::swap(usrname_, other->usrname_); 423 | std::swap(passwd_, other->passwd_); 424 | std::swap(email_, other->email_); 425 | std::swap(_has_bits_[0], other->_has_bits_[0]); 426 | _unknown_fields_.Swap(&other->_unknown_fields_); 427 | std::swap(_cached_size_, other->_cached_size_); 428 | } 429 | } 430 | 431 | ::google::protobuf::Metadata helloworld::GetMetadata() const { 432 | protobuf_AssignDescriptorsOnce(); 433 | ::google::protobuf::Metadata metadata; 434 | metadata.descriptor = helloworld_descriptor_; 435 | metadata.reflection = helloworld_reflection_; 436 | return metadata; 437 | } 438 | 439 | 440 | // @@protoc_insertion_point(namespace_scope) 441 | 442 | } // namespace im 443 | 444 | // @@protoc_insertion_point(global_scope) 445 | --------------------------------------------------------------------------------