C++ Template Example

Templates can be viewed as a form of static compile time virtualization. Was that a mouthful or what?!? Here is a template example that highlights template specialization and default template parameters that can be over-riden by a class. Remember to always have your template definitions AND implementations in the one cpp file. Do not have a seperate header file for the definitions and a seperate file for the implementation as is customary.

Let us take a look at the example. I strongly suggest you copy this code and compile it and run it on your desktop for a better understanding.

 1. #include <stdio.h>
 2. 
 3. class deer{
 4. public:
 5.     char* getWhat(){return (char*)"I'm a deer";}
 6. };
 7. 
 8. class hog{
 9. public:
10.     char* getWhat(){return (char*)"I'm a hog";}
11. };
12. 
13. class buffalo{
14. public:
15.     char* getWhat(){return (char*)"I'm a buffalo";}
16. };
17. 
18. class man{
19. public:
20.     char* getWhat(){return (char*)"I'm a man - dear god";}
21. };
22. 
23. /*Default digestive classes*/
24. template <typename T>
25. class crocdigest{
26. public:
27.     void digest(T& t){
28.         printf("default template digest %s\n", t.getWhat());
29.     }
30. };
31. 
32. /*Special digestive class*/
33. template <typename T>
34. class special_crocdigest{
35. public:
36.     void digest(T& t){
37.         printf("special class digest %s\n", t.getWhat());
38.     }
39. };
40. 
41. 
42. /*Default croc's meals*/
43. template <class T, class c_crocdigest = crocdigest<T> >
44. class crocsmeal{
45.     c_crocdigest crocdig;
46. public:
47.     int gobble(T&);
48. };
49. template <typename T, class c_crocdigest> 
50. int crocsmeal<T,c_crocdigest>::gobble(T& t){
51.   crocdig.digest(t);
52.   return 0;
53. }
54. 
55. /*specialized template for hogs meal*/
56. template <>
57. class crocsmeal<hog>{
58.   public:
59.     int gobble(const hog& t){
60.       printf("template specialization gobbling the hog\n");
61.       return 0;    
62.     }
63. };
64. 
65. 
66. int main(){
67. 
68.   hog        h;
69.   deer        d;
70.   man        m;
71.   buffalo    b;
72.   
73.   crocsmeal<deer> mealdeer;
74.   crocsmeal<hog> mealhog;
75.   crocsmeal<buffalo> mealbuffalo;
76.   crocsmeal<man, special_crocdigest<man>> mealman;
77.   
78.   mealhog.gobble(h);
79.   mealdeer.gobble(d);
80.   mealbuffalo.gobble(b);
81.   mealman.gobble(m);
82. }
Hide line numbers

Javascript Weather Widget

Find out what the weather is in any country using this customizable weather widget. This helpful widget can be easily embedded in your site. It's pure javascript and doesn't require any back-end scripting such as php, perl etc.


Sri Lanka Weather



Learn more about this widget
A widget that displays the weather of a region in a country. This uses the Yahoo RSS weather feed and the google Feed api to read and display the information.

Javascript Weather Widget Using Yahoo Weather and Google RSS Reader

Description
The aim of this tutorial is to show how you can combine different services from different providers to create a rich, original and free widget. In this example we will use Yahoo's RSS Weather service along with Google's RSS Reader api to create a pure javascript weather widget. In this example the widget will display the weather in Sri Lanka. Sri Lanka is the birthplace of righteous.

View this widget

You can download the zipped example here.

HTML Code
First let us have a look at the html code
 1. <html>
 2. <head>
 3. <title></title>
 4. </head>
 5. <body style="margin-top:0px;margin-left:0px">
 6. <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAU34fqM8XohIf42U76GcxNhQD6263AO_J-cN-o5MVlTXsf8kbcBRgf8XZKHTaVeqWfZVYZJ5ub5dW3A"></script>
 7. <script type="text/javascript" src="weather.js"></script>
 8. <h2>Sri Lanka Weather</h2>
 9. <table width="100%"><tbody><tr><td><select onChange="YW_CallWeather(this.options[this.selectedIndex].value)">
10. <option value="http://weather.yahooapis.com/forecastrss?p=CEXX0001&u=c">Colombo</option>
11. <option value="http://weather.yahooapis.com/forecastrss?p=CEXX0002&u=c">Moratuwa</option>
12. <option value="http://weather.yahooapis.com/forecastrss?p=CEXX0003&u=c">Negombo</option>
13. <option value="http://weather.yahooapis.com/forecastrss?p=CEXX0004&u=c">Sri Jayawardenepura</option>
14. <option value="http://weather.yahooapis.com/forecastrss?p=CEXX0005&u=c">Katunayake</option>
15. </select></td></tr>
16. <tr><td><script>
17. YW_CallWeather("http://weather.yahooapis.com/forecastrss?p=CEXX0001&u=c");
18. </script>
19. </td></tr></tbody></table>
20. </body>
21. </html>
Hide line numbers

line 6 Loading the google api with your key. Please substitute your key

Line 7 This is the javascript file.

Line 9 This has the onchange javascript handler that feeds the YW_CallWeather function with the Yahoo RSS feed for that region. In the case of Colombo, Sri Lanka, that value is
http://weather.yahooapis.com/forecastrss?p=CEXX0001&u=c

You can find out this value for regions/countries that interest you by going to weather.yahoo.com, selecting your region 'Australasia' then country 'Australia' then State 'Australian capital terrotory' then the county, 'Canberra.' now click on the orange RSS Icon on the right hand side for the URL.
http://weather.yahooapis.com/forecastrss?p=ASXX0023&u=f

This is how the drop down box is populated.


RSS Reader Script
Next we have to create the script that reads the RSS and allows us to manipulate the result. We will use google's RSS api for this.
 1. /*      scriptdiaries.blogspot.com 
 2.         Righteous Ninja
 3.         Free to use and modify.*/
 4.         
 5. google.load("feeds", "1");
 6. var YW_ContainerOk=false;
 7. function YW_GetWeatherResults(result){
 8.   if (!result.error){
 9.         s="<table  style=\"border-width:1px;border-collapse:collapse;border-style:solid;"+ 
10.       "border-color:#6D7B8D;width:1px;height:1px\" bgcolor=\"#ffffff\">"+
11.       "<tbody><tr>"+
12.       "<td>"+result.feed.entries[0].content.match(/<img .*?>/)+"</td></tr>"+
13.       "</tbody></table>";
14.     document.getElementById("YW_weather_container").innerHTML="<b>"+
15.     result.feed.entries[0].title+"</b><br/>"+
16.     result.feed.entries[0].content.replace(/<img .*?>/,s);
17.     }
18. }
19. function YW_CallWeather(hrf){
20.     if(!YW_ContainerOk){
21.         document.write("<div id=\"YW_weather_container\">There is no weather information available for this location at this time</div>");
22.         YW_ContainerOk=true;
23.     }
24.     (new google.feeds.Feed(hrf)).load(YW_GetWeatherResults);
25. }
Hide line numbers

Line 19 This is the function that gets called when a value is selected in the drop down box. This gets called with the yahoo rss url. We then use google's google.feeds.Feed to submit the url and give the name of the callback function YW_GetWeatherResults.

Line 7 YW_GetWeatherResults gets called with the result. We then use the result.feed.entries[0] object to access the various parts of the feeds and display the result in a div.