Archive

Posts Tagged ‘C++’

C++ Using template type to make decision about how to extend class

June 4, 2013 Leave a comment

so the problem was a follow, I had a parent template class and as usual template child class extended from the parent class. Objective was depending upon what type of the Child class to select the type of the parent template.

Therefore went looking for how to pick up the  type of the variable depending upon the template. Found this really nice post. http://stackoverflow.com/questions/8243134/c-templates-select-different-type-based-on-value-of-template-parameter

Using information in it and little experimentation following code shows how to do it.

//============================================================================
// Name        : forTestingAndTrying.cpp
// Author      : Mohsen Ali
// Version     :
// Copyright   : use freely on your own risk, but contribute
// Description : testing template
//============================================================================
#include <iostream>
#include <type_traits>
using namespace std;

/*Parent Class*/
template<typename T>
struct parentMe{
T myData;
void print(void){
cout << "parentMe datasize " << sizeof(myData) <<endl;
}

};

template<bool B>
struct stUseless: parentMe<typename std::conditional<B, double, float>::type>{
typedef typename std::conditional<B, char, int>::type T;
typedef typename std::conditional<B, double, float>::type T2;

T dat;
void print(){
cout <<"Size is " <<  sizeof(dat) <<endl;
parentMe<T2>::print();
cout <<" --------------------  " <<  endl;
}
};

int main() {
stUseless<false> a;
a.print();

stUseless<true> b;
b.print();

cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}

and the output is below

Size is 4
parentMe datasize 4
——————–
Size is 1
parentMe datasize 8
——————–
!!!Hello World!!!

Advertisements
Categories: C++ Tags: , , ,

parsing jason nested array using jsoncpp

May 22, 2013 Leave a comment

so first experiments with the json and json cpp.

the json string is as follow

{“id”: 1,”tags”: [ [800, 99.7], [], [76] ],”stock”: {“warehouse”: 300,”retail”: 20}}

Wanted to read the nested arrays of “tags” as you can see such an array could be e.g. cell datatype in the matlab or could be represented as the array of vectors

int main (){</pre>
unsigned int i,j;
char fileName[] ="abc.json";

ifstream infile;
infile.exceptions (ios::failbit|ios::badbit);
try
{
infile.open (fileName);
}
catch (ifstream::failure& e)
{
cerr<<"\n Exception opening file "<<fileName<<": "<<e.what ();
return 0;
}

string input;
Json::Reader reader;
Json::Value root;
bool parsingSuccessful;

try
{
while (getline (infile,input,'\n'))
{
if (false == (parsingSuccessful = reader.parse (input,root)))
{
cerr<<"\nFailed to parse configuration:"
<<reader.getFormatedErrorMessages ();
return 0;
}
else
{

int verbName = root.get ("id","").asInt ();
Json::Value data = root.get ("tags",0);
cout << "size is " << data.size() <<endl;

//vector<float> freq (mWidth * mHeight);
for (int i = 0; i < data.size(); i++){
int sizeint = data[i].size();
cout <<endl <<"---------------" <<endl;
cout << "Size of individual is " <<sizeint << endl;
Json::Value dat1 = data[i];

for(int j=0; j< dat1.size(); j++){
cout << dat1[j].asDouble() << " ,";
}

}
}
}
}
catch (ifstream::failure& e)
{

}

return 0;
}
<pre>
Categories: C++, code, jsoncpp, matlab Tags: , , , , , ,

Running Makefile on the Windows

August 1, 2012 Leave a comment

There are many ways to run make program on the Windows, many of them will ask you to run in cygwin or similar shells. I was interested in running the makefile in the Windows so that the application generated could be used by the other Windows programs.
So the solution is to use the Visual Studio Command Prompt, run it, cd to the directory where the makefile is and then use the following

nmake -f Makefile

If the Makefile is using g++ or gcc in it you have to change it to “cl”, this the compiler being used by the VC. You might have to make little changes but then it should work fine.

 

Have a look at following

http://stackoverflow.com/questions/2532234/how-to-run-a-makefile-in-windows

 

Categories: C++, code Tags: , , , ,

Dynamic Multi-dimensional memory allocation C/C++

October 15, 2010 Leave a comment

An excellent library for the multi dimensional memory allocation.

http://www8.cs.umu.se/~isak/snippets/mdalloc.c

some other code snippets are http://www8.cs.umu.se/~isak/snippets/

Recovering 2D motion between the frames of video

June 28, 2010 Leave a comment

Motion2D is C/C++ library by Irsia/INRIA to estimate 2D parametric motion in between the frames of video.  Works very good. Have a good example code.

Only headache is I am having problem in compiling with the VC. They don’t have any dsp file for compilation of code in Windows, so don’t know which libaries to create. Have to use cygwin.

stl Sort in C++; simple code for sorting the array of structures

February 18, 2009 Leave a comment

Simple Code that shows how to use the C++ STL function sort()


<blockquote>
#include <iostream>
#include <algorithm>

using namespace std;

struct Numb{
double num;
int pos1;

bool operator &lt;(const Numb &amp;n1){
return num &lt; n1.num;
};
};
////////////////////////////////
void main(void){

Numb numb[10];

for(int i=0; i &lt; 10; i++){
numb[i].num = rand();
numb[i].pos1 = i;
}
cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;
for(int i=0; i &lt; 10; i++){
cout &lt;&lt; " " &lt;&lt; "(" &lt;&lt; numb[i].pos1 &lt;&lt; ") " &lt;&lt; numb[i].num;
}
cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;

std::sort( numb, numb+10);
cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;
for(int i=0; i &lt; 10; i++){
cout &lt;&lt; " " &lt;&lt; "(" &lt;&lt; numb[i].pos1 &lt;&lt; ") " &lt;&lt; numb[i].num;
}

cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;

}
Categories: code, STL Tags: , , , ,

(ReadShader)Simple File Reading Code in C++ with File Size buffer

April 5, 2008 Leave a comment

//impressed by lighthouse 3d and help taken from sources on the internet.
//this is not my idea, not my full code.

//It reads the full file in single go.
#ifndef READSHADER_H
#define READSHADER_H
#include
#include

using namespace std;

char * readShader(char *fileName){

cout <<“nothing”;
ifstream input(fileName);
if(!input){
cout << endl << “UNABLE TO OPEN FILE ” << fileName <<endl;
return 0;
}

int st = input.tellg();
input.seekg(0, ios::end);
int end = input.tellg();
int fileSize = end – st;
if(fileSize <= 0){return 0;}

char *data = new char[fileSize+50];//50 added to just have buffer
memset(data, ”,fileSize+10);
input.seekg(0, ios::beg);

input.read(data,fileSize);
input.close();
data[fileSize]= ”; //appending the zero as the read does not append that
return data;

};
#endif