|
|
Home » Developing U++ » U++ Developers corner » Question: Simple plugin implementation
Question: Simple plugin implementation [message #30804] |
Sun, 23 January 2011 22:20  |
 |
koldo
Messages: 3432 Registered: August 2008
|
Senior Veteran |
|
|
Hello all
I will ask you the question with an example based in OfficeAutomation:
There is an API of functions to handle spreadsheets. As they can be handled using OpenOffice, LibreOffice, Excel or other programs, the same API can be set for all programs. In run time it is possible to choose which program to use.
To do it, I have prepared something similar to the enclosed code. However, I think it is ugly and something much better could be done in U++ using C++.
Do you have any idea?
#include <Core/Core.h>
using namespace Upp;
class SpreadsheetPlugin {
public:
virtual bool Open(char *filename) {return false;};
virtual bool SetData(int row, int col, Value val) {return false;};
};
class OpenSpreadsheet : public SpreadsheetPlugin {
virtual bool Open(char *filename);
virtual bool SetData(int row, int col, Value val);
};
bool OpenSpreadsheet::Open(char *filename) {
// Do stuff
}
bool OpenSpreadsheet::SetData(int row, int col, Value val) {
// Do stuff
}
class ExcelSpreadsheet : public SpreadsheetPlugin {
virtual bool Open(char *filename);
virtual bool SetData(int row, int col, Value val);
};
bool ExcelSpreadsheet::Open(char *filename) {
// Do stuff
}
bool ExcelSpreadsheet::SetData(int row, int col, Value val) {
// Do stuff
}
class Spreadsheet : public SpreadsheetPlugin {
private:
SpreadsheetPlugin *data;
public:
Spreadsheet() {data = 0;};
~Spreadsheet() {
if (data)
delete data;
}
void Init(String type) {
if (type == "Open" || type == "Libre")
data = new OpenSpreadsheet();
else
data = new ExcelSpreadsheet();
}
virtual bool Open(char *filename) {return data->Open(filename);}
virtual bool SetData(int row, int col, Value val) {return data->SetData(row, col, val);}
};
CONSOLE_APP_MAIN
{
Spreadsheet spreadsheet;
spreadsheet.Init("Libre");
spreadsheet.Open("c:\\myfile.xls");
spreadsheet.SetData(4, 6, "Hello world");
}
Best regards
Iñaki
|
|
|
|
|
|
|
Re: Question: Simple plugin implementation [message #30873 is a reply to message #30862] |
Thu, 27 January 2011 10:34   |
 |
koldo
Messages: 3432 Registered: August 2008
|
Senior Veteran |
|
|
Hello Mirek
A little bit better (not as rich as StreamRaster, but clearer for me ). Added "new" instead of ugly global var:
#include <Core/Core.h>
using namespace Upp;
#include "Spreadsheet.h"
class ExcelSpreadsheet : public SpreadsheetPlugin {
Spreadsheet_METHOD_LIST
};
INITBLOCK {
RegisterPlugin<ExcelSpreadsheet>("Excel");
}
bool ExcelSpreadsheet::Open(const char *filename) {
puts("ExcelSpreadsheet::Open");
return false;
}
bool ExcelSpreadsheet::SetData(int row, int col, Value val) {
puts("ExcelSpreadsheet::SetData");
return false;
}
However all possible plugins are initialized, not just the one to be used.
It is the same in class StreamRaster (file Raster.h):
template <class T> static StreamRaster *FactoryFn() { return new T; }
Every registered class has to be initialized.
[Edit: Simplified INITBLOCK with templates]
Best regards
Iñaki
[Updated on: Thu, 27 January 2011 13:07] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Question: Simple plugin implementation [message #31186 is a reply to message #31175] |
Fri, 11 February 2011 22:47  |
 |
koldo
Messages: 3432 Registered: August 2008
|
Senior Veteran |
|
|
mirek wrote on Fri, 11 February 2011 16:50 | Well, but then you keep all plugins in memory, including all data in memory they needed for the last operation or you need to implement some sort of 'free' for them.
Allocating/deallocating memory is actually quite fast operation in U++.
|
Hello Mirek
It is only necessary to allocate the used plugin. It is done in
INITBLOCK {
RegisterPlugin<ExcelSpreadsheet>("Excel");
}
In fact it is interesting to have it allocated while the plugin is used as it can keep some variables.
For example for an spreadsheet, the Open() method load some variables that will be used by other methods like SetData(Value v, int row, int col).
Best regards
Iñaki
|
|
|
Goto Forum:
Current Time: Mon Apr 28 10:49:30 CEST 2025
Total time taken to generate the page: 0.00807 seconds
|
|
|