GNU CommonC++
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
70namespace ost {
71#endif
72
75
92{
93private:
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
105protected:
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
174public:
181 inline int getPages(void)
182 {return pages;};
183};
184
194class __EXPORT StackPager : protected MemPager
195{
196private:
197 typedef struct frame {
198 struct frame *next;
199 char data[1];
200 } frame_t;
201
202 frame_t *stack;
203
204public:
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
250class __EXPORT SharedMemPager : public MemPager, public Mutex
251{
252protected:
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
352class __EXPORT Keydata : protected MemPager
353{
354public:
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
381private:
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
399protected:
400 Keysym* getSymbol(const char *sym, bool create);
401
402public:
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
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{
636public:
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{
676private:
677 struct entry {
678 const char *id;
679 entry *next;
680 void *data;
681 };
682
683 entry *entries[KEYDATA_INDEX_SIZE];
684
685protected:
687 virtual ~Assoc();
688
689 void clear(void);
690
691 virtual void *getMemory(size_t size) = 0;
692
693public:
694 void *getPointer(const char *id) const;
695 void setPointer(const char *id, void *data);
696};
697
708class __EXPORT Runlist : public Mutex
709{
710private:
711 Runable *first, *last;
712
713protected:
714 unsigned limit, used;
715 void check(void);
716
717public:
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{
759private:
760 friend class Runlist;
761 Runlist *list;
762 Runable *next, *prev;
763
764protected:
766 virtual ~Runable();
767
772 virtual void ready(void) = 0;
773
774public:
781 bool starting(Runlist *list);
782
788 void stoping(void);
789};
790
791#ifdef CCXX_NAMESPACES
792}
793#endif
794
795#endif
796
This class is used to associate (object) pointers with named strings.
Definition misc.h:675
virtual ~Assoc()
virtual void * getMemory(size_t size)=0
void setPointer(const char *id, void *data)
void * getPointer(const char *id) const
void clear(void)
Keydata objects are used to load and hold "configuration" data for a given application.
Definition misc.h:353
const char *const * getList(const char *sym)
Return a list of all values set for the given keyword returned in order.
unsigned getCount(void)
Get the count of keyword indexes that are actually available so one can allocate a table to receive g...
Keysym * getSymbol(const char *sym, bool create)
bool getBool(const char *key)
Get a bool value.
Keydata(Define *pairs, const char *keypath=NULL)
Alternate constructor can take a define list and an optional pathfile to parse.
const char * getString(const char *sym, const char *def=NULL)
Get a string value, with an optional default if missing.
void unlink(void)
Unlink the keydata object from the cache file stream.
Keydata(const char *keypath)
Create a new key data object and use "Load" method to load an initial config file section into it.
int getCount(const char *sym)
Get a count of the number of data "values" that is associated with a specific keyword.
void clrValue(const char *sym)
Clear all values associated with a given keyword.
const char * getFirst(const char *sym)
Get the first data value for a given keyword.
double getDouble(const char *key, double def=0.)
Get a floating value.
void loadFile(const char *filepath, const char *keys=NULL, const char *pre=NULL)
Load additional keys into the current object using a real filename that is directly passed rather tha...
bool isKey(const char *sym)
Find if a given key exists.
const char * getLast(const char *sym)
Get the last (most recently set) value for a given keyword.
Keydata()
Create an empty key data object.
void setValue(const char *sym, const char *data)
Set (replace) the value of a given keyword.
void load(Define *pairs)
Load default keywords into the current object.
unsigned getIndex(char **data, unsigned max)
Get an index array of ALL keywords that are stored by the current keydata object.
void load(const char *keypath)
Load additional key values into the currrent object from the specfied config source (a config file/se...
friend void endKeydata(void)
Shutdown the file stream cache.
Definition misc.h:623
void loadPrefix(const char *prefix, const char *keypath)
Load additional key values into the currrent object from the specfied config source (a config file/se...
long getLong(const char *sym, long def=0)
Get a long value, with an optional default if missing.
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
virtual ~Keydata()
Destroy the keydata object and all allocated memory.
static void end(void)
static member to end keydata i/o allocations.
The memory pager is used to allocate cumulative memory pages for storing object specific "persistant"...
Definition misc.h:92
int getPages(void)
Return the total number of pages that have been allocated for this memory pool.
Definition misc.h:181
char * first(char *str)
Allocate a string from the memory pager pool and copy the string into it's new memory area.
virtual ~MemPager()
Delete the memory pool and all allocated memory.
void purge(void)
purge the current memory pool.
MemPager(size_t pagesize=4096)
Create a paged memory pool for cumulative storage.
void clean(void)
Clean for memory cleanup before exiting.
char * alloc(const char *str)
Allocate a string from the memory pager pool and copy the string inti it's new memory area.
virtual void * first(size_t size)
Allocate first workspace from paged memory.
virtual void * alloc(size_t size)
Allocate memory from either the currently active page, or allocate a new page for the object.
This class is used to create derived classes which are constructed within a memory pager pool.
Definition misc.h:635
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:187
A container for objects that can be queued against a runlist.
Definition misc.h:758
virtual ~Runable()
virtual void ready(void)=0
Method handler that is invoked when a wait-listed object becomes ready to run.
bool starting(Runlist *list)
Start the object against a run list.
void stoping(void)
Stop the object, called when stopping or ready completes.
A runlist is used to restrict concurrent exection to a limited set of concurrent sessions,...
Definition misc.h:709
void check(void)
void set(unsigned limit)
Set the limit.
bool add(Runable *run)
Add a runable object to this runlist.
void del(Runable *run)
Remove a runable object from the wait list or notify when it is done running so that the used count c...
Runlist(unsigned count=1)
Create a new runlist with a specified limit.
unsigned limit
Definition misc.h:714
The shared mempager uses a mutex to protect key access methods.
Definition misc.h:251
void * alloc(size_t size)
Get the last memory page after locking.
SharedMemPager(size_t pagesize=4096, const char *name=NULL)
Create a mempager mutex pool.
void * first(size_t size)
Get the first memory page after locking.
void purge(void)
Purge the memory pool while locked.
The StackPager provides a repository to stash and retrieve working data in last-in-first-out order.
Definition misc.h:195
void * push(const void *object, size_t size)
Push an arbitrary object onto the stack.
void * push(const char *string)
Push a string onto the stack.
StackPager(size_t pagesize)
Create a lifo pager as a mempager.
void * pull(void)
Retrieve next object from stack.
void purge(void)
Purge the stack of all objects and memory allocations.
This is a generic and portable string class.
Definition string.h:81
#define __EXPORT
Definition config.h:1045
#define KEYDATA_INDEX_SIZE
Definition misc.h:56
#define KEYDATA_PATH_SIZE
Definition misc.h:66
substitute functions which may be missing in target platform libc.
Definition address.h:64
__EXPORT void endKeydata(void)
Definition misc.h:623
Definition misc.h:372
const char * value
Definition misc.h:374
const char * keyword
Definition misc.h:373
Definition misc.h:364
const char ** list
Definition misc.h:367
short count
Definition misc.h:368
Keysym * next
Definition misc.h:365
Keyval * data
Definition misc.h:366
Definition misc.h:359
Keyval * next
Definition misc.h:360
Synchronization and threading services.