File Coverage

blib/lib/Stlgen.pm
Criterion Covered Total %
statement 12 59 20.3
branch 0 6 0.0
condition 0 5 0.0
subroutine 4 8 50.0
pod 4 4 100.0
total 20 82 24.3


line stmt bran cond sub pod time code
1             package Stlgen;
2              
3 1     1   483421 use warnings;
  1         3  
  1         30  
4 1     1   5 use strict;
  1         2  
  1         29  
5              
6 1     1   48405 use Template;
  1         52676  
  1         37  
7 1     1   1213 use Data::Dumper;
  1         9061  
  1         739  
8              
9             =head1 NAME
10              
11             Stlgen - Create "Standard Template Library" (STL) C++ type containers but generate code in other languages.
12              
13             =head1 VERSION
14              
15             Version 0.012
16              
17             =cut
18              
19             our $VERSION = '0.012';
20              
21              
22             =head1 SYNOPSIS
23              
24             Stlgen is based off the Standard Template Library (STL) for C++ here:
25              
26             http://www.cplusplus.com/reference/stl/
27              
28             The difference is that Stlgen will generate instances of STL templates
29             in a different language. The default language is c.
30              
31             This example below uses Stlgen to generate list_uint.(c/h) files which will implement
32             a linked list container coded in the c language.
33              
34             #!/usr/bin/perl -w
35              
36             use Stlgen;
37              
38             my $inst = Stlgen->New(
39             Template=>'list',
40             Instancename => 'uint',
41             payload => [
42             {name=>'uint', type=>'unsigned int', dumper=>'printf("\t\tuint = %u\n", currelement->uint);'},
43             ],
44             );
45              
46             $inst->Instantiate();
47              
48             You could use these files in a main.c program like this:
49              
50             #include
51             #include "list_uint.h"
52              
53             int main (void) {
54              
55             struct list_uint_list *mylist;
56              
57             mylist = list_uint_constructor();
58            
59             list_uint_push_back(mylist, 21);
60             list_uint_push_back(mylist, 99);
61             list_uint_push_back(mylist, 33);
62             list_uint_push_back(mylist, 34);
63             list_uint_push_back(mylist, 67);
64             list_uint_push_back(mylist, 12);
65             list_uint_push_back(mylist, 28);
66             list_uint_push_back(mylist, 55);
67             list_uint_push_back(mylist, 76);
68              
69             list_uint_sort(mylist);
70              
71             printf("\n\n\nThis is the sorted list\n");
72             list_uint_list_dumper(mylist);
73              
74             return 0;
75             }
76              
77             The above c program currently works and produces the following output
78             when you compile and execute it:
79              
80             This is the sorted list
81             // list at address 140644360{
82             'beforefirst' marker:
83             // element at address 8621018
84             prev = 0
85             next = 8621088
86             uint = 0
87             user elements:
88             // element at address 8621088
89             prev = 8621018
90             next = 8621038
91             uint = 12
92             // element at address 8621038
93             prev = 8621088
94             next = 8621098
95             uint = 21
96             // element at address 8621098
97             prev = 8621038
98             next = 8621058
99             uint = 28
100             // element at address 8621058
101             prev = 8621098
102             next = 8621068
103             uint = 33
104             // element at address 8621068
105             prev = 8621058
106             next = 86210a8
107             uint = 34
108             // element at address 86210a8
109             prev = 8621068
110             next = 8621078
111             uint = 55
112             // element at address 8621078
113             prev = 86210a8
114             next = 86210b8
115             uint = 67
116             // element at address 86210b8
117             prev = 8621078
118             next = 8621048
119             uint = 76
120             // element at address 8621048
121             prev = 86210b8
122             next = 8621028
123             uint = 99
124             'afterlast' marker:
125             // element at address 8621028
126             prev = 8621048
127             next = 0
128             uint = 0
129              
130              
131             Note: this is a pre-alpha version. Currently the only STL container
132             implemented is the linked list. And that hasn't been tested very well yet.
133             The "push", "pop", "size", "sort", and "dumper" functions are known to work.
134              
135              
136              
137             =head1 SUBROUTINES/METHODS
138              
139             =head2 New
140              
141             Create a Stlgen object.
142              
143             =cut
144              
145             sub New {
146 0     0 1   my $class = shift(@_);
147              
148 0           my $href={
149             Separator => '/',
150             Language => 'c',
151             Extension => ['template'], # possible extensions that template file might have.
152             TemplateSubdir => ['templates'], # subdirectory to look for containing the templates.
153             Path => [], # ref to an array containing a list of paths to look for template in.
154             Template => undef, # this would be "list" or "hash" or whatever STL container name you want
155             };
156              
157 0           my %useroverrides = @_;
158              
159 0           while( my($key,$data)=each(%useroverrides) ){
160 0           print "override key '$key' to data '$data'\n";
161 0           $href->{$key}=$data;
162             }
163              
164 0           my $obj = bless($href,$class);
165              
166 0           return $obj;
167             }
168              
169              
170              
171              
172             =head2 FindTemplate
173              
174             Given all the configuration info we know, find the template that the user wants to use.
175              
176             =cut
177              
178              
179             sub FindTemplate{
180              
181 0     0 1   my($obj)=@_;
182              
183             #print Dumper \%INC;
184              
185 0           my $sep = $obj->{Separator};
186              
187 0           my $class = ref($obj);
188             #print "class is '$class'\n";
189              
190 0           my $modulename = "$class.pm";
191              
192 0 0         unless(exists($INC{$modulename})) {
193 0           die "ERROR: unable to find modulename in \%INC, '$modulename'";
194             }
195              
196 0           my $modulepath = $INC{$modulename};
197              
198 0           $modulepath =~ s{\.pm\Z}{};
199              
200 0           my @paths = ($modulepath);
201              
202             # figure out the name of the template subdirs we're looking for
203 0           my $templatename = $obj->{Template};
204 0           my $language = $obj->{Language};
205              
206             # now go through the paths in order and look for the class that the object is blessed as.
207 0           foreach my $path (@paths) {
208 0           foreach my $templatesubdir (@{$obj->{TemplateSubdir}}) {
  0            
209 0           my $lookingfor =
210             $path.$sep.$templatesubdir.$sep.
211             $language.$sep.$templatename;
212              
213 0           warn "Stlgen is looking for template '$lookingfor'";
214              
215 0 0 0       if(-e $lookingfor and -d $lookingfor) {
216 0           warn "Stlgen found template '$lookingfor'";
217 0           return $lookingfor;
218             }
219             }
220             }
221              
222 0           die "Error: unable to find template file";
223              
224             }
225              
226              
227             =head2 Instantiate
228              
229             Instantiate a template based on a particular type.
230              
231             =cut
232              
233             sub Instantiate {
234 0     0 1   my($obj)=@_;
235              
236             #print Dumper $obj;
237              
238 0           my $templatepath = $obj->FindTemplate();
239            
240 0           print "templatepath is '$templatepath'\n";
241              
242 0   0       my $tt = Template->new({
243             ABSOLUTE => 1, # allow absolute filename paths
244             RELATIVE => 1, # allow relative filename paths
245             INTERPOLATE => 0,
246             }) || die "$Template::ERROR\n";
247              
248 0           $obj->{payloadsize}=scalar(@{$obj->{payload}});
  0            
249              
250              
251 0           my @templates = `ls -1 $templatepath`;
252 0           foreach my $template (@templates) {
253 0           chomp($template);
254             }
255             #print Dumper \@templates;
256              
257 0           my $sep = $obj->{Separator};
258 0           my $shortname = $obj->{Instancename};
259              
260 0           foreach my $template (@templates) {
261 0           my $templatefile = $templatepath.$sep.$template;
262              
263 0           my $named_template = $template;
264 0           $named_template =~ s{NAME}{$shortname};
265 0           my $instname = '.'.$sep.$named_template;
266              
267 0           print "Instantiating template file '$templatefile' as '$instname'\n";
268 0 0         $tt->process($templatefile, $obj, $instname )
269             || die $tt->error(), "\n";
270             }
271              
272              
273              
274             }
275              
276              
277              
278             =head2 function2
279              
280             =cut
281              
282 0     0 1   sub function2 {
283             }
284              
285             =head1 AUTHOR
286              
287             Greg London, C<< >>
288              
289             =head1 BUGS
290              
291             Please report any bugs or feature requests to C, or through
292             the web interface at L. I will be notified, and then you'll
293             automatically be notified of progress on your bug as I make changes.
294              
295              
296              
297              
298             =head1 SUPPORT
299              
300             You can find documentation for this module with the perldoc command.
301              
302             perldoc Stlgen
303              
304              
305             You can also look for information at:
306              
307             =over 4
308              
309             =item * RT: CPAN's request tracker
310              
311             L
312              
313             =item * AnnoCPAN: Annotated CPAN documentation
314              
315             L
316              
317             =item * CPAN Ratings
318              
319             L
320              
321             =item * Search CPAN
322              
323             L
324              
325             =back
326              
327              
328             =head1 ACKNOWLEDGEMENTS
329              
330              
331             =head1 LICENSE AND COPYRIGHT
332              
333             Copyright 2010 Greg London.
334              
335             This program is free software; you can redistribute it and/or modify it
336             under the terms of either: the GNU General Public License as published
337             by the Free Software Foundation; or the Artistic License.
338              
339             See http://dev.perl.org/licenses/ for more information.
340              
341              
342             =cut
343              
344             1; # End of Stlgen
345              
346