GridGain C++ Client Library
portable_containers.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) GridGain Systems. All Rights Reserved.
3  * _________ _____ __________________ _____
4  * __ ____/___________(_)______ /__ ____/______ ____(_)_______
5  * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \
6  * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / /
7  * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/
8  */
9 
15 #ifndef _GRIDGAIN_PORTABLE_CONTAINERS
16 #define _GRIDGAIN_PORTABLE_CONTAINERS
17 
18 #include <stdint.h>
19 
20 #include "gridgain/impl/portable/portable_writer_impl.h"
21 #include "gridgain/impl/portable/portable_reader_impl.h"
22 #include "gridgain/impl/utils.h"
24 
25 namespace gridgain
26 {
27  namespace portable
28  {
32  class IGNITE_IMPORT_EXPORT PortableStringArrayWriter
33  {
34  public:
41  PortableStringArrayWriter(impl::portable::PortableWriterImpl* impl, int32_t id);
42 
48  void Write(const char* val);
49 
56  void Write(const char* val, int32_t len);
57 
63  void Write(const std::string& val)
64  {
65  Write(val.c_str());
66  }
67 
71  void Close();
72  private:
74  impl::portable::PortableWriterImpl* impl;
75 
77  const int32_t id;
78  };
79 
83  template<typename T>
84  class IGNITE_IMPORT_EXPORT PortableArrayWriter
85  {
86  public:
93  PortableArrayWriter(impl::portable::PortableWriterImpl* impl, int32_t id) : impl(impl), id(id)
94  {
95  // No-op.
96  }
97 
103  void Write(const T& val)
104  {
105  impl->WriteElement<T>(id, val);
106  }
107 
111  void Close()
112  {
113  impl->CommitContainer(id);
114  }
115  private:
117  impl::portable::PortableWriterImpl* impl;
118 
120  const int32_t id;
121  };
122 
126  template<typename T>
127  class IGNITE_IMPORT_EXPORT PortableCollectionWriter
128  {
129  public:
136  PortableCollectionWriter(impl::portable::PortableWriterImpl* impl, int32_t id) : impl(impl), id(id)
137  {
138  // No-op.
139  }
140 
146  void Write(const T& val)
147  {
148  impl->WriteElement<T>(id, val);
149  }
150 
154  void Close()
155  {
156  impl->CommitContainer(id);
157  }
158  private:
160  impl::portable::PortableWriterImpl* impl;
161 
163  const int32_t id;
164  };
165 
169  template<typename K, typename V>
170  class IGNITE_IMPORT_EXPORT PortableMapWriter
171  {
172  public:
178  PortableMapWriter(impl::portable::PortableWriterImpl* impl, int32_t id) : impl(impl), id(id)
179  {
180  // No-op.
181  }
182 
189  void Write(const K& key, const V& val)
190  {
191  impl->WriteElement<K, V>(id, key, val);
192  }
193 
197  void Close()
198  {
199  impl->CommitContainer(id);
200  }
201  private:
203  impl::portable::PortableWriterImpl* impl;
204 
206  const int32_t id;
207  };
208 
212  class IGNITE_IMPORT_EXPORT PortableStringArrayReader
213  {
214  public:
222  PortableStringArrayReader(impl::portable::PortableReaderImpl* impl, int32_t id, int32_t size);
223 
229  bool HasNext() const;
230 
242  int32_t GetNext(char* res, int32_t len);
243 
249  std::string GetNext()
250  {
251  int32_t len = GetNext(NULL, 0);
252 
253  if (len != -1)
254  {
255  impl::utils::SafeArray<char> arr(len + 1);
256 
257  GetNext(arr.target, len + 1);
258 
259  return std::string(arr.target);
260  }
261  else
262  return std::string();
263  }
264 
270  int32_t GetSize() const;
271 
275  bool IsNull() const;
276  private:
278  impl::portable::PortableReaderImpl* impl;
279 
281  const int32_t id;
282 
284  const int32_t size;
285  };
286 
290  template<typename T>
292  {
293  public:
301  PortableArrayReader(impl::portable::PortableReaderImpl* impl, int32_t id, int32_t size) :
302  impl(impl), id(id), size(size)
303  {
304  // No-op.
305  }
306 
312  bool HasNext() const
313  {
314  return impl->HasNextElement(id);
315  }
316 
323  {
324  return impl->ReadElement<T>(id);
325  }
326 
332  int32_t GetSize() const
333  {
334  return size;
335  }
336 
340  bool IsNull() const
341  {
342  return size == -1;
343  }
344  private:
346  impl::portable::PortableReaderImpl* impl;
347 
349  const int32_t id;
350 
352  const int32_t size;
353  };
354 
358  template<typename T>
360  {
361  public:
370  PortableCollectionReader(impl::portable::PortableReaderImpl* impl, int32_t id,
371  CollectionType type, int32_t size) : impl(impl), id(id), type(type), size(size)
372  {
373  // No-op.
374  }
375 
381  bool HasNext() const
382  {
383  return impl->HasNextElement(id);
384  }
385 
392  {
393  return impl->ReadElement<T>(id);
394  }
395 
402  {
403  return type;
404  }
405 
411  int32_t GetSize() const
412  {
413  return size;
414  }
415 
419  bool IsNull() const
420  {
421  return size == -1;
422  }
423  private:
425  impl::portable::PortableReaderImpl* impl;
426 
428  const int32_t id;
429 
431  const CollectionType type;
432 
434  const int32_t size;
435  };
436 
440  template<typename K, typename V>
442  {
443  public:
452  PortableMapReader(impl::portable::PortableReaderImpl* impl, int32_t id, MapType type,
453  int32_t size) : impl(impl), id(id), type(type), size(size)
454  {
455  // No-op.
456  }
457 
463  bool HasNext() const
464  {
465  return impl->HasNextElement(id);
466  }
467 
474  void GetNext(K* key, V* val)
475  {
476  return impl->ReadElement<K, V>(id, key, val);
477  }
478 
484  MapType GetType() const
485  {
486  return type;
487  }
488 
494  int32_t GetSize() const
495  {
496  return size;
497  }
498 
502  bool IsNull() const
503  {
504  return size == -1;
505  }
506  private:
508  impl::portable::PortableReaderImpl* impl;
509 
511  const int32_t id;
512 
514  const MapType type;
515 
517  const int32_t size;
518  };
519  }
520 }
521 
522 #endif
bool HasNext() const
Check whether next element is available for read.
Definition: portable_containers.h:463
Portable collection writer.
Definition: portable_containers.h:127
PortableArrayWriter(impl::portable::PortableWriterImpl *impl, int32_t id)
Constructor.
Definition: portable_containers.h:93
PortableMapReader(impl::portable::PortableReaderImpl *impl, int32_t id, MapType type, int32_t size)
Constructor.
Definition: portable_containers.h:452
int32_t GetSize() const
Get array size.
Definition: portable_containers.h:332
bool IsNull() const
Whether array is NULL.
Definition: portable_containers.h:340
Portable collection writer.
Definition: portable_containers.h:84
PortableMapWriter(impl::portable::PortableWriterImpl *impl, int32_t id)
Constructor.
Definition: portable_containers.h:178
void Write(const T &val)
Write a value.
Definition: portable_containers.h:146
Portable array reader.
Definition: portable_containers.h:291
Portable string array reader.
Definition: portable_containers.h:212
CollectionType
Portable collection types.
Definition: portable_consts.h:27
bool IsNull() const
Whether collection is NULL.
Definition: portable_containers.h:419
Portable string array writer.
Definition: portable_containers.h:32
void GetNext(K *key, V *val)
Read next element.
Definition: portable_containers.h:474
T GetNext()
Read next element.
Definition: portable_containers.h:322
Portable map writer.
Definition: portable_containers.h:170
T GetNext()
Read next element.
Definition: portable_containers.h:391
Portable collection reader.
Definition: portable_containers.h:359
PortableArrayReader(impl::portable::PortableReaderImpl *impl, int32_t id, int32_t size)
Constructor.
Definition: portable_containers.h:301
Portable map reader.
Definition: portable_containers.h:441
void Write(const K &key, const V &val)
Write a value.
Definition: portable_containers.h:189
PortableCollectionReader(impl::portable::PortableReaderImpl *impl, int32_t id, CollectionType type, int32_t size)
Constructor.
Definition: portable_containers.h:370
Declares specific portable constants.
void Write(const T &val)
Write a value.
Definition: portable_containers.h:103
bool HasNext() const
Check whether next element is available for read.
Definition: portable_containers.h:312
void Close()
Close the writer.
Definition: portable_containers.h:197
PortableCollectionWriter(impl::portable::PortableWriterImpl *impl, int32_t id)
Constructor.
Definition: portable_containers.h:136
CollectionType GetType() const
Get collection type.
Definition: portable_containers.h:401
void Close()
Close the writer.
Definition: portable_containers.h:111
void Close()
Close the writer.
Definition: portable_containers.h:154
int32_t GetSize() const
Get collection size.
Definition: portable_containers.h:411
std::string GetNext()
Get next element.
Definition: portable_containers.h:249
bool IsNull() const
Whether map is NULL.
Definition: portable_containers.h:502
GridGain API.
Definition: cache.h:35
MapType
Portable map types.
Definition: portable_consts.h:68
bool HasNext() const
Check whether next element is available for read.
Definition: portable_containers.h:381
MapType GetType() const
Get map type.
Definition: portable_containers.h:484
int32_t GetSize() const
Get map size.
Definition: portable_containers.h:494
void Write(const std::string &val)
Write string.
Definition: portable_containers.h:63