Jump to content

[SOLVED] add custom operator support of extern class in C++ api


photo

Recommended Posts

Posted

I don't know if there is someone need this, but any script library should support custom operator export with c++.

 

Situation is I'm creating a date class to engine with boost::date_time, it's very convenient for any date plus some day or minus some day to get a new date with simple +/- operator. But uniginescript don't support operator export. So I have to write like

date d = new date("2012-2-29");
d.add(1);

 

although this can do most work, but if I want to add some day to a date and get a new date without change the original date class, this can not be done:

date d1 = new date("2012-2-29");
// want d2 to be 2012-3-1 while d1 still be 2012-2-29
date d2 = d1 +1;

 

any script library like lua/python all support of custom operator. So I need unigine also support this.

Posted

and I don't think this is a very complex work, like Variable class already support in script class's operator. so adding this to extern class won't be a huge work.

Posted

What about the following solution:

class date {
 date() { datee = new date_extern(); }
 ~date() { delete datee; }
 date operator+(date d0,date d1) {
date ret = new date();
ret.datee.add(d0.datee,d1.datee);
return ret;
 }
 date_extern datee;
};

Posted (edited)

I've thought about this when I post this thread, but this will require more action to be done, any project need date operation will need the date plugin and a script class, but before custom operator available to c++, this is the only way to make it work.

 

EDIT:

already tried with this, I need to write a wrapper class for whole c++ exported date class again in uniginescript...

 

I still have to say, support user class operator while not support c++ operator to script engine is not a so "powerful" script engine.

Edited by steve3d
Posted

after some test and deep thought about this approach, this won't work at all. one big problem is user class don't support overloaded constructor.. and there is no assign operator( = operator).

 

so big problem if you create the class like this

class date {
 date(string ds) {
//...
 }
}

 

then you can only use a string to create a new date, and these code will be wrong

date d0 = new date("2012-2-20");
// create a not-a-date is not possible without overloaded constructor
// below line won't work
// date d1 = new date(); 
date d1 = d0;
d1 = d1 + 1;
// after this, right, d1 is 2012-2-21, but d1 is a reference to d0, so d0 also becomes 2012-2-21

 

this will cause a big problem!

Posted

Implementation of custom c++ operators is a simple task. But in this case you should respond to memory management and It will be not easy task.

For example if you have an operator+ for two objects which returns a new object. Who should delete this object? Or this object should be deleted automatically via reference counting system?

Posted

like the boost::python did, it support both handling way of new object delete, the way of deleting the newly created object is specified with the export template function in c++.

 

Just leave it to user to decide this, you can set the new object to be deleted via reference counting system or by user himself.

 

EDIT:

 

need more about this, operator overload, adding two date is really impossible (it's useless to add 2012-2-20 with 2012-3-20, right? ), but in my case a date will need add weeks, months, years. so operator+ should support overload. ( this really useful to add 2 weeks, 3 months, years or more simple days to a date)

 

this should also applies to operator- . subtraction two date is meaningful.

Posted

It is not a rational idea since you will have to create multiple additional classes like Minute, Hour, Day, etc. to implement this feature in the object-oriented style.

Date date = new Date("2012");
date += new Minute(1);
date += new Hour(1);

In this case you operator+ should recognise different types of arguments (in pseudo syntax):

Date *operator+(Date *date,const Variable &v) {
 if(v.isMinute()) date += v.getMinute();
 if(v.isHour()) date += v.getHour();
}

I think this is a much harder than create several functions like addMinute/addHour into the Date class.

Posted

I think we are talking about custom operator export, not how to create complete date calendar system, as to the hour mins things, they are not exist in boost::date_time for time calculation, there is time_duration class which has microsecond and nanosecond support. Adding months and weeks can be easily convert to adding days, date calculation don't need to care about hours , if you need hours and seconds , then use boost::posix_time. Please check www.boost.org for the details.

 

But I really need the custom operator even without the overloaded operator.

Posted

You can achieve absolutely the same result via:

  • User class wrapper for C++ extern class
  • Functions instead of operators

We'll consider your request, but there's far too many features to be implemented from a feature list, sorry.

Posted

OK, after a deeper thought, I think there is no need to support a overloaded operator support in C++, just use Unigine::Variable as the operator argument, then this will solve the overloaded operator thing, in operator, user need to respond of distinguish the different type.

 

I think this is more simpler thing.

 

Well if you guys decided to not support this, I'll write a simpler date class by script.

Posted

still need this, I've just created a script version of date class, but it still need a overloaded subtract operator, because there is no variable class in script, that Unigine::Variable only exists in c++ part, so a date can be subtracted by a int (days) or another date to get the days in between.

 

but with script class, it can not use variable as argument, so write code like

date d0 = new date("2012-2-23");
date d2 = new date("2011-2-23");

date d3 = d0 - 2; // to get 2012-2-21
int days = d0.daysBetween(d2);
/* that can not use
int days = d0 - d2;
*/

 

all this mathematical or logical operation must be used as a named function like add(), sub(), greater()... really looks ridiculous.

 

and to frustum, I saw in script, all operator returned a new variable class are all managed by the script's garbage collector, then why in c++ part, these new object should be user's respond to manage it's delete? it should be all managed by the garbage collector.

Posted

Bar is a wrapper for Foo class. You can simplify you mathematical operations if you want:

 

class Foo {
int a;
Foo() { }
Foo(int a_) { a = a_; }
Foo add(Foo f) { return new Foo(a + f.a); }
};

Foo f0 = new Foo(12);
Foo f1 = new Foo(13);
Foo f2 = f0.add(f1);
printf("%d\n",f2.a);

class Bar {
Foo foo;
Bar(int a) {
	if(is_int(a)) foo = new Foo(a);
	else foo = a;
}
Bar operator+(Bar b0,Bar b1) { return new Bar(b0.foo.add(b1.foo)); }
};

Bar b0 = new Bar(12);
Bar b1 = new Bar(13);
Bar b2 = b0 + b1;
printf("%d\n",b2.foo.a);

Posted

Thanks again. And I'm already created a simple date class without any c++ plugin, the only problem is that I can not use date a - date b and date a-int days together.

 

But that's not a big problem, because dates subtraction is only usefull to interpolate two dates, so I created an interpolate function directly do this.

 

But I still hope unigine can support custom overloaded operator in c++ export

Posted

Sorry, not planned for the near future. Since there's an efficient workaround, we better focus on other issues from a wish list.

×
×
  • Create New...