Skip to content
This repository was archived by the owner on Dec 16, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 31beaf4

Browse files
committedNov 3, 2021
Add more scalar methods and improve readme
1 parent 4896085 commit 31beaf4

File tree

3 files changed

+216
-31
lines changed

3 files changed

+216
-31
lines changed
 

‎README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ Type::int64Array([1, 2, 3]); // object CData<int64_t[3]> { cdata: [1, 2, 3] }
4343
Type::uint64(42); // object CData<uint64_t> { cdata: 42 }
4444
Type::uint64Array([1, 2, 3]); // object CData<uint64_t[3]> { cdata: [1, 2, 3] }
4545

46+
Type::short(42); // object CData<signed short int> { cdata: 42 }
47+
Type::shortArray([1, 2, 3]); // object CData<signed short int[3]> { cdata: [1, 2, 3] }
48+
Type::ushort(42); // object CData<unsigned short int> { cdata: 42 }
49+
Type::ushortArray([1, 2, 3]); // object CData<unsigned short int[3]> { cdata: [1, 2, 3] }
50+
51+
Type::int(42); // object CData<signed int> { cdata: 42 }
52+
Type::intArray([1, 2, 3]); // object CData<signed int[3]> { cdata: [1, 2, 3] }
53+
Type::uint(42); // object CData<unsigned int> { cdata: 42 }
54+
Type::uintArray([1, 2, 3]); // object CData<unsigned int[3]> { cdata: [1, 2, 3] }
55+
56+
Type::long(42); // object CData<signed long int> { cdata: 42 }
57+
Type::longArray([1, 2, 3]); // object CData<signed long int[3]> { cdata: [1, 2, 3] }
58+
Type::ulong(42); // object CData<unsigned long int> { cdata: 42 }
59+
Type::ulongArray([1, 2, 3]); // object CData<unsigned long int[3]> { cdata: [1, 2, 3] }
60+
4661
Type::float(42); // object CData<float> { cdata: 42.0 }
4762
Type::floatArray([1, 2, 3]); // object CData<float[3]> { cdata: [1.0, 2.0, 3.0] }
4863

@@ -65,7 +80,6 @@ Type::wideString('hi'); // object CData<wchar_t[3]> { cdata: ['h', '
6580
Type::wideStringArray(['a', 'b']); // object CData<wchar_t[2][2]> { cdata: [['a' '\0\0'], ['b', '\0\0']] }
6681

6782
// Direct API
68-
6983
Type::create($ffi->type('example'), $value); // object CData<example> { cdata: ... }
7084
Type::array($ffi->type('example'), [$value]); // object CData<example[1]> { cdata: [ ... ] }
7185
```
@@ -76,6 +90,7 @@ Type::array($ffi->type('example'), [$value]); // object CData<example[1]> { cdat
7690
use FFI\Scalar\Type;
7791

7892
Type::toString($cdata); // string(x) "..."
93+
Type::toWideString($cdata); // string(x) "..."
7994
Type::toInt($cdata); // int(x)
8095
Type::toFloat($cdata); // float(x)
8196
Type::toBool($cdata); // bool(x)

‎resources/.phpstorm.meta.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace PHPSTORM_META {
4+
5+
registerArgumentsSet('FFIScalarType',
6+
'void *',
7+
8+
'bool',
9+
10+
'float',
11+
'double',
12+
'long double',
13+
14+
'char',
15+
'signed char',
16+
'unsigned char',
17+
'int',
18+
'signed int',
19+
'unsigned int',
20+
'long',
21+
'signed long',
22+
'unsigned long',
23+
'long long',
24+
'signed long long',
25+
'unsigned long long',
26+
27+
'intptr_t',
28+
'uintptr_t',
29+
'size_t',
30+
'ssize_t',
31+
'ptrdiff_t',
32+
'off_t',
33+
'va_list',
34+
'__builtin_va_list',
35+
'__gnuc_va_list',
36+
37+
// stdint.h
38+
'int8_t',
39+
'uint8_t',
40+
'int16_t',
41+
'uint16_t',
42+
'int32_t',
43+
'uint32_t',
44+
'int64_t',
45+
'uint64_t',
46+
);
47+
48+
expectedArguments(\FFI\Scalar\Type::array(), 0, argumentsSet('FFIScalarType'));
49+
expectedArguments(\FFI\Scalar\Type::create(), 0, argumentsSet('FFIScalarType'));
50+
}

‎src/TypeConstructorsTrait.php

+150-30
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@
2020
*/
2121
trait TypeConstructorsTrait
2222
{
23-
/**
24-
* @param int $value
25-
* @param bool $owned
26-
* @return CData
27-
*/
28-
public static function int8(int $value = 0, bool $owned = true): CData
29-
{
30-
return self::create('int8_t', $value, $owned);
31-
}
32-
3323
/**
3424
* @param string|CType $type
3525
* @param mixed $value
@@ -44,16 +34,6 @@ public static function create(string|CType $type, mixed $value, bool $owned = tr
4434
return $instance;
4535
}
4636

47-
/**
48-
* @param int[] $value
49-
* @param bool $owned
50-
* @return CData
51-
*/
52-
public static function int8Array(iterable $value = [], bool $owned = true): CData
53-
{
54-
return self::array('int8_t', $value, $owned);
55-
}
56-
5737
/**
5838
* @param string|CType $type
5939
* @param iterable $initializer
@@ -99,13 +79,153 @@ private static function iterableValues(iterable $initializer): array
9979
* @param bool $owned
10080
* @return CData
10181
*/
82+
public static function short(int $value = 0, bool $owned = true): CData
83+
{
84+
return self::create('short', $value, $owned);
85+
}
86+
87+
/**
88+
* @param array<int> $value
89+
* @param bool $owned
90+
* @return CData
91+
*/
92+
public static function shortArray(iterable $value = [], bool $owned = true): CData
93+
{
94+
return self::array('short', $value, $owned);
95+
}
96+
97+
/**
98+
* @param positive-int|0 $value
99+
* @param bool $owned
100+
* @return CData
101+
*/
102+
public static function ushort(int $value = 0, bool $owned = true): CData
103+
{
104+
return self::create('unsigned short', $value, $owned);
105+
}
106+
107+
/**
108+
* @param array<positive-int|0> $value
109+
* @param bool $owned
110+
* @return CData
111+
*/
112+
public static function ushortArray(iterable $value = [], bool $owned = true): CData
113+
{
114+
return self::array('unsigned short', $value, $owned);
115+
}
116+
117+
/**
118+
* @param int $value
119+
* @param bool $owned
120+
* @return CData
121+
*/
122+
public static function int(int $value = 0, bool $owned = true): CData
123+
{
124+
return self::create('int', $value, $owned);
125+
}
126+
127+
/**
128+
* @param array<int> $value
129+
* @param bool $owned
130+
* @return CData
131+
*/
132+
public static function intArray(iterable $value = [], bool $owned = true): CData
133+
{
134+
return self::array('int', $value, $owned);
135+
}
136+
137+
/**
138+
* @param positive-int|0 $value
139+
* @param bool $owned
140+
* @return CData
141+
*/
142+
public static function uint(int $value = 0, bool $owned = true): CData
143+
{
144+
return self::create('unsigned int', $value, $owned);
145+
}
146+
147+
/**
148+
* @param array<positive-int|0> $value
149+
* @param bool $owned
150+
* @return CData
151+
*/
152+
public static function uintArray(iterable $value = [], bool $owned = true): CData
153+
{
154+
return self::array('unsigned int', $value, $owned);
155+
}
156+
157+
/**
158+
* @param int $value
159+
* @param bool $owned
160+
* @return CData
161+
*/
162+
public static function long(int $value = 0, bool $owned = true): CData
163+
{
164+
return self::create('long', $value, $owned);
165+
}
166+
167+
/**
168+
* @param array<int> $value
169+
* @param bool $owned
170+
* @return CData
171+
*/
172+
public static function longArray(iterable $value = [], bool $owned = true): CData
173+
{
174+
return self::array('long', $value, $owned);
175+
}
176+
177+
/**
178+
* @param positive-int|0 $value
179+
* @param bool $owned
180+
* @return CData
181+
*/
182+
public static function ulong(int $value = 0, bool $owned = true): CData
183+
{
184+
return self::create('unsigned long', $value, $owned);
185+
}
186+
187+
/**
188+
* @param array<positive-int|0> $value
189+
* @param bool $owned
190+
* @return CData
191+
*/
192+
public static function ulongArray(iterable $value = [], bool $owned = true): CData
193+
{
194+
return self::array('unsigned long', $value, $owned);
195+
}
196+
197+
/**
198+
* @param int $value
199+
* @param bool $owned
200+
* @return CData
201+
*/
202+
public static function int8(int $value = 0, bool $owned = true): CData
203+
{
204+
return self::create('int8_t', $value, $owned);
205+
}
206+
207+
/**
208+
* @param array<int> $value
209+
* @param bool $owned
210+
* @return CData
211+
*/
212+
public static function int8Array(iterable $value = [], bool $owned = true): CData
213+
{
214+
return self::array('int8_t', $value, $owned);
215+
}
216+
217+
/**
218+
* @param positive-int|0 $value
219+
* @param bool $owned
220+
* @return CData
221+
*/
102222
public static function uint8(int $value = 0, bool $owned = true): CData
103223
{
104224
return self::create('uint8_t', $value, $owned);
105225
}
106226

107227
/**
108-
* @param int[] $value
228+
* @param array<positive-int|0> $value
109229
* @param bool $owned
110230
* @return CData
111231
*/
@@ -125,7 +245,7 @@ public static function int16(int $value = 0, bool $owned = true): CData
125245
}
126246

127247
/**
128-
* @param int[] $value
248+
* @param array<int> $value
129249
* @param bool $owned
130250
* @return CData
131251
*/
@@ -135,7 +255,7 @@ public static function int16Array(iterable $value = [], bool $owned = true): CDa
135255
}
136256

137257
/**
138-
* @param int $value
258+
* @param positive-int|0 $value
139259
* @param bool $owned
140260
* @return CData
141261
*/
@@ -145,7 +265,7 @@ public static function uint16(int $value = 0, bool $owned = true): CData
145265
}
146266

147267
/**
148-
* @param int[] $value
268+
* @param array<positive-int|0> $value
149269
* @param bool $owned
150270
* @return CData
151271
*/
@@ -165,7 +285,7 @@ public static function int32(int $value = 0, bool $owned = true): CData
165285
}
166286

167287
/**
168-
* @param int[] $value
288+
* @param array<int> $value
169289
* @param bool $owned
170290
* @return CData
171291
*/
@@ -175,7 +295,7 @@ public static function int32Array(iterable $value = [], bool $owned = true): CDa
175295
}
176296

177297
/**
178-
* @param int $value
298+
* @param positive-int|0 $value
179299
* @param bool $owned
180300
* @return CData
181301
*/
@@ -185,7 +305,7 @@ public static function uint32(int $value = 0, bool $owned = true): CData
185305
}
186306

187307
/**
188-
* @param int[] $value
308+
* @param array<positive-int|0> $value
189309
* @param bool $owned
190310
* @return CData
191311
*/
@@ -205,7 +325,7 @@ public static function int64(int $value = 0, bool $owned = true): CData
205325
}
206326

207327
/**
208-
* @param int[] $value
328+
* @param array<int> $value
209329
* @param bool $owned
210330
* @return CData
211331
*/
@@ -215,7 +335,7 @@ public static function int64Array(iterable $value = [], bool $owned = true): CDa
215335
}
216336

217337
/**
218-
* @param int $value
338+
* @param positive-int|0 $value
219339
* @param bool $owned
220340
* @return CData
221341
*/
@@ -225,7 +345,7 @@ public static function uint64(int $value = 0, bool $owned = true): CData
225345
}
226346

227347
/**
228-
* @param int[] $value
348+
* @param array<positive-int|0> $value
229349
* @param bool $owned
230350
* @return CData
231351
*/

0 commit comments

Comments
 (0)
This repository has been archived.