File: globals.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (289 lines) | stat: -rw-r--r-- 5,805 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
   FALCON - The Falcon Programming Language
   FILE: globals.cpp

   Engine static/global data setup and initialization
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Mon, 02 Mar 2009 20:33:22 +0100

   -------------------------------------------------------------------
   (C) Copyright 2009: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Engine static/global data setup and initialization
*/

#include <falcon/globals.h>
#include <falcon/genericmap.h>
#include <falcon/string.h>
#include <falcon/mt.h>
#include <falcon/mempool.h>
#include <falcon/vfs_file.h>
#include <falcon/strtable.h>
#include <falcon/modulecache.h>

namespace Falcon
{
#define FLC_DECLARE_ENGINE_MSG
#include <falcon/eng_messages.h>
#undef FLC_DECLARE_ENGINE_MSG

StringTable * engineStrings = 0;

namespace Engine
{
   static Mutex s_mtx;
   static Map *s_serviceMap = 0;
   static String* s_sIOEnc = 0;
   static String* s_sSrcEnc = 0;
   static String* s_searchPath = 0;
   static ModuleCache* s_moduleCache = 0;

#ifdef FALCON_SYSTEM_WIN
   static bool s_bWindowsNamesConversion = true;
#else
   static bool s_bWindowsNamesConversion = false;
#endif

   /** Release language data. */
   void releaseLanguage();
   /** Release encoding data. */
   void releaseEncodings();

   bool addVFS( VFSProvider *prv )
   {
      fassert( prv != 0 );
      return addVFS(prv->protocol(),prv);
   }

   bool addVFS( const String &protocol, VFSProvider *prv )
   {
      fassert( prv != 0 );

      s_mtx.lock();
      if ( s_serviceMap == 0 )
      {
         s_serviceMap = new Map( &traits::t_string(), &traits::t_voidp() );
      }
      else
      {
         void *oldPrv = s_serviceMap->find( &protocol );
         if( oldPrv != 0 )
         {
            s_mtx.unlock();
            return false;
         }
      }

      s_serviceMap->insert( &protocol, prv );
      s_mtx.unlock();

      return true;
   }

   VFSProvider* getVFS( const String &name )
   {
      s_mtx.lock();
      if ( s_serviceMap == 0 )
      {
         s_mtx.unlock();
         return 0;
      }

      void *prv = s_serviceMap->find( &name );
      s_mtx.unlock();
      if ( prv != 0 )
         return *(VFSProvider**) prv;

      return 0;
   }

   void Init()
   {
      // Default language
      setLanguage( "C" );
      setEncodings( "C", "C" );

      // create the default mempool.
      memPool = new MemPool;
      memPool->start();

      // create the default file VSF
      addVFS( "file", new VFSFile );
      addVFS( "", new VFSFile );

      // Ok, we're ready
   }

   void PerformGC()
   {
     memPool->performGC();
   }
   
   void Shutdown()
   {
      if( s_searchPath != 0 )
         delete s_searchPath;
     
      delete memPool;
      memPool = 0;

      delete s_moduleCache;
      s_moduleCache = 0;

      releaseLanguage();
      releaseEncodings();

      // clear all the service ( and VSF );
      s_mtx.lock();
      if( s_serviceMap )
      {
         MapIterator mi = s_serviceMap->begin();
         while ( mi.hasCurrent() ) {
            delete *(VFSProvider**) mi.currentValue();
            mi.next();
         }

         delete s_serviceMap;
         s_serviceMap = 0;
      }
      s_mtx.unlock();

	  traits::releaseTraits();

	  gcMemShutdown(); 
   }



   const String &getMessage( uint32 id )
   {
      const String *data = engineStrings->get( id );
      fassert( data != 0 );
      return *data;
   }

   bool setTable( StringTable *tab )
   {
      if ( engineStrings == 0 || engineStrings->size() == tab->size() )
      {
         engineStrings = tab;
         return true;
      }
      return false;
   }

   bool setLanguage( const String &language )
   {
      delete engineStrings;
      engineStrings = new StringTable;
      if( language == "C" )
      {
         #define  FLC_REALIZE_ENGINE_MSG
         #include <falcon/eng_messages.h>
         #undef FLC_REALIZE_ENGINE_MSG
         return true;
      }
      // ... signal that we didn't found the language.
      return false;
   }

   void releaseLanguage()
   {
	   delete engineStrings;
	   engineStrings = 0;
   }

   void setEncodings( const String &sSrcEnc, const String &sIOEnc )
   {
      s_mtx.lock();
      
      releaseEncodings();

      s_sSrcEnc = new String(sSrcEnc);
      s_sIOEnc = new String(sIOEnc);
      s_sSrcEnc->bufferize();
      s_sIOEnc->bufferize();
      s_mtx.unlock();
   }

   void releaseEncodings()
   {
	   delete s_sSrcEnc;
	   delete s_sIOEnc;
   }

   void getEncodings( String &sSrcEnc, String &sIOEnc )
   {
      s_mtx.lock();
      sSrcEnc = *s_sSrcEnc;
      sIOEnc = *s_sIOEnc;
      s_mtx.unlock();
   }

   void setSearchPath( const String &str )
   {
      s_mtx.lock();
      if( s_searchPath != 0 )
         delete s_searchPath;

      s_searchPath = new String( str );
      s_searchPath->bufferize();
      s_mtx.unlock();
   }

   String getSearchPath()
   {
      s_mtx.lock();
      if( s_searchPath != 0 )
      {
         String temp( *s_searchPath );
         temp.bufferize();
         s_mtx.unlock();
         return temp;
      }

      s_mtx.unlock();
      return "";
   }

   void setWindowsNamesConversion( bool s )
   {
      s_bWindowsNamesConversion = s;
   }

   bool getWindowsNamesConversion()
   {
      return s_bWindowsNamesConversion;
   }

   void cacheModules( bool tmode )
   {
      s_mtx.lock();
      if ( tmode )
      {
         if ( s_moduleCache == 0 )
            s_moduleCache = new ModuleCache;
      }
      else
      {
         delete s_moduleCache;
         s_moduleCache = 0;
      }
      s_mtx.unlock();
   }

   ModuleCache* getModuleCache()
   {
      return s_moduleCache;
   }
}

}


/* end of globals.cpp */