Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

yateclass.h

00001 /*
00002  * yateclass.h
00003  * This file is part of the YATE Project http://YATE.null.ro
00004  *
00005  * Base classes and types, not related to the engine or telephony
00006  *
00007  * Yet Another Telephony Engine - a fully featured software PBX and IVR
00008  * Copyright (C) 2004-2006 Null Team
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
00023  */
00024 
00025 #ifndef __YATECLASS_H
00026 #define __YATECLASS_H
00027 
00028 #ifndef __cplusplus
00029 #error C++ is required
00030 #endif
00031 
00032 #include <sys/types.h>
00033 #include <stddef.h>
00034 #include <unistd.h>
00035 #include <errno.h>
00036 
00037 #ifndef _WORDSIZE
00038 #if defined(__arch64__) || defined(__x86_64__) \
00039     || defined(__amd64__) || defined(__ia64__) \
00040     || defined(__alpha__) || defined(__sparcv9)
00041 #define _WORDSIZE 64
00042 #else
00043 #define _WORDSIZE 32
00044 #endif
00045 #endif
00046 
00047 #ifndef _WINDOWS
00048 #if defined(WIN32) || defined(_WIN32)
00049 #define _WINDOWS
00050 #endif
00051 #endif
00052 
00053 #ifdef _WINDOWS
00054 
00055 #include <windows.h>
00056 #include <io.h>
00057 #include <direct.h>
00058 
00062 typedef signed __int8 int8_t;
00063 typedef unsigned __int8 u_int8_t;
00064 typedef unsigned __int8 uint8_t;
00065 typedef signed __int16 int16_t;
00066 typedef unsigned __int16 u_int16_t;
00067 typedef unsigned __int16 uint16_t;
00068 typedef signed __int32 int32_t;
00069 typedef unsigned __int32 u_int32_t;
00070 typedef unsigned __int32 uint32_t;
00071 typedef signed __int64 int64_t;
00072 typedef unsigned __int64 u_int64_t;
00073 typedef unsigned __int64 uint64_t;
00074 
00075 typedef int pid_t;
00076 typedef int socklen_t;
00077 typedef unsigned long in_addr_t;
00078 
00079 #ifndef strcasecmp
00080 #define strcasecmp _stricmp
00081 #endif
00082 
00083 #ifndef strncasecmp
00084 #define strncasecmp _strnicmp
00085 #endif
00086 
00087 #define vsnprintf _vsnprintf
00088 #define snprintf _snprintf
00089 #define strdup _strdup
00090 #define open _open
00091 #define dup2 _dup2
00092 #define read _read
00093 #define write _write
00094 #define close _close
00095 #define getpid _getpid
00096 #define chdir _chdir
00097 #define mkdir(p,m) _mkdir(p)
00098 #define unlink _unlink
00099 
00100 #define O_RDWR   _O_RDWR
00101 #define O_RDONLY _O_RDONLY
00102 #define O_WRONLY _O_WRONLY
00103 #define O_APPEND _O_APPEND
00104 #define O_BINARY _O_BINARY
00105 #define O_EXCL   _O_EXCL
00106 #define O_CREAT  _O_CREAT
00107 #define O_TRUNC  _O_TRUNC
00108 #define O_NOCTTY 0
00109 
00110 #define S_IRUSR _S_IREAD
00111 #define S_IWUSR _S_IWRITE
00112 #define S_IXUSR 0
00113 #define S_IRWXU (_S_IREAD|_S_IWRITE)
00114 
00115 #ifdef LIBYATE_EXPORTS
00116 #define YATE_API __declspec(dllexport)
00117 #else
00118 #ifndef LIBYATE_STATIC
00119 #define YATE_API __declspec(dllimport)
00120 #endif
00121 #endif
00122 
00123 #define FMT64 "%I64d"
00124 #define FMT64U "%I64u"
00125 
00126 #else /* _WINDOWS */
00127 
00128 #include <sys/time.h>
00129 #include <sys/socket.h>
00130 
00131 #if defined(__FreeBSD__)
00132 #include <netinet/in_systm.h>
00133 #endif
00134 
00135 #include <netinet/in.h>
00136 #include <netinet/ip.h>
00137 #include <netinet/tcp.h>
00138 #include <arpa/inet.h>
00139 #include <netdb.h>
00140 
00144 #ifndef SOCKET
00145 typedef int SOCKET;
00146 #endif
00147 #ifndef HANDLE
00148 typedef int HANDLE;
00149 #endif
00150 
00151 #ifndef O_BINARY
00152 #define O_BINARY 0
00153 #endif
00154 
00155 #if _WORDSIZE == 64
00156 #define FMT64 "%ld"
00157 #define FMT64U "%lu"
00158 #else
00159 #define FMT64 "%lld"
00160 #define FMT64U "%llu"
00161 #endif
00162 
00163 #endif /* ! _WINDOWS */
00164 
00165 #ifndef IPTOS_LOWDELAY
00166 #define IPTOS_LOWDELAY      0x10
00167 #define IPTOS_THROUGHPUT    0x08
00168 #define IPTOS_RELIABILITY   0x04
00169 #define IPTOS_MINCOST       0x02
00170 #endif
00171 
00172 #ifndef YATE_API
00173 #define YATE_API
00174 #endif
00175 
00176 #ifdef _WINDOWS
00177 #undef RAND_MAX
00178 #define RAND_MAX 2147483647
00179 extern "C" {
00180 YATE_API long int random();
00181 YATE_API void srandom(unsigned int seed);
00182 }
00183 #endif
00184 
00188 namespace TelEngine {
00189 
00190 #ifdef HAVE_GCC_FORMAT_CHECK
00191 #define FORMAT_CHECK(f) __attribute__((format(printf,(f),(f)+1)))
00192 #else
00193 #define FORMAT_CHECK(f)
00194 #endif
00195 
00200 YATE_API void abortOnBug();
00201 
00206 YATE_API bool abortOnBug(bool doAbort);
00207 
00211 YATE_API void setDebugTimestamp();
00212 
00218 enum DebugLevel {
00219     DebugFail = 0,
00220     DebugGoOn = 2,
00221     DebugStub = 4,
00222     DebugWarn = 5,
00223     DebugMild = 6,
00224     DebugCall = 7,
00225     DebugNote = 8,
00226     DebugInfo = 9,
00227     DebugAll = 10
00228 };
00229 
00234 YATE_API int debugLevel();
00235 
00241 YATE_API int debugLevel(int level);
00242 
00248 YATE_API bool debugAt(int level);
00249 
00256 YATE_API const char* debugColor(int level);
00257 
00263 class YATE_API DebugEnabler
00264 {
00265 public:
00271     inline DebugEnabler(int level = TelEngine::debugLevel(), bool enabled = true)
00272         : m_level(DebugFail), m_enabled(enabled), m_chain(0), m_name(0)
00273         { debugLevel(level); }
00274 
00275     inline ~DebugEnabler()
00276         { m_name = 0; m_chain = 0; }
00277 
00282     inline int debugLevel() const
00283         { return m_chain ? m_chain->debugLevel() : m_level; }
00284 
00290     int debugLevel(int level);
00291 
00296     inline bool debugEnabled() const
00297         { return m_chain ? m_chain->debugEnabled() : m_enabled; }
00298 
00303     inline void debugEnabled(bool enable)
00304         { m_enabled = enable; m_chain = 0; }
00305 
00310     inline const char* debugName() const
00311         { return m_name; }
00312 
00318     bool debugAt(int level) const;
00319 
00324     inline bool debugChained() const
00325         { return m_chain != 0; }
00326 
00331     inline void debugChain(const DebugEnabler* chain = 0)
00332         { m_chain = (chain != this) ? chain : 0; }
00333 
00338     void debugCopy(const DebugEnabler* original = 0);
00339 
00340 protected:
00345     inline void debugName(const char* name)
00346         { m_name = name; }
00347 
00348 private:
00349     int m_level;
00350     bool m_enabled;
00351     const DebugEnabler* m_chain;
00352     const char* m_name;
00353 };
00354 
00355 #if 0 /* for documentation generator */
00356 
00361 void DDebug(int level, const char* format, ...);
00362 
00368 void DDebug(const char* facility, int level, const char* format, ...);
00369 
00375 void DDebug(const DebugEnabler* local, int level, const char* format, ...);
00376 
00382 void XDebug(int level, const char* format, ...);
00383 
00389 void XDebug(const char* facility, int level, const char* format, ...);
00390 
00396 void XDebug(const DebugEnabler* local, int level, const char* format, ...);
00397 
00403 void NDebug(int level, const char* format, ...);
00404 
00410 void NDebug(const char* facility, int level, const char* format, ...);
00411 
00417 void NDebug(const DebugEnabler* local, int level, const char* format, ...);
00418 #endif
00419 
00420 #ifdef _DEBUG
00421 #undef DEBUG
00422 #define DEBUG
00423 #endif
00424 
00425 #ifdef XDEBUG
00426 #undef DEBUG
00427 #define DEBUG
00428 #endif
00429 
00430 #ifdef DEBUG
00431 #define DDebug Debug
00432 #else
00433 #ifdef _WINDOWS
00434 #define DDebug
00435 #else
00436 #define DDebug(arg...)
00437 #endif
00438 #endif
00439 
00440 #ifdef XDEBUG
00441 #define XDebug Debug
00442 #else
00443 #ifdef _WINDOWS
00444 #define XDebug
00445 #else
00446 #define XDebug(arg...)
00447 #endif
00448 #endif
00449 
00450 #ifndef NDEBUG
00451 #define NDebug Debug
00452 #else
00453 #ifdef _WINDOWS
00454 #define NDebug
00455 #else
00456 #define NDebug(arg...)
00457 #endif
00458 #endif
00459 
00465 YATE_API void Debug(int level, const char* format, ...) FORMAT_CHECK(2);
00466 
00473 YATE_API void Debug(const char* facility, int level, const char* format, ...) FORMAT_CHECK(3);
00474 
00481 YATE_API void Debug(const DebugEnabler* local, int level, const char* format, ...) FORMAT_CHECK(3);
00482 
00487 YATE_API void Output(const char* format, ...) FORMAT_CHECK(1);
00488 
00495 class YATE_API Debugger
00496 {
00497 public:
00503     Debugger(const char* name, const char* format = 0, ...);
00504 
00511     Debugger(int level, const char* name, const char* format = 0, ...);
00512 
00516     ~Debugger();
00517 
00522     static void setOutput(void (*outFunc)(const char*,int) = 0);
00523 
00528     static void setIntOut(void (*outFunc)(const char*,int) = 0);
00529 
00535     static void enableOutput(bool enable = true, bool colorize = false);
00536 
00537 private:
00538     const char* m_name;
00539 };
00540 
00545 struct TokenDict {
00549     const char* token;
00550 
00554     int value;
00555 };
00556 
00557 class String;
00558 
00559 #if 0 /* for documentation generator */
00560 
00565 void YCLASS(class type,class base);
00566 
00573 class* YOBJECT(class type,GenObject* pntr);
00574 #endif
00575 
00576 #define YCLASS(type,base) \
00577 public: virtual void* getObject(const String& name) const \
00578 { return (name == #type) ? const_cast<type*>(this) : base::getObject(name); }
00579 
00580 #define YOBJECT(type,pntr) (static_cast<type*>((pntr) ? (pntr)->getObject(#type) : 0))
00581 
00585 class YATE_API GenObject
00586 {
00587 public:
00591     virtual ~GenObject() { }
00592 
00599     virtual bool alive() const;
00600 
00604     virtual void destruct();
00605 
00612     virtual const String& toString() const;
00613 
00619     virtual void* getObject(const String& name) const;
00620 };
00621 
00626 class YATE_API RefObject : public GenObject
00627 {
00628 public:
00633     RefObject()
00634         : m_refcount(1) { }
00635 
00639     virtual ~RefObject();
00640 
00647     virtual bool alive() const;
00648 
00653     bool ref();
00654 
00663     bool deref();
00664 
00669     inline int refcount() const
00670         { return m_refcount; }
00671 
00676     virtual void destruct();
00677 
00678 protected:
00683     virtual void zeroRefs();
00684 
00690     bool resurrect();
00691 
00692 private:
00693     int m_refcount;
00694 };
00695 
00701 class YATE_API RefPointerBase
00702 {
00703 protected:
00707     inline RefPointerBase()
00708         : m_pointer(0) { }
00709 
00716     void assign(RefObject* oldptr, RefObject* newptr, void* pointer);
00717 
00721     void* m_pointer;
00722 };
00723 
00727 template <class Obj = RefObject> class YATE_API RefPointer : public RefPointerBase
00728 {
00729 protected:
00734     inline Obj* pointer() const
00735         { return static_cast<Obj*>(m_pointer); }
00736 
00741     inline void assign(Obj* object = 0)
00742         { RefPointerBase::assign(pointer(),object,object); }
00743 
00744 public:
00748     inline RefPointer()
00749         { }
00750 
00755     inline RefPointer(const RefPointer<Obj>& value)
00756         { assign(value); }
00757 
00762     inline RefPointer(Obj* object)
00763         { assign(object); }
00764 
00768     inline ~RefPointer()
00769         { assign(); }
00770 
00774     inline RefPointer<Obj>& operator=(const RefPointer<Obj>& value)
00775         { assign(value.pointer()); return *this; }
00776 
00780     inline RefPointer<Obj>& operator=(Obj* object)
00781         { assign(object); return *this; }
00782 
00787     inline operator Obj*() const
00788         { return pointer(); }
00789 
00793     inline Obj* operator->() const
00794         { return pointer(); }
00795 
00799     inline Obj& operator*() const
00800         { return *pointer(); }
00801 };
00802 
00806 template <class Obj = GenObject> class YATE_API GenPointer : public GenObject
00807 {
00808 private:
00812     Obj* m_pointer;
00813 
00814 public:
00818     inline GenPointer()
00819         : m_pointer(0)
00820         { }
00821 
00826     inline GenPointer(const GenPointer<Obj>& value)
00827         : m_pointer(value)
00828         { }
00829 
00834     inline GenPointer(Obj* object)
00835         : m_pointer(object)
00836         { }
00837 
00841     inline GenPointer<Obj>& operator=(const GenPointer<Obj>& value)
00842         { m_pointer = value; return *this; }
00843 
00847     inline GenPointer<Obj>& operator=(Obj* object)
00848         { m_pointer = object; return *this; }
00849 
00854     inline operator Obj*() const
00855         { return m_pointer; }
00856 
00860     inline Obj* operator->() const
00861         { return m_pointer; }
00862 
00866     inline Obj& operator*() const
00867         { return *m_pointer; }
00868 };
00869 
00874 class YATE_API ObjList : public GenObject
00875 {
00876 public:
00880     ObjList();
00881 
00885     virtual ~ObjList();
00886 
00892     virtual void* getObject(const String& name) const;
00893 
00898     unsigned int length() const;
00899 
00904     unsigned int count() const;
00905 
00910     inline GenObject* get() const
00911         { return m_obj; }
00912 
00919     GenObject* set(const GenObject* obj, bool delold = true);
00920 
00925     inline ObjList* next() const
00926         { return m_next; }
00927 
00932     ObjList* last() const;
00933 
00938     ObjList* skipNull() const;
00939 
00944     ObjList* skipNext() const;
00945 
00951     ObjList* operator+(int index) const;
00952 
00958     GenObject* operator[](int index) const;
00959 
00965     GenObject* operator[](const String& str) const;
00966 
00972     ObjList* find(const GenObject* obj) const;
00973 
00979     ObjList* find(const String& str) const;
00980 
00987     ObjList* insert(const GenObject* obj, bool compact = true);
00988 
00995     ObjList* append(const GenObject* obj, bool compact = true);
00996 
01002     GenObject* remove(bool delobj = true);
01003 
01010     GenObject* remove(GenObject* obj, bool delobj = true);
01011 
01015     void clear();
01016 
01021     inline bool autoDelete()
01022         { return m_delete; }
01023 
01028     inline void setDelete(bool autodelete)
01029         { m_delete = autodelete; }
01030 
01031 private:
01032     ObjList* m_next;
01033     GenObject* m_obj;
01034     bool m_delete;
01035 };
01036 
01045 class YATE_API Array : public RefObject
01046 {
01047 public:
01053     Array(int columns = 0, int rows = 0);
01054 
01058     virtual ~Array();
01059 
01065     virtual void* getObject(const String& name) const;
01066 
01073     bool addRow(ObjList* row = 0, int index = -1);
01074     
01081     bool addColumn(ObjList* column = 0, int index = -1);
01082     
01088     bool delRow(int index);
01089 
01095     bool delColumn(int index);
01096  
01103     GenObject* get(int column, int row) const;
01104     
01112     bool set(GenObject* obj, int column, int row);
01113     
01118     inline int getRows() const
01119         { return m_rows; }
01120 
01125     inline int getColumns() const
01126         { return m_columns; }
01127 
01128 private:
01129     int m_rows;
01130     int m_columns;
01131     ObjList m_obj;
01132 };
01133 
01134 class Regexp;
01135 class StringMatchPrivate;
01136 
01144 class YATE_API String : public GenObject
01145 {
01146 public:
01150     String();
01151 
01157     String(const char* value, int len = -1);
01158 
01164     String(char value, unsigned int repeat = 1);
01165 
01170     String(int value);
01171 
01176     String(unsigned int value);
01177 
01182     String(bool value);
01183 
01188     String(const String& value);
01189 
01194     String(const String* value);
01195 
01199     virtual ~String();
01200 
01206     virtual void* getObject(const String& name) const;
01207 
01211     static const String& empty();
01212 
01218     inline static const char* boolText(bool value)
01219         { return value ? "true" : "false"; }
01220 
01225     inline const char* c_str() const
01226         { return m_string; }
01227 
01232     inline const char* safe() const
01233         { return m_string ? m_string : ""; }
01234 
01239     inline unsigned int length() const
01240         { return m_length; }
01241 
01246     inline bool null() const
01247         { return !m_string; }
01248 
01253     unsigned int hash() const;
01254 
01259     static unsigned int hash(const char* value);
01260 
01264     void clear();
01265 
01271     char at(int index) const;
01272 
01279     String substr(int offs, int len = -1) const;
01280 
01284     String& trimBlanks();
01285 
01290     virtual const String& toString() const;
01291 
01298     int toInteger(int defvalue = 0, int base = 0) const;
01299 
01307     int toInteger(const TokenDict* tokens, int defvalue = 0, int base = 0) const;
01308 
01314     double toDouble(double defvalue = 0.0) const;
01315 
01321     bool toBoolean(bool defvalue = false) const;
01322 
01327     bool isBoolean() const;
01328 
01333     String& toUpper();
01334 
01339     String& toLower();
01340 
01346     inline char operator[](int index) const
01347         { return at(index); }
01348 
01353     inline operator const char*() const
01354         { return m_string; };
01355 
01362     String& assign(const char* value, int len = -1);
01363 
01370     String& assign(char value, unsigned int repeat = 1);
01371 
01380     String& hexify(void* data, unsigned int len, char sep = 0, bool upCase = false);
01381 
01385     inline String& operator=(const String& value)
01386         { return operator=(value.c_str()); }
01387 
01392     inline String& operator=(const String* value)
01393         { return operator=(value ? value->c_str() : ""); }
01394 
01399     String& operator=(const char* value);
01400 
01404     String& operator=(char value);
01405 
01409     String& operator=(int value);
01410 
01414     String& operator=(unsigned int value);
01415 
01419     inline String& operator=(bool value)
01420         { return operator=(boolText(value)); }
01421 
01426     String& operator+=(const char* value);
01427 
01431     String& operator+=(char value);
01432 
01436     String& operator+=(int value);
01437 
01441     String& operator+=(unsigned int value);
01442 
01446     inline String& operator+=(bool value)
01447         { return operator+=(boolText(value)); }
01448 
01452     bool operator==(const char* value) const;
01453 
01457     bool operator!=(const char* value) const;
01458 
01462     bool operator==(const String& value) const;
01463 
01467     bool operator!=(const String& value) const;
01468 
01472     bool operator&=(const char* value) const;
01473 
01477     bool operator|=(const char* value) const;
01478 
01482     inline String& operator<<(const char* value)
01483         { return operator+=(value); }
01484 
01488     inline String& operator<<(char value)
01489         { return operator+=(value); }
01490 
01494     inline String& operator<<(int value)
01495         { return operator+=(value); }
01496 
01500     inline String& operator<<(unsigned int value)
01501         { return operator+=(value); }
01502 
01506     inline String& operator<<(bool value)
01507         { return operator+=(value); }
01508 
01513     String& operator>>(const char* skip);
01514 
01518     String& operator>>(char& store);
01519 
01523     String& operator>>(int& store);
01524 
01528     String& operator>>(unsigned int& store);
01529 
01533     String& operator>>(bool& store);
01534 
01541     String& append(const char* value, const char* separator = 0, bool force = false);
01542 
01548     String& append(double value, unsigned int decimals = 3);
01549 
01556     int find(char what, unsigned int offs = 0) const;
01557 
01564     int find(const char* what, unsigned int offs = 0) const;
01565 
01571     int rfind(char what) const;
01572 
01580     bool startsWith(const char* what, bool wordBreak = false, bool caseInsensitive = false) const;
01581 
01589     bool endsWith(const char* what, bool wordBreak = false, bool caseInsensitive = false) const;
01590 
01602     bool startSkip(const char* what, bool wordBreak = true, bool caseInsensitive = false);
01603 
01609     virtual bool matches(const String& value) const
01610         { return operator==(value); }
01611 
01617     bool matches(Regexp& rexp);
01618 
01624     int matchOffset(int index = 0) const;
01625 
01631     int matchLength(int index = 0) const;
01632 
01638     inline String matchString(int index = 0) const
01639         { return substr(matchOffset(index),matchLength(index)); }
01640 
01646     String replaceMatches(const String& templ) const;
01647 
01652     int matchCount() const;
01653 
01660     ObjList* split(char separator, bool emptyOK = true) const;
01661 
01668     static String msgEscape(const char* str, char extraEsc = 0);
01669 
01675     inline String msgEscape(char extraEsc = 0) const
01676         { return msgEscape(c_str(),extraEsc); }
01677 
01685     static String msgUnescape(const char* str, int* errptr = 0, char extraEsc = 0);
01686 
01693     inline String msgUnescape(int* errptr = 0, char extraEsc = 0) const
01694         { return msgUnescape(c_str(),errptr,extraEsc); }
01695 
01702     static String sqlEscape(const char* str, char extraEsc = 0);
01703 
01709     inline String sqlEscape(char extraEsc = 0) const
01710         { return sqlEscape(c_str(),extraEsc); }
01711 
01718     static String uriEscape(const char* str, char extraEsc = 0);
01719 
01725     inline String uriEscape(char extraEsc = 0) const
01726         { return uriEscape(c_str(),extraEsc); }
01727 
01734     static String uriUnescape(const char* str, int* errptr = 0);
01735 
01741     inline String uriUnescape(int* errptr = 0) const
01742         { return uriUnescape(c_str(),errptr); }
01743 
01744 protected:
01748      virtual void changed();
01749 
01750 private:
01751     void clearMatches();
01752     char* m_string;
01753     unsigned int m_length;
01754     // I hope every C++ compiler now knows about mutable...
01755     mutable unsigned int m_hash;
01756     StringMatchPrivate* m_matches;
01757 };
01758 
01764 inline const char *c_safe(const char* str)
01765     { return str ? str : ""; }
01766 
01772 inline bool null(const char* str)
01773     { return !(str && *str); }
01774 
01778 YATE_API String operator+(const String& s1, const String& s2);
01779 
01783 YATE_API String operator+(const String& s1, const char* s2);
01784 
01788 YATE_API String operator+(const char* s1, const String& s2);
01789 
01794 inline const char *strcpy(String& dest, const char* src)
01795     { dest = src; return dest.c_str(); }
01796 
01801 inline const char *strcat(String& dest, const char* src)
01802     { dest += src; return dest.c_str(); }
01803 
01812 YATE_API int lookup(const char* str, const TokenDict* tokens, int defvalue = 0, int base = 0);
01813 
01820 YATE_API const char* lookup(int value, const TokenDict* tokens, const char* defvalue = 0);
01821 
01822 
01827 class YATE_API Regexp : public String
01828 {
01829     friend class String;
01830 public:
01834     Regexp();
01835 
01842     Regexp(const char* value, bool extended = false, bool insensitive = false);
01843 
01848     Regexp(const Regexp& value);
01849 
01853     virtual ~Regexp();
01854 
01858     inline Regexp& operator=(const char* value)
01859         { String::operator=(value); return *this; }
01860 
01865     bool compile();
01866 
01872     bool matches(const char* value) const;
01873 
01879     virtual bool matches(const String& value) const
01880         { return Regexp::matches(value.safe()); }
01881 
01887     void setFlags(bool extended, bool insensitive);
01888 
01893     bool isExtended() const;
01894 
01899     bool isCaseInsensitive() const;
01900 
01901 protected:
01905     virtual void changed();
01906 
01907 private:
01908     void cleanup();
01909     bool matches(const char *value, StringMatchPrivate *matchlist);
01910     void* m_regexp;
01911     int m_flags;
01912 };
01913 
01918 class YATE_API NamedString : public String
01919 {
01920 public:
01926     NamedString(const char* name, const char* value = 0);
01927 
01932     inline const String& name() const
01933         { return m_name; }
01934 
01939     virtual const String& toString() const;
01940 
01944     inline NamedString& operator=(const char* value)
01945         { String::operator=(value); return *this; }
01946 
01947 private:
01948     NamedString(); // no default constructor please
01949     String m_name;
01950 };
01951 
01959 class YATE_API HashList : public GenObject
01960 {
01961 public:
01966     HashList(unsigned int size = 17);
01967 
01971     virtual ~HashList();
01972 
01978     virtual void* getObject(const String& name) const;
01979 
01984     inline unsigned int length() const
01985         { return m_size; }
01986 
01991     unsigned int count() const;
01992 
01999     inline ObjList* getList(unsigned int index) const
02000         { return (index < m_size) ? m_lists[index] : 0; }
02001 
02007     inline ObjList* getHashList(unsigned int hash) const
02008         { return getList(hash % m_size); }
02009 
02015     inline ObjList* getHashList(const String& str) const
02016         { return getHashList(str.hash()); }
02017 
02023     GenObject* operator[](const String& str) const;
02024 
02030     ObjList* find(const GenObject* obj) const;
02031 
02037     ObjList* find(const String& str) const;
02038 
02044     ObjList* append(const GenObject* obj);
02045 
02052     GenObject* remove(GenObject* obj, bool delobj = true);
02053 
02057     void clear();
02058 
02065     bool resync(GenObject* obj);
02066 
02072     bool resync();
02073 
02074 private:
02075     unsigned int m_size;
02076     ObjList** m_lists;
02077 };
02078 
02085 class YATE_API ListIterator
02086 {
02087 public:
02093     ListIterator(ObjList& list);
02094 
02100     ListIterator(HashList& list);
02101 
02105     ~ListIterator();
02106 
02111     inline unsigned int length() const
02112         { return m_length; }
02113 
02120     GenObject* get(unsigned int index) const;
02121 
02134     GenObject* get();
02135 
02140     inline bool eof() const
02141         { return m_current >= m_length; }
02142 
02146     inline void reset()
02147         { m_current = 0; }
02148 
02149 private:
02150     ObjList* m_objList;
02151     HashList* m_hashList;
02152     GenObject** m_objects;
02153     unsigned int m_length;
02154     unsigned int m_current;
02155 };
02156 
02161 class YATE_API Time
02162 {
02163 public:
02167     inline Time()
02168         : m_time(now())
02169         { }
02170 
02175     inline Time(u_int64_t usec)
02176         : m_time(usec)
02177         { }
02178 
02183     inline Time(const struct timeval* tv)
02184         : m_time(fromTimeval(tv))
02185         { }
02186 
02191     inline Time(const struct timeval& tv)
02192         : m_time(fromTimeval(tv))
02193         { }
02194 
02199     inline ~Time()
02200         { }
02201 
02206     inline u_int32_t sec() const
02207         { return (u_int32_t)((m_time+500000) / 1000000); }
02208 
02213     inline u_int64_t msec() const
02214         { return (m_time+500) / 1000; }
02215 
02220     inline u_int64_t usec() const
02221         { return m_time; }
02222 
02226     inline operator u_int64_t() const
02227         { return m_time; }
02228 
02232     inline Time& operator=(u_int64_t usec)
02233         { m_time = usec; return *this; }
02234 
02238     inline Time& operator+=(int64_t delta)
02239         { m_time += delta; return *this; }
02240 
02244     inline Time& operator-=(int64_t delta)
02245         { m_time -= delta; return *this; }
02246 
02251     inline void toTimeval(struct timeval* tv) const
02252         { toTimeval(tv, m_time); }
02253 
02259     static void toTimeval(struct timeval* tv, u_int64_t usec);
02260 
02266     static u_int64_t fromTimeval(const struct timeval* tv);
02267 
02273     inline static u_int64_t fromTimeval(const struct timeval& tv)
02274         { return fromTimeval(&tv); }
02275 
02280     static u_int64_t now();
02281 
02286     static u_int64_t msecNow();
02287 
02292     static u_int32_t secNow();
02293 
02294 private:
02295     u_int64_t m_time;
02296 };
02297 
02302 class YATE_API DataBlock : public GenObject
02303 {
02304 public:
02305 
02309     DataBlock();
02310 
02314     DataBlock(const DataBlock& value);
02315 
02322     DataBlock(void* value, unsigned int len, bool copyData = true);
02323 
02327     virtual ~DataBlock();
02328 
02334     virtual void* getObject(const String& name) const;
02335 
02339     static const DataBlock& empty();
02340 
02345     inline void* data() const
02346         { return m_data; }
02347 
02352     inline bool null() const
02353         { return !m_data; }
02354 
02359     inline unsigned int length() const
02360         { return m_length; }
02361 
02366     void clear(bool deleteData = true);
02367 
02374     DataBlock& assign(void* value, unsigned int len, bool copyData = true);
02375 
02380     void append(const DataBlock& value);
02381 
02386     void append(const String& value);
02387 
02392     void insert(const DataBlock& value);
02393 
02398     void truncate(unsigned int len);
02399 
02404     void cut(int len);
02405 
02409     DataBlock& operator=(const DataBlock& value);
02410 
02414     inline DataBlock& operator+=(const DataBlock& value)
02415         { append(value); return *this; }
02416 
02420     inline DataBlock& operator+=(const String& value)
02421         { append(value); return *this; }
02422 
02431     bool convert(const DataBlock& src, const String& sFormat,
02432         const String& dFormat, unsigned maxlen = 0);
02433 
02434 private:
02435     void* m_data;
02436     unsigned int m_length;
02437 };
02438 
02443 class YATE_API MD5
02444 {
02445 public:
02449     MD5();
02450 
02455     MD5(const MD5& original);
02456 
02462     MD5(const void* buf, unsigned int len);
02463 
02468     MD5(const DataBlock& data);
02469 
02474     MD5(const String& str);
02475 
02479     ~MD5();
02480 
02484     MD5& operator=(const MD5& original);
02485 
02489     void clear();
02490 
02495     void finalize();
02496 
02503     bool update(const void* buf, unsigned int len);
02504 
02510     inline bool update(const DataBlock& data)
02511         { return update(data.data(), data.length()); }
02512 
02518     inline bool update(const String& str)
02519         { return update(str.c_str(), str.length()); }
02520 
02524     inline MD5& operator<<(const String& value)
02525         { update(value); return *this; }
02526 
02530     inline MD5& operator<<(const DataBlock& data)
02531         { update(data); return *this; }
02532 
02536     MD5& operator<<(const char* value);
02537 
02543     const unsigned char* rawDigest();
02544 
02550     const String& hexDigest();
02551 
02552 private:
02553     void init();
02554     void* m_private;
02555     String m_hex;
02556     unsigned char m_bin[16];
02557 };
02558 
02563 class YATE_API NamedList : public String
02564 {
02565 public:
02570     NamedList(const char* name);
02571 
02576     NamedList(const NamedList& original);
02577 
02583     virtual void* getObject(const String& name) const;
02584 
02589     inline unsigned int length() const
02590         { return m_params.length(); }
02591 
02596     inline unsigned int count() const
02597         { return m_params.count(); }
02598 
02603     NamedList& addParam(NamedString* param);
02604 
02610     NamedList& addParam(const char* name, const char* value);
02611 
02616     NamedList& setParam(NamedString* param);
02617 
02623     NamedList& setParam(const char* name, const char* value);
02624 
02629     NamedList& clearParam(const String& name);
02630 
02636     NamedString* getParam(const String& name) const;
02637 
02643     NamedString* getParam(unsigned int index) const;
02644 
02651     const char* getValue(const String& name, const char* defvalue = 0) const;
02652 
02659     int getIntValue(const String& name, int defvalue = 0) const;
02660 
02668     int getIntValue(const String& name, const TokenDict* tokens, int defvalue = 0) const;
02669 
02676     double getDoubleValue(const String& name, double defvalue = 0.0) const;
02677 
02684     bool getBoolValue(const String& name, bool defvalue = false) const;
02685 
02693     int replaceParams(String& str, bool sqlEsc = false, char extraEsc = 0) const;
02694 
02695 private:
02696     NamedList(); // no default constructor please
02697     NamedList& operator=(const NamedList& value); // no assignment please
02698     ObjList m_params;
02699 };
02700 
02706 class YATE_API URI : public String
02707 {
02708 public:
02712     URI();
02713 
02718     URI(const URI& uri);
02719 
02724     URI(const String& uri);
02725 
02730     URI(const char* uri);
02731 
02740     URI(const char* proto, const char* user, const char* host, int port = 0, const char* desc = 0);
02741 
02745     void parse() const;
02746 
02751     inline URI& operator=(const URI& value)
02752         { String::operator=(value); return *this; }
02753 
02758     inline URI& operator=(const String& value)
02759         { String::operator=(value); return *this; }
02760 
02765     inline URI& operator=(const char* value)
02766         { String::operator=(value); return *this; }
02767 
02772     inline const String& getDescription() const
02773         { parse(); return m_desc; }
02774 
02779     inline const String& getProtocol() const
02780         { parse(); return m_proto; }
02781 
02786     inline const String& getUser() const
02787         { parse(); return m_user; }
02788 
02793     inline const String& getHost() const
02794         { parse(); return m_host; }
02795 
02800     inline int getPort() const
02801         { parse(); return m_port; }
02802 protected:
02808     virtual void changed();
02809     mutable bool m_parsed;
02810     mutable String m_desc;
02811     mutable String m_proto;
02812     mutable String m_user;
02813     mutable String m_host;
02814     mutable int m_port;
02815 };
02816 
02817 class MutexPrivate;
02818 class ThreadPrivate;
02819 
02824 class YATE_API Mutex
02825 {
02826     friend class MutexPrivate;
02827 public:
02831     Mutex();
02832 
02838     Mutex(bool recursive);
02839 
02844     Mutex(const Mutex& original);
02845 
02849     ~Mutex();
02850 
02855     Mutex& operator=(const Mutex& original);
02856 
02862     bool lock(long maxwait = -1);
02863 
02867     void unlock();
02868 
02874     bool locked() const;
02875 
02881     bool check(long maxwait = -1);
02882 
02887     bool recursive() const;
02888 
02893     static int count();
02894 
02899     static int locks();
02900 
02906     static void wait(unsigned long maxwait);
02907 
02908 private:
02909     MutexPrivate* privDataCopy() const;
02910     MutexPrivate* m_private;
02911 };
02912 
02918 class YATE_API Lock
02919 {
02920 public:
02926     inline Lock(Mutex& mutex, long maxwait = -1)
02927         { m_mutex = mutex.lock(maxwait) ? &mutex : 0; }
02928 
02934     inline Lock(Mutex* mutex, long maxwait = -1)
02935         { m_mutex = (mutex && mutex->lock(maxwait)) ? mutex : 0; }
02936 
02940     inline ~Lock()
02941         { if (m_mutex) m_mutex->unlock(); }
02942 
02947     inline Mutex* mutex() const
02948         { return m_mutex; }
02949 
02953     inline void drop()
02954         { if (m_mutex) m_mutex->unlock(); m_mutex = 0; }
02955 
02956 private:
02957     Mutex* m_mutex;
02958 
02960     inline void* operator new(size_t);
02961 
02963     inline void* operator new[](size_t);
02964 
02966     inline Lock(const Lock&);
02967 };
02968 
02975 class YATE_API Lock2
02976 {
02977 public:
02984     inline Lock2(Mutex* mx1, Mutex* mx2, long maxwait = -1)
02985         : m_mx1(0), m_mx2(0)
02986         { lock(mx1,mx2,maxwait); }
02987 
02994     inline Lock2(Mutex& mx1, Mutex& mx2, long maxwait = -1)
02995         : m_mx1(0), m_mx2(0)
02996         { lock(&mx1,&mx2); }
02997 
03001     inline ~Lock2()
03002         { drop(); }
03003 
03008     inline bool locked() const
03009         { return m_mx1 != 0; }
03010 
03018     bool lock(Mutex* mx1, Mutex* mx2, long maxwait = -1);
03019 
03027     inline bool lock(Mutex& mx1, Mutex& mx2, long maxwait = -1)
03028         { return lock(&mx1,&mx2); }
03029 
03033     void drop();
03034 
03035 private:
03036     Mutex* m_mx1;
03037     Mutex* m_mx2;
03038 
03040     inline void* operator new(size_t);
03041 
03043     inline void* operator new[](size_t);
03044 
03046     inline Lock2(const Lock2&);
03047 };
03048 
03054 class YATE_API Runnable
03055 {
03056 public:
03061     virtual void run() = 0;
03062 
03066     virtual ~Runnable();
03067 };
03068 
03075 class YATE_API Thread : public Runnable
03076 {
03077     friend class ThreadPrivate;
03078     friend class MutexPrivate;
03079 public:
03083     enum Priority {
03084         Lowest,
03085         Low,
03086         Normal,
03087         High,
03088         Highest
03089     };
03090 
03094     virtual void cleanup();
03095 
03100     bool startup();
03101 
03106     bool error() const;
03107 
03112     bool running() const;
03113 
03118     inline int locks() const
03119         { return m_locks; }
03120 
03125     inline bool locked() const
03126         { return m_locking || m_locks; }
03127 
03132     const char* name() const;
03133 
03138     static const char* currentName();
03139 
03145     static void yield(bool exitCheck = false);
03146 
03152     static void sleep(unsigned int sec, bool exitCheck = false);
03153 
03159     static void msleep(unsigned long msec, bool exitCheck = false);
03160 
03167     static void usleep(unsigned long usec, bool exitCheck = false);
03168 
03174     static Thread* current();
03175 
03180     static int count();
03181 
03187     static bool check(bool exitNow = true);
03188 
03192     static void exit();
03193 
03198     void cancel(bool hard = false);
03199 
03204     inline bool isCurrent() const
03205         { return current() == this; }
03206 
03207 
03214     static Priority priority(const char* name, Priority defvalue = Normal);
03215 
03221     static const char* priority(Priority prio);
03222 
03227     static void killall();
03228 
03233     static void preExec();
03234 
03235 protected:
03241     Thread(const char *name = 0, Priority prio = Normal);
03242 
03248     Thread(const char *name, const char* prio);
03249 
03253     virtual ~Thread();
03254 
03255 private:
03256     ThreadPrivate* m_private;
03257     int m_locks;
03258     bool m_locking;
03259 };
03260 
03265 class YATE_API SocketAddr : public GenObject
03266 {
03267 public:
03271     inline SocketAddr()
03272         : m_address(0), m_length(0)
03273         { }
03274 
03279     inline SocketAddr(const SocketAddr& value)
03280         : m_address(0), m_length(0)
03281         { assign(value.address(),value.length()); }
03282 
03287     SocketAddr(int family);
03288 
03294     SocketAddr(const struct sockaddr* addr, socklen_t len = 0);
03295 
03299     virtual ~SocketAddr();
03300 
03305     inline SocketAddr& operator=(const SocketAddr& value)
03306         { assign(value.address(),value.length()); return *this; }
03307 
03313     bool operator==(const SocketAddr& other) const;
03314 
03320     inline bool operator!=(const SocketAddr& other) const
03321         { return !operator==(other); }
03322 
03326     void clear();
03327 
03333     bool assign(int family);
03334 
03340     void assign(const struct sockaddr* addr, socklen_t len = 0);
03341 
03347     bool local(const SocketAddr& remote);
03348 
03353     inline bool valid() const
03354         { return m_length && m_address; }
03355 
03360     inline bool null() const
03361         { return !(m_length && m_address); }
03362 
03367     inline int family() const
03368         { return m_address ? m_address->sa_family : 0; }
03369 
03374     inline const String& host() const
03375         { return m_host; }
03376 
03381     virtual bool host(const String& name);
03382 
03387     int port() const;
03388 
03394     bool port(int newport);
03395 
03400     inline struct sockaddr* address() const
03401         { return m_address; }
03402 
03407     inline socklen_t length() const
03408         { return m_length; }
03409 
03415     static bool supports(int family);
03416 
03417 protected:
03421     virtual void stringify();
03422 
03423     struct sockaddr* m_address;
03424     socklen_t m_length;
03425     String m_host;
03426 };
03427 
03432 class YATE_API Stream
03433 {
03434 public:
03438     virtual ~Stream();
03439 
03444     inline int error() const
03445         { return m_error; }
03446 
03451     virtual bool terminate() = 0;
03452 
03457     virtual bool canRetry() const;
03458 
03463     virtual bool valid() const = 0;
03464 
03470     virtual bool setBlocking(bool block = true);
03471 
03478     virtual int writeData(const void* buffer, int length) = 0;
03479 
03485     int writeData(const char* str);
03486 
03492     inline int writeData(const String& str)
03493         { return writeData(str.c_str(), str.length()); }
03494 
03500     inline int writeData(const DataBlock& buf)
03501         { return writeData(buf.data(), buf.length()); }
03502 
03509     virtual int readData(void* buffer, int length) = 0;
03510 
03517     static bool allocPipe(Stream*& reader, Stream*& writer);
03518 
03525     static bool allocPair(Stream*& str1, Stream*& str2);
03526 
03531     static bool supportsPipes();
03532 
03537     static bool supportsPairs();
03538 
03539 protected:
03543     inline Stream()
03544         : m_error(0)
03545         { }
03546 
03550     inline void clearError()
03551         { m_error = 0; }
03552 
03553     int m_error;
03554 };
03555 
03560 class YATE_API File : public Stream
03561 {
03562 public:
03566     File();
03567 
03572     File(HANDLE handle);
03573 
03577     virtual ~File();
03578 
03589     virtual bool openPath(const char* name, bool canWrite = false, bool canRead = true,
03590         bool create = false, bool append = false, bool binary = false);
03591 
03596     virtual bool terminate();
03597 
03602     void attach(HANDLE handle);
03603 
03608     HANDLE detach();
03609 
03614     inline HANDLE handle() const
03615         { return m_handle; }
03616 
03621     virtual bool canRetry() const;
03622 
03627     virtual bool valid() const;
03628 
03633     static HANDLE invalidHandle();
03634 
03640     virtual bool setBlocking(bool block = true);
03641 
03646     virtual unsigned int length();
03647 
03654     virtual int writeData(const void* buffer, int length);
03655 
03662     virtual int readData(void* buffer, int length);
03663 
03669     static bool remove(const char* name);
03670 
03677     static bool createPipe(File& reader, File& writer);
03678 
03679 protected:
03680 
03684     void copyError();
03685 
03686     HANDLE m_handle;
03687 };
03688 
03693 class YATE_API Socket : public Stream
03694 {
03695 public:
03699     enum TOS {
03700         LowDelay       = IPTOS_LOWDELAY,
03701         MaxThroughput  = IPTOS_THROUGHPUT,
03702         MaxReliability = IPTOS_RELIABILITY,
03703         MinCost        = IPTOS_MINCOST,
03704     };
03705 
03709     Socket();
03710 
03715     Socket(SOCKET handle);
03716 
03723     Socket(int domain, int type, int protocol = 0);
03724 
03728     virtual ~Socket();
03729 
03737     bool create(int domain, int type, int protocol = 0);
03738 
03743     virtual bool terminate();
03744 
03749     void attach(SOCKET handle);
03750 
03755     SOCKET detach();
03756 
03761     inline SOCKET handle() const
03762         { return m_handle; }
03763 
03768     virtual bool canRetry() const;
03769 
03774     virtual bool valid() const;
03775 
03780     static SOCKET invalidHandle();
03781 
03786     static int socketError();
03787 
03796     bool setOption(int level, int name, const void* value = 0, socklen_t length = 0);
03797 
03806     bool getOption(int level, int name, void* buffer, socklen_t* length);
03807 
03813     bool setTOS(int tos);
03814     
03820     virtual bool setBlocking(bool block = true);
03821 
03829     bool setReuse(bool reuse = true, bool exclusive = false);
03830 
03837     bool setLinger(int seconds = -1);
03838 
03845     bool bind(struct sockaddr* addr, socklen_t addrlen);
03846 
03852     inline bool bind(const SocketAddr& addr)
03853         { return bind(addr.address(), addr.length()); }
03854 
03860     bool listen(unsigned int backlog = 0);
03861 
03868     Socket* accept(struct sockaddr* addr = 0, socklen_t* addrlen = 0);
03869 
03875     Socket* accept(SocketAddr& addr);
03876 
03883     SOCKET acceptHandle(struct sockaddr* addr = 0, socklen_t* addrlen = 0);
03884 
03890     Socket* peelOff(unsigned int assoc);
03891 
03897     SOCKET peelOffHandle(unsigned int assoc);
03898 
03905     bool connect(struct sockaddr* addr, socklen_t addrlen);
03906 
03912     inline bool connect(const SocketAddr& addr)
03913         { return connect(addr.address(), addr.length()); }
03914 
03921     bool shutdown(bool stopReads, bool stopWrites);
03922 
03929     bool getSockName(struct sockaddr* addr, socklen_t* addrlen);
03930 
03936     bool getSockName(SocketAddr& addr);
03937 
03944     bool getPeerName(struct sockaddr* addr, socklen_t* addrlen);
03945 
03951     bool getPeerName(SocketAddr& addr);
03952 
03962     int sendTo(const void* buffer, int length, const struct sockaddr* addr, socklen_t adrlen, int flags = 0);
03963 
03972     inline int sendTo(const void* buffer, int length, const SocketAddr& addr, int flags = 0)
03973         { return sendTo(buffer, length, addr.address(), addr.length(), flags); }
03974 
03982     int send(const void* buffer, int length, int flags = 0);
03983 
03990     virtual int writeData(const void* buffer, int length);
03991 
04001     int recvFrom(void* buffer, int length, struct sockaddr* addr = 0, socklen_t* adrlen = 0, int flags = 0);
04002 
04011     int recvFrom(void* buffer, int length, SocketAddr& addr, int flags = 0);
04012 
04020     int recv(void* buffer, int length, int flags = 0);
04021 
04028     virtual int readData(void* buffer, int length);
04029 
04038     bool select(bool* readok, bool* writeok, bool* except, struct timeval* timeout = 0);
04039 
04048     bool select(bool* readok, bool* writeok, bool* except, int64_t timeout);
04049 
04057     static bool createPair(Socket& sock1, Socket& sock2, int domain = AF_UNIX);
04058 
04059 protected:
04060 
04064     void copyError();
04065 
04072     bool checkError(int retcode, bool strict = false);
04073 
04074     SOCKET m_handle;
04075 };
04076 
04082 class YATE_API SysUsage
04083 {
04084 public:
04088     enum Type {
04089         WallTime,
04090         UserTime,
04091         KernelTime
04092     };
04093 
04097     static void init();
04098 
04103     static u_int64_t startTime();
04104 
04110     static u_int64_t usecRunTime(Type type = WallTime);
04111 
04117     static u_int64_t msecRunTime(Type type = WallTime);
04118 
04124     static u_int32_t secRunTime(Type type = WallTime);
04125 
04131     static double runTime(Type type = WallTime);
04132 
04133 };
04134 
04135 }; // namespace TelEngine
04136 
04137 #endif /* __YATECLASS_H */
04138 
04139 /* vi: set ts=8 sw=4 sts=4 noet: */

Generated on Mon Sep 18 20:56:12 2006 for Yate by  doxygen 1.4.4