forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-type.h
1294 lines (1070 loc) · 32 KB
/
php-type.h
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* php-type.h
*
* Copyright (C) Roger P. Gee
*/
#ifndef PHPGIT2_TYPE_H
#define PHPGIT2_TYPE_H
#include "php-array.h"
#include "php-resource.h"
#include <new>
namespace php_git2
{
// Provide abstract base class for value that can be parsed from a parameter
// zval.
class php_parameter
{
public:
void parse(zval* zvp,int argno)
{
// NOTE: Parse assumes 'value' is empty.
parse_impl(zvp,argno);
}
void parse_with_context(zval* zvp,const char* ctx);
private:
virtual void parse_impl(zval* zvp,int argno) = 0;
};
// Provide output parameter type.
class php_output_parameter:
public php_parameter
{
public:
php_output_parameter():
zvp(nullptr)
{
}
zval* get_value()
{
return zvp;
}
private:
zval* zvp;
virtual void parse_impl(zval* zvp,int argno);
};
// Provide abstract base class for managing PHP values. The purpose of this
// class is to provide a read-only view of the zval. The class does NOT
// manage the lifecycle of the wrapped zval.
class php_value_base:
public php_parameter
{
public:
php_value_base()
{
ZVAL_UNDEF(&value);
}
zval* get_value()
{
return &value;
}
void set_value(zval* zvp)
{
ZVAL_COPY_VALUE(&value,zvp);
}
bool is_null() const
{
return Z_TYPE(value) == IS_NULL;
}
protected:
zval value;
};
class php_value_generic:
public php_value_base
{
private:
virtual void parse_impl(zval* zvp,int argno);
};
template<typename T>
class php_value;
template<>
class php_value<zend_long>:
public php_value_base
{
public:
zend_long byval_git2() const
{
return Z_LVAL(value);
}
void ret(zval* return_value) const
{
RETVAL_LONG(Z_LVAL(value));
}
private:
virtual void parse_impl(zval* zvp,int argno)
{
int result;
zend_long dummy;
result = zend_parse_parameter(ZEND_PARSE_PARAMS_THROW,argno,zvp,"l",&dummy);
if (result == FAILURE) {
throw php_git2_propagated_exception();
}
ZVAL_COPY_VALUE(&value,zvp);
}
};
template<>
class php_value<bool>:
public php_value_base
{
public:
bool byval_git2() const
{
return Z_TYPE(value) == IS_TRUE;
}
void ret(zval* return_value) const
{
if (Z_TYPE(value) == IS_TRUE) {
RETURN_TRUE
}
RETURN_FALSE
}
private:
virtual void parse_impl(zval* zvp,int argno)
{
int result;
zend_bool dummy;
result = zend_parse_parameter(ZEND_PARSE_PARAMS_THROW,argno,zvp,"b",&dummy);
if (result == FAILURE) {
throw php_git2_propagated_exception();
}
ZVAL_COPY_VALUE(&value,zvp);
}
};
template<>
class php_value<double>:
public php_value_base
{
public:
double byval_git2() const
{
return Z_DVAL(value);
}
void ret(zval* return_value) const
{
RETVAL_DOUBLE(Z_DVAL(value));
}
private:
virtual void parse_impl(zval* zvp,int argno)
{
int result;
double dummy;
result = zend_parse_parameter(ZEND_PARSE_PARAMS_THROW,argno,zvp,"d",&dummy);
if (result == FAILURE) {
throw php_git2_propagated_exception();
}
ZVAL_COPY_VALUE(&value,zvp);
}
};
template<>
class php_value<const char*>:
public php_value_base
{
public:
const char* byval_git2() const
{
return Z_STRVAL(value);
}
void ret(zval* return_value) const
{
// This will copy the string buffer (i.e. duplicate it).
RETVAL_STRING(Z_STRVAL(value));
}
protected:
virtual void parse_impl(zval* zvp,int argno)
{
char* s;
size_t l;
int result;
result = zend_parse_parameter(ZEND_PARSE_PARAMS_THROW,argno,zvp,"s",&s,&l);
if (result == FAILURE) {
throw php_git2_propagated_exception();
}
ZVAL_COPY_VALUE(&value,zvp);
}
};
// Provide basic type definitions for the core types.
using php_bool = php_value<bool>;
using php_long = php_value<zend_long>;
using php_double = php_value<double>;
using php_string = php_value<const char*>;
// Provide a nullable string type.
class php_string_nullable:
virtual public php_string
{
public:
const char* byval_git2() const
{
if (is_null()) {
return nullptr;
}
return php_string::byval_git2();
}
protected:
virtual void parse_impl(zval* zvp,int argno)
{
if (Z_TYPE_P(zvp) == IS_NULL) {
ZVAL_NULL(&value);
return;
}
php_string::parse_impl(zvp,argno);
}
};
// Provide a returnable string type that is set by reference.
class php_string_ref
{
public:
php_string_ref():
ptr(nullptr)
{
}
const char** byval_git2()
{
return &ptr;
}
void ret(zval* return_value)
{
if (ptr != NULL) {
RETVAL_STRING(ptr);
return;
}
ZVAL_NULL(return_value);
}
private:
const char* ptr;
};
// Provide a string type that can be returned through an out parameter.
class php_string_out:
public php_output_parameter
{
public:
php_string_out():
ptr(nullptr)
{
}
~php_string_out()
{
if (ptr == nullptr) {
ZVAL_NULL(get_value());
}
else {
ZVAL_STRING(get_value(),ptr);
}
}
const char** byval_git2()
{
return &ptr;
}
private:
const char* ptr;
};
// Provide a string connector that returns the string length.
template<typename IntType,typename StringType = php_string>
class php_string_length_connector
{
public:
using connect_t = StringType;
typedef IntType target_t;
php_string_length_connector(connect_t& obj):
conn(obj)
{
}
target_t byval_git2()
{
return static_cast<IntType>(Z_STRLEN_P(conn.get_value()));
}
protected:
connect_t& conn;
};
template<typename IntType,typename StringType = php_string_nullable>
class php_string_length_connector_nullable:
public php_string_length_connector<IntType,StringType>
{
using base_type = php_string_length_connector<IntType,StringType>;
public:
using typename base_type::connect_t;
using typename base_type::target_t;
php_string_length_connector_nullable(connect_t& obj):
base_type(obj)
{
}
target_t byval_git2()
{
zval* zvp = base_type::conn.get_value();
if (Z_TYPE_P(zvp) != IS_STRING) {
return 0;
}
return static_cast<IntType>(Z_STRLEN_P(zvp));
}
};
// Provide a connector for an arbitrary string buffer that can be returned
// to PHP userspace. The connector connects to a php_long which represents
// the desired buffer length.
class php_string_buffer_connector
{
public:
using connect_t = php_long;
using target_t = char*;
php_string_buffer_connector(connect_t& obj)
{
bufsz = static_cast<size_t>(obj.byval_git2());
buffer = static_cast<char*>(emalloc(bufsz));
}
~php_string_buffer_connector()
{
efree(buffer);
}
target_t byval_git2()
{
return buffer;
}
void ret(zval* return_value)
{
RETVAL_STRINGL(buffer,bufsz);
}
private:
char* buffer;
size_t bufsz;
};
// Provide a type that casts a php_long to any arbitrary integer type.
template<typename IntType>
class php_long_cast:
public php_long
{
public:
IntType byval_git2() const
{
return static_cast<IntType>(php_long::byval_git2());
}
};
// Provide a type that accepts a numeric value from an underlying call.
template<typename IntType>
class php_long_ref
{
public:
IntType* byval_git2()
{
return &n;
}
void ret(zval* return_value)
{
RETVAL_LONG(static_cast<zend_long>(n));
}
IntType get_value() const
{
return n;
}
private:
IntType n;
};
// Provide a type like php_long_ref that returns bool.
template<typename IntType>
class php_bool_ref:
public php_long_ref<IntType>
{
public:
void ret(zval* return_value)
{
if (get_value()) {
RETURN_TRUE
}
RETURN_FALSE
}
protected:
using php_long_ref<IntType>::get_value;
};
// Provide a type for returning a numeric value using an out parameter.
template<typename IntType>
class php_long_out:
public php_output_parameter
{
public:
~php_long_out()
{
ZVAL_LONG(get_value(),static_cast<zend_long>(n));
}
IntType* byval_git2()
{
return &n;
}
private:
IntType n;
};
// Provide generic resource types for libgit2 objects. The parameter should
// be instantiated with some instantiation of 'git2_resource<>' (or some
// derived class thereof). We provide one type for when a resource is used
// as a value (for a created resource) and another when it is used by
// reference (for an uncreated resource).
class php_resource_base:
public php_value_base
{
protected:
virtual void parse_impl(zval* zvp,int argno);
};
template<typename GitResource>
class php_resource:
public php_resource_base
{
public:
typedef GitResource resource_t;
typename GitResource::git2_type byval_git2()
{
return get_object()->get_handle();
}
// This member function is used to retrieve the resource object. We must
// make sure it has been fetched from the resource value.
GitResource* get_object()
{
return reinterpret_cast<GitResource*>(Z_RES_VAL(value));
}
protected:
virtual void parse_impl(zval* zvp,int argno)
{
void* result;
php_resource_base::parse_impl(zvp,argno);
result = zend_fetch_resource(Z_RES(value),
GitResource::resource_name(),
GitResource::resource_le());
if (result == nullptr) {
throw php_git2_propagated_exception();
}
}
};
template<typename GitResource>
class php_resource_owner:
public php_resource<GitResource>
{
public:
typename GitResource::git2_type byval_git2()
{
GitResource* res = php_resource<GitResource>::get_object();
// Ensure that the resource owns the underlying handle before
// returning it.
if (!res->is_owner()) {
throw php_git2_exception(
"Cannot execute libgit2 call on non-owner resource");
}
return res->get_handle();
}
};
template<typename GitResource>
class php_resource_ref
{
public:
php_resource_ref():
rsrc(nullptr)
{
}
typename GitResource::git2_type* byval_git2()
{
// Create a resource backing instance if it does not already exist.
if (rsrc == nullptr) {
rsrc = php_git2_create_resource<GitResource>();
}
return rsrc->get_handle_byref();
}
typename GitResource::const_git2_type* byval_git2() const
{
// Create a resource backing instance if it does not already exist.
if (rsrc == nullptr) {
rsrc = php_git2_create_resource<GitResource>();
}
// Cast the resource object to have the same kind of constness to
// force the compiler to call the correct overloaded member
// function. We have to do this since it's mutable.
return const_cast<const GitResource*>(rsrc)->get_handle_byref();
}
void ret(zval* return_value) const
{
zend_resource* zr;
zr = zend_register_resource(rsrc,GitResource::resource_le());
RETVAL_RES(zr);
}
GitResource* get_object() const
{
// Retrieve the resource object, creating it if it does not exist.
if (rsrc == nullptr) {
rsrc = php_git2_create_resource<GitResource>();
}
return rsrc;
}
void set_object(typename GitResource::git2_type obj)
{
// Create a resource backing instance if it does not already exist.
if (rsrc == nullptr) {
rsrc = php_git2_create_resource<GitResource>();
}
rsrc->set_handle(obj);
}
void set_object(typename GitResource::const_git2_type obj) const
{
// Create a resource backing instance if it does not already exist.
if (rsrc == nullptr) {
rsrc = php_git2_create_resource<GitResource>();
}
rsrc->set_handle(const_cast<typename GitResource::git2_type>(obj));
}
private:
mutable GitResource* rsrc;
};
// Provide a variant of php_resource_ref that can return null if the output
// value was NULL.
template<typename GitResource>
class php_resource_nullable_ref
{
public:
php_resource_nullable_ref():
rsrc(nullptr), handle(nullptr)
{
}
typename GitResource::git2_type*
byval_git2()
{
// Just return the handle. Delay creating the resource backing until
// later and after we know the return value is not NULL.
return &handle;
}
void ret(zval* return_value)
{
// If the handle was non-NULL, create a resource backing. Then pass
// it off to a new resource zval.
if (handle != nullptr) {
zend_resource* zr;
if (rsrc == nullptr) {
rsrc = php_git2_create_resource<GitResource>();
rsrc->set_handle(handle);
}
zr = zend_register_resource(rsrc,GitResource::resource_le());
RETVAL_RES(zr);
}
else {
RETVAL_NULL();
}
}
GitResource* get_object()
{
// Return the resource backing if the handle was non-NULL. Otherwise
// return NULL.
if (handle != nullptr) {
if (rsrc == nullptr) {
rsrc = php_git2_create_resource<GitResource>();
rsrc->set_handle(handle);
}
return rsrc;
}
return nullptr;
}
private:
GitResource* rsrc;
typename GitResource::git2_type handle;
};
// Provide out parameter variants of php_resource_ref and friends. These
// allow new resources to be returned through output parameters.
template<typename GitResource>
class php_resource_ref_out:
public php_output_parameter,
public php_resource_ref<GitResource>
{
public:
~php_resource_ref_out()
{
php_resource_ref<GitResource>::ret(get_value());
}
};
template<typename GitResource>
class php_resource_nullable_ref_out:
public php_output_parameter,
public php_resource_nullable_ref<GitResource>
{
public:
~php_resource_nullable_ref_out()
{
php_resource_nullable_ref<GitResource>::ret(get_value());
}
};
// Provide a type that represents an optional resource value (one that could
// be null).
template<typename GitResource>
class php_resource_nullable:
public php_resource<GitResource>
{
using base = php_resource<GitResource>;
public:
typename GitResource::git2_type byval_git2()
{
// Resource may be null.
if (base::is_null()) {
return nullptr;
}
return base::byval_git2();
}
GitResource* get_object()
{
if (base::is_null()) {
return nullptr;
}
return base::get_object();
}
protected:
using base::value;
private:
virtual void parse_impl(zval* zvp,int argno)
{
if (Z_TYPE_P(zvp) == IS_NULL) {
ZVAL_NULL(&value);
return;
}
base::parse_impl(zvp,argno);
}
};
// Provide a type that cleans up the resource before it returns the
// underlying git2 handle type.
template<typename GitResource>
class php_resource_cleanup:
public php_resource<GitResource>
{
public:
typename GitResource::git2_type byval_git2()
{
// Delete the PHP resource. This will cause the resource to be
// invalidated across any zvals that reference it and the underlying
// handle will be destroyed (if it has no more references).
zend_list_close(Z_RES(value));
// The return value should not be used. We do not attempt frees
// directly from user space.
return nullptr;
}
protected:
using php_resource<GitResource>::value;
};
template<typename GitResource>
class php_resource_cleanup_delayed:
public php_resource<GitResource>
{
public:
~php_resource_cleanup_delayed()
{
// Delete the PHP resource. This will cause the resource to be
// invalidated across any zvals that reference it and the underlying
// handle will be destroyed (if it has no more references).
zend_list_close(Z_RES(value));
}
protected:
using php_resource<GitResource>::value;
};
class php_git_oid
{
public:
git_oid* byval_git2()
{
return &oid;
}
void ret(zval* return_value)
{
char buf[GIT_OID_HEXSZ + 1];
git_oid_tostr(buf,sizeof(buf),&oid);
RETVAL_STRING(buf);
}
private:
git_oid oid;
};
class php_git_oid_fromstr:
virtual public php_string
{
public:
git_oid* byval_git2()
{
// Convert PHP string to git_oid.
convert_oid_fromstr(&oid,Z_STRVAL(value),Z_STRLEN(value));
return &oid;
}
private:
git_oid oid;
};
class php_git_oid_fromstr_nullable:
public php_string_nullable,
public php_git_oid_fromstr
{
public:
git_oid* byval_git2()
{
if (Z_TYPE(value) == IS_STRING) {
return php_git_oid_fromstr::byval_git2();
}
return nullptr;
}
private:
git_oid oid;
using php_string_nullable::parse_impl;
};
class php_git_oid_byval_fromstr:
public php_git_oid_fromstr
{
public:
git_oid byval_git2()
{
return *php_git_oid_fromstr::byval_git2();
}
};
// Provide a type for returning an OID value using an out parameter.
class php_git_oid_out:
public php_output_parameter,
public php_git_oid
{
public:
~php_git_oid_out()
{
ret(get_value());
}
};
// Wrap 'git_strarray' and provide conversions to PHP userspace array. Note
// that we never accept this type as an argument from userspace. The
// strarray structure itself is also created on the stack.
class php_git_strarray
{
public:
php_git_strarray()
{
memset(&arr,0,sizeof(git_strarray));
}
~php_git_strarray()
{
git_strarray_free(&arr);
}
git_strarray* byval_git2()
{
return &arr;
}
void ret(zval* return_value) const
{
// Convert the strarray to a PHP array.
array_init(return_value);
for (size_t i = 0;i < arr.count;++i) {
add_next_index_string(return_value,arr.strings[i]);
}
}
private:
git_strarray arr;
};
// Wrap 'git_oidarray' and provide conversions to PHP userspace array. Note
// that we never accept this type from userspace.
class php_git_oidarray
{
public:
php_git_oidarray()
{
memset(&arr,0,sizeof(git_oidarray));
}
~php_git_oidarray()
{
git_oidarray_free(&arr);
}
git_oidarray* byval_git2()
{
return &arr;
}
void ret(zval* return_value) const
{
array_init(return_value);
for (size_t i = 0;i < arr.count;++i) {
char buf[GIT_OID_HEXSZ + 1];
git_oid_tostr(buf,sizeof(buf),arr.ids + i);
add_next_index_string(return_value,buf);
}
}
private:
git_oidarray arr;
};
// Wrap 'git_buf' and make it to convert to a PHP string.
class php_git_buf
{
public:
php_git_buf()
{
memset(&buf,0,sizeof(git_buf));
}
~php_git_buf()
{
git_buf_free(&buf);
}
git_buf* byval_git2()
{
return &buf;
}
void ret(zval* return_value) const
{
// Convert the git_buf into a PHP string. Make sure to copy the
// buffer since the destructor will free the git_buf.
RETVAL_STRINGL(buf.ptr,buf.size);
}
private:
git_buf buf;
};
// Provide a type for returning a git_buf value using an out parameter.
class php_git_buf_out:
public php_output_parameter,
public php_git_buf
{
public:
~php_git_buf_out()
{
ret(get_value());
}
};
// Provide a fixed-length buffer type.
template<unsigned MaxLength>
class php_fixed_buffer
{
public:
char* byval_git2()
{
return buffer;
}
void ret(zval* return_value)
{
RETVAL_STRING(buffer);
}