Purpose of a singleton:
To have a class which has only one instance(object)
All the objects of this class refer to same global object
that is created only once
This design pattern provides a mechanism for providing
namespaces to global variables
Download here. singleton-design-pattern
C++ example for singleton design pattern: click here
System Verilog example for singleton design pattern
// Filename : singleton.sv
// Author : A.G.Raja
// License : GPL
// Website : http://agraja.wordpress.com
module tb;
class Singleton;
// object that is created only once
extern static Singleton single;
// constructor that is called only once
extern function new;
// method to get object
extern function Singleton get_instance;
// data to check the functionality
int data;
endclass
initial Singleton::single = null;
function Singleton::new;
data = 14;
endfunction
function Singleton Singleton::get_instance;
// call the constructor for the first time
// if the object has been created already
// return reference to the object already created
if(single==null) single = new;
// return the reference to the object
return single;
endfunction
Singleton s, t;
initial begin
// don’t use s= new;
// since it will create new instances
// instead use the get_instance method
s = s.get_instance();
t = t.get_instance();
$display(“initial value of s.data = “,s.data);
s.data = 25;
$display(“modified value of s.data = “,s.data);
$display(“value of t.data = “,t.data);
end
endmodule
Result:
initial value of s->data = 14
modified value of s->data = 25
value of t->data = 25
Download here. singleton-design-pattern


C, comments, documentation, help, Perl, program, script
Perl script to display comments
In Bookmarks on March 19, 2008 at 1:57 pmDownload here. c_help.pdf
#!/usr/bin/perl
# Filename: chelp.pl
# Author : A. G. Raja
# Website : agraja.wordpress.com
# License : GPL
# pass c file as argument to this script
# this script displays all the comments
use strict;
use warnings;
open(FILE,$ARGV[0]) or die “File not Found”;
my $line;
my $start=0;
while ( $line = <FILE> ) {
if($line =~ /\/\//)
{
$line=~ s/(.*)\/\/(.*)/$2/;
print $line;
}
if($line =~ /\/\*/)
{
$start=1;
$line=~ s/(.*)\/\*(.*)/$2/;
}
if($line =~ /\*\//)
{
$line=~ s/(.*)\*\//$1/;
print $line;
$start=0;
}
if($start) { print $line; }
}
close FILE;
Tips:
Use a shell script to redirect the output
of the above script to another program.
Example
#!/bin/bash
# Filename: help.sh
# Author : A.G.Raja
# Website : agraja.wordpress.com
# License : GPL
# rename chelp.pl to chelp
# Install chelp and add to path
chelp $1 | less
Demo:
Below is a C file
// Filename : demo.c
// Author : cfile.pl
// License : GPL
#include <stdio.h>
#define print(x) printf(“str=%s\n”,x);
int main(int argc, char **argv)
{
/* this is a multi-
line comment */
char *str=”File name:”;// this will print file name
print(argv[0])
return 0;
}
Type at the terminal:
[raja@agraja ]$ chelp demo.c
The following will be printed to stdout
Filename : demo.c
Author : cfile.pl
License : GPL
this is a multi-
line comment
this will print file name
Download here. c_help.pdf