tvg_loader: introduce tvg interpreter base class for future extension.

tvg binary format might break the compatibility if any major features have been changed.
It's allowed to do it when the major version is upgraded.

In that case, still we need to support the backward compatibility,
we can provide multiple binary interpreters and choose the proper one
based on the current loading tvg binary format version.

Thus, you can add further interpreters if it's necessary in the future.
Our policy is to derive the TvgBinInterpreterBase class to make it running on
the interface.

for example, if the major version is upgraded 1.x, you can implement TvgBinInterpreter1.
This commit is contained in:
Hermet Park 2021-08-04 20:53:51 +09:00 committed by Hermet Park
parent d0efdb9854
commit 78f6f9d896
5 changed files with 44 additions and 14 deletions

View file

@ -1,8 +1,8 @@
source_file = [
'tvgTvgLoader.h',
'tvgTvgCommon.h',
'tvgTvgLoader.cpp',
'tvgTvgLoadParser.h',
'tvgTvgLoadParser.cpp',
'tvgTvgBinInterpreter.cpp',
]
subloader_dep += [declare_dependency(

View file

@ -19,9 +19,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <memory.h>
#include "tvgTvgLoadParser.h"
#include "tvgTvgCommon.h"
/************************************************************************/
@ -465,7 +464,7 @@ static Paint* _parsePaint(TvgBinBlock baseBlock)
/* External Class Implementation */
/************************************************************************/
unique_ptr<Scene> tvgLoadData(const char *ptr, const char* end)
unique_ptr<Scene> TvgBinInterpreter::run(const char *ptr, const char* end)
{
auto scene = Scene::gen();
if (!scene) return nullptr;

View file

@ -20,8 +20,8 @@
* SOFTWARE.
*/
#ifndef _TVG_TVG_LOAD_PARSER_H_
#define _TVG_TVG_LOAD_PARSER_H_
#ifndef _TVG_TVG_COMMON_H_
#define _TVG_TVG_COMMON_H_
#include "tvgCommon.h"
#include "tvgBinaryDesc.h"
@ -30,6 +30,25 @@
#define READ_UI32(dst, src) memcpy(dst, (src), sizeof(uint32_t))
#define READ_FLOAT(dst, src) memcpy(dst, (src), sizeof(float))
unique_ptr<Scene> tvgLoadData(const char* ptr, const char* end);
#endif //_TVG_TVG_LOAD_PARSER_H_
/* Interface for Tvg Binary Interpreter */
class TvgBinInterpreterBase
{
public:
virtual ~TvgBinInterpreterBase() {}
/* ptr: points the tvg binary body (after header)
end: end of the tvg binary data */
virtual unique_ptr<Scene> run(const char* ptr, const char* end) = 0;
};
/* Version 0 */
class TvgBinInterpreter : public TvgBinInterpreterBase
{
public:
unique_ptr<Scene> run(const char* ptr, const char* end) override;
};
#endif //_TVG_TVG_COMMON_H_

View file

@ -19,24 +19,28 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <fstream>
#include <memory.h>
#include <fstream>
#include "tvgLoader.h"
#include "tvgTvgLoader.h"
#include "tvgTvgLoadParser.h"
/************************************************************************/
/* Internal Class Implementation */
/************************************************************************/
void TvgLoader::clear()
{
if (copy) free((char*)data);
ptr = data = nullptr;
size = 0;
copy = false;
if (interpreter) {
delete(interpreter);
interpreter = nullptr;
}
}
@ -64,6 +68,9 @@ bool TvgLoader::readHeader()
READ_FLOAT(&h, ptr);
ptr += SIZE(float);
//Decide the proper Tvg Binary Interpreter based on the current file version
if (this->version >= 0) interpreter = new TvgBinInterpreter;
return true;
}
@ -175,7 +182,9 @@ bool TvgLoader::close()
void TvgLoader::run(unsigned tid)
{
if (root) root.reset();
root = tvgLoadData(ptr, data + size);
root = interpreter->run(ptr, data + size);
if (!root) clear();
}

View file

@ -24,6 +24,8 @@
#define _TVG_TVG_LOADER_H_
#include "tvgTaskScheduler.h"
#include "tvgTvgCommon.h"
class TvgLoader : public LoadModule, public Task
{
@ -33,6 +35,7 @@ public:
uint32_t size = 0;
uint16_t version = 0;
unique_ptr<Scene> root = nullptr;
TvgBinInterpreterBase* interpreter = nullptr;
bool copy = false;
~TvgLoader();