00001
00002
00003
00004
00005
00006
00007 #ifndef SAGA_SAGA_BUFFER_HPP
00008 #define SAGA_SAGA_BUFFER_HPP
00009
00010 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00011 # pragma once
00012 #endif
00013
00014 #include <saga/saga/util.hpp>
00015 #include <saga/saga/base.hpp>
00016 #include <saga/saga/types.hpp>
00017 #include <saga/saga/object.hpp>
00018
00019
00020 #if defined(BOOST_MSVC)
00021 #pragma warning(push)
00022 #pragma warning(disable: 4251 4231 4660)
00023 #endif
00024
00026 namespace saga
00027 {
00037 class SAGA_EXPORT const_buffer
00038 : public saga::object
00039 {
00040 protected:
00042
00043 TR1::shared_ptr<saga::impl::const_buffer> get_impl_sp (void) const;
00044 saga::impl::const_buffer* get_impl (void) const;
00045 friend class saga::impl::const_buffer;
00046
00047 const_buffer(saga::impl::const_buffer* impl);
00049
00050 public:
00058 const_buffer(void const* data, saga::ssize_t size);
00059
00064 ~const_buffer();
00065
00071 saga::ssize_t get_size() const;
00072
00078 void const* get_data() const;
00079
00085 void close(double timeout = 0.0);
00086 };
00087
00092 class SAGA_EXPORT mutable_buffer
00093 : public const_buffer
00094 {
00095 protected:
00097
00098 TR1::shared_ptr <saga::impl::buffer> get_impl_sp (void) const;
00099 saga::impl::buffer* get_impl (void) const;
00100
00101 friend class saga::impl::buffer;
00102
00103 mutable_buffer(saga::impl::buffer* impl);
00105
00106 public:
00108
00109 typedef void buffer_deleter_type(void* data);
00110 typedef TR1::function<buffer_deleter_type> buffer_deleter;
00111
00112
00113 static void default_buffer_deleter(void* data);
00115
00116 public:
00121 mutable_buffer(saga::ssize_t size = -1);
00122
00127 mutable_buffer(void* data, saga::ssize_t size);
00128
00133 mutable_buffer(void* data, saga::ssize_t size, buffer_deleter cb);
00134
00139 ~mutable_buffer();
00140
00145 void set_size(saga::ssize_t size = -1);
00146
00151 void set_data(void* data, saga::ssize_t size,
00152 buffer_deleter cb = default_buffer_deleter);
00153
00158 void* get_data();
00159 };
00160
00162 inline mutable_buffer
00163 buffer(void* data, saga::size_t size)
00164 {
00165 return mutable_buffer(data, size);
00166 }
00167
00168 inline const_buffer
00169 buffer(void const* data, saga::size_t size)
00170 {
00171 return const_buffer(data, size);
00172 }
00173
00175 template <typename PodType, std::size_t N>
00176 inline mutable_buffer
00177 buffer(PodType (&data)[N])
00178 {
00179 return mutable_buffer(data, N * sizeof(PodType));
00180 }
00181
00182 template <typename PodType, std::size_t N>
00183 inline const_buffer
00184 buffer(PodType const (&data)[N])
00185 {
00186 return const_buffer(data, N * sizeof(PodType));
00187 }
00188
00190 template <typename PodType, std::size_t N>
00191 inline mutable_buffer
00192 buffer(PodType (&data)[N], saga::size_t max_size)
00193 {
00194 return mutable_buffer(data,
00195 N * sizeof(PodType) < max_size ? N * sizeof(PodType) : max_size);
00196 }
00197
00198 template <typename PodType, std::size_t N>
00199 inline const_buffer
00200 buffer(PodType const (&data)[N], saga::size_t max_size)
00201 {
00202 return const_buffer(data,
00203 N < max_size ? N * sizeof(PodType) : max_size * sizeof(PodType));
00204 }
00205
00207 template <typename PodType, typename Allocator>
00208 inline mutable_buffer
00209 buffer(std::vector<PodType, Allocator>& data)
00210 {
00211 return mutable_buffer(&data[0], data.size() * sizeof(PodType));
00212 }
00213
00214 template <typename PodType, typename Allocator>
00215 inline const_buffer
00216 buffer(std::vector<PodType, Allocator> const& data)
00217 {
00218 return const_buffer(&data[0], data.size() * sizeof(PodType));
00219 }
00220
00222 template <typename PodType, typename Allocator>
00223 inline mutable_buffer
00224 buffer(std::vector<PodType, Allocator>& data, saga::size_t max_size)
00225 {
00226 return mutable_buffer(&data[0],
00227 data.size() < max_size ?
00228 data.size() * sizeof(PodType) : max_size * sizeof(PodType));
00229 }
00230
00231 template <typename PodType, typename Allocator>
00232 inline const_buffer
00233 buffer(std::vector<PodType, Allocator> const& data, saga::size_t max_size)
00234 {
00235 return const_buffer(&data[0],
00236 data.size() < max_size ?
00237 data.size() * sizeof(PodType) : max_size * sizeof(PodType));
00238 }
00239
00241 template <typename Char, typename Traits, typename Allocator>
00242 inline const_buffer
00243 buffer(std::basic_string<Char, Traits, Allocator> const& data)
00244 {
00245 return const_buffer(data.data(), data.size());
00246 }
00247
00248 template <typename Char, typename Traits, typename Allocator>
00249 inline const_buffer
00250 buffer(std::basic_string<Char, Traits, Allocator> const& data,
00251 saga::size_t max_size)
00252 {
00253 return const_buffer(data.data(), data.size() < max_size ?
00254 data.size() * sizeof(Char) : max_size * sizeof(Char));
00255 }
00256
00257 }
00258
00259
00260 #if defined(BOOST_MSVC)
00261 #pragma warning(pop)
00262 #endif
00263
00264 #endif // SAGA_SAGA_BUFFER_HPP