1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#ifndef CUSTOM_BUFFER_H
#define CUSTOM_BUFFER_H
#include <Ice/Ice.h>
#if defined(_MSC_VER)
# pragma warning( disable : 4800 )
#endif
namespace Test
{
template<typename T>
class CustomBuffer
{
public:
CustomBuffer() : _buf(0), _count(0)
{
}
CustomBuffer(const CustomBuffer& o) : _buf(0), _count(o._count)
{
if(_count > 0)
{
_buf = new T[_count];
for(size_t i = 0; i < _count; ++i)
{
_buf[i] = o._buf[i];
}
}
}
~CustomBuffer()
{
if(_buf != 0)
{
delete[] _buf;
}
}
CustomBuffer& operator=(const CustomBuffer& o)
{
_count = o._count;
if(_count > 0)
{
_buf = new T[_count];
for(size_t i = 0; i < _count; ++i)
{
_buf[i] = o._buf[i];
}
}
return *this;
}
size_t
count() const
{
return _count;
}
T* get() const
{
return _buf;
}
void set(T* buf, size_t count)
{
_buf = buf;
_count = count;
}
void setAndInit(T* buf, size_t count)
{
_buf = buf;
_count = count;
for(size_t i = 0; i < count; ++i)
{
_buf[i] = static_cast<T>(rand());
}
}
private:
T* _buf;
size_t _count;
};
template<typename T> bool
operator!=(const CustomBuffer<T>& lhs, const CustomBuffer<T>& rhs)
{
if(lhs.count() != rhs.count())
{
return true;
}
for(size_t i = 0; i < lhs.count(); ++i)
{
if(lhs.get()[i] != rhs.get()[i])
{
return true;
}
}
return false;
}
template<typename T> bool
operator==(const CustomBuffer<T>& lhs, const CustomBuffer<T>& rhs)
{
return !operator!=(lhs, rhs);
}
template<typename T> bool
operator<(const CustomBuffer<T>& lhs, const CustomBuffer<T>& rhs)
{
if(lhs.count() < rhs.count())
{
return true;
}
else if(lhs.count() > rhs.count())
{
return false;
}
for(size_t i = 0; i < lhs.count(); ++i)
{
if(lhs.get()[i] >= rhs.get()[i])
{
return false;
}
}
return true;
}
}
namespace Ice
{
template<typename T>
struct StreamableTraits< ::Test::CustomBuffer<T> >
{
static const StreamHelperCategory helper = StreamHelperCategorySequence;
static const int minWireSize = 1;
static const bool fixedLength = false;
};
template<typename T>
struct StreamHelper< ::Test::CustomBuffer<T>, StreamHelperCategorySequence>
{
template<class S> static inline void
write(S* stream, const ::Test::CustomBuffer<T>& v)
{
stream->write(v.get(), v.get() + v.count());
}
template<class S> static inline void
read(S* stream, ::Test::CustomBuffer<T>& v)
{
#ifdef ICE_CPP11_MAPPING
std::pair<const T*, const T*> a;
stream->read(a);
size_t count = static_cast<size_t>(a.second - a.first);
if(count > 0)
{
auto b = new T[count];
for(size_t i = 0; i < count; ++i)
{
b[i] = a.first[i];
}
v.set(b, count);
}
else
{
v.set(0, 0);
}
#else
IceUtil::ScopedArray<T> p;
std::pair<const T*, const T*> a;
stream->read(a, p);
T* b = p.release();
size_t count = static_cast<size_t>(a.second - a.first);
if(b == 0 && count > 0)
{
b = new T[count];
for(size_t i = 0; i < count; ++i)
{
b[i] = a.first[i];
}
}
v.set(b, count);
#endif
}
};
}
#endif
|