I am trying to override the default on_erase signal handler of Gtk::TextView. I derive TextBuffer and override the default handler; but the override is not working. When using cast_static, the RefPtr is valid, but when using cast_dynamic the RefPtr is null.
#include <gtkmm.h>
#include "CTextBuffer.h"
class CMain : public Gtk::Window
{
public:
CMain();
virtual ~CMain();
private:
Gtk::TextView m_textView;
Glib::RefPtr<CTextBuffer> m_refTextBuffer;
};
#include "CMain.h"
CMain::CMain()
{
Glib::RefPtr<Gtk::TextBuffer> refTextBuffer = Gtk::TextBuffer::create();
m_refTextBuffer = Glib::RefPtr<CTextBuffer>::cast_dynamic(refTextBuffer);
m_textView.set_buffer(m_refTextBuffer);
add(m_textView);
show_all();
}
CMain::~CMain()
{}
#include <gtkmm.h>
class CTextBuffer : public Gtk::TextBuffer
{
public:
CTextBuffer();
virtual ~CTextBuffer();
protected:
virtual void on_erase(const TextBuffer::iterator& range_start, const TextBuffer::iterator& range_end);
};
#include "CTextBuffer.h"
CTextBuffer::CTextBuffer() {}
CTextBuffer::~CTextBuffer() {}
void CTextBuffer::on_erase (const TextBuffer::iterator& range_start, const TextBuffer::iterator& range_end)
{
Gtk::TextBuffer::on_erase(range_start, range_end);
}
Simply use a ref to your own type:
CMain::CMain()
{
Glib::RefPtr<CTextBuffer> refTextBuffer{ new CTextBuffer };
m_textView.set_buffer(refTextBuffer);
add(m_textView);
show_all();
}
void CTextBuffer::on_erase (const TextBuffer::iterator& range_start, const TextBuffer::iterator& range_end)
{
std::cout << "Own override was called" << std::endl;
Gtk::TextBuffer::on_erase(range_start, range_end);
}
You find some more information on how to usw own derived widgets here:
https://developer.gnome.org/gtkmm-tutorial/stable/sec-builder-using-derived-widgets.html.en
I am trying to set a delegate that takes a local variable as a parameter. The declaration looks like this:
ref class Main
{
private:
Func<String^>^ _expensiveMethodDelegate;
public:
property Func<String^>^ ExpensiveMethodDelegate
{
Func<String^>^ get() { return this->_expensiveMethodDelegate; }
void set(Func<String^>^ value) { this->_expensiveMethodDelegate = value; }
};
void DoWork()
{
String^ result = this->_expensiveMethodDelegate();
Debug::WriteLine(result);
}
};
In C# the code will look like this:
string parameter = "value";
Main main = new Main();
main.ExpensiveMethodDelegate = () =>
{
Thread.Sleep(1000); // do expensive work
return parameter + "1";
};
main.DoWork();
What is the easiest way of achieving this goal using managed C++ (VS 2015)? Note: I read articles Workaround for not having lambdas that can capture managed variables and Lambda expressions as CLR (.NET) delegates / event handlers in Visual C++ 2010 and still cannot figure out what is the solution.
I tried code like this (using make_delegate from the second article), but it fails to compile:
String^ parameter = L"value";
Main^ main = gcnew Main();
main->ExpensiveMethodDelegate = make_delegate(
[](String^ parameter) -> String^
{
Threading::Thread::Sleep(1000); // do work
return parameter + L"1";
});
main->DoWork();
This is what I came up with:
#pragma once
#include <new>
using namespace std::tr1;
using namespace System;
namespace helper
{
private struct return_type_helper
{
private:
template<class D>
struct dependent_false { enum { value = false }; };
template <class D>
struct illegal_delegate_type
{
static_assert(dependent_false<D>::value, "Delegates with more than 2 parameters, or with parameters of tracking reference types (T%), are not supported.");
};
struct anything
{
template<class T>
operator T() const;
};
public:
template<class D>
static decltype(static_cast<D^>(nullptr)(anything())) dummy(int(*)[2]);
template<class D>
static decltype(static_cast<D^>(nullptr)(anything(), anything())) dummy(int(*)[3]);
};
template<class Func, class Aligner = char, bool Match = (alignment_of<Func>::value == alignment_of<Aligner>::value)>
struct aligner
{
static_assert(Match, "Function object has unsupported alignment");
};
template<class Func, class Aligner>
private struct aligner<Func, Aligner, true>
{
typedef Aligner type;
};
template<class F>
private ref class lambda_wrapper
{
public:
lambda_wrapper(const F& f)
{
pin_ptr<F> pf = (interior_ptr<F>)&f_storage;
new(pf) F(f);
}
~lambda_wrapper()
{
pin_ptr<F> pf = (interior_ptr<F>)&f_storage;
pf->~F();
}
template <class D>
operator D ^ ()
{
D^ d = nullptr;
return gcnew D(this, &lambda_wrapper<F>::invoke<decltype(return_type_helper::dummy<D>(0))>);
}
private:
template<class T>
[System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential, Size = sizeof(T))]
value struct embedded_storage
{
private:
typename aligner<T>::type dummy;
};
embedded_storage<F> f_storage;
template<class R, class A1>
R invoke(A1 a1)
{
pin_ptr<F> pf = (interior_ptr<F>)&f_storage;
return (*pf)(a1);
}
template<class R, class A1, class A2>
R invoke(A1 a1, A2 a2)
{
pin_ptr<F> pf = (interior_ptr<F>)&f_storage;
return (*pf)(a1, a2);
}
};
template <typename...>
ref class DelegateHelper;
template<class TParam1, class TResult>
ref class DelegateHelper<TParam1, TResult>
{
private:
Func<TParam1, TResult>^ _lambda;
TParam1 _param1;
TResult Execute()
{
return this->_lambda(this->_param1);
}
public:
template<class TLambda>
DelegateHelper(TLambda lambda, TParam1 param1)
{
this->_lambda = gcnew helper::lambda_wrapper<TLambda>(lambda);
this->_param1 = param1;
}
static operator Func<TResult> ^ (DelegateHelper<TParam1, TResult>^ value)
{
return gcnew Func<TResult>(value, &DelegateHelper<TParam1, TResult>::Execute);
}
};
template<class TParam1, class TParam2, class TResult>
ref class DelegateHelper<TParam1, TParam2, TResult>
{
private:
Func<TParam1, TParam2, TResult>^ _lambda;
TParam1 _param1;
TParam2 _param2;
TResult Execute()
{
return this->_lambda(this->_param1, this->_param2);
}
public:
template<class TLambda>
DelegateHelper(TLambda lambda, TParam1 param1, TParam2 param2)
{
this->_lambda = gcnew helper::lambda_wrapper<TLambda>(lambda);
this->_param1 = param1;
this->_param2 = param2;
}
static operator Func<TResult> ^ (DelegateHelper<TParam1, TParam2, TResult>^ value)
{
return gcnew Func<TResult>(value, &DelegateHelper<TParam1, TParam2, TResult>::Execute);
}
};
}
This is how to use it:
String^ parameter1 = L"value1";
String^ parameter2 = L"value2";
Main^ main = gcnew Main();
auto lambda1 = [](String^ parameter) -> String^
{
Threading::Thread::Sleep(1000);
return parameter;
};
main->ExpensiveMethodDelegate = gcnew helper::DelegateHelper<String^, String^>(lambda1, parameter1);
main->DoWork();
auto lambda2 = [](String^ parameter1, String^ parameter2) -> String^
{
Threading::Thread::Sleep(1000);
return parameter1 + parameter2;
};
main->ExpensiveMethodDelegate = gcnew helper::DelegateHelper<String^, String^, String^>(lambda2, parameter1, parameter2);
main->DoWork();
Not sure if it is the most elegant way, but it does the work I was looking for.
I have a c++ code that needs to be called in c# by a cli wrapper. I am stuck at the operator overloading part.
//this is my code
//Cppclass.h
#ifdef CPP_EXP_EXPORTS
#define CPP_EXP __declspec(dllexport)
#else
#define CPP_EXP __declspec(dllimport)
#endif
class CPP_EXP Cppclass;
CPP_EXP Cppclass operator-(Cppclass const &, Cppclass const &);
class CPP_EXP Cppclass
{
public:
friend CPP_EXP Cppclass operator-(Cppclass const &, Cppclass const &);
};
//this is my managed c++ code.
#include "Cppclass.h"
namespace Wrapper
{
public ref class cppwrapclass
{
public:
static cppwrapclass ^ operator-(cppwrapclass%A,cppwrapclass%B)
{
operator-(A.obj,B.obj);
return gcnew cppwrapclass();
}
private:
Cppclass *obj;
};
}
Its showing an intellisense error and not getting compiled.
You write a wrapper like this:
public ref class cppwrapclass
{
public:
cppwrapclass(Cppclass *obj)
: obj(obj)
{
}
~cppwrapclass()
{
this->!cppwrapclass();
}
!cppwrapclass()
{
if (obj)
{
delete obj;
obj = nullptr;
}
}
static cppwrapclass^ operator-(cppwrapclass% A, cppwrapclass% B)
{
return gcnew cppwrapclass(new Cppclass(*A.obj - *B.obj));
}
private:
Cppclass* obj;
};
A.obj is of type Cppclass*, therefore *A.obj is of type Cppclass, and *A.obj - *B.obj is a temporary Cppclass which needs to be moved to the heap in order to be referenced by the wrapper, hence the copy constructor call: new Cppclass(*A.obj - *B.obj).
The rest is the Dispose pattern and finalizer plumbing code.
I'm trying to call some functions from GenDList.h inside GenQueue.h after including, but I'm getting a couple errors.
This is my header, GenDList.h:
#ifndef GENDLIST_H
#define GENDLIST_H
#include <iostream>
#include "GenListNode.h"
using namespace std;
template <class T>
class GenDList
{
public:
GenListNode<T> *front;
GenListNode<T> *back;
unsigned int size;
GenDList();
~GenDList();
void insertFront(T data);
void insertBack(T data);
bool insertAfter(int key, T data);
T removeFront();
T removeBack(); //for doubly
T removeAt(int position);
int isEmpty();
unsigned int getSize();
void printList();
T find(T d);
};
#endif
This is my other header that I included the previous header file into, GenQueue.h:
#ifndef GENQUEUE_H
#define GENQUEUE_H
#include <iostream>
#include "GenDList.h"
using namespace std;
template <class T>
class GenQueue
{
public:
GenQueue();
~GenQueue();
void insert(T data);
T remove();
T peek();
int isEmpty();
int isFull();
};
#endif
//***********implementation***************
template <class T>
GenQueue<T>::GenQueue()
{
GenDList<T> list;
}
template <class T>
GenQueue<T>::~GenQueue()
{
}
template <class T>
void GenQueue<T>::insert(T data)
{
GenDList<T>::insertBack(data);
}//error: call to non-static member function without an object argument
template <class T>
T GenQueue<T>::remove()
{
GenDList<T>::removeFront();
}
template <class T>
T GenQueue<T>::peek()
{
return front; //error: use of undeclared identifier here
}
Serializing simple class "A" in Apache module done without error but when I tried to serialize my complex object like "X" which has a member, type of "A", I got segfault in Apache module. ( this doesn't happen to a executable console application )
------------------------- here is my code : ---------------------
class A {
private:
friend class boost::serialization::access; // to enable boost "access" class to call private "serialize" method of class "A"
template<class ArchT>
void serialize(ArchT &ar, unsigned int version) { // method for both serializing and deserializing
ar & memA; // (de)serialize member "memA"
}
std::string memA; // sample member
public:
A(){}
A(std::string pmemA) :
memA(pmemA) {
}
std::string GetMemA()
{
return memA;
}
};
class X {
private:
friend class boost::serialization::access;
template<class ArchT>
void serialize(ArchT &ar, unsigned int version) {
ar & memX;
ar & a;
}
std::string memX;
A a;
public:
X(std::string pmemX, A pa) :
memX(pmemX), a(pa) {
}
X(){}
};
-------------------
string st=GetRandomFileName();
ofstream out(st.c_str());
boost::archive::text_oarchive out_r(out);
A a("Amem");
X x("Xmem", a);
out_r << x; // not works
out_r << a; // works!
------------------- here is stack trace from gdb for apache ----------------
boost::serialization::typeid_system::extended_type_info_typeid_0::is_less_than(boost::serialization::extended_type_info const&) const () from /tmp/libIRSProWebApacheModule.so
2 0xb7223c61 in std::_Rb_tree