1 #ifndef INFINISPAN_HOTROD_PORTABLE_H
2 #define INFINISPAN_HOTROD_PORTABLE_H
18 namespace infinispan {
23 # define strncpy_safe strncpy_s
26 # define _TRUNCATE ((size_t)-1)
28 static int strncpy_safe(
char *dest,
size_t numberOfElements,
const char *src,
size_t count) {
30 if (!dest || !src || !numberOfElements)
return -1;
31 size_t end = count !=
_TRUNCATE && count < numberOfElements ? count : numberOfElements - 1;
33 for (; i < end && src[i]; ++i) {
36 if (!src[i] || end == count || count ==
_TRUNCATE) {
45 #define PORTABLE_STRING_BUF_SIZE 48
53 inline void init(
const char *str,
size_t len)
60 m_dynamic =
new char[len + 1];
61 strncpy_safe(m_dynamic, len + 1, str, len);
72 init(str, strlen(str));
77 init(str.c_str(), str.length());
82 init(o.m_dynamic == 0 ? o.m_buf : o.m_dynamic, o.m_length);
87 if (m_dynamic != 0)
delete[] m_dynamic;
88 init(o.m_dynamic == 0 ? o.m_buf : o.m_dynamic, o.m_length);
94 if (m_dynamic != 0)
delete[] m_dynamic;
99 if (m_dynamic != 0)
delete[] m_dynamic;
100 init(str.c_str(), str.length());
106 return m_dynamic == 0 ? m_buf : m_dynamic;
111 return std::string(m_dynamic == 0 ? m_buf : m_dynamic);
114 static int cmp(
const std::string &other,
const string &str)
116 return cmp(other.c_str(), str);
119 static int cmp(
const char *other,
const string &str)
121 if (str.m_dynamic == 0) {
122 return strncmp(other, str.m_buf, str.m_length);
124 return strncmp(other, str.m_dynamic, str.m_length);
142 B operator()(
const A &);
167 vector(T *array,
size_t s): m_array(array), m_size(s) {}
169 template<
typename Iterable>
vector(
const Iterable &v)
175 m_array =
new T[v.size()];
177 for (
typename Iterable::const_iterator it = v.begin(); it != v.end(); ++it) {
183 template<
typename Iterable,
typename Converter>
vector(
const Iterable &v, Converter convert)
189 m_array =
new T[v.size()];
191 for (
typename Iterable::const_iterator it = v.begin(); it != v.end(); ++it) {
192 m_array[i++] = convert(*it);
201 m_array =
new T[o.m_size];
202 for (
size_t i = 0; i < o.m_size; ++i) {
203 m_array[i] = o.m_array[i];
209 if (m_size != 0)
delete[] m_array;
214 if (m_size < o.m_size) {
216 m_array =
new T[o.m_size];
217 }
else if (o.m_size == 0 && m_size != 0) {
221 for (
size_t i = 0; i < o.m_size; ++i) {
222 m_array[i] = o.m_array[i];
227 vector(move_ref mr): m_array(mr.m_ref.m_array), m_size(mr.m_ref.m_size) {}
233 m_size = mr.m_ref.m_size;
234 m_array = mr.m_ref.m_array;
236 mr.m_ref.m_array = 0;
245 return move_ref(*
this);
252 for (
size_t i = 0; i < m_size; ++i) {
253 v.push_back(m_array[i]);
269 template<
typename K,
typename V>
class pair {
275 template<
typename K,
typename V>
class map
297 template<
typename K2,
typename KC,
typename V2,
typename VC>
298 static my_pair *to_array(
const std::map<K2, V2> &m,
299 KC convertKey, VC convertValue)
303 for (
typename std::map<K2, V2>::const_iterator it = m.begin(); it != m.end(); ++it) {
304 dp->
key = convertKey(it->first);
305 dp->
value = convertValue(it->second);
326 map(
const std::map<K, V> &m):
329 template<
typename K2,
typename KC,
typename V2,
typename VC>
331 m_vec(to_array(m, convertKey, convertValue), m.
size()) {}
347 m_vec = mr.m_ref.m_vec.move();
358 for (
size_t i = 0; i < m_vec.size(); ++i) {
359 const my_pair *dp = m_vec.data() + i;
365 template<
typename K2,
typename KC,
typename V2,
typename VC> std::map<K2, V2>
std_map(
366 KC convertKey, VC convertValue)
const
369 for (
size_t i = 0; i < m_vec.size(); ++i) {
370 const my_pair *dp = m_vec.data() + i;
371 m[convertKey(dp->
key)] = convertValue(dp->
value);
376 template<
typename K2>
const my_pair *
get(K2 key, int (*cmp)(K2,
const K &))
const {
377 for (
size_t i = 0; i < m_vec.size(); ++i) {
378 const my_pair *dp = m_vec.data() + i;
379 if (!cmp(key, dp->
key))
return dp;
415 template<
typename T>
class counting_ptr {
422 inline void dec_and_destroy() {
423 if (m_ptr != 0 && --(m_ptr->m_counter) == 0) {
424 if (m_destroy == 0) {
427 m_destroy((T *)m_ptr);
450 m_destroy = o.m_destroy;
471 const T *
get()
const {
478 return (
const T *)m_ptr;
484 typedef void (*destroy)(T *);
492 const T *
get()
const {
return m_ptr; }
493 T *
get() {
return m_ptr; }
494 void set(T *ptr,
void (*dtor)(T *)) {
495 if (m_ptr) m_destroy(m_ptr);
503 #endif // INFINISPAN_HOTROD_PORTABLE_H
map(const std::map< K2, V2 > &m, KC convertKey=identity< K >(), VC convertValue=identity< V >())
Definition: portable.h:330
string(const std::string &str)
Definition: portable.h:75
~local_ptr()
Definition: portable.h:491
string operator()(const std::string &x)
Definition: portable.h:130
vector(const Iterable &v)
Definition: portable.h:169
std::map< K, V > std_map() const
Definition: portable.h:355
T & operator()()
Definition: portable.h:412
virtual ~counted_object()
Definition: portable.h:404
const T * operator->() const
Definition: portable.h:477
Definition: portable.h:128
local_ptr()
Definition: portable.h:488
vector(T *array, size_t s)
Definition: portable.h:167
#define PORTABLE_STRING_BUF_SIZE
Definition: portable.h:45
#define HR_EXTERN
Definition: ImportExport.h:35
vector< T > & operator=(const vector< T > &o)
Definition: portable.h:212
std::string operator()(const string &x)
Definition: portable.h:134
V value
Definition: portable.h:272
size_t size() const
Definition: portable.h:263
string & operator=(const std::string &str)
Definition: portable.h:97
string()
Definition: portable.h:65
map(move_ref mr)
Definition: portable.h:344
Definition: portable.h:407
Definition: portable.h:159
local_ptr & operator=(const local_ptr &)
Definition: portable.h:490
map< K, V > & operator=(const map< K, V > &o)
Definition: portable.h:338
Definition: portable.h:275
local_ptr(const local_ptr &)
Definition: portable.h:489
counting_ptr & operator=(T *rc)
Definition: portable.h:456
string(const char *str)
Definition: portable.h:70
counting_ptr(const counting_ptr &o)
Definition: portable.h:442
~counting_ptr()
Definition: portable.h:439
vector(move_ref mr)
Definition: portable.h:227
string & operator=(const string &o)
Definition: portable.h:85
Definition: portable.h:152
Definition: portable.h:396
const char * c_string() const
Definition: portable.h:104
static int cmp(const char *other, const string &str)
Definition: portable.h:119
size_t size() const
Definition: portable.h:389
std::vector< T > std_vector() const
Definition: portable.h:248
map(const map< K, V > &o)
Definition: portable.h:333
map(const std::map< K, V > &m)
Definition: portable.h:326
vector(const Iterable &v, Converter convert)
Definition: portable.h:183
vector()
Definition: portable.h:166
void set(T *ptr, void(*dtor)(T *))
Definition: portable.h:494
string(const string &o)
Definition: portable.h:80
counting_ptr & reset(T *rc, destroy d)
Definition: portable.h:459
map< K, V > & operator=(move_ref mr)
Definition: portable.h:345
T * operator->()
Definition: portable.h:474
~vector()
Definition: portable.h:207
vector(const vector< T > &o)
Definition: portable.h:197
counting_ptr & operator=(const counting_ptr &o)
Definition: portable.h:447
void(* destroy)(T *)
Definition: portable.h:417
std::map< K2, V2 > std_map(KC convertKey, VC convertValue) const
Definition: portable.h:365
K key
Definition: portable.h:271
T operator()(const T &x)
Definition: portable.h:147
map()
Definition: portable.h:319
counting_ptr(T *obj, destroy d=0)
Definition: portable.h:433
const T * data() const
Definition: portable.h:258
vector< T > & operator=(move_ref mr)
Definition: portable.h:228
counted_object()
Definition: portable.h:403
~string()
Definition: portable.h:92
Definition: portable.h:145
Definition: portable.h:312
move_ref move()
Definition: portable.h:350
Definition: portable.h:269
std::string std_string() const
Definition: portable.h:109
Definition: portable.h:482
counted_wrapper(const T &o)
Definition: portable.h:411
counting_ptr()
Definition: portable.h:432
Definition: portable.h:141
move_ref move()
Definition: portable.h:243
#define _TRUNCATE
Definition: portable.h:26
Definition: portable.h:398
Definition: portable.h:47
static int cmp(const std::string &other, const string &str)
Definition: portable.h:114
const my_pair * data() const
Definition: portable.h:384