28 October 2018

C++: Template of retrieving array size

 template<typename T, std::size_t S>  
 std::size_t SizeOf(T(&)[S]) {  
      return S;  
 }  
 int main()  
 {  
      double d[10];  
      cout << "d size is " << SizeOf(d) << endl;  
      char ch;  
      cin.getline(&ch, 1);  
     return 0;  
 }  

C++: Using shared pointer to point to object array in object tree example

 #include "stdafx.h"  

 #include <iostream> 
 using namespace std; 
 static int numobject = 0; 
 #define NUM_CHILDREN 3 
 template<typename T> 
 inline std::shared_ptr<T> MakeArray(int size) 
 { 
      return std::shared_ptr<T>(new T[size], [](T *p) { delete[] p; }); 
 } 
 template<typename T> 
 inline std::shared_ptr<T> MakeArray2(int size) 
 { 
      return std::shared_ptr<T>(new T[size], std::default_delete<OtC[]>()); 
 } 
 class OtC 
 { 
 public: 
      OtC() 
      { 
           id = ++numobject; 
           cout << "Otc constructor " << id << endl; 
      } 
      ~OtC() 
      { 
           cout << "Otc destructor " << id << endl; 
      } 
      int id; 
      std::shared_ptr<OtC> children = nullptr; 
      OtC * parent; 
      std::shared_ptr<OtC> GetChildren() 
      { 
           if (children == nullptr) 
           { 
                cout << "creaing children for the first time" << endl; 
                CreateChildren(); 
                return children; 
           } 
           else 
           { 
                cout << "children has already been created" << endl; 
                return children; 
           } 
      } 
      void CreateChildren() 
      { 
           OtC* parent = this; 
           cout << "In create children" << endl; 
           children = MakeArray<OtC>(NUM_CHILDREN); 
           children.get()[0].id = children.get()[0].id + 10; 
           children.get()[1].id = children.get()[1].id + 10; 
           children.get()[2].id = children.get()[2].id + 10; 
           children.get()[0].parent = this; 
           children.get()[1].parent = this; 
           children.get()[2].parent = this; 
      } 
 }; 
 void SingleObject() 
 { 
      shared_ptr<OtC> o = make_shared<OtC>(); 
 } 
 void MultiObjects() 
 { 
      std::shared_ptr<OtC> p = MakeArray2<OtC>(4); 
 } 
 void ChainObject() 
 { 
      shared_ptr<OtC> o = make_shared<OtC>(); 
      shared_ptr<OtC> children = o->GetChildren(); 
      cout << "first call use count " << children.use_count() << endl; 
      for (int i = 0; i < NUM_CHILDREN; i++) 
      { 
           cout << "child " << i << " id is " << children.get()[i].id << endl; 
      } 
      shared_ptr<OtC> children2 = o->GetChildren(); 
      cout << "second call use count " << children.use_count() << endl; 
      for (int i = 0; i < NUM_CHILDREN; i++) 
      { 
           cout << "child " << i << " id is " << children.get()[i].id << endl; 
           cout << "child " << i << " parent id is " << children.get()[i].parent->id << endl; 
      } 
 } 
 int main() 
 { 
      cout << "Shared pointer object children tree example" << endl; 
      ChainObject(); 
      char ch; 
      cin.getline(&ch, 1); 
   return 0; 
 } 

08 August 2018

Windows 10 Sky Drive security flaw

If you are going to create an account on a share Windows 10 computer, just beware that Windows will download ALL you skydrive data locally and anyone else who has administrative privilege on that computer can see all your data.

They are stored in C:\Users\{Username}\OneDrive

So be careful, next time you use a share computer

Telstra vs Optus vs Vodafone - Aug 2018


I had been with Vodafone for many years and quite recently, I decided to go prepaid to try out the three different networks and compare their performance.  Here is what I found

Telstra

I can call people at home, the reception is good.
I can talk to clients at office, the reception is like landline.
Internet connection is good on the Seaford trainline except certain areas near Hallett Cove where the reception can drop to 50% for a few minutes.

Optus

I can call people at home, the reception is good.
I can talk to clients at office, the reception is good.
Internet connection is good on the Seaford trainline except certain areas near Hallett Cove where there is no reception for a few minutes.

Vodafone

I can call people at home, the reception is good.
I can’t talk to clients at office, reception is intermittent.
Internet connection is average on the Seaford trainline and no reception near Hallett Cove and Seacliff area.

Money for value wise

Telstra charges $49 a month for 20G
Optus charges $36 a month for 30G
Vodafone charges $45 a month for 30G

Conclusion

If voice reception and data connection is critical to you, go with Telstra
If you want more data and pay less and are willing to put up with lesser reception at times, go with Optus


12 June 2018

tsconfig example with compileOnSave

{
    "compilerOptions": {
        "module": "system",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "outFile": "wwwroot/js/out.js",
        "sourceMap": true
    },
    "include": [
        "Pages/*.ts"
    ],
    "compileOnSave": true
}

21 April 2018

Kendo UI grid center the progress bar

I found a solution on how to center the Kendo UI grid progress bar today.  Thought I share, it turns out to be real easy.  Just use overwrite the CSS
 <style>  
   .k-loading-mask .k-loading-image {  
     background-image: url('loadingtest2.png') !important;  
     position: fixed !important;  
     top: 50% !important;  
     left: 50% !important;  
     /* bring your own prefixes */  
     transform: translate(-50%, -50%) !important;  
   }  
 </style>  

This method can of course be apply to anything you want to display on the center of the screen.



Full source code based on the Grid\Api example from their sample codes is as below

 <!DOCTYPE html>  
 <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
 <head>  
   <title>API</title>  
   <meta charset="utf-8">  
   <link href="../content/shared/styles/examples-offline.css" rel="stylesheet">  
   <link href="../../styles/kendo.common.min.css" rel="stylesheet">  
   <link href="../../styles/kendo.rtl.min.css" rel="stylesheet">  
   <link href="../../styles/kendo.default.min.css" rel="stylesheet">  
   <link href="../../styles/kendo.default.mobile.min.css" rel="stylesheet">  
   <script src="../../js/jquery.min.js"></script>  
   <script src="../../js/jszip.min.js"></script>  
   <script src="../../js/kendo.all.min.js"></script>  
   <script src="../content/shared/js/console.js"></script>  
   <script>  
   </script>  
 </head>  
 <style>  
   .k-loading-mask .k-loading-image {  
     background-image: url('loadingtest2.png') !important;  
     position: fixed !important;  
     top: 50% !important;  
     left: 50% !important;  
     /* bring your own prefixes */  
     transform: translate(-50%, -50%) !important;  
   }  
 </style>  
 <body>  
   <a class="offline-button" href="../index.html">Back</a>  
   <div id="example">  
     <div class="box wide">  
       <div class="box-col">  
         <h4>Selection</h4>  
         <ul class="options">  
           <li>  
             <input type="text" value="0" id="selectRow" class="k-textbox" />  
             <button class="selectRow k-button">Select row</button>  
           </li>  
           <li>  
             <button class="clearSelection k-button">Clear selected rows</button>  
           </li>  
         </ul>  
       </div>  
       <div class="box-col">  
         <h4>Expand/Collapse</h4>  
         <ul class="options">  
           <li>  
             <input type="text" value="0" id="groupRow" class="k-textbox"/>  
             <button class="toggleGroup k-button">Collapse/Expand group</button>  
           </li>  
         </ul>  
       </div>  
     </div>  
     <div id="grid"></div>  
     <script>  
         $(document).ready(function () {  
           $("#grid").kendoGrid({  
             dataSource: {  
               transport: {  
                 read: {  
                   url: "https://demos.telerik.com/kendo-ui/service/Products",  
                   dataType: "jsonp"  
                 }  
               },  
               pageSize: 5,  
               group: {  
                 field: "UnitsInStock",  
                 dir: "asc"  
               }  
             },  
             selectable: "multiple row",  
             pageable: {  
               buttonCount: 5  
             },  
             scrollable: false,  
             groupable: true,  
             columns: [  
               {  
                 field: "ProductName",  
                 title: "Product Name"  
               },  
               {  
                 field: "UnitPrice",  
                 title: "Unit Price",  
                 format: "{0:c}"  
               },  
               {  
                 field: "UnitsInStock",  
                 title: "Units In Stock"  
               }  
             ]  
           });  
           $(".clearSelection").click(function () {  
             $("#grid").data("kendoGrid").clearSelection();  
           });  
           var selectRow = function (e) {  
             if (e.type != "keypress" || kendo.keys.ENTER == e.keyCode) {  
               var grid = $("#grid").data("kendoGrid"),  
                 rowIndex = $("#selectRow").val(),  
                 row = grid.tbody.find(">tr:not(.k-grouping-row)").eq(rowIndex);  
               grid.select(row);  
             }  
           },  
             toggleGroup = function (e) {  
               if (e.type != "keypress" || kendo.keys.ENTER == e.keyCode) {  
                 var grid = $("#grid").data("kendoGrid"),  
                   rowIndex = $("#groupRow").val(),  
                   row = grid.tbody.find(">tr.k-grouping-row").eq(rowIndex);  
                 if (row.has(".k-i-collapse").length) {  
                   grid.collapseGroup(row);  
                 } else {  
                   grid.expandGroup(row);  
                 }  
               }  
             };  
           $(".selectRow").click(selectRow);  
           $("#selectRow").keypress(selectRow);  
           $(".toggleGroup").click(toggleGroup);  
           $("#groupRow").keypress(toggleGroup);  
         });  
     </script>  
   </div>  
 </body>  
 </html>  

04 April 2018

Custom JavaScript event illustrated

The trigger function is called when user clicks on p, myCustomEvent is bubble up from p to div1 and div2, but not pass on to div3 because of event.stopPropagation();


 <!DOCTYPE html>  
 <head>  
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
 </head>  
 <style>  
   form, div, p {  
     border-style: solid;  
     border-width: 1px;  
   }  
   p{  
     background-color:green;  
   }  
 </style>  
 <form>  
 FORM  
   <div id="div3">div3  
     <div id="div2">div2  
       <div id="div1">div1  
         <p onclick="trigger(this)">P</p>  
       </div>  
     </div>  
   </div>  
 </form>  
 <script>  
   $(function () {  
     $("#div3").on("myCustomEvent",  
       function (event, arg1, arg2) {  
         alert("div3 will not receive the event");  
       });  
     $("#div2").on("myCustomEvent",   
       function (event, arg1, arg2) {  
         var s = "div2 arg1:" + arg1 + ", arg2:" + arg2;  
         alert(s);  
         event.stopPropagation();  
       });  
     $("#div1").on("myCustomEvent",  
       function (event, arg1, arg2) {  
         alert("div1 caught myCustomEvent and pass it on");  
       });  
   });  
   function trigger(source)  
   {  
     //trigger myCustomEvent which will bubble up to its parents  
     $(source).trigger("myCustomEvent", ["hello", "world"]);  
   }  
 </script>