/usr/include/ucommon/generics.h is in libucommon-dev 7.0.0-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
// Copyright (C) 2015 Cherokees of Idaho.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
/**
* Generic templates for C++. These are templates that do not depend
* on any ucommon classes. They can be used for generic C++ programming.
* @file ucommon/generics.h
*/
#ifndef _UCOMMON_GENERICS_H_
#define _UCOMMON_GENERICS_H_
#ifndef _UCOMMON_CPR_H_
#include <ucommon/cpr.h>
#endif
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#ifndef UCOMMON_SYSRUNTIME
#define THROW(x) throw x
#define THROWS(x) throw(x)
#define THROWS_ANY throw()
#else
#define THROW(x) ::abort()
#define THROWS(x)
#define THROWS_ANY
#endif
namespace ucommon {
/**
* Generic smart pointer class. This is the original Common C++ "Pointer"
* class with a few additions.
* @author David Sugar <dyfet@gnutelephony.org>
*/
template <typename T>
class pointer
{
protected:
unsigned *counter;
T *object;
public:
inline void release(void) {
if(counter && --(*counter)==0) {
delete counter;
delete object;
}
object = NULL;
counter = NULL;
}
inline void retain(void) {
if(counter)
++*counter;
}
inline void set(T* ptr) {
if(object != ptr) {
release();
counter = new unsigned;
*counter = 1;
object = ptr;
}
}
inline void set(const pointer<T> &ref) {
if(object == ref.object)
return;
if(counter && --(*counter)==0) {
delete counter;
delete object;
}
object = ref.object;
counter = ref.counter;
if(counter)
++(*counter);
}
inline pointer() {
counter = NULL;
object = NULL;
}
inline explicit pointer(T* ptr = NULL) : object(ptr) {
if(object) {
counter = new unsigned;
*counter = 1;
}
else
counter = NULL;
}
inline pointer(const pointer<T> &ref) {
object = ref.object;
counter = ref.counter;
if(counter)
++(*counter);
}
inline pointer& operator=(const pointer<T> &ref) {
this->set(ref);
return *this;
}
inline pointer& operator=(T *ptr) {
this->set(ptr);
return *this;
}
inline ~pointer() {
release();
}
inline T& operator*() const {
return *object;
}
inline T* operator->() const {
return object;
}
inline bool operator!() const {
return (counter == NULL);
}
inline operator bool() const {
return counter != NULL;
}
};
/**
* Generic smart array class. This is the original Common C++ "Pointer" class
* with a few additions for arrays.
* @author David Sugar <dyfet@gnutelephony.org>
*/
template <typename T>
class array_pointer
{
protected:
unsigned *counter;
T *array;
public:
inline void release(void) {
if(counter && --(*counter)==0) {
delete counter;
delete[] array;
}
array = NULL;
counter = NULL;
}
inline void retain(void) {
if(counter)
++*counter;
}
inline void set(T* ptr) {
if(array != ptr) {
release();
counter = new unsigned;
*counter = 1;
array = ptr;
}
}
inline void set(const array_pointer<T> &ref) {
if(array == ref.array)
return;
if(counter && --(*counter)==0) {
delete counter;
delete[] array;
}
array = ref.array;
counter = ref.counter;
if(counter)
++(*counter);
}
inline array_pointer() {
counter = NULL;
array = NULL;
}
inline explicit array_pointer(T* ptr = NULL) : array(ptr) {
if(array) {
counter = new unsigned;
*counter = 1;
}
else
counter = NULL;
}
inline array_pointer(const array_pointer<T> &ref) {
array = ref.array;
counter = ref.counter;
if(counter)
++(*counter);
}
inline array_pointer& operator=(const array_pointer<T> &ref) {
this->set(ref);
return *this;
}
inline array_pointer& operator=(T *ptr) {
this->set(ptr);
return *this;
}
inline ~array_pointer() {
release();
}
inline T* operator*() const {
return array;
}
inline T& operator[](size_t offset) const {
return array[offset];
}
inline T* operator()(size_t offset) const {
return &array[offset];
}
inline bool operator!() const {
return (counter == NULL);
}
inline operator bool() const {
return counter != NULL;
}
};
/**
* Save and restore global objects in function call stack frames.
* @author David Sugar <dyfet@gnutelephony.org>
*/
template<typename T>
class save_restore
{
private:
T *original;
T temp;
save_restore() __DELETED;
public:
/**
* Save object into local copy and keep reference to the original object.
* @param object to save.
*/
inline save_restore(T& object) {
original = &object; temp = object;
}
/**
* Restore original when stack frame is released.
*/
inline ~save_restore() {
*original = temp;
}
};
/**
* Convenience function to validate object assuming it is castable to bool.
* @param object we are testing.
* @return true if object valid.
*/
template<typename T>
inline bool is(T& object) {
return object.operator bool();
}
/**
* Convenience function to test pointer object. This solves issues where
* some compilers get confused between bool and pointer operators.
* @param object we are testing.
* @return true if object points to NULL.
*/
template<typename T>
inline bool isnull(T& object) {
return (bool)(object.operator*() == nullptr);
}
/**
* Convenience function to test pointer-pointer object. This solves issues
* where some compilers get confused between bool and pointer operators.
* @param object we are testing.
* @return true if object points to NULL.
*/
template<typename T>
inline bool isnullp(T *object) {
return (bool)(object->operator*() == nullptr);
}
/**
* Convenience function to duplicate object pointer to heap.
* @param object we are duping.
* @return heap pointer instance.
*/
template<typename T>
inline T* dup(const T& object) {
return new T(object);
}
template<typename T>
inline void dupfree(T object) {
delete object;
}
template<>
inline char *dup<char>(const char& object) {
return strdup(&object);
}
template<>
inline void dupfree<char*>(char* object) {
::free(object);
}
/**
* Convenience function to reset an existing object.
* @param object type to reset.
*/
template<typename T>
inline void reset_unsafe(T& object) {
new((caddr_t)&object) T;
}
/**
* Convenience function to zero an object and restore type info.
* @param object to zero in memory.
*/
template<typename T>
inline void zero_unsafe(T& object) {
memset((void *)&object, 0, sizeof(T)); new((caddr_t)&object) T;
}
/**
* Convenience function to copy class.
* @param target to copy into.
* @param source to copy from.
*/
template<typename T>
inline void copy_unsafe(T* target, const T* source) {
memcpy((void *)target, (void *)source, sizeof(T));
}
/**
* Convenience function to store object pointer into object.
* @param target to copy into.
* @param source to copy from.
*/
template<typename T>
inline void store_unsafe(T& target, const T* source) {
memcpy((void *)&target, (void *)source, sizeof(T));
}
/**
* Convenience function to swap objects. Can be specialized.
* @param o1 to swap.
* @param o2 to swap.
*/
template<typename T>
inline void swap(T& o1, T& o2) {
cpr_memswap(&o1, &o2, sizeof(T));
}
/**
* Convenience function to copy objects.
*/
template<typename T>
inline T copy(const T& src) {
return T(src);
}
template<typename T>
inline T& copy(const T& src, T& to) {
new((caddr_t)&to) T(src);
return to;
}
/**
* Convenience function to move objects.
*/
template<typename T>
inline T& move(T& src, T& to) {
memcpy((void *)&to, (void *)&src, sizeof(T));
new((caddr_t)&src) T();
return to;
}
template<typename T>
inline T& clear(T& o) {
o.~T();
new((caddr_t)&o) T();
return o;
}
/**
* Convenience function to check memory arrays.
* @param pointer to validate.
* @param base address of array.
* @param count of objects.
* @return true if in boundry.
*/
template<typename T>
inline bool bound(const T* pointer, const T* base, size_t count) {
if(pointer < base || pointer >= &base[count])
return false;
if(((size_t)pointer) % sizeof(T))
return false;
return true;
}
/**
* Convenience function to return max of two objects.
* @param o1 to check.
* @param o2 to check.
* @return max object.
*/
template<typename T>
inline T& (max)(T& o1, T& o2) {
return o1 > o2 ? o1 : o2;
}
/**
* Convenience function to return min of two objects.
* @param o1 to check.
* @param o2 to check.
* @return min object.
*/
template<typename T>
inline T& (min)(T& o1, T& o2) {
return o1 < o2 ? o1 : o2;
}
/**
* Convenience macro to range restrict values.
* @param value to check.
* @param low value.
* @param high value.
* @return adjusted value.
*/
template<typename T>
inline T& (limit)(T& value, T& low, T& high) {
return (value < low) ? low : ((value > high) ? high : value);
}
/**
* Convert a pointer to a reference with type checking. This is
* mostly convenience for documenting behavior.
* @param pointer to convert.
* @return object reference.
*/
template<typename T>
inline T& deref_pointer(T *pointer) {
__THROW_DEREF(pointer);
return *pointer;
}
} // namespace ucommon
#endif
|