Bayonne2 / Common C++ 2 Framework
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
misc.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
45 #ifndef CCXX_MISC_H_
46 #define CCXX_MISC_H_
47 
48 #ifndef CCXX_MISSING_H_
49 #include <cc++/missing.h>
50 #endif
51 
52 #ifndef CCXX_THREAD_H_
53 #include <cc++/thread.h>
54 #endif
55 
56 #define KEYDATA_INDEX_SIZE 97
57 #define KEYDATA_PAGER_SIZE 512
58 
59 #if defined(PATH_MAX)
60 #if PATH_MAX > 512
61 #define KEYDATA_PATH_SIZE 512
62 #else
63 #define KEYDATA_PATH_SIZE PATH_MAX
64 #endif
65 #else
66 #define KEYDATA_PATH_SIZE 256
67 #endif
68 
69 #ifdef CCXX_NAMESPACES
70 namespace ost {
71 #endif
72 
75 
92 {
93 private:
94  friend class String;
95  friend class MemPagerObject;
96 
97  size_t pagesize;
98  unsigned int pages;
99 
100  struct _page {
101  struct _page *next;
102  size_t used;
103  } *page;
104 
105 protected:
115  virtual void* first(size_t size);
116 
124  virtual void* alloc(size_t size);
125 
135  char* first(char *str);
136 
146  char* alloc(const char *str);
147 
157  MemPager(size_t pagesize = 4096);
158 
162  void purge(void);
163 
167  void clean(void);
168 
172  virtual ~MemPager();
173 
174 public:
181  inline int getPages(void)
182  {return pages;};
183 };
184 
194 class __EXPORT StackPager : protected MemPager
195 {
196 private:
197  typedef struct frame {
198  struct frame *next;
199  char data[1];
200  } frame_t;
201 
203 
204 public:
210  StackPager(size_t pagesize);
211 
219  void *push(const void *object, size_t size);
220 
227  void *push(const char *string);
228 
234  void *pull(void);
235 
239  void purge(void);
240 };
241 
250 class __EXPORT SharedMemPager : public MemPager, public Mutex
251 {
252 protected:
259  SharedMemPager(size_t pagesize = 4096, const char *name = NULL);
260 
264  void purge(void);
265 
272  void* first(size_t size);
273 
280  void* alloc(size_t size);
281 };
282 
283 __EXPORT void endKeydata(void);
284 
352 class __EXPORT Keydata : protected MemPager
353 {
354 public:
355 #ifdef CCXX_PACKED
356 #pragma pack(1)
357 #endif
358 
359  struct Keyval {
361  char val[1];
362  };
363 
364  struct Keysym {
367  const char **list;
368  short count;
369  char sym[1];
370  };
371 
372  struct Define {
373  const char *keyword;
374  const char *value;
375  };
376 
377 #ifdef CCXX_PACKED
378 #pragma pack()
379 #endif
380 
381 private:
382  static std::ifstream *cfgFile;
383  static char lastpath[KEYDATA_PATH_SIZE + 1];
384  static int count;
385  static int sequence;
386 
387  int link;
388 
390 
397  unsigned getIndex(const char *sym);
398 
399 protected:
400  Keysym* getSymbol(const char *sym, bool create);
401 
402 public:
414  void load(const char *keypath);
415 
429  void loadPrefix(const char *prefix, const char *keypath);
430 
440  void loadFile(const char *filepath, const char *keys = NULL, const char *pre = NULL);
441 
450  void load(Define *pairs);
451 
455  Keydata();
456 
464  Keydata(const char *keypath);
465 
473  Keydata(Define *pairs, const char *keypath = NULL);
474 
480  virtual ~Keydata();
481 
489  void unlink(void);
490 
499  int getCount(const char *sym);
500 
508  const char* getFirst(const char *sym);
509 
517  const char* getLast(const char *sym);
518 
525  bool isKey(const char *sym);
526 
534  const char *getString(const char *sym, const char *def = NULL);
535 
543  long getLong(const char *sym, long def = 0);
544 
551  bool getBool(const char *key);
552 
560  double getDouble(const char *key, double def = 0.);
561 
570  unsigned getIndex(char **data, unsigned max);
571 
578  unsigned getCount(void);
579 
588  void setValue(const char *sym, const char *data);
589 
597  const char * const* getList(const char *sym);
598 
605  void clrValue(const char *sym);
606 
611  inline const char *operator[](const char *keyword)
612  {return getLast(keyword);};
613 
617  static void end(void);
618 
623  friend inline void endKeydata(void)
624  {Keydata::end();};
625 };
626 
635 {
636 public:
643  inline void *operator new(size_t size, MemPager &pager)
644  {return pager.alloc(size);};
645 
652  inline void *operator new[](size_t size, MemPager &pager)
653  {return pager.alloc(size);};
654 
658  inline void operator delete(void *) {};
659 
663  inline void operator delete[](void *) {};
664 };
665 
675 {
676 private:
677  struct entry {
678  const char *id;
680  void *data;
681  };
682 
684 
685 protected:
686  Assoc();
687  virtual ~Assoc();
688 
689  void clear(void);
690 
691  virtual void *getMemory(size_t size) = 0;
692 
693 public:
694  void *getPointer(const char *id) const;
695  void setPointer(const char *id, void *data);
696 };
697 
708 class __EXPORT Runlist : public Mutex
709 {
710 private:
711  Runable *first, *last;
712 
713 protected:
714  unsigned limit, used;
715  void check(void);
716 
717 public:
723  Runlist(unsigned count = 1);
724 
733  bool add(Runable *run);
734 
741  void del(Runable *run);
742 
748  void set(unsigned limit);
749 };
750 
758 {
759 private:
760  friend class Runlist;
762  Runable *next, *prev;
763 
764 protected:
765  Runable();
766  virtual ~Runable();
767 
772  virtual void ready(void) = 0;
773 
774 public:
781  bool starting(Runlist *list);
782 
788  void stoping(void);
789 };
790 
791 #ifdef CCXX_NAMESPACES
792 }
793 #endif
794 
795 #endif
796 
The shared mempager uses a mutex to protect key access methods.
Definition: misc.h:250
The memory pager is used to allocate cumulative memory pages for storing object specific "persistant"...
Definition: misc.h:91
size_t used
Definition: misc.h:102
const char * keyword
Definition: misc.h:373
class __EXPORT Runlist
Definition: misc.h:73
unsigned used
Definition: misc.h:714
struct frame * next
Definition: misc.h:198
int getPages(void)
Return the total number of pages that have been allocated for this memory pool.
Definition: misc.h:181
#define KEYDATA_INDEX_SIZE
Definition: misc.h:56
entry * next
Definition: misc.h:679
substitute functions which may be missing in target platform libc.
static void end(void)
static member to end keydata i/o allocations.
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Definition: thread.h:186
This class is used to associate (object) pointers with named strings.
Definition: misc.h:674
#define KEYDATA_PATH_SIZE
Definition: misc.h:66
This is a generic and portable string class.
Definition: string.h:77
static int sequence
Definition: misc.h:385
size_t pagesize
Definition: misc.h:97
unsigned int pages
Definition: misc.h:98
This class is used to create derived classes which are constructed within a memory pager pool...
Definition: misc.h:634
frame_t * stack
Definition: misc.h:202
The StackPager provides a repository to stash and retrieve working data in last-in-first-out order...
Definition: misc.h:194
__EXPORT void endKeydata(void)
Definition: misc.h:623
static std::ifstream * cfgFile
Definition: misc.h:382
Keydata objects are used to load and hold "configuration" data for a given application.
Definition: misc.h:352
const char ** list
Definition: misc.h:367
Synchronization and threading services.
struct _page * next
Definition: misc.h:101
short count
Definition: misc.h:368
const char * operator[](const char *keyword)
A convient notation for accessing the keydata as an associative array of keyword/value pairs through ...
Definition: misc.h:611
Runable * last
Definition: misc.h:711
const char * id
Definition: misc.h:678
static int count
Definition: misc.h:384
const char * value
Definition: misc.h:374
#define __EXPORT
Definition: audio2.h:51
Runable * prev
Definition: misc.h:762
int link
Definition: misc.h:387
void * data
Definition: misc.h:680
A runlist is used to restrict concurrent exection to a limited set of concurrent sessions, much like a semaphore.
Definition: misc.h:708
Runlist * list
Definition: misc.h:761
class __EXPORT Runable
Definition: misc.h:74
A container for objects that can be queued against a runlist.
Definition: misc.h:757
Keysym * next
Definition: misc.h:365
Keyval * data
Definition: misc.h:366
Keyval * next
Definition: misc.h:360