Explicit deletion of copy constructor and assignment in Pybind11 - copy

If you have a class with explicit deletion of the copy constructor and assignment operation as in the following example
Foo(const Foo& other) = delete;
Foo& operator=(const Foo& other) = delete;
how can you specify it in the Pybind11 wrapper for the class? Ultimately the purpose is to follow the intended use for the C++ class which enforces moving and forbids copying
Foo(const Foo& other) = delete;
Foo& operator=(const Foo& other) = delete;
Foo(Foo&& other) = default;
Foo& operator=(Foo&& other) = default;

Related

What is const & method in C++

I saw code like that.
What this & means and what is the official name of this kind of methods?
struct S{
int get() const &{
return 5;
}
};
int main(){
S s;
return s.get();
}
The const & on the method signature means that the calling object shall be bounded to a const lvalue reference.
However, since rvalue can be bound to a const lvalue ref, the following code would compile:
struct S{
int get() const &{
return 5;
}
};
int main(){
return S{}.get(); // compiles, S{} can be bound to const lvalue
}
To see the actual meaning of the & you can either add a version for && or drop the const:
With an overload for const &&:
struct S{
int get() const &{
return 5;
}
int get() const && {
return -5;
}
};
int main(){
return S{}.get(); // returns -5
}
With & but without const:
struct S{
int get() & {
return 5;
}
};
int main(){
return S{}.get(); // compilation error cannot bind rvalue to lvalue
}
The const means the function itself is const - it is a compile time error for that function to modify the member data items of the class/struct, and therefore the function can be called using a const reference to an instance of the class.
The trailing & is very unusual (I've never seen one in the wild). It means the reference to the instance that is used to invoke this function must be an lvalue. A function followed by && must be called from an rvalue.
I believe a function can be declared and defined twice, once with & and once with &&, as it forms part of the signature. This is useful for certain obscure optimizations (apparently).

What's the point of using the bitwise XOR operator "^" to get/generate the hashcode of an object?

While reading some code, it came to my attention that some developers use the bitwise XOR operator, ^, to generate the hashcode of an object.
What's the point of doing it like this? Does it have some advantages over other methods to get/generate the hashcode of an object?
Here is a code example.
class Student {
final String name;
final int age;
Student(this.name, this.age);
#override
bool operator ==(other) {
return (other is Student) && other.name == name && other.age == age;
}
#override
int get hashCode => age.hashCode ^ name.hashCode; // <-- Here
}
It has to be a single number, and the more it varies on more of the bits of any member of the object, the better.

Addition program in Drools

just out of curiosity i am trying to write a very simple rule in Drools of addition.(I know its not a very mature thing, but still...)
But getting error that $firstNum and $secondNum can't be assign to a variable. help me out...
rule "Addition rule"
when
act : CalcOperation(CalcOperation.ADD, $firstNum : firstNum, $secondNum : secondNum)
then
$out : $firstNum + $secondNum;;
//logger.info("Result of addition is : "+$out);
end
...............................................................................
Here is the CalcOperation class.
public class CalcOperation {
Double firstNum;
Double secondNum;
public static Boolean ADD;
public static Boolean SUB;
public static Boolean MUL;
public static Boolean DIV;
public CalcOperation(Boolean operation, Double m, Double n){
this.firstNum = m;
this.secondNum = n;
}
You'll need to dig more into Java, and a lot more into Drools documentation.
A static variable isn't useful in the context you are using it. If you want to have separate objects for addition, subtraction etc. you could subclass Operation. Instance variables should be private, and at least have a getter.
In a rule, you cannot bind a variable ($firstNum etc.) to a field unless it has a proper getter (or is public - not recommended).
The then-part or consequence must be written in Java. $out : $firstNum + $secondNum;; is not a valid Java statement.
Here's a Java class:
public enum Operator { ADD, SUB, MUL, DIV };
public class Operation {
private double op1;
private double op2;
private Operator op;
public Operation( Operator op, double op1, double op2 ){
this.op = op;
//...
}
public Operator getOp(){ return op; }
//...
}
And here's the rule:
rule "exec op"
when
$op: Operation( $op: Operator.ADD, $op1: op1, $op2: op2 )
then
System.out.writeln( "result: " + ($op1 + $op2) );
end

unmanaged var as member of managed class c++

I'm novice in .net c++ and trying to create class looking like:
public ref class Klient
{
public:
Klient(){}
// zmienne
static DWORD klienty[41][2];
static int i = 1;
static DWORD* pid;
static HANDLE* handle;
//funkcje
};
but MSV says that:
error C4368: cannot define 'klienty' as a member of managed 'Klient': mixed types are not supported
What's wrong with this code?
You can have .NET basic data types as members of your managed class (static int i), or pointers to anything unmanaged (DWORD* pid, HANDLE* handle), but you're not allowed to have an unmanaged object directly, and the array of integers counts as an unmanaged object for this purpose.
Since the only item giving you a problem here is the unmanaged array, you could switch it to a managed array.
public ref class Klient
{
public:
Klient(){}
// zmienne
static array<DWORD,2>^ klienty = gcnew array<DWORD,2>(41,2);
static int i = 1;
static DWORD* pid;
static HANDLE* handle;
//funkcje
};
Or, you can declare a unmanaged class, put whatever you need to in there, and have a pointer to it from the managed class. (If you do this in a non-static context, don't forget to delete the unmanaged memory from your finalizer.)
public class HolderOfUnmanagedStuff
{
public:
DWORD klienty[41][2];
int i;
DWORD* pid;
HANDLE* handle;
HolderOfUnmanagedStuff()
{
i = 1;
}
};
public ref class Klient
{
public:
Klient(){}
// zmienne
static HolderOfUnmanagedStuff* unmanagedStuff = new HolderOfUnmanagedStuff();
//funkcje
};

Case-insensitive indexing with Hibernate-Search?

Is there a simple way to make Hibernate Search to index all its values in lower case ? Instead of the default mixed-case.
I'm using the annotation #Field. But I can't seem to be able to configure some application-level set
Fool that I am ! The StandardAnalyzer class is already indexing in lowercase. It's just a matter of setting the search terms in lowercase too. I was assuming the query would do that.
However, if a different analyzer were to be used, application-wide, then it can be set using the property hibernate.search.analyzer.
Lowercasing, term splitting, removing common terms and many more advanced language processing functions are applied by the Analyzer.
Usually you should process user input meant to match indexed strings with the same Analyzer used at indexing; configuring hibernate.search.analyzer sets the default (global) Analyzer, but you can customize it per index, per entity type, per field and even on different entity instances.
It is for example useful to have language specific analysis, so to process Chinese descriptions with Chinese specific routines, Italian descriptions with Italian tokenizers.
The default analyzer is ok for most use cases, and does lowercasing and splits terms on whitespace.
Consider as well that when using the Lucene Queryparser the API requests you the appropriate Analyzer.
When using the Hibernate Search QueryBuilder it attempts to apply the correct Analyzer on each field; see also http://docs.jboss.org/hibernate/search/4.1/reference/en-US/html_single/#search-query-querydsl .
There are multiple way to make sort insensitive in string type field only.
1.First Way is add #Fields annotation in field/property on entity.
Like
#Fields({#Field(index=Index.YES,analyze=Analyze.YES,store=Store.YES),
#Field(index=Index.YES,name = "nameSort",analyzer = #Analyzer(impl=KeywordAnalyzer.class), store = Store.YES)})
private String name;
suppose you have name property with custom analyzer and sort on that. so it's not possible then you can add new Field in index with nameSort apply sort on that field.
you must apply Keyword Analyzer class because that is not tokeniz field and by default apply lowercase factory class in field.
2.Second way is that you can implement your comparison class on sorting like
#Override
public FieldComparator newComparator(String field, int numHits, int sortPos, boolean reversed) throws IOException {
return new StringValComparator(numHits, field);
}
Make one class with extend FieldComparatorSource class and implement above method.
Created new Class name with StringValComparator and implements FieldComparator
and implement following method
class StringValComparator extends FieldComparator {
private String[] values;
private String[] currentReaderValues;
private final String field;
private String bottom;
StringValComparator(int numHits, String field) {
values = new String[numHits];
this.field = field;
}
#Override
public int compare(int slot1, int slot2) {
final String val1 = values[slot1];
final String val2 = values[slot2];
if (val1 == null) {
if (val2 == null) {
return 0;
}
return -1;
} else if (val2 == null) {
return 1;
}
return val1.toLowerCase().compareTo(val2.toLowerCase());
}
#Override
public int compareBottom(int doc) {
final String val2 = currentReaderValues[doc];
if (bottom == null) {
if (val2 == null) {
return 0;
}
return -1;
} else if (val2 == null) {
return 1;
}
return bottom.toLowerCase().compareTo(val2.toLowerCase());
}
#Override
public void copy(int slot, int doc) {
values[slot] = currentReaderValues[doc];
}
#Override
public void setNextReader(IndexReader reader, int docBase) throws IOException {
currentReaderValues = FieldCache.DEFAULT.getStrings(reader, field);
}
#Override
public void setBottom(final int bottom) {
this.bottom = values[bottom];
}
#Override
public String value(int slot) {
return values[slot];
}
}
Apply sorting on Fields Like
new SortField("name",new StringCaseInsensitiveComparator(), true);