misc.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 // 
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 // 
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however    
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.    
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // Common C++.  If you copy code from other releases into a copy of GNU
00028 // Common C++, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU Common C++, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00044 #ifndef CCXX_MISC_H_
00045 #define CCXX_MISC_H_
00046 
00047 #ifndef CCXX_MISSING_H_
00048 #include <cc++/missing.h>
00049 #endif
00050 
00051 #ifndef CCXX_THREAD_H_
00052 #include <cc++/thread.h>
00053 #endif
00054 
00055 #define KEYDATA_INDEX_SIZE      97
00056 #define KEYDATA_PAGER_SIZE      512
00057 
00058 #if defined(PATH_MAX)
00059 #if PATH_MAX > 512
00060 #define KEYDATA_PATH_SIZE       512
00061 #else
00062 #define KEYDATA_PATH_SIZE       PATH_MAX
00063 #endif
00064 #else
00065 #define KEYDATA_PATH_SIZE       256
00066 #endif
00067 
00068 #ifdef  CCXX_NAMESPACES
00069 namespace ost {
00070 #endif
00071 
00072 class __EXPORT Runlist;
00073 class __EXPORT Runable;
00074 
00090 class __EXPORT MemPager
00091 {
00092 private:
00093         friend class String;
00094         friend class MemPagerObject;
00095 
00096         size_t pagesize;
00097         unsigned int pages;
00098 
00099         struct _page
00100         {
00101                 struct _page *next;
00102                 size_t used;
00103         } *page;
00104 
00105 protected:
00115         virtual void* first(size_t size);
00116 
00124         virtual void* alloc(size_t size);
00125 
00135         char* first(char *str);
00136 
00146         char* alloc(const char *str);
00147 
00157         MemPager(size_t pagesize = 4096);
00158 
00162         void purge(void);
00163 
00167         void clean(void);
00168 
00172         virtual ~MemPager();
00173 
00174 public:
00181         inline int getPages(void)
00182                 {return pages;};
00183 };
00184 
00194 class __EXPORT StackPager : protected MemPager
00195 {
00196 private:
00197         typedef struct frame
00198         {
00199                 struct frame *next;
00200                 char data[1];
00201         }       frame_t;
00202 
00203         frame_t *stack;
00204 
00205 public:
00211         StackPager(size_t pagesize);
00212 
00220         void *push(const void *object, size_t size);
00221 
00228         void *push(const char *string);
00229 
00235         void *pull(void);
00236 
00240         void purge(void);
00241 };
00242 
00251 class __EXPORT SharedMemPager : public MemPager, public Mutex
00252 {
00253 protected:
00260         SharedMemPager(size_t pagesize = 4096, const char *name = NULL);
00261 
00265         void purge(void);
00266 
00273         void* first(size_t size);
00274 
00281         void* alloc(size_t size);
00282 };
00283 
00351 class __EXPORT Keydata : protected MemPager
00352 {
00353 public:
00354 #ifdef  CCXX_PACKED
00355 #pragma pack(1)
00356 #endif
00357 
00358         struct Keyval
00359         {
00360                 Keyval *next;
00361                 char val[1];
00362         };
00363 
00364         struct Keysym
00365         {
00366                 Keysym *next;
00367                 Keyval *data;
00368                 const char **list;
00369                 short count;
00370                 char sym[1];
00371         };
00372 
00373         struct Define
00374         {
00375                 char *keyword;
00376                 char *value;
00377         };
00378 
00379 #ifdef  CCXX_PACKED
00380 #pragma pack()
00381 #endif
00382 
00383 private:
00384         static std::ifstream *cfgFile;
00385         static char lastpath[KEYDATA_PATH_SIZE + 1];
00386         static int count;
00387         static int sequence;
00388 
00389         int link;
00390 
00391         Keysym *keys[KEYDATA_INDEX_SIZE];
00392 
00399         unsigned getIndex(const char *sym);
00400 
00401 protected:
00402         Keysym* getSymbol(const char *sym, bool create);
00403 
00404 public:
00416         void load(const char *keypath);
00417 
00431         void loadPrefix(const char *prefix, const char *keypath);
00432 
00442         void loadFile(const char *filepath, const char *keys = NULL, const char *pre = NULL);
00443 
00452         void load(Define *pairs);
00453         
00457         Keydata();
00458 
00466         Keydata(const char *keypath);
00467 
00475         Keydata(Define *pairs, const char *keypath = NULL);
00476 
00482         virtual ~Keydata();
00483 
00491         void unlink(void);
00492 
00501         int getCount(const char *sym);
00502 
00510         const char* getFirst(const char *sym);
00511 
00519         const char* getLast(const char *sym);
00520 
00527         bool isKey(const char *sym);
00528 
00536         const char *getString(const char *sym, const char *def = NULL);
00537 
00545         long getLong(const char *sym, long def = 0);
00546 
00553         bool getBool(const char *key);
00554 
00562         double getDouble(const char *key, double def = 0.); 
00563 
00572         unsigned getIndex(char **data, unsigned max);
00573 
00580         unsigned getCount(void);
00581 
00590         void setValue(const char *sym, const char *data);
00591 
00599         const char * const* getList(const char *sym);
00600 
00607         void clrValue(const char *sym);
00608 
00613         inline const char *operator[](const char *keyword)
00614                 {return getLast(keyword);};
00615 
00619         static void end(void);
00620 
00625         friend inline void endKeydata(void)
00626                 {Keydata::end();};
00627 };
00628 
00636 class __EXPORT MemPagerObject
00637 {
00638 public:
00645         inline void *operator new(size_t size, MemPager &pager)
00646                 {return pager.alloc(size);};
00647 
00654         inline void *operator new[](size_t size, MemPager &pager)
00655                 {return pager.alloc(size);};
00656 
00660         inline void operator delete(void *) {};
00661 
00665         inline void operator delete[](void *) {};
00666 };
00667 
00676 class __EXPORT Assoc
00677 {
00678 private:
00679         class __EXPORT entry
00680         {
00681         public:
00682                 const char *id;
00683                 entry *next;
00684                 void *data;
00685         };
00686 
00687         entry *entries[KEYDATA_INDEX_SIZE];
00688 
00689 protected:
00690         Assoc();
00691         virtual ~Assoc();
00692 
00693         void clear(void);
00694 
00695         virtual void *getMemory(size_t size) = 0;
00696 
00697 public:
00698         void *getPointer(const char *id);
00699         void setPointer(const char *id, void *data);
00700 };
00701 
00712 class __EXPORT Runlist : public Mutex
00713 {
00714 private:
00715         Runable *first, *last;
00716 
00717 protected:
00718         unsigned limit, used;
00719         void check(void);
00720 
00721 public:
00727         Runlist(unsigned count = 1);
00728         
00737         bool add(Runable *run);
00738 
00745         void del(Runable *run);
00746 
00752         void set(unsigned limit);
00753 };
00754 
00761 class __EXPORT Runable
00762 {
00763 private:
00764         friend class __EXPORT Runlist;
00765         Runlist *list;
00766         Runable *next, *prev;
00767 
00768 protected:
00769         Runable();
00770         virtual ~Runable();
00771 
00776         virtual void ready(void) = 0;
00777 
00778 public:
00785         bool starting(Runlist *list);
00786         
00792         void stoping(void);
00793 };
00794 
00795 #ifdef  CCXX_NAMESPACES
00796 }
00797 #endif
00798 
00799 #endif
00800 

Generated on Tue Sep 11 05:08:45 2007 for GNU CommonC++ by  doxygen 1.5.2