$search
00001 /* 00002 * Copyright (c) 2009, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #ifndef TEST_ROSCP_SERIALIZATION_HELPERS_H 00031 #define TEST_ROSCP_SERIALIZATION_HELPERS_H 00032 00033 #include <boost/shared_array.hpp> 00034 #include <ros/serialization.h> 00035 00036 namespace test_roscpp_serialization 00037 { 00038 namespace ser = ros::serialization; 00039 namespace mt = ros::message_traits; 00040 typedef boost::shared_array<uint8_t> Array; 00041 00042 template<typename T> 00043 Array serializeAndDeserialize(const T& ser_val, T& deser_val) 00044 { 00045 uint32_t len = ser::serializationLength(ser_val); 00046 boost::shared_array<uint8_t> b(new uint8_t[len]); 00047 ser::OStream ostream(b.get(), len); 00048 ser::serialize(ostream, ser_val); 00049 ser::IStream istream(b.get(), len); 00050 ser::deserialize(istream, deser_val); 00051 00052 return b; 00053 } 00054 00055 template<class T> class Allocator; 00056 00057 // specialize for void: 00058 template<> 00059 class Allocator<void> 00060 { 00061 public: 00062 typedef void* pointer; 00063 typedef const void* const_pointer; 00064 // reference to void members are impossible. 00065 typedef void value_type; 00066 00067 template<class U> 00068 struct rebind 00069 { 00070 typedef Allocator<U> other; 00071 }; 00072 }; 00073 00074 template<class T> 00075 class Allocator 00076 { 00077 public: 00078 typedef size_t size_type; 00079 typedef ptrdiff_t difference_type; 00080 typedef T* pointer; 00081 typedef const T* const_pointer; 00082 typedef T& reference; 00083 typedef const T& const_reference; 00084 typedef T value_type; 00085 00086 template<class U> 00087 struct rebind 00088 { 00089 typedef Allocator<U> other; 00090 }; 00091 00092 Allocator() throw () 00093 { 00094 } 00095 template<class U> 00096 Allocator(const Allocator<U>& u) throw () 00097 { 00098 } 00099 00100 ~Allocator() throw () 00101 { 00102 } 00103 00104 pointer address(reference r) const 00105 { 00106 return &r; 00107 } 00108 const_pointer address(const_reference r) const 00109 { 00110 return &r; 00111 } 00112 size_type max_size() const throw () 00113 { 00114 return ~size_type(0); 00115 } 00116 pointer allocate(size_type n, Allocator<void>::const_pointer hint = 0) 00117 { 00118 return reinterpret_cast<pointer> (malloc(n * sizeof(T))); 00119 } 00120 void deallocate(pointer p, size_type n) 00121 { 00122 free(p); 00123 } 00124 00125 void construct(pointer p, const_reference val) 00126 { 00127 new (p) T(val); 00128 } 00129 void destroy(pointer p) 00130 { 00131 p->~T(); 00132 } 00133 }; 00134 00135 template<class T1, class T2> 00136 inline bool operator==(const Allocator<T1>& a1, const Allocator<T2>& a2) throw () 00137 { 00138 return true; 00139 } 00140 00141 template<class T1, class T2> 00142 inline bool operator!=(const Allocator<T1>& a1, const Allocator<T2>& a2) throw () 00143 { 00144 return false; 00145 } 00146 00147 } 00148 00149 #endif // TEST_ROSCP_SERIALIZATION_HELPERS_H