Mercurial > projects > dwt2
comparison base/src/java/lang/util.d @ 27:1bf55a6eb092
Renamed java tree to base
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 21 Mar 2009 11:33:57 +0100 |
parents | java/src/java/lang/util.d@f713da8bc051 |
children | 2e09b0e6857a |
comparison
equal
deleted
inserted
replaced
26:f589fc20a5f9 | 27:1bf55a6eb092 |
---|---|
1 module java.lang.util; | |
2 | |
3 public import java.lang.wrappers; | |
4 public import java.lang.String; | |
5 public import java.lang.interfaces; | |
6 | |
7 version(Tango){ | |
8 static import tango.text.convert.Format; | |
9 static import tango.core.Exception; | |
10 static import tango.util.log.Log; | |
11 static import tango.stdc.stdlib; | |
12 | |
13 alias tango.stdc.stdlib.exit exit; | |
14 } else { // Phobos | |
15 static import core.exception; | |
16 static import std.c.stdlib; | |
17 alias std.c.stdlib.exit exit; | |
18 } | |
19 | |
20 version(Tango){ | |
21 } else { // Phobos | |
22 } | |
23 version(Tango){ | |
24 } else { // Phobos | |
25 } | |
26 version(Tango){ | |
27 } else { // Phobos | |
28 } | |
29 | |
30 interface IDwtLogger { | |
31 void trace( String file, ulong line, String fmt, ... ); | |
32 void info( String file, ulong line, String fmt, ... ); | |
33 void warn( String file, ulong line, String fmt, ... ); | |
34 void error( String file, ulong line, String fmt, ... ); | |
35 void fatal( String file, ulong line, String fmt, ... ); | |
36 } | |
37 | |
38 version(Tango){ | |
39 class DwtLogger : IDwtLogger { | |
40 tango.util.log.Log.Logger logger; | |
41 private this( char[] name ){ | |
42 logger = tango.util.log.Log.Log.lookup( name ); | |
43 } | |
44 private char[] format( String file, ulong line, String fmt, TypeInfo[] types, void* argptr ){ | |
45 auto msg = Format.convert( types, argptr, fmt ); | |
46 auto text = Format( "{} {}: {}", file, line, msg ); | |
47 return text; | |
48 } | |
49 void trace( String file, ulong line, String fmt, ... ){ | |
50 if( logger.trace ){ | |
51 logger.trace( format( file, line, fmt, _arguments, _argptr )); | |
52 } | |
53 } | |
54 void info( String file, ulong line, String fmt, ... ){ | |
55 if( logger.info ){ | |
56 logger.info( format( file, line, fmt, _arguments, _argptr )); | |
57 } | |
58 } | |
59 void warn( String file, ulong line, String fmt, ... ){ | |
60 if( logger.warn ){ | |
61 logger.warn( format( file, line, fmt, _arguments, _argptr )); | |
62 } | |
63 } | |
64 void error( String file, ulong line, String fmt, ... ){ | |
65 if( logger.error ){ | |
66 logger.error( format( file, line, fmt, _arguments, _argptr )); | |
67 } | |
68 } | |
69 void fatal( String file, ulong line, String fmt, ... ){ | |
70 if( logger.fatal ){ | |
71 logger.fatal( format( file, line, fmt, _arguments, _argptr )); | |
72 } | |
73 } | |
74 } | |
75 } else { // Phobos | |
76 class DwtLogger : IDwtLogger { | |
77 private this( String name ){ | |
78 } | |
79 void trace( String file, ulong line, String fmt, ... ){ | |
80 } | |
81 void info( String file, ulong line, String fmt, ... ){ | |
82 } | |
83 void warn( String file, ulong line, String fmt, ... ){ | |
84 } | |
85 void error( String file, ulong line, String fmt, ... ){ | |
86 } | |
87 void fatal( String file, ulong line, String fmt, ... ){ | |
88 } | |
89 } | |
90 } | |
91 | |
92 private DwtLogger dwtLoggerInstance; | |
93 | |
94 IDwtLogger getDwtLogger(){ | |
95 if( dwtLoggerInstance is null ){ | |
96 synchronized{ | |
97 if( dwtLoggerInstance is null ){ | |
98 dwtLoggerInstance = new DwtLogger( "dwt" ); | |
99 } | |
100 } | |
101 } | |
102 return dwtLoggerInstance; | |
103 } | |
104 | |
105 void implMissing( String file, uint line ){ | |
106 getDwtLogger().fatal( file, line, "implementation missing in file {} line {}", file, line ); | |
107 getDwtLogger().fatal( file, line, "Please create a bug report at http://www.dsource.org/projects/dwt" ); | |
108 getDwtLogger().fatal( file, line, "exiting ..." ); | |
109 exit(1); | |
110 } | |
111 | |
112 version(Tango){ | |
113 public alias tango.text.convert.Format.Format Format; | |
114 } else { // Phobos | |
115 class Format{ | |
116 static String opCall( String fmt, ... ){ | |
117 implMissing(__FILE__,__LINE__); | |
118 return null; | |
119 } | |
120 } | |
121 } | |
122 | |
123 | |
124 private struct GCStats { | |
125 size_t poolsize; // total size of pool | |
126 size_t usedsize; // bytes allocated | |
127 size_t freeblocks; // number of blocks marked FREE | |
128 size_t freelistsize; // total of memory on free lists | |
129 size_t pageblocks; // number of blocks marked PAGE | |
130 } | |
131 private extern(C) GCStats gc_stats(); | |
132 | |
133 size_t RuntimeTotalMemory(){ | |
134 GCStats s = gc_stats(); | |
135 return s.poolsize; | |
136 } | |
137 | |
138 | |
139 template arraycast(T) { | |
140 T[] arraycast(U) (U[] u) { | |
141 static if ( | |
142 (is (T == interface ) && is (U == interface )) || | |
143 (is (T == class ) && is (U == class ))) { | |
144 return(cast(T[])u); | |
145 } | |
146 else { | |
147 int l = u.length; | |
148 T[] res; | |
149 res.length = l; | |
150 for (int i = 0; i < l; i++) { | |
151 res[i] = cast(T)u[i]; | |
152 } | |
153 return(res); | |
154 } | |
155 } | |
156 } | |
157 | |
158 | |
159 bool ArrayEquals(T)( T[] a, T[] b ){ | |
160 if( a.length !is b.length ){ | |
161 return false; | |
162 } | |
163 for( int i = 0; i < a.length; i++ ){ | |
164 static if( is( T==class) || is(T==interface)){ | |
165 if( a[i] !is null && b[i] !is null ){ | |
166 if( a[i] != b[i] ){ | |
167 return false; | |
168 } | |
169 } | |
170 else if( a[i] is null && b[i] is null ){ | |
171 } | |
172 else{ | |
173 return false; | |
174 } | |
175 } | |
176 else{ | |
177 if( a[i] != b[i] ){ | |
178 return false; | |
179 } | |
180 } | |
181 } | |
182 return true; | |
183 } | |
184 | |
185 int arrayIndexOf(T)( T[] arr, T v ){ | |
186 int res = -1; | |
187 int idx = 0; | |
188 foreach( p; arr ){ | |
189 if( p == v){ | |
190 res = idx; | |
191 break; | |
192 } | |
193 idx++; | |
194 } | |
195 return res; | |
196 } | |
197 | |
198 T[] arrayIndexRemove(T)(T[] arr, uint n) { | |
199 if (n is 0) | |
200 return arr[1..$]; | |
201 if (n > arr.length) | |
202 return arr; | |
203 if (n is arr.length-1) | |
204 return arr[0..n-1]; | |
205 // else | |
206 return arr[0..n] ~ arr[n+1..$]; | |
207 } | |
208 | |
209 struct ImportData{ | |
210 void[] data; | |
211 String name; | |
212 | |
213 public static ImportData opCall( void[] data, String name ){ | |
214 ImportData res; | |
215 res.data = data; | |
216 res.name = name; | |
217 return res; | |
218 } | |
219 } | |
220 | |
221 template getImportData(String name ){ | |
222 const ImportData getImportData = ImportData( import(name), name ); | |
223 } | |
224 | |
225 |