1    	/* /% C++ %/ */
2    	/***********************************************************************
3    	 * cint (C/C++ interpreter)
4    	 ************************************************************************
5    	 * Source file FastAllocString.h
6    	 ************************************************************************
7    	 * Description:
8    	 * String object with fast allocation (pooled memory)
9    	 ************************************************************************
10   	 * Copyright(c) 1995~2009  Masaharu Goto 
11   	 *
12   	 * For the licensing terms see the file COPYING
13   	 *
14   	 ************************************************************************/
15   	
16   	#ifndef G__FASTALLOGSTRING_H
17   	#define G__FASTALLOGSTRING_H
18   	
19   	#include <stdarg.h>
20   	#include <stddef.h>
21   	
22   	// For G__EXPORT
23   	#include "G__ci.h"
24   	
25   	namespace Cint {
26   	   namespace Internal {
27   	      class G__BufferReservoir;
28   	   }
29   	}
30   	
31   	//_____________________________________________________________
32   	//
33   	// A tiny object representing a char array.
34   	// Create it with the desired size of the char array and it
35   	// will try to retrieve a previsouly allocated buffer from
36   	// an internal resrevoir of buffers, or allocate a new one
37   	// if none is available. This is a lot faster than mallocs /
38   	// free calls for each char array, and it considerably reduces
39   	// the used stack size by functions previsouly using static
40   	// size, stack based chart arrays. It also allows to make the
41   	// buffer size dynamic, adopted e.g. to strlen(expression),
42   	// instead of a value defined at compile time (a la G__LONGBUF).
43   	// When the G__FastAllocString object leaves the scope it will put its
44   	// buffer (back) into the internal buffer reservoir for later
45   	// use by a G__FastAllocString object requesting a same of smaller size
46   	// buffer. This class is optimized for both speed and low memory
47   	// use despite the reservoir.
48   	//
49   	class 
50   	#ifndef __CINT__
51   	G__EXPORT
52   	#endif
53   	G__FastAllocString {
54   	public:
55   	   G__FastAllocString(size_t reqsize = 1024): fBuf(0), fCapacity(reqsize) {
56   	      // GetBuf takes as parameter the size in bytes
57   	      // and modify the parameter (fBucket) to hold the 
58   	      // bucket number.
59   	      fBuf = GetBuf(fCapacity);
60   	   }
61   	   G__FastAllocString(const char* s);
62   	   G__FastAllocString(const G__FastAllocString&);
63   	
64   	   ~G__FastAllocString();
65   	
66   	   // plenty of char* conversion functions:
67   	   operator char*() { return fBuf; }
68   	   operator const char*() const { return fBuf; }
69   	   const char* operator()() const { return fBuf; }
70   	
71   	   // DON'T: these create ambiguities with ::op[char*, int] etc
72   	   //char& operator[](int i) { return fBuf[i]; }
73   	   //char operator[](int i) const { return fBuf[i]; }
74   	   //char* operator+(int i) { return fBuf + i; }
75   	   //const char* operator+(int i) const { return fBuf + i; }
76   	
77   	   const char* data() const { return fBuf; }
78   	
79   	   int FormatArgList(const char *fmt, va_list args);
80   	   int FormatArgList(size_t offset, const char *fmt, va_list args);
81   	   G__FastAllocString& Format(const char *fmt, ...);
82   	   G__FastAllocString& Format(size_t offset, const char *fmt, ...);
83   	
84   	   size_t Capacity() const { return fCapacity; }
85   	
86   	   G__FastAllocString& operator=(const G__FastAllocString& s) {
87   	      // Copy s into *this.
88   	      // Cannot rely on operator=(const char*) overload - compiler-generated one wins resolution!
89   	      operator=(s.data());
90   	      return *this;
91   	   }
92   	   G__FastAllocString& operator=(const char*);
93   	   G__FastAllocString& operator+=(const char*);
94   	   G__FastAllocString& Swap(G__FastAllocString&);
95   	   void Resize(size_t cap);
96   	
97   	   void Set(size_t pos, char c) {
98   	      // Set character at position pos to c; resize if needed.
99   	      Resize(pos + 1);
100  	      fBuf[pos] = c;
101  	   }
102  	   /*
103  	   size_t Set(size_t& pos, const char* s) {
104  	      // Overwrite string at position pos with s; resize if needed.
105  	      // Return pos incremented by strlen(s)
106  	      size_t len = strlen(s);
107  	      Resize(pos + len + 1);
108  	      memcpy(fBuf + pos, s, len + 1);
109  	      return pos + len;
110  	      }*/
111  	
112  	   void Replace(size_t where, const char *replacement);
113  	                
114  	protected:
115  	   static char* GetBuf(size_t &size);
116  	
117  	   void ResizeToBucketNoCopy(int newbucket);
118  	   void ResizeNoCopy(size_t cap);
119  	         
120  	private:
121  	   char*  fBuf;    // the buffer
122  	   size_t fCapacity; // measure representing the buffer's size, used by the internal reservoir
123  	};
124  	
125  	// Those 6 functions are intentionally not implemented as their are 'illegal'
126  	// and we should call the equivalent member function instead.
127  	void G__strlcpy(G__FastAllocString&, const char *, size_t);
128  	void G__strlcat(G__FastAllocString&, const char *, size_t);
129  	void G__snprintf(G__FastAllocString&, size_t, const char *, ...);
130  	void strcpy(G__FastAllocString&, const char *);
131  	void strcat(G__FastAllocString&, const char *);
132  	void sprintf(G__FastAllocString&, const char *, ...);
133  	
134  	#endif // G__FASTALLOGSTRING_H
135