This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Document T_PACKEDARRAY
[perl5.git] / ext / XS-Typemap / Typemap.xs
CommitLineData
ea035a69
JH
1/*
2 XS code to test the typemap entries
3
4 Copyright (C) 2001 Tim Jenness.
5 All Rights Reserved
6
7*/
8
3c53ff3f
NC
9#define PERL_NO_GET_CONTEXT
10
ea035a69
JH
11#include "EXTERN.h" /* std perl include */
12#include "perl.h" /* std perl include */
13#include "XSUB.h" /* XSUB include */
14
15/* Prototypes for external functions */
16FILE * xsfopen( const char * );
17int xsfclose( FILE * );
18int xsfprintf( FILE *, const char *);
19
20/* Type definitions required for the XS typemaps */
21typedef SV * SVREF; /* T_SVREF */
22typedef int SysRet; /* T_SYSRET */
23typedef int Int; /* T_INT */
24typedef int intRef; /* T_PTRREF */
25typedef int intObj; /* T_PTROBJ */
26typedef int intRefIv; /* T_REF_IV_PTR */
27typedef int intArray; /* T_ARRAY */
604db645
SM
28typedef int intTINT; /* T_INT */
29typedef int intTLONG; /* T_LONG */
ea035a69
JH
30typedef short shortOPQ; /* T_OPAQUE */
31typedef int intOpq; /* T_OPAQUEPTR */
604db645 32typedef unsigned intUnsigned; /* T_U_INT */
ea035a69 33
5abff6f9
TJ
34/* A structure to test T_OPAQUEPTR */
35struct t_opaqueptr {
36 int a;
37 int b;
38 double c;
39};
40
41typedef struct t_opaqueptr astruct;
42
ea035a69 43/* Some static memory for the tests */
052980ee
TJ
44static I32 xst_anint;
45static intRef xst_anintref;
46static intObj xst_anintobj;
47static intRefIv xst_anintrefiv;
48static intOpq xst_anintopq;
ea035a69 49
b64f48ff 50/* A different type to refer to for testing the different
1d2615b4 51 * AV*, HV*, etc typemaps */
b64f48ff
SM
52typedef AV AV_FIXED;
53typedef HV HV_FIXED;
1d2615b4
SM
54typedef CV CV_FIXED;
55typedef SVREF SVREF_FIXED;
b64f48ff 56
ea035a69
JH
57/* Helper functions */
58
59/* T_ARRAY - allocate some memory */
60intArray * intArrayPtr( int nelem ) {
61 intArray * array;
a02a5408 62 Newx(array, nelem, intArray);
ea035a69
JH
63 return array;
64}
65
66
67MODULE = XS::Typemap PACKAGE = XS::Typemap
68
69PROTOTYPES: DISABLE
70
09186e9e
SM
71TYPEMAP: <<END_OF_TYPEMAP
72
73# Typemap file for typemap testing
74# includes bonus typemap entries
75# Mainly so that all the standard typemaps can be exercised even when
76# there is not a corresponding type explicitly identified in the standard
77# typemap
78
79svtype T_ENUM
80intRef * T_PTRREF
81intRef T_IV
82intObj * T_PTROBJ
83intObj T_IV
84intRefIv * T_REF_IV_PTR
85intRefIv T_IV
86intArray * T_ARRAY
87intOpq T_IV
88intOpq * T_OPAQUEPTR
89intUnsigned T_U_INT
90intTINT T_INT
91intTLONG T_LONG
92shortOPQ T_OPAQUE
93shortOPQ * T_OPAQUEPTR
94astruct * T_OPAQUEPTR
95AV_FIXED * T_AVREF_REFCOUNT_FIXED
96HV_FIXED * T_HVREF_REFCOUNT_FIXED
97CV_FIXED * T_CVREF_REFCOUNT_FIXED
98SVREF_FIXED T_SVREF_REFCOUNT_FIXED
99
100END_OF_TYPEMAP
101
ea035a69
JH
102=head1 TYPEMAPS
103
8447d1c7
SM
104The more you think about interfacing between two languages, the more
105you'll realize that the majority of programmer effort has to go into
106converting between the data structures that are native to either of
107the languages involved. This trumps other matter such as differing
108calling conventions because the problem space is so much greater.
109There are simply more ways to shove data into memory than there are
110ways to implement a function call.
111
112Perl XS' attempt at a solution to this is the concept of typemaps.
113At an abstract level, a Perl XS typemap is nothing but a recipe for
114converting from a certain Perl data structure to a certain C
115data structure and/or vice versa. Since there can be C types that
116are sufficiently similar to warrant converting with the same logic,
117XS typemaps are represented by a unique identifier, called XS type
118henceforth in this document. You can then tell the XS compiler that
119multiple C types are to be mapped with the same XS typemap.
120
121In your XS code, when you define an argument with a C type or when
122you are using a C<CODE:> and an C<OUTPUT:> section together with a
123C return type of your XSUB, it'll be the typemapping mechanism that
124makes this easy.
125
126=head2 Anatomy of a typemap File
127
853ee02d
SM
128Traditionally, typemaps needed to be written to a separate file,
129conventionally called C<typemap>. With ExtUtils::ParseXS (the XS
130compiler) version 3.00 or better (comes with perl 5.16), typemaps
131can also be embedded directly into your XS code using a HERE-doc
132like syntax:
133
134 TYPEMAP: <<HERE
135 ...
136 HERE
137
138where C<HERE> can be replaced by other identifiers like with normal
139Perl HERE-docs. All details below about the typemap textual format
140remain valid.
141
142A typemap file generally has three sections: The C<TYPEMAP>
143section is used to associate C types with XS type identifiers.
144The C<INPUT> section is used to define the typemaps for I<input>
145into the XSUB from Perl, and the C<OUTPUT> section has the opposite
146conversion logic for getting data out of an XSUB back into Perl.
147
148Each section is started by the section name in capital letters on a
149line of its own. A typemap file implicitly starts in the C<TYPEMAP>
150section. Each type of section can appear an arbitrary number of times
151and does not have to appear at all. For example, a typemap file may
152lack C<INPUT> and C<OUTPUT> sections if all it needs to do is
153associate additional C types with core XS types like T_PTROBJ.
154Lines that start with a hash C<#> are considered comments and ignored
155in the C<TYPEMAP> section, but are considered significant in C<INPUT>
156and C<OUTPUT>. Blank lines are generally ignored.
157
158The C<TYPEMAP> section should contain one pair of C type and
159XS type per line as follows. An example from the core typemap file:
160
161 TYPEMAP
162 # all variants of char* is handled by the T_PV typemap
163 char * T_PV
164 const char * T_PV
165 unsigned char * T_PV
166 ...
167
168The C<INPUT> and C<OUTPUT> sections have identical formats, that is,
169each unindented line starts a new in- or output map respectively.
170A new in- or output map must start with the name of the XS type to
171map on a line by itself, followed by the code that implements it
172indented on the following lines. Example:
173
174 INPUT
175 T_PV
176 $var = ($type)SvPV_nolen($arg)
177 T_PTR
178 $var = INT2PTR($type,SvIV($arg))
179
180We'll get to the meaning of those Perlish-looking variables in a
181little bit.
182
183Finally, here's an example of the full typemap file for mapping C
184strings of the C<char *> type to Perl scalars/strings:
185
186 TYPEMAP
187 char * T_PV
188
189 INPUT
190 T_PV
191 $var = ($type)SvPV_nolen($arg)
192
193 OUTPUT
194 T_PV
195 sv_setpv((SV*)$arg, $var);
8447d1c7
SM
196
197=head2 The Role of the typemap File in Your Distribution
198
199For CPAN distributions, you can assume that the XS types defined by
200the perl core are already available. Additionally, the core typemap
201has default XS types for a large number of C types. For example, if
202you simply return a C<char *> from your XSUB, the core typemap will
203have this C type associated with the T_PV XS type. That means your
204C string will be copied into the PV (pointer value) slot of a new scalar
205that will be returned from your XSUB to to Perl.
206
207If you're developing a CPAN distribution using XS, you may add your own
208file called F<typemap> to the distribution. That file may contain
209typemaps that either map types that are specific to your code or that
210override the core typemap file's mappings for common C types.
211
090ff051
SM
212=head2 Sharing typemaps Between CPAN Distributions
213
214Starting with ExtUtils::ParseXS version 3.12 (comes with perl 5.16
215and better), it is rather easy to share typemap code between multiple
216CPAN distributions. The general idea is to share it as a module that
217offers a certain API and have the dependent modules declare that as a
218built-time requirement and import the typemap into the XS. An example
219of such a typemap-sharing module on CPAN is
220C<ExtUtils::Typemaps::Basic>. Two steps to getting that module's
221typemaps available in your code:
222
223=over 4
224
225=item *
226
227Declare C<ExtUtils::Typemaps::Basic> as a built-time dependency
228in C<Makefile.PL> (use C<BUILD_REQUIRES>), or in your C<Build.PL>
229(use C<build_requires>).
230
231=item *
232
233Include the following line in the XS section of your XS file:
234(don't break the line)
235
236 INCLUDE_COMMAND: $^X -MExtUtils::Typemaps::Cmd
237 -e "print embeddable_typemap(q{Basic})"
238
239=back
240
8447d1c7
SM
241=head2 Full Listing of Core Typemaps
242
ea035a69 243Each C type is represented by an entry in the typemap file that
ac23f157 244is responsible for converting perl variables (SV, AV, HV, CV, etc.)
8447d1c7
SM
245to and from that type. The following sections list all XS types
246that come with perl by default.
ea035a69
JH
247
248=over 4
249
250=item T_SV
251
252This simply passes the C representation of the Perl variable (an SV*)
253in and out of the XS layer. This can be used if the C code wants
254to deal directly with the Perl variable.
255
256=cut
257
258SV *
259T_SV( sv )
260 SV * sv
261 CODE:
262 /* create a new sv for return that is a copy of the input
263 do not simply copy the pointer since the SV will be marked
264 mortal by the INPUT typemap when it is pushed back onto the stack */
265 RETVAL = sv_mortalcopy( sv );
266 /* increment the refcount since the default INPUT typemap mortalizes
267 by default and we don't want to decrement the ref count twice
268 by mistake */
269 SvREFCNT_inc(RETVAL);
270 OUTPUT:
271 RETVAL
272
273=item T_SVREF
274
275Used to pass in and return a reference to an SV.
276
1d2615b4
SM
277Note that this typemap does not decrement the reference count
278when returning the reference to an SV*.
279See also: T_SVREF_REFCOUNT_FIXED
280
ea035a69
JH
281=cut
282
283SVREF
284T_SVREF( svref )
285 SVREF svref
286 CODE:
287 RETVAL = svref;
288 OUTPUT:
289 RETVAL
290
1d2615b4
SM
291=item T_SVREF_FIXED
292
293Used to pass in and return a reference to an SV.
294This is a fixed
295variant of T_SVREF that decrements the refcount appropriately
296when returning a reference to an SV*. Introduced in perl 5.15.4.
297
298=cut
299
300SVREF_FIXED
301T_SVREF_REFCOUNT_FIXED( svref )
302 SVREF_FIXED svref
303 CODE:
304 SvREFCNT_inc(svref);
305 RETVAL = svref;
306 OUTPUT:
307 RETVAL
308
ea035a69
JH
309=item T_AVREF
310
311From the perl level this is a reference to a perl array.
312From the C level this is a pointer to an AV.
313
b64f48ff
SM
314Note that this typemap does not decrement the reference count
315when returning an AV*. See also: T_AVREF_REFCOUNT_FIXED
316
ea035a69
JH
317=cut
318
319AV *
320T_AVREF( av )
321 AV * av
322 CODE:
323 RETVAL = av;
324 OUTPUT:
325 RETVAL
326
b64f48ff
SM
327=item T_AVREF_REFCOUNT_FIXED
328
329From the perl level this is a reference to a perl array.
330From the C level this is a pointer to an AV. This is a fixed
331variant of T_AVREF that decrements the refcount appropriately
332when returning an AV*. Introduced in perl 5.15.4.
333
334=cut
335
336AV_FIXED*
337T_AVREF_REFCOUNT_FIXED( av )
338 AV_FIXED * av
339 CODE:
340 SvREFCNT_inc(av);
341 RETVAL = av;
342 OUTPUT:
343 RETVAL
344
ea035a69
JH
345=item T_HVREF
346
347From the perl level this is a reference to a perl hash.
d1be9408 348From the C level this is a pointer to an HV.
ea035a69 349
b64f48ff
SM
350Note that this typemap does not decrement the reference count
351when returning an HV*. See also: T_HVREF_REFCOUNT_FIXED
352
ea035a69
JH
353=cut
354
355HV *
356T_HVREF( hv )
357 HV * hv
358 CODE:
359 RETVAL = hv;
360 OUTPUT:
361 RETVAL
362
b64f48ff
SM
363=item T_HVREF_REFCOUNT_FIXED
364
365From the perl level this is a reference to a perl hash.
366From the C level this is a pointer to an HV. This is a fixed
367variant of T_HVREF that decrements the refcount appropriately
368when returning an HV*. Introduced in perl 5.15.4.
369
370=cut
371
372HV_FIXED*
373T_HVREF_REFCOUNT_FIXED( hv )
374 HV_FIXED * hv
375 CODE:
376 SvREFCNT_inc(hv);
377 RETVAL = hv;
378 OUTPUT:
379 RETVAL
380
381
ea035a69
JH
382=item T_CVREF
383
384From the perl level this is a reference to a perl subroutine
385(e.g. $sub = sub { 1 };). From the C level this is a pointer
386to a CV.
387
1d2615b4
SM
388Note that this typemap does not decrement the reference count
389when returning an HV*. See also: T_HVREF_REFCOUNT_FIXED
390
ea035a69
JH
391=cut
392
393CV *
394T_CVREF( cv )
395 CV * cv
396 CODE:
397 RETVAL = cv;
398 OUTPUT:
399 RETVAL
400
1d2615b4
SM
401=item T_CVREF_REFCOUNT_FIXED
402
403From the perl level this is a reference to a perl subroutine
404(e.g. $sub = sub { 1 };). From the C level this is a pointer
405to a CV.
406
407This is a fixed
408variant of T_HVREF that decrements the refcount appropriately
409when returning an HV*. Introduced in perl 5.15.4.
410
411=cut
412
413CV_FIXED *
414T_CVREF_REFCOUNT_FIXED( cv )
415 CV_FIXED * cv
416 CODE:
417 SvREFCNT_inc(cv);
418 RETVAL = cv;
419 OUTPUT:
420 RETVAL
ea035a69
JH
421
422=item T_SYSRET
423
424The T_SYSRET typemap is used to process return values from system calls.
425It is only meaningful when passing values from C to perl (there is
426no concept of passing a system return value from Perl to C).
427
428System calls return -1 on error (setting ERRNO with the reason)
429and (usually) 0 on success. If the return value is -1 this typemap
430returns C<undef>. If the return value is not -1, this typemap
431translates a 0 (perl false) to "0 but true" (which
432is perl true) or returns the value itself, to indicate that the
433command succeeded.
434
435The L<POSIX|POSIX> module makes extensive use of this type.
436
437=cut
438
439# Test a successful return
440
441SysRet
442T_SYSRET_pass()
443 CODE:
444 RETVAL = 0;
445 OUTPUT:
446 RETVAL
447
448# Test failure
449
450SysRet
451T_SYSRET_fail()
452 CODE:
453 RETVAL = -1;
454 OUTPUT:
455 RETVAL
456
457=item T_UV
458
459An unsigned integer.
460
461=cut
462
463unsigned int
464T_UV( uv )
465 unsigned int uv
466 CODE:
467 RETVAL = uv;
468 OUTPUT:
469 RETVAL
470
471=item T_IV
472
ac23f157 473A signed integer. This is cast to the required integer type when
d1be9408 474passed to C and converted to an IV when passed back to Perl.
ea035a69
JH
475
476=cut
477
478long
479T_IV( iv )
480 long iv
481 CODE:
482 RETVAL = iv;
483 OUTPUT:
484 RETVAL
485
486=item T_INT
487
488A signed integer. This typemap converts the Perl value to a native
489integer type (the C<int> type on the current platform). When returning
490the value to perl it is processed in the same way as for T_IV.
491
492Its behaviour is identical to using an C<int> type in XS with T_IV.
493
604db645
SM
494=cut
495
496intTINT
497T_INT( i )
498 intTINT i
499 CODE:
500 RETVAL = i;
501 OUTPUT:
502 RETVAL
503
ea035a69
JH
504=item T_ENUM
505
506An enum value. Used to transfer an enum component
507from C. There is no reason to pass an enum value to C since
508it is stored as an IV inside perl.
509
510=cut
511
512# The test should return the value for SVt_PVHV.
513# 11 at the present time but we can't not rely on this
514# for testing purposes.
515
516svtype
517T_ENUM()
518 CODE:
519 RETVAL = SVt_PVHV;
520 OUTPUT:
521 RETVAL
522
523=item T_BOOL
524
525A boolean type. This can be used to pass true and false values to and
526from C.
527
528=cut
529
530bool
531T_BOOL( in )
532 bool in
533 CODE:
534 RETVAL = in;
535 OUTPUT:
536 RETVAL
537
538=item T_U_INT
539
540This is for unsigned integers. It is equivalent to using T_UV
541but explicitly casts the variable to type C<unsigned int>.
542The default type for C<unsigned int> is T_UV.
543
604db645
SM
544=cut
545
546intUnsigned
547T_U_INT( uint )
548 intUnsigned uint
549 CODE:
550 RETVAL = uint;
551 OUTPUT:
552 RETVAL
553
ea035a69
JH
554=item T_SHORT
555
556Short integers. This is equivalent to T_IV but explicitly casts
557the return to type C<short>. The default typemap for C<short>
558is T_IV.
559
604db645
SM
560=cut
561
562short
563T_SHORT( s )
564 short s
565 CODE:
566 RETVAL = s;
567 OUTPUT:
568 RETVAL
569
ea035a69
JH
570=item T_U_SHORT
571
572Unsigned short integers. This is equivalent to T_UV but explicitly
573casts the return to type C<unsigned short>. The default typemap for
574C<unsigned short> is T_UV.
575
576T_U_SHORT is used for type C<U16> in the standard typemap.
577
578=cut
579
580U16
581T_U_SHORT( in )
582 U16 in
583 CODE:
584 RETVAL = in;
585 OUTPUT:
586 RETVAL
587
588
589=item T_LONG
590
591Long integers. This is equivalent to T_IV but explicitly casts
592the return to type C<long>. The default typemap for C<long>
593is T_IV.
594
604db645
SM
595=cut
596
597intTLONG
598T_LONG( in )
599 intTLONG in
600 CODE:
601 RETVAL = in;
602 OUTPUT:
603 RETVAL
604
ea035a69
JH
605=item T_U_LONG
606
607Unsigned long integers. This is equivalent to T_UV but explicitly
608casts the return to type C<unsigned long>. The default typemap for
609C<unsigned long> is T_UV.
610
611T_U_LONG is used for type C<U32> in the standard typemap.
612
613=cut
614
615U32
616T_U_LONG( in )
617 U32 in
618 CODE:
619 RETVAL = in;
620 OUTPUT:
621 RETVAL
622
623=item T_CHAR
624
625Single 8-bit characters.
626
627=cut
628
629char
630T_CHAR( in );
631 char in
632 CODE:
633 RETVAL = in;
634 OUTPUT:
635 RETVAL
636
637
638=item T_U_CHAR
639
640An unsigned byte.
641
642=cut
643
644unsigned char
645T_U_CHAR( in );
646 unsigned char in
647 CODE:
648 RETVAL = in;
649 OUTPUT:
650 RETVAL
651
652
653=item T_FLOAT
654
655A floating point number. This typemap guarantees to return a variable
656cast to a C<float>.
657
658=cut
659
660float
661T_FLOAT( in )
662 float in
663 CODE:
664 RETVAL = in;
665 OUTPUT:
666 RETVAL
667
668=item T_NV
669
670A Perl floating point number. Similar to T_IV and T_UV in that the
671return type is cast to the requested numeric type rather than
672to a specific type.
673
674=cut
675
676NV
677T_NV( in )
678 NV in
679 CODE:
680 RETVAL = in;
681 OUTPUT:
682 RETVAL
683
684=item T_DOUBLE
685
686A double precision floating point number. This typemap guarantees to
687return a variable cast to a C<double>.
688
689=cut
690
691double
692T_DOUBLE( in )
693 double in
694 CODE:
695 RETVAL = in;
696 OUTPUT:
697 RETVAL
698
699=item T_PV
700
701A string (char *).
702
703=cut
704
705char *
706T_PV( in )
707 char * in
708 CODE:
709 RETVAL = in;
710 OUTPUT:
711 RETVAL
712
713=item T_PTR
714
715A memory address (pointer). Typically associated with a C<void *>
716type.
717
718=cut
719
720# Pass in a value. Store the value in some static memory and
721# then return the pointer
722
723void *
724T_PTR_OUT( in )
725 int in;
726 CODE:
052980ee
TJ
727 xst_anint = in;
728 RETVAL = &xst_anint;
ea035a69
JH
729 OUTPUT:
730 RETVAL
731
732# pass in the pointer and return the value
733
734int
735T_PTR_IN( ptr )
736 void * ptr
737 CODE:
738 RETVAL = *(int *)ptr;
739 OUTPUT:
740 RETVAL
741
742=item T_PTRREF
743
744Similar to T_PTR except that the pointer is stored in a scalar and the
745reference to that scalar is returned to the caller. This can be used
746to hide the actual pointer value from the programmer since it is usually
747not required directly from within perl.
748
749The typemap checks that a scalar reference is passed from perl to XS.
750
751=cut
752
753# Similar test to T_PTR
754# Pass in a value. Store the value in some static memory and
755# then return the pointer
756
757intRef *
758T_PTRREF_OUT( in )
759 intRef in;
760 CODE:
052980ee
TJ
761 xst_anintref = in;
762 RETVAL = &xst_anintref;
ea035a69
JH
763 OUTPUT:
764 RETVAL
765
766# pass in the pointer and return the value
767
768intRef
769T_PTRREF_IN( ptr )
770 intRef * ptr
771 CODE:
772 RETVAL = *ptr;
773 OUTPUT:
774 RETVAL
775
776
777
778=item T_PTROBJ
779
780Similar to T_PTRREF except that the reference is blessed into a class.
781This allows the pointer to be used as an object. Most commonly used to
782deal with C structs. The typemap checks that the perl object passed
783into the XS routine is of the correct class (or part of a subclass).
784
785The pointer is blessed into a class that is derived from the name
786of type of the pointer but with all '*' in the name replaced with
787'Ptr'.
788
789=cut
790
791# Similar test to T_PTRREF
792# Pass in a value. Store the value in some static memory and
793# then return the pointer
794
795intObj *
796T_PTROBJ_OUT( in )
797 intObj in;
798 CODE:
052980ee
TJ
799 xst_anintobj = in;
800 RETVAL = &xst_anintobj;
ea035a69
JH
801 OUTPUT:
802 RETVAL
803
804# pass in the pointer and return the value
805
806MODULE = XS::Typemap PACKAGE = intObjPtr
807
808intObj
809T_PTROBJ_IN( ptr )
810 intObj * ptr
811 CODE:
812 RETVAL = *ptr;
813 OUTPUT:
814 RETVAL
815
816MODULE = XS::Typemap PACKAGE = XS::Typemap
817
818=item T_REF_IV_REF
819
820NOT YET
821
822=item T_REF_IV_PTR
823
824Similar to T_PTROBJ in that the pointer is blessed into a scalar object.
825The difference is that when the object is passed back into XS it must be
826of the correct type (inheritance is not supported).
827
828The pointer is blessed into a class that is derived from the name
829of type of the pointer but with all '*' in the name replaced with
830'Ptr'.
831
832=cut
833
834# Similar test to T_PTROBJ
835# Pass in a value. Store the value in some static memory and
836# then return the pointer
837
838intRefIv *
839T_REF_IV_PTR_OUT( in )
840 intRefIv in;
841 CODE:
052980ee
TJ
842 xst_anintrefiv = in;
843 RETVAL = &xst_anintrefiv;
ea035a69
JH
844 OUTPUT:
845 RETVAL
846
847# pass in the pointer and return the value
848
849MODULE = XS::Typemap PACKAGE = intRefIvPtr
850
851intRefIv
852T_REF_IV_PTR_IN( ptr )
853 intRefIv * ptr
854 CODE:
855 RETVAL = *ptr;
856 OUTPUT:
857 RETVAL
858
859
860MODULE = XS::Typemap PACKAGE = XS::Typemap
861
862=item T_PTRDESC
863
864NOT YET
865
866=item T_REFREF
867
d3ba6280
SM
868Similar to T_PTRREF, except the pointer stored in the referenced scalar
869is dereferenced and copied to the output variable. This means that
870T_REFREF is to T_PTRREF as T_OPAQUE is to T_OPAQUEPTR. All clear?
871
872Only the INPUT part of this is implemented (Perl to XSUB) and there
873are no known users in core or on CPAN.
874
875=cut
ea035a69
JH
876
877=item T_REFOBJ
878
879NOT YET
880
881=item T_OPAQUEPTR
882
5abff6f9
TJ
883This can be used to store bytes in the string component of the
884SV. Here the representation of the data is irrelevant to perl and the
885bytes themselves are just stored in the SV. It is assumed that the C
886variable is a pointer (the bytes are copied from that memory
887location). If the pointer is pointing to something that is
888represented by 8 bytes then those 8 bytes are stored in the SV (and
889length() will report a value of 8). This entry is similar to T_OPAQUE.
ea035a69 890
5abff6f9
TJ
891In principal the unpack() command can be used to convert the bytes
892back to a number (if the underlying type is known to be a number).
893
894This entry can be used to store a C structure (the number
895of bytes to be copied is calculated using the C C<sizeof> function)
896and can be used as an alternative to T_PTRREF without having to worry
897about a memory leak (since Perl will clean up the SV).
ea035a69
JH
898
899=cut
900
901intOpq *
902T_OPAQUEPTR_IN( val )
903 intOpq val
904 CODE:
052980ee
TJ
905 xst_anintopq = val;
906 RETVAL = &xst_anintopq;
ea035a69
JH
907 OUTPUT:
908 RETVAL
909
910intOpq
911T_OPAQUEPTR_OUT( ptr )
912 intOpq * ptr
913 CODE:
914 RETVAL = *ptr;
915 OUTPUT:
916 RETVAL
917
aa921f48
TJ
918short
919T_OPAQUEPTR_OUT_short( ptr )
920 shortOPQ * ptr
921 CODE:
922 RETVAL = *ptr;
923 OUTPUT:
924 RETVAL
925
5abff6f9
TJ
926# Test it with a structure
927astruct *
928T_OPAQUEPTR_IN_struct( a,b,c )
929 int a
930 int b
931 double c
932 PREINIT:
933 struct t_opaqueptr test;
934 CODE:
935 test.a = a;
936 test.b = b;
937 test.c = c;
938 RETVAL = &test;
939 OUTPUT:
940 RETVAL
941
942void
943T_OPAQUEPTR_OUT_struct( test )
944 astruct * test
945 PPCODE:
946 XPUSHs(sv_2mortal(newSViv(test->a)));
947 XPUSHs(sv_2mortal(newSViv(test->b)));
948 XPUSHs(sv_2mortal(newSVnv(test->c)));
949
950
ea035a69
JH
951=item T_OPAQUE
952
5abff6f9
TJ
953This can be used to store data from non-pointer types in the string
954part of an SV. It is similar to T_OPAQUEPTR except that the
955typemap retrieves the pointer directly rather than assuming it
ac23f157 956is being supplied. For example, if an integer is imported into
052980ee
TJ
957Perl using T_OPAQUE rather than T_IV the underlying bytes representing
958the integer will be stored in the SV but the actual integer value will not
959be available. i.e. The data is opaque to perl.
ea035a69 960
5abff6f9
TJ
961The data may be retrieved using the C<unpack> function if the
962underlying type of the byte stream is known.
963
964T_OPAQUE supports input and output of simple types.
965T_OPAQUEPTR can be used to pass these bytes back into C if a pointer
966is acceptable.
ea035a69
JH
967
968=cut
969
970shortOPQ
971T_OPAQUE_IN( val )
972 int val
973 CODE:
974 RETVAL = (shortOPQ)val;
975 OUTPUT:
976 RETVAL
977
5abff6f9
TJ
978IV
979T_OPAQUE_OUT( val )
980 shortOPQ val
981 CODE:
982 RETVAL = (IV)val;
983 OUTPUT:
984 RETVAL
985
ea035a69
JH
986=item Implicit array
987
988xsubpp supports a special syntax for returning
989packed C arrays to perl. If the XS return type is given as
990
991 array(type, nelem)
992
993xsubpp will copy the contents of C<nelem * sizeof(type)> bytes from
994RETVAL to an SV and push it onto the stack. This is only really useful
995if the number of items to be returned is known at compile time and you
996don't mind having a string of bytes in your SV. Use T_ARRAY to push a
997variable number of arguments onto the return stack (they won't be
998packed as a single string though).
999
1000This is similar to using T_OPAQUEPTR but can be used to process more than
1001one element.
1002
1003=cut
1004
1005array(int,3)
1006T_OPAQUE_array( a,b,c)
1007 int a
1008 int b
1009 int c
1010 PREINIT:
3d5d53b8 1011 int array[3];
ea035a69
JH
1012 CODE:
1013 array[0] = a;
1014 array[1] = b;
1015 array[2] = c;
1016 RETVAL = array;
1017 OUTPUT:
1018 RETVAL
1019
1020
1021=item T_PACKED
1022
f018eb7c
SM
1023Calls user-supplied functions for conversion. For C<OUTPUT>
1024(XSUB to Perl), a function named C<XS_pack_$ntype> is called
1025with the output Perl scalar and the C variable to convert from.
1026C<$ntype> is the normalized C type that is to be mapped to
1027Perl. Normalized means that all C<*> are replaced by the
1028string C<Ptr>. The return value of the function is ignored.
1029
1030Conversely for C<INPUT> (Perl to XSUB) mapping, the
1031function named C<XS_unpack_$ntype> is called with the input Perl
1032scalar as argument and the return value is cast to the mapped
1033C type and assigned to the output C variable.
1034
1035An example conversion function for a typemapped struct
1036C<foo_t *> might be:
1037
1038 static void
1039 XS_pack_foo_tPtr(SV *out, foo_t *in)
1040 {
1041 HV* hash = newHV();
1042 hv_stores(hash, "int_member", newSViv(in->int_member));
1043 hv_stores(hash, "float_member", newSVnv(in->float_member));
1044 /* ... */
1045
1046 /* mortalize as thy stack is not refcounted */
1047 sv_setsv(out, sv_2mortal(newRV_noinc((SV*)hash)));
1048 }
1049
1050The conversion from Perl to C is left as an exercise to the reader,
1051but the prototype would be:
1052
1053 static foo_t *
1054 XS_unpack_foo_tPtr(SV *in);
ea035a69
JH
1055
1056=item T_PACKEDARRAY
1057
6b86c0f1
SM
1058T_PACKEDARRAY is similar to T_PACKED. In fact, the C<INPUT> (Perl
1059to XSUB) typemap is indentical, but the C<OUTPUT> typemap passes
1060an additional argument to the C<XS_pack_$ntype> function. This
1061third parameter indicates the number of elements in the output
1062so that the function can handle C arrays sanely. The variable
1063needs to be declared by the user and must have the name
1064C<count_$ntype> where C<$ntype> is the normalized C type name
1065as explained above. The signature of the function would be for
1066the example above and C<foo_t **>:
1067
1068 static void
1069 XS_pack_foo_tPtrPtr(SV *out, foo_t *in, UV count_foo_tPtrPtr);
1070
1071The type of the third parameter is arbitrary as far as the typemap
1072is concerned. It just has to be in line with the declared variable.
ea035a69
JH
1073
1074=item T_DATAUNIT
1075
1076NOT YET
1077
1078=item T_CALLBACK
1079
1080NOT YET
1081
1082=item T_ARRAY
1083
1084This is used to convert the perl argument list to a C array
1085and for pushing the contents of a C array onto the perl
1086argument stack.
1087
1088The usual calling signature is
1089
1090 @out = array_func( @in );
1091
1092Any number of arguments can occur in the list before the array but
1093the input and output arrays must be the last elements in the list.
1094
1095When used to pass a perl list to C the XS writer must provide a
1096function (named after the array type but with 'Ptr' substituted for
1097'*') to allocate the memory required to hold the list. A pointer
1098should be returned. It is up to the XS writer to free the memory on
1099exit from the function. The variable C<ix_$var> is set to the number
1100of elements in the new array.
1101
1102When returning a C array to Perl the XS writer must provide an integer
1103variable called C<size_$var> containing the number of elements in the
1104array. This is used to determine how many elements should be pushed
1105onto the return argument stack. This is not required on input since
1106Perl knows how many arguments are on the stack when the routine is
1107called. Ordinarily this variable would be called C<size_RETVAL>.
1108
1109Additionally, the type of each element is determined from the type of
1110the array. If the array uses type C<intArray *> xsubpp will
1111automatically work out that it contains variables of type C<int> and
1112use that typemap entry to perform the copy of each element. All
1113pointer '*' and 'Array' tags are removed from the name to determine
1114the subtype.
1115
1116=cut
1117
1118# Test passes in an integer array and returns it along with
1119# the number of elements
1120# Pass in a dummy value to test offsetting
1121
1122# Problem is that xsubpp does XSRETURN(1) because we arent
1123# using PPCODE. This means that only the first element
1124# is returned. KLUGE this by using CLEANUP to return before the
1125# end.
ac23f157
SM
1126# Note: I read this as: The "T_ARRAY" typemap is really rather broken,
1127# at least for OUTPUT. That is apart from the general design
1128# weaknesses. --Steffen
ea035a69
JH
1129
1130intArray *
1131T_ARRAY( dummy, array, ... )
4d0439ce 1132 int dummy = 0;
ea035a69
JH
1133 intArray * array
1134 PREINIT:
1135 U32 size_RETVAL;
1136 CODE:
8876ff82 1137 dummy += 0; /* Fix -Wall */
ea035a69
JH
1138 size_RETVAL = ix_array;
1139 RETVAL = array;
1140 OUTPUT:
1141 RETVAL
1142 CLEANUP:
1143 Safefree(array);
1144 XSRETURN(size_RETVAL);
1145
1146
1147=item T_STDIO
1148
1149This is used for passing perl filehandles to and from C using
1150C<FILE *> structures.
1151
1152=cut
1153
1154FILE *
1155T_STDIO_open( file )
1156 const char * file
1157 CODE:
1158 RETVAL = xsfopen( file );
1159 OUTPUT:
1160 RETVAL
1161
1162SysRet
c72de6e4
TJ
1163T_STDIO_close( f )
1164 PerlIO * f
1165 PREINIT:
1166 FILE * stream;
ea035a69 1167 CODE:
c72de6e4
TJ
1168 /* Get the FILE* */
1169 stream = PerlIO_findFILE( f );
c72de6e4
TJ
1170 /* Release the FILE* from the PerlIO system so that we do
1171 not close the file twice */
1172 PerlIO_releaseFILE(f,stream);
6b54a403
NC
1173 /* Must release the file before closing it */
1174 RETVAL = xsfclose( stream );
ea035a69
JH
1175 OUTPUT:
1176 RETVAL
1177
1178int
1179T_STDIO_print( stream, string )
1180 FILE * stream
1181 const char * string
1182 CODE:
1183 RETVAL = xsfprintf( stream, string );
1184 OUTPUT:
1185 RETVAL
1186
1187
1188=item T_IN
1189
1190NOT YET
1191
1192=item T_INOUT
1193
1194This is used for passing perl filehandles to and from C using
1195C<PerlIO *> structures. The file handle can used for reading and
1196writing.
1197
1198See L<perliol> for more information on the Perl IO abstraction
1199layer. Perl must have been built with C<-Duseperlio>.
1200
1201=item T_OUT
1202
1203NOT YET
1204
1205=back
1206
1207=cut
1208