GNU CommonC++
misc.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
44 #ifndef CCXX_MISC_H_
45 #define CCXX_MISC_H_
46 
47 #ifndef CCXX_MISSING_H_
48 #include <cc++/missing.h>
49 #endif
50 
51 #ifndef CCXX_THREAD_H_
52 #include <cc++/thread.h>
53 #endif
54 
55 #define KEYDATA_INDEX_SIZE 97
56 #define KEYDATA_PAGER_SIZE 512
57 
58 #if defined(PATH_MAX)
59 #if PATH_MAX > 512
60 #define KEYDATA_PATH_SIZE 512
61 #else
62 #define KEYDATA_PATH_SIZE PATH_MAX
63 #endif
64 #else
65 #define KEYDATA_PATH_SIZE 256
66 #endif
67 
68 #ifdef CCXX_NAMESPACES
69 namespace ost {
70 #endif
71 
74 
91 {
92 private:
93  friend class String;
94  friend class MemPagerObject;
95 
96  size_t pagesize;
97  unsigned int pages;
98 
99  struct _page {
100  struct _page *next;
101  size_t used;
102  } *page;
103 
104 protected:
114  virtual void* first(size_t size);
115 
123  virtual void* alloc(size_t size);
124 
134  char* first(char *str);
135 
145  char* alloc(const char *str);
146 
156  MemPager(size_t pagesize = 4096);
157 
161  void purge(void);
162 
166  void clean(void);
167 
171  virtual ~MemPager();
172 
173 public:
180  inline int getPages(void)
181  {return pages;};
182 };
183 
193 class __EXPORT StackPager : protected MemPager
194 {
195 private:
196  typedef struct frame {
197  struct frame *next;
198  char data[1];
199  } frame_t;
200 
201  frame_t *stack;
202 
203 public:
209  StackPager(size_t pagesize);
210 
218  void *push(const void *object, size_t size);
219 
226  void *push(const char *string);
227 
233  void *pull(void);
234 
238  void purge(void);
239 };
240 
249 class __EXPORT SharedMemPager : public MemPager, public Mutex
250 {
251 protected:
258  SharedMemPager(size_t pagesize = 4096, const char *name = NULL);
259 
263  void purge(void);
264 
271  void* first(size_t size);
272 
279  void* alloc(size_t size);
280 };
281 
282 __EXPORT void endKeydata(void);
283 
351 class __EXPORT Keydata : protected MemPager
352 {
353 public:
354 #ifdef CCXX_PACKED
355 #pragma pack(1)
356 #endif
357 
358  struct Keyval {
360  char val[1];
361  };
362 
363  struct Keysym {
366  const char **list;
367  short count;
368  char sym[1];
369  };
370 
371  struct Define {
372  char *keyword;
373  char *value;
374  };
375 
376 #ifdef CCXX_PACKED
377 #pragma pack()
378 #endif
379 
380 private:
381  static std::ifstream *cfgFile;
382  static char lastpath[KEYDATA_PATH_SIZE + 1];
383  static int count;
384  static int sequence;
385 
386  int link;
387 
388  Keysym *keys[KEYDATA_INDEX_SIZE];
389 
396  unsigned getIndex(const char *sym);
397 
398 protected:
399  Keysym* getSymbol(const char *sym, bool create);
400 
401 public:
413  void load(const char *keypath);
414 
428  void loadPrefix(const char *prefix, const char *keypath);
429 
439  void loadFile(const char *filepath, const char *keys = NULL, const char *pre = NULL);
440 
449  void load(Define *pairs);
450 
454  Keydata();
455 
463  Keydata(const char *keypath);
464 
472  Keydata(Define *pairs, const char *keypath = NULL);
473 
479  virtual ~Keydata();
480 
488  void unlink(void);
489 
498  int getCount(const char *sym);
499 
507  const char* getFirst(const char *sym);
508 
516  const char* getLast(const char *sym);
517 
524  bool isKey(const char *sym);
525 
533  const char *getString(const char *sym, const char *def = NULL);
534 
542  long getLong(const char *sym, long def = 0);
543 
550  bool getBool(const char *key);
551 
559  double getDouble(const char *key, double def = 0.);
560 
569  unsigned getIndex(char **data, unsigned max);
570 
577  unsigned getCount(void);
578 
587  void setValue(const char *sym, const char *data);
588 
596  const char * const* getList(const char *sym);
597 
604  void clrValue(const char *sym);
605 
610  inline const char *operator[](const char *keyword)
611  {return getLast(keyword);};
612 
616  static void end(void);
617 
622  friend inline void endKeydata(void)
623  {Keydata::end();};
624 };
625 
634 {
635 public:
642  inline void *operator new(size_t size, MemPager &pager)
643  {return pager.alloc(size);};
644 
651  inline void *operator new[](size_t size, MemPager &pager)
652  {return pager.alloc(size);};
653 
657  inline void operator delete(void *) {};
658 
662  inline void operator delete[](void *) {};
663 };
664 
674 {
675 private:
676  struct entry {
677  const char *id;
678  entry *next;
679  void *data;
680  };
681 
682  entry *entries[KEYDATA_INDEX_SIZE];
683 
684 protected:
685  Assoc();
686  virtual ~Assoc();
687 
688  void clear(void);
689 
690  virtual void *getMemory(size_t size) = 0;
691 
692 public:
693  void *getPointer(const char *id) const;
694  void setPointer(const char *id, void *data);
695 };
696 
707 class __EXPORT Runlist : public Mutex
708 {
709 private:
710  Runable *first, *last;
711 
712 protected:
713  unsigned limit, used;
714  void check(void);
715 
716 public:
722  Runlist(unsigned count = 1);
723 
732  bool add(Runable *run);
733 
740  void del(Runable *run);
741 
747  void set(unsigned limit);
748 };
749 
757 {
758 private:
759  friend class Runlist;
760  Runlist *list;
761  Runable *next, *prev;
762 
763 protected:
764  Runable();
765  virtual ~Runable();
766 
771  virtual void ready(void) = 0;
772 
773 public:
780  bool starting(Runlist *list);
781 
787  void stoping(void);
788 };
789 
790 #ifdef CCXX_NAMESPACES
791 }
792 #endif
793 
794 #endif
795