GNU CommonC++

misc.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free software
00019 // library without restriction.  Specifically, if other files instantiate
00020 // templates or use macros or inline functions from this file, or you compile
00021 // this file and link it with other files to produce an executable, this
00022 // file does not by itself cause the resulting executable to be covered by
00023 // the GNU General Public License.  This exception does not however
00024 // invalidate any other reasons why the executable file might be covered by
00025 // the GNU General Public License.
00026 //
00027 // This exception applies only to the code released under the name GNU
00028 // Common C++.  If you copy code from other releases into a copy of GNU
00029 // Common C++, as the General Public License permits, the exception does
00030 // not apply to the code that you add in this way.  To avoid misleading
00031 // anyone as to the status of such modified files, you must delete
00032 // this exception notice from them.
00033 //
00034 // If you write modifications of your own for GNU Common C++, it is your choice
00035 // whether to permit this exception to apply to your modifications.
00036 // If you do not wish that, delete this exception notice.
00037 //
00038 
00045 #ifndef CCXX_MISC_H_
00046 #define CCXX_MISC_H_
00047 
00048 #ifndef CCXX_MISSING_H_
00049 #include <cc++/missing.h>
00050 #endif
00051 
00052 #ifndef CCXX_THREAD_H_
00053 #include <cc++/thread.h>
00054 #endif
00055 
00056 #define KEYDATA_INDEX_SIZE  97
00057 #define KEYDATA_PAGER_SIZE  512
00058 
00059 #if defined(PATH_MAX)
00060 #if PATH_MAX > 512
00061 #define KEYDATA_PATH_SIZE   512
00062 #else
00063 #define KEYDATA_PATH_SIZE   PATH_MAX
00064 #endif
00065 #else
00066 #define KEYDATA_PATH_SIZE   256
00067 #endif
00068 
00069 #ifdef  CCXX_NAMESPACES
00070 namespace ost {
00071 #endif
00072 
00073 class __EXPORT Runlist;
00074 class __EXPORT Runable;
00075 
00091 class __EXPORT MemPager
00092 {
00093 private:
00094     friend class String;
00095     friend class MemPagerObject;
00096 
00097     size_t pagesize;
00098     unsigned int pages;
00099 
00100     struct _page {
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         struct frame *next;
00199         char data[1];
00200     }   frame_t;
00201 
00202     frame_t *stack;
00203 
00204 public:
00210     StackPager(size_t pagesize);
00211 
00219     void *push(const void *object, size_t size);
00220 
00227     void *push(const char *string);
00228 
00234     void *pull(void);
00235 
00239     void purge(void);
00240 };
00241 
00250 class __EXPORT SharedMemPager : public MemPager, public Mutex
00251 {
00252 protected:
00259     SharedMemPager(size_t pagesize = 4096, const char *name = NULL);
00260 
00264     void purge(void);
00265 
00272     void* first(size_t size);
00273 
00280     void* alloc(size_t size);
00281 };
00282 
00283 __EXPORT void endKeydata(void);
00284 
00352 class __EXPORT Keydata : protected MemPager
00353 {
00354 public:
00355 #ifdef  CCXX_PACKED
00356 #pragma pack(1)
00357 #endif
00358 
00359     struct Keyval {
00360         Keyval *next;
00361         char val[1];
00362     };
00363 
00364     struct Keysym {
00365         Keysym *next;
00366         Keyval *data;
00367         const char **list;
00368         short count;
00369         char sym[1];
00370     };
00371 
00372     struct Define {
00373         const char *keyword;
00374         const char *value;
00375     };
00376 
00377 #ifdef  CCXX_PACKED
00378 #pragma pack()
00379 #endif
00380 
00381 private:
00382     static std::ifstream *cfgFile;
00383     static char lastpath[KEYDATA_PATH_SIZE + 1];
00384     static int count;
00385     static int sequence;
00386 
00387     int link;
00388 
00389     Keysym *keys[KEYDATA_INDEX_SIZE];
00390 
00397     unsigned getIndex(const char *sym);
00398 
00399 protected:
00400     Keysym* getSymbol(const char *sym, bool create);
00401 
00402 public:
00414     void load(const char *keypath);
00415 
00429     void loadPrefix(const char *prefix, const char *keypath);
00430 
00440     void loadFile(const char *filepath, const char *keys = NULL, const char *pre = NULL);
00441 
00450     void load(Define *pairs);
00451 
00455     Keydata();
00456 
00464     Keydata(const char *keypath);
00465 
00473     Keydata(Define *pairs, const char *keypath = NULL);
00474 
00480     virtual ~Keydata();
00481 
00489     void unlink(void);
00490 
00499     int getCount(const char *sym);
00500 
00508     const char* getFirst(const char *sym);
00509 
00517     const char* getLast(const char *sym);
00518 
00525     bool isKey(const char *sym);
00526 
00534     const char *getString(const char *sym, const char *def = NULL);
00535 
00543     long getLong(const char *sym, long def = 0);
00544 
00551     bool getBool(const char *key);
00552 
00560     double getDouble(const char *key, double def = 0.);
00561 
00570     unsigned getIndex(char **data, unsigned max);
00571 
00578     unsigned getCount(void);
00579 
00588     void setValue(const char *sym, const char *data);
00589 
00597     const char * const* getList(const char *sym);
00598 
00605     void clrValue(const char *sym);
00606 
00611     inline const char *operator[](const char *keyword)
00612         {return getLast(keyword);};
00613 
00617     static void end(void);
00618 
00623     friend inline void endKeydata(void)
00624         {Keydata::end();};
00625 };
00626 
00634 class __EXPORT MemPagerObject
00635 {
00636 public:
00643     inline void *operator new(size_t size, MemPager &pager)
00644         {return pager.alloc(size);};
00645 
00652     inline void *operator new[](size_t size, MemPager &pager)
00653         {return pager.alloc(size);};
00654 
00658     inline void operator delete(void *) {};
00659 
00663     inline void operator delete[](void *) {};
00664 };
00665 
00674 class __EXPORT Assoc
00675 {
00676 private:
00677     struct entry {
00678         const char *id;
00679         entry *next;
00680         void *data;
00681     };
00682 
00683     entry *entries[KEYDATA_INDEX_SIZE];
00684 
00685 protected:
00686     Assoc();
00687     virtual ~Assoc();
00688 
00689     void clear(void);
00690 
00691     virtual void *getMemory(size_t size) = 0;
00692 
00693 public:
00694     void *getPointer(const char *id) const;
00695     void setPointer(const char *id, void *data);
00696 };
00697 
00708 class __EXPORT Runlist : public Mutex
00709 {
00710 private:
00711     Runable *first, *last;
00712 
00713 protected:
00714     unsigned limit, used;
00715     void check(void);
00716 
00717 public:
00723     Runlist(unsigned count = 1);
00724 
00733     bool add(Runable *run);
00734 
00741     void del(Runable *run);
00742 
00748     void set(unsigned limit);
00749 };
00750 
00757 class __EXPORT Runable
00758 {
00759 private:
00760     friend class Runlist;
00761     Runlist *list;
00762     Runable *next, *prev;
00763 
00764 protected:
00765     Runable();
00766     virtual ~Runable();
00767 
00772     virtual void ready(void) = 0;
00773 
00774 public:
00781     bool starting(Runlist *list);
00782 
00788     void stoping(void);
00789 };
00790 
00791 #ifdef  CCXX_NAMESPACES
00792 }
00793 #endif
00794 
00795 #endif
00796