14 April 2019

C++ Use Boost property_tree to manipulate xml files


#include "pch.h"
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

using namespace std;
int main()
{
 string path = "C:\\Work\\Students.xml";
 string path2 = "C:\\Work\\Students2.xml";
 try
 {
  std::cout << "Using property tree to manipulate XML files\n";
  //how to load an xml file
  boost::property_tree::ptree pt;
  read_xml(path, pt);

  boost::property_tree::ptree& Students = pt.get_child("Students");

  //how to remove a node using Id
  for (auto it = Students.begin(); it != Students.end();)
  {
   int id = it->second.get(".Id");
   if (id == 1)
   {
    it = Students.erase(it);
    
   }
   else
   {
    ++it;
   }
  }
  //how to add nodes
  for (auto data : { 3, 4, 5 })
  {
   boost::property_tree::ptree& Student = Students.add("Student", "");
   Student.add(".Id", data);
   Student.add("LastName", "McDonalds");
   Student.add("FirstName", "Robert");
  }

  //how to save
  write_xml(path2, pt, std::locale(), boost::property_tree::xml_writer_make_settings('\t', 1));

 }
 catch (exception e)
 {
  string s = e.what();
  std::cout << s;
 }
}

08 April 2019

SWIG, pass C++ array of classes back to C# example

Step 1 Create a C++ Windows DLL Project StudentDll


















Step 2 Add Student.h file

#pragma once
#include
#include
class Student
{
public:
std::string FirstName;
std::string LastName;
Student(std::string _FirstName, std::string _LastName);
Student();
};

std::vector GetStudents();

Step 3 Add Student.cpp file

#include "stdafx.h"
#include "Student.h"

Student::Student(std::string _FirstName, std::string _LastName)
{
FirstName = _FirstName;
LastName = _LastName;
}
Student::Student()
{

}

std::vector GetStudents()
{
std::vector sv;
sv.push_back(Student("John", "Smith"));
sv.push_back(Student("Mary", "Boyle"));
return sv;
}

Step 4 Add Student.i file

%module StudentDll
%include "std_string.i"

%apply const std::string& {std::string* FirstName};
%apply const std::string& {std::string* LastName};
%{
#include "student.h"
%}

%include

%include "student.h"

Step 4a


  • Select Student.i, Properties, General, Item Type as Custom Build Tool.
  • Select Apply to create the Custom Build Tool property group.
  • In Custom Build Tool, General, Command Line enter:
    swig -csharp -c++ -outdir  Student.i
  • In Outputs, enter Student_wrap.cxx, and click OK to close the dialog.
  • Right-click Student.i and Compile. This should create four files: three in the C# Generated folder and one in the C++ project.
  • Create a Generated Files filter in the C++ project and add Student_wrap.cxx to it.

  • Step 5 Add StudentVector.i file

    %module StudentVectorDll

    %include std_vector.i
    %include std_string.i

    /* allow partial c# classes */
    %typemap(csclassmodifiers) SWIGTYPE "public partial class"

    /* generate template around vector */

    %template(StudentVector) std::vector;


    Step 5a


  • Select Student.i, Properties, General, Item Type as Custom Build Tool.
  • Select Apply to create the Custom Build Tool property group.
  • In Custom Build Tool, General, Command Line enter:
    swig -csharp -c++ -outdir GeneratedFolderPath StudentVector.i
  • In Outputs, enter StudentVector_wrap.cxx, and click OK to close the dialog.
  • Right-click StudentVector.i and Compile. This should create four files: three in the C# Generated folder and one in the C++ project.
  • Create a Generated Files filter in the C++ project and add StudentVector_wrap.cxx to it.


  • Step 6 Create a C# .Net Framework project StudentConsumer

















    Fill in the default Program.cs file as follow

    using System.Text;
    using System.Threading.Tasks;

    namespace StudentConsumer
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World");
                var s = StudentDllPINVOKE.GetStudents();
                StudentVector sv = new StudentVector(s, true);
                var sv0 = sv[0];
                var sv00 = SWIGTYPE_p_Student.getCPtr(sv0);
                var sv01 = new Student(sv00);
                Console.WriteLine(sv01.FirstName + ", " + sv01.LastName);

                Console.ReadKey();
            }
        }

    }

    Step 7 Add Generated folder in the C# Student Consumer project
    Step 7a Add the content of GeneratedFolderPath folder from C++ StudentDll project to the Generated folder
    Step 7b Add an extra Constructor in the Student.cs

    public class Student : global::System.IDisposable {
      private global::System.Runtime.InteropServices.HandleRef swigCPtr;
      protected bool swigCMemOwn;

      public Student(global::System.Runtime.InteropServices.HandleRef _swigCPtr)
        {
            swigCPtr = _swigCPtr;
        }



















    Step 8 Set the StartUp project as StudentConsumer and Run it










    swig is a good tool for converting interpolating code between C++ and other languages
    http://www.swig.org/

    22 January 2019

    C++ how using reserve method in vector reduces the number of times copy constructor is called

     #include "stdafx.h"  
     #include <iostream>  
     #include <vector>  
     #include <queue>  
     #include <ctime>  
     #include <chrono>  
     #include <type_traits>  
     #include <string>  
     #include <fstream>  
     using namespace std;  
     using namespace std::chrono;  
     class Point  
     {  
     public:  
          int x; int y;  
          Point(int _x, int _y)  
          {  
               x = _x;  
               y = _y;  
               cout << "constructor " << x << ", " << y << endl;  
          }  
          ~Point()  
          {  
               cout << "destructor " << x << ", " << y <<endl;  
          }  
          Point(const Point& p)  
          {  
               x = p.x;  
               y = p.y;  
               cout << "!!! copy constructor " << x << ", " << y << endl;  
          }  
          friend std::ostream& operator<< (std::ostream& stream, const Point& p) {  
               return stream << "(" << p.x << ", " << p.y << ")";  
          }  
     };  
     Point CreatePoint()  
     {  
          Point p(100, 101);  
          return p;  
     }  
     int main()  
     {  
          vector<Point> p;  
          p.push_back(Point(1, 2));  
          p.push_back(Point(3, 4));  
          p.push_back(Point(5, 6));  
          vector<Point> p2;  
          p2.reserve(3);  
          p2.push_back(Point(10, 20));  
          p2.push_back(Point(30, 40));  
          p2.push_back(Point(50, 60));  
          Point cp = CreatePoint();  
          cout << "tesing completed." << endl;  
          getchar();  
       return 0;  
     }  
    
    Output

    constructor 1, 2
    !!! copy constructor 1, 2
    destructor 1, 2
    constructor 3, 4
    !!! copy constructor 3, 4
    !!! copy constructor 1, 2
    destructor 1, 2
    destructor 3, 4
    constructor 5, 6
    !!! copy constructor 5, 6
    !!! copy constructor 1, 2
    !!! copy constructor 3, 4
    destructor 1, 2
    destructor 3, 4
    destructor 5, 6
    constructor 10, 20
    !!! copy constructor 10, 20
    destructor 10, 20
    constructor 30, 40
    !!! copy constructor 30, 40
    destructor 30, 40
    constructor 50, 60
    !!! copy constructor 50, 60
    destructor 50, 60
    constructor 100, 101
    tesing completed.
    destructor 100, 101
    destructor 10, 20
    destructor 30, 40
    destructor 50, 60
    destructor 1, 2
    destructor 3, 4
    destructor 5, 6

    C++ array vs vector vs queue vs vector with reserve

     #include "stdafx.h"  
     #include <iostream>  
     #include <vector>  
     #include <queue>  
     #include <ctime>  
     #include <chrono>  
     #include <type_traits>  
     #include <string>  
     #include <fstream>  
     using namespace std;  
     using namespace std::chrono;  
     string filepath = "D:\\Temp\\out.txt";  
     class Point  
     {  
     public:  
          int x; int y;  
          Point(int _x, int _y)  
          {  
               x = _x;  
               y = _y;  
          }  
          friend std::ostream& operator<< (std::ostream& stream, const Point& p) {  
               return stream << "(" << p.x << ", " << p.y << ")";  
          }  
     };  
     int rndrange(int r)  
     {  
          int ret = rand() % r;  
          return ret;  
     }  
     Point** CreateArrayPoint(int range)  
     {  
          Point** p = new Point*[range];  
          for (int i = 0; i < range; i++)  
          {  
               p[i] = new Point(rndrange(10), rndrange(10));  
          }  
          return p;  
     }  
     double ArrayTestPoint(int range)  
     {  
          system_clock::time_point p1 = system_clock::now();  
          auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();  
          Point** p = CreateArrayPoint(range);  
          for (int i = 0; i < range; i++)  
          {  
               Point r = *p[i];  
          }  
          system_clock::time_point p2 = system_clock::now();  
          auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();  
          for (int i = 0; i < range; i++)  
          {  
               delete p[i];  
          }  
          delete[] p;  
          double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;  
          return d12;  
     }  
     vector<Point> CreateVectorPoint(int range)  
     {  
          vector<Point> v;  
          for (int i = 0; i < range; i++)  
          {  
               v.push_back(Point(rndrange(10), rndrange(10)));  
          }  
          return v;  
     }  
     double VectorTestPoint(int range)  
     {  
          system_clock::time_point p1 = system_clock::now();  
          auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();  
          vector<Point> q = CreateVectorPoint(range);  
          for (int i = 0; i < q.size(); i++)  
          {  
               Point r = q[i];  
          }  
          system_clock::time_point p2 = system_clock::now();  
          auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();  
          double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;  
          return d12;  
     }  
     vector<Point> CreateRVectorPoint(int range)  
     {  
          vector<Point> v;  
          v.reserve(range);  
          for (int i = 0; i < range; i++)  
          {  
               v.push_back(Point(rndrange(10), rndrange(10)));  
          }  
          return v;  
     }  
     double RVectorTestPoint(int range)  
     {  
          system_clock::time_point p1 = system_clock::now();  
          auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();  
          vector<Point> q = CreateRVectorPoint(range);  
          for (int i = 0; i < q.size(); i++)  
          {  
               Point r = q[i];  
          }  
          system_clock::time_point p2 = system_clock::now();  
          auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();  
          double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;  
          return d12;  
     }  
     queue<Point> CreateQuePoint(int range)  
     {  
          std::queue<Point> dq;  
          for (int i = 0; i < range; i++)  
          {  
               dq.push(Point(rndrange(10), rndrange(10)));  
          }  
          return dq;  
     }  
     double QueTestPoint(int range)  
     {  
          system_clock::time_point p1 = system_clock::now();  
          auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();  
          queue<Point> q = CreateQuePoint(range);  
          for (int i = 0; i < q.size(); i++)  
          {  
               Point r = q.front();  
               q.pop();  
          }  
          system_clock::time_point p2 = system_clock::now();  
          auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();  
          double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;  
          return d12;  
     }  
     int* CreateArrayInt(int range)  
     {  
          int* p = new int[range];  
          for (int i = 0; i < range; i++)  
          {  
               p[i] = rndrange(10);  
          }  
          return p;  
     }  
     double ArrayTestInt(int range)  
     {  
          system_clock::time_point p1 = system_clock::now();  
          auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();  
          int* p = CreateArrayInt(range);  
          for (int i = 0; i < range; i++)  
          {  
               int r = p[i];  
          }  
          system_clock::time_point p2 = system_clock::now();  
          auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();  
          delete[] p;  
          double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;  
          return d12;  
     }  
     vector<int> CreateVectorInt(int range)  
     {  
          vector<int> v;  
          for (int i = 0; i < range; i++)  
          {  
               v.push_back(rndrange(10));  
          }  
          return v;  
     }  
     double VectorTestInt(int range)  
     {  
          system_clock::time_point p1 = system_clock::now();  
          auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();  
          vector<int> q = CreateVectorInt(range);  
          for (int i = 0; i < q.size(); i++)  
          {  
               int r = q[i];  
          }  
          system_clock::time_point p2 = system_clock::now();  
          auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();  
          double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;  
          return d12;  
     }  
     vector<int> CreateRVectorInt(int range)  
     {  
          vector<int> v;  
          v.reserve(range);  
          for (int i = 0; i < range; i++)  
          {  
               v.push_back(rndrange(10));  
          }  
          return v;  
     }  
     double RVectorTestInt(int range)  
     {  
          system_clock::time_point p1 = system_clock::now();  
          auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();  
          vector<int> q = CreateRVectorInt(range);  
          for (int i = 0; i < q.size(); i++)  
          {  
               int r = q[i];  
          }  
          system_clock::time_point p2 = system_clock::now();  
          auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();  
          double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;  
          return d12;  
     }  
     queue<int> CreateQueInt(int range)  
     {  
          std::queue<int> dq;  
          for (int i = 0; i < range; i++)  
          {  
               dq.push(rndrange(10));  
          }  
          return dq;  
     }  
     double QueTestInt(int range)  
     {  
          system_clock::time_point p1 = system_clock::now();  
          auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();  
          queue<int> q = CreateQueInt(range);  
          for (int i = 0; i < q.size(); i++)  
          {  
               int r = q.front();  
               q.pop();  
          }  
          system_clock::time_point p2 = system_clock::now();  
          auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();  
          double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;  
          return d12;  
     }  
     int main()  
     {  
          queue<double> arrayTestResults;  
          queue<double> vectorTestResults;  
          queue<double> queTestResults;  
          queue<double> rvectorTestResults;  
          queue<double> arrayTestResults2;  
          queue<double> vectorTestResults2;  
          queue<double> queTestResults2;  
          queue<double> rvectorTestResults2;  
          for (int i=0; i<=10000000; i+=100000)  
          {  
               double at = ArrayTestInt(i);  
               double vt = VectorTestInt(i);  
               double qt = QueTestInt(i);  
               double rvt = RVectorTestInt(i);  
               arrayTestResults.push(at);  
               vectorTestResults.push(vt);  
               queTestResults.push(qt);  
               rvectorTestResults.push(rvt);  
               double at2 = ArrayTestPoint(i);  
               double vt2 = VectorTestPoint(i);  
               double qt2 = QueTestPoint(i);  
               double rvt2 = RVectorTestPoint(i);  
               arrayTestResults2.push(at2);  
               vectorTestResults2.push(vt2);  
               queTestResults2.push(qt2);  
               rvectorTestResults2.push(rvt2);  
          }  
          ofstream myfile;  
          myfile.open(filepath);  
          for (int i=0; i<10000000; i+=100000)  
          {  
               double at = arrayTestResults.front();  
               double vt = vectorTestResults.front();  
               double qt = queTestResults.front();  
               double rvt = rvectorTestResults.front();  
               double at2 = arrayTestResults2.front();  
               double vt2 = vectorTestResults2.front();  
               double qt2 = queTestResults2.front();  
               double rvt2 = rvectorTestResults2.front();  
               arrayTestResults.pop();  
               vectorTestResults.pop();  
               queTestResults.pop();  
               rvectorTestResults.pop();  
               arrayTestResults2.pop();  
               vectorTestResults2.pop();  
               queTestResults2.pop();  
               rvectorTestResults2.pop();  
               //myfile << i << "," << vt << "," << qt << endl;  
               myfile << i << "," << at << "," << vt << "," << qt << "," << rvt <<",";  
               myfile << at2 << "," << vt2 << "," << qt2 << "," << rvt2 << endl;  
          }  
          myfile.close();  
          cout << "tesing completed." << endl;  
          getchar();  
       return 0;  
     }  
    

    12 January 2019

    C++ How to dynamically create an array of objects

     #include "stdafx.h"  
     #include <iostream>  
     using namespace std;  
     class Point  
     {  
     public:  
          int x; int y;  
          Point(int _x, int _y)  
          {  
               x = _x;  
               y = _y;  
               cout << "Point constructor:(" << x << "," << y << ")" << endl;  
          }  
          ~Point()  
          {  
               cout << "Point destructor:(" << x << "," << y << ")" << endl;  
          }  
     };  
     std::ostream& operator<< (std::ostream& stream, const Point& p) {  
          return stream << "(" << p.x << "," << p.y << ")" << endl;  
     }  
     double* CreateDoubles()  
     {  
          double* d = new double[3];  
          d[0] = 1.1;  
          d[1] = 2.2;  
          d[2] = 3.3;  
          return d;  
     }  
     Point** CreatePoints()  
     {  
          Point** p = new Point*[2];  
          p[0] = new Point(1, 2);  
          p[1] = new Point(3, 4);  
          return p;  
     }  
     void Test()  
     {  
          double* d = CreateDoubles();  
          cout << "Dynamically created doubles: " << d[0] << ", " << d[1] << ", " << d[2] <<endl <<endl;  
          delete[] d;  
          Point** p = CreatePoints();  
          cout << "Dynamically created Points: " << endl;  
          cout << *p[0] << endl;  
          cout << *p[1] << endl;  
          delete p[0];  
          delete p[1];  
          delete[] p;  
     }  
     int main()  
     {  
          Test();  
          getchar();  
       return 0;  
     }