6 Recent Changes in Origin C

Contents

Version Info

Minimum Origin Version Required: Origin 2027

Improvement

keyword override

To extend or modify the abstract or virtual implementation of an inherited method, property. It can help to find out errors when base class’s prototype is modified but derived class is not. See more.

class CTestBase
{
public:
	virtual void TestV()
	{
		printf("CTestBase::TestV\r\n");
	}
	virtual void TestV(int aa)
	{
		printf("CTestBase::TestV with int\r\n");
	}
};
class CTestObj : public CTestBase
{
public:
	CTestObj() {}
	void TestV() override;	
	void TestV(int aa, double dd) override //compile error, different from base class’s prototype
	{
	}
};

keyword __super

Call a base-class implementation for a function that you are overriding. See more.

class base
{
public:
    virtual void fun(){ out_str("Base"); }
};
class A :public base
{
public:
    void fun(){ out_str("A");}
};
class B :public A
{
public:
    void fun()
    {
        __super::fun();// call A::fun()
        out_str("B");
    }
};
void main()
{
    B b;
    b.fun();
}

class oset and class omap

class oset to support set of primitive type

void set_ex()
{
	oset<string> s1;
	s1.Insert("hello");
	s1.Insert("Hello");
	s1.Insert("world");
	if (s1.Contains("Hello"))
		out_str("Hello exists.");
	
	vector<string> vs;
	s1.GetAsVector(vs);
}

class omap to support map of basic type

void map_ex()
{
	omap<int, string> m1;
	m1.Set(100, "Hello");
	m1.Set(200, "World");
	string str;
	if (m1.Get(200, str))
		printf("Found value at 200: %s\r\n", str);
}

vector of simple struct

struct StSimpleTypes
{
	int			nValue;
	double			rValue;
	
};

void vector_struct_simple_type()
{
	vector<StSimpleTypes>	vst;
	
	StSimpleTypes		st1;
	
	st1.nValue = 100;
	st1.rValue = 1000.88;
	vst.Add(st1);
	
	st1.nValue = 101;
	st1.rValue = 1001.88;
	vst.Add(st1);

	st1.nValue = 102;
	st1.rValue = 1002.88;
	vst.Add(st1);
	
	out_int("size=", vst.GetSize());
	vst.RemoveAt(0);
	
	vector<StSimpleTypes>	vst2;	
	StSimpleTypes		st2;
	
	st2.nValue = 1;
	st2.rValue = 10.88;
	vst2.Add(st2);
	
	st2.nValue = 11;
	st2.rValue = 11.88;
	vst2.Add(st2);

	vst.Append(vst2);
}

For more examples.

ffor() multi-threaded for loop

ffor is a multithreaded faster for statement. Inside ffor can call external VC function:

//active matrix sheet should have 3 matrix object
// this will multiply the first two and put results on third one
void ffor_ex()
{
	MatrixPage mPage = Project.Pages();
	if(!mPage)
		return;
	MatrixLayer mlyr1 = mPage.Layers(0);
	Matrix matLayer1(mlyr1, 0);
	Matrix matLayer2(mlyr1, 1);
	Matrix matLayer3(mlyr1, 2);
	matrix mat1, mat2, mat3;
	mat1 = matLayer1;
	mat2 = matLayer2;
	mat3 = matLayer3;
	int nCols = mat1.GetNumCols();
	int nRows = mat1.GetNumRows();
	if(nRows != nCols)
		return;
	
	ffor(int nR; 0;  nRows)
	{
		ffor(int nC; 0; nRows)
		{
			double temp = 0;
			ffor(int k; 0; nRows)
			{
				double dmat = mat1[nR][k] * mat2[k][nC];
				AtomicAddDbl(&temp, dmat);
			}
			mat3[nR][nC] = temp;
		}
	}
	matLayer3 = mat3;
}

or call #pragma ffunc() function:

void ffor_ffunc_ex(int n = 5)
{
 	Profiler  junk;
 	
    vector v1;
    vector<int> v2;;
    vector<int> v3;;
	v1.Data(5, n+5);
	v2.Data(5, n+5);	
	v3.Data(5, n+5);	      
  	ffor(int ii; 0;  n)
  	{
   		v1[ii] = myAdd(v2[ii], v3[ii]);
   	}          
}

#pragma ffunc(1)
int myAdd(int &n1, int &n2)
{
	myDuplicate(n1);
	myDuplicate(n2);
	return n1 + n2;
}
void myDuplicate(int &n)
{  
	n *= 2;
}
#pragma ffunc(0)

keyword auto

Automatically deduce the data type. For more.

EX1

void auto_ex()
{
	auto v1 = 1; // int
	auto v2 = 3.14; // double
	auto v3 = "hello"; // const char*
	//auto v5 = 1, v6 = 3; // prevented, should not pass
}

EX2

vector GetVector() {vector vv;return vv;}
void auto_ex2()
{
	auto v7 = GetVector(); // vector, copy constructor
}

static_assert in OC compiler

Compile-time assertion. For more.

EX1

static_assert ( sizeof(int) == 4);	// pass, no message
static_assert ( sizeof(int) == 4 && 2 > 1, "pass 1"); // pass
static_assert ( sizeof(int) != 4, "fail 1");	// should fail

EX2

#define MY_CONST 4
void test()
{
	static_assert (MY_CONST != 4, "fail 2");	// should fail
}

EX3

int test_int()
{
	return 4;
}
static_assert ( test_int() == 4);	// should not compile, because the expression is not compile-time constant

nullptr for pointer

Representation of the null pointer.

void test_nullptr()
{
	Worksheet wks = Project.ActiveLayer();
	if(wks)
	{
		int r1, c1,r2, c2;
		wks.GetSelection(c1, c2, r1, r2, nullptr);
		printf("r1 = %d\tc1 = %d\tr2 = %d\tc2 = %d\n", r1, c1, r2, c2);
	}
}

json to/from string for matrix objects

void  testmatrix()
{
	matrix<double> mm, mm2;
	mm.SetSize(2, 3);
	int i;
	for (i = 0; i < 2; i++) {
		vector<double> vPara;
		vPara.Add(i+2);
		mm.SetRow(vPara, i);
	}
	string str;
	JSON.ToString(mm, str); 
	JSON.FromString(mm2, str); 
}

For more: FromString, ToString

copy constructor for vector

vector::vector

vector _get_data()
{
	vector vv(5);
	vv.Data(1.0, 10.0, 1.5);
	return vv;
}

void vector_ex()
{
	vector v1 = _get_data();
	vector v2 = v1;
	vector v3(v2);
}

switch() over string

switch

void switch_ex()
{	
	switch(string strInput = InputBox("Month(e.g., May):"))
	{
	case "March":
		out_int(strInput, 3);
		break;
	case "April":
		out_int(strInput, 4);
		break;
	case "May":
		out_int(strInput, 5);
		break;
	case "June":
		out_int(strInput, 6);
		break;
	case "July":
		out_int(strInput, 7);
		break;
	default:
		out_str("no match");
		break;
	}
}

for() support iterating over range

//for ([int k, string v] : map) {}
//for (auto& [k, v] : map) {}
void for_map_ex()
{
	omap<int, string> m1;
	m1.Set(100, "Hello");
	m1.Set(200, "World");
	m1.Set(300, "test");
	for ([int k, string v] : m1)
	{
		printf("%d == %s\r\n", k, v);
	}
	for (auto& [key, val], index: m1)
	{
		printf("[%d] %d -> %s\r\n", index, key, val);
	}
}

More examples.

init in if() and switch()

void init_in_if_ex()
{
	if (int x = rand()) 
		out_int("x=",x);
	else 
		out_str("int x = rand()");	
	//x = 0; // x in then/else only

	if (int a = 1, b = -1; a + b) // OK (init-statement may have several)
		out_str("if (int a = 1, b = 2; a + b)");
}

void init_in_switch_ex()
{
	switch (int n = mod(36, 10))
	{
	case 1: break;
	default: break;
	}

	string strList ="Confucius\\Mencius\\someone";
    string strDelimiter="\\";;
	switch (auto s = get_str_from_str_list(strList, strDelimiter, 1); s)
	{
	case "Mencius": break;
	default: break;
	}
}

For more: if, switch

Sticky Scroll in Code Builder

StickyscrollforCodeBuilder.gif

If you want to turn Sticky Scroll off, right-click at the Sticky Scroll header area and select Sticky Scroll. For more.

Code Builder support drag the file to other app

From Origin2027, Code Builder support drag the file from MDI or workspace tree to other app.


Use Profiler to identify the slower functions

Both "Start Profiler" under Debug menu and the Profiler class can show that.