Volatile Function
Posted: Mon Jan 21, 2013 10:47 pm
Hello,
How do I define a volatile function in a C++ addin??
Thanks,
Saleem
How do I define a volatile function in a C++ addin??
Thanks,
Saleem
User community support forum for Apache OpenOffice, LibreOffice and all the OpenOffice.org derivatives
https://forum.openoffice.org/en/forum/
Code: Select all
#include <com/sun/star/uno/XInterface.idl>
#include <com/sun/star/lang/XInitialization.idl>
#include <com/sun/star/lang/XServiceName.idl>
#include <com/sun/star/lang/XLocalizable.idl>
#include <com/sun/star/sheet/XVolatileResult.idl>
module com {module volresult
{
interface XVolatile
{
string IncrementCounter( [in] string aName );
com::sun::star::sheet::XVolatileResult getCounter( [in] string aName );
};
service Volatile
{
interface XVolatile;
interface com::sun::star::sheet::XVolatileResult;
interface com::sun::star::lang::XInitialization;
interface com::sun::star::lang::XServiceName;
};};
};
Code: Select all
#include <cppuhelper/implbase1.hxx>
Code: Select all
typedef ::cppu::WeakImplHelper1< ::com::sun::star::sheet::XResultListener> ResultListenerHelper;
Code: Select all
class MyResultListener: public ResultListenerHelper{
public:
void SAL_CALL modified(const ResultEvent &) throw (::com::sun::star::uno::RuntimeException);
void SAL_CALL disposing(const EventObject &) throw (::com::sun::star::uno::RuntimeException);
};
void SAL_CALL MyResultListener::modified(const ResultEvent & aEvent) throw (::com::sun::star::uno::RuntimeException)
{
return;
}
void SAL_CALL MyResultListener::disposing(const EventObject & aEvent) throw (::com::sun::star::uno::RuntimeException)
{
return;
}
Code: Select all
typedef ::cppu::WeakImplHelper1< ::com::sun::star::sheet::XVolatileResult> VolatileResultHelper;
Code: Select all
class ExampleAddInResult: public VolatileResultHelper {
private:
OUString aName;
long nValue;
vector<Reference< XResultListener >> myListeners;
com::sun::star::sheet::ResultEvent getResult();
public:
ExampleAddInResult(OUString);
ExampleAddInResult();
void SAL_CALL addResultListener(const Reference<XResultListener> & aListener) throw (::com::sun::star::uno::RuntimeException);
void SAL_CALL removeResultListener(const Reference<XResultListener> & aListener) throw (::com::sun::star::uno::RuntimeException);
void incrementValue() {
long i;
++nValue;
com::sun::star::sheet::ResultEvent aEvent = getResult();
for(i = 0;i < myListeners.size(); i++)
myListeners[i]->modified(aEvent);
}
};
void SAL_CALL ExampleAddInResult::addResultListener(const Reference<XResultListener> & aListener) throw (::com::sun::star::uno::RuntimeException) {
myListeners.push_back(aListener);
// immediately notify of initial value
com::sun::star::sheet::ResultEvent aEvent = getResult();
myListeners.back()->modified(aEvent);
}
void SAL_CALL ExampleAddInResult::removeResultListener(const Reference<XResultListener> & aListener) throw (::com::sun::star::uno::RuntimeException) {
myListeners.pop_back();
}
Code: Select all
ExampleAddInResult::ExampleAddInResult(OUString aNewName)
{
aName = aNewName;
myListeners.reserve(10);
nValue = 0;
}
ExampleAddInResult::ExampleAddInResult()
{
aName = OUString();
nValue = 0;
}
Code: Select all
com::sun::star::sheet::ResultEvent ExampleAddInResult::getResult()
{
com::sun::star::sheet::ResultEvent aEvent;
OUString ou;
Reference< XInterface > x( static_cast< lang::XTypeProvider * >( this ) );
aEvent.Value <<= aName + OUString( RTL_CONSTASCII_USTRINGPARAM(" ") ) + ou.valueOf((sal_Int32) nValue);
aEvent.Source = x;
return aEvent;
}
Code: Select all
static unordered_map<OUString,ExampleAddInResult *,OUStringHash> aResults;
Code: Select all
unordered_map<OUString,ExampleAddInResult *,OUStringHash> VolatileImpl::aResults;
Code: Select all
Reference<XVolatileResult> SAL_CALL VolatileImpl::getCounter( OUString const & aName)
throw (RuntimeException)
{
ExampleAddInResult *aResult;
Reference<XVolatileResult> vResult;
MyResultListener *aListener = new MyResultListener;
Reference<XResultListener> xListener = static_cast< XResultListener * > ( aListener );
if(aResults.count(aName) == 0) {
aResult = new ExampleAddInResult(aName);
aResults[aName] = aResult;
} else {
aResult = aResults[aName];
}
vResult = static_cast< XVolatileResult * > (aResult);
return vResult;
}
Code: Select all
=GETCOUNTER("x")
Code: Select all
x 0
Code: Select all
void incrementValue() {
long i;
++nValue;
com::sun::star::sheet::ResultEvent aEvent = getResult();
for(i = 0;i < myListeners.size(); i++)
myListeners[i]->modified(aEvent);
}
Code: Select all
OUString SAL_CALL VolatileImpl::IncrementCounter( OUString const & aName)
throw (RuntimeException)
{
ExampleAddInResult *aResult;
if(aResults.count(aName) == 0) {
return aName + OUString(RTL_CONSTASCII_USTRINGPARAM(" is null."));
} else {
aResult = aResults[aName];
aResult->incrementValue();
return aName + OUString(RTL_CONSTASCII_USTRINGPARAM(" incremented."));
}
}
Code: Select all
=INCREMENTCOUNTER("x")
Code: Select all
Sub Main
IncIt("x")
End Sub
Sub IncIt(aName As String)
Dim svc As Object
svc = createUnoService("com.sun.star.sheet.FunctionAccess")
svc.callFunction("com.volresult.my_volatile_implementation.Volatile.IncrementCounter",Array(aName))
End Sub
Yes!saleem145 wrote:Can a volatile function return a 2 dimensional array of data??
Saleem
Code: Select all
com::sun::star::sheet::ResultEvent ExampleAddInResult::getResult()
{
com::sun::star::sheet::ResultEvent aEvent;
Sequence<Sequence<Any>> ResultArray(1);
ResultArray[0] = Sequence<Any>(2);
ResultArray[0][0] <<= aName;
ResultArray[0][1] <<= (sal_Int32) nValue;
aEvent.Value <<= ResultArray;
Reference< XInterface > x( static_cast< lang::XTypeProvider * >( this ) );
aEvent.Source = x;
return aEvent;
}
=TRANSPOSE(GETCOUNTER("x"))Charlie Young wrote:I'll leave it as an exercise to return the entries in a column.
Got me , that works.Villeroy wrote:=TRANSPOSE(GETCOUNTER("x"))