/** @file * @author Edouard DUPIN * @copyright 2010, Edouard DUPIN, all right reserved * @license GPL v3 (see license file) */ #pragma once #include #include #include #include #include namespace appl { template class TextViewerPluginData : public appl::TextViewerPlugin { protected: TextViewerPluginData() { // nothing to do ... addObjectType("appl::TextViewerPluginData"); } public: DECLARE_FACTORY(TextViewerPluginData); virtual ~TextViewerPluginData() { for (size_t iii = 0; iii < m_specificData.size() ; ++iii) { if (m_specificData[iii].second != null) { remove(*m_specificData[iii].second); } } m_specificData.clear(); } private: etk::Vector ,ememory::UniquePtr>> m_specificData; protected: TYPE* getDataRef(appl::TextViewer& _textDrawer) { auto it = m_specificData.begin(); while(it != m_specificData.end()) { ememory::SharedPtr buf = it->first.lock(); if (buf == null) { it = m_specificData.erase(it); continue; } if (buf == _textDrawer.internalGetBuffer()) { return it->second.get(); } ++it; } ememory::UniquePtr data(ETK_NEW(TYPE)); if (data == null) { APPL_ERROR("ALLOCATION plugin data error"); return null; } TYPE* copyPocalPointer = data.get(); ememory::WeakPtr tmpBuffer = _textDrawer.internalGetBuffer(); m_specificData.pushBack(etk::makePair(tmpBuffer, etk::move(data))); // create a new one ... return copyPocalPointer; } protected: // Wrap all element with their internal data: (do not use theses function) bool onReceiveShortCut(appl::TextViewer& _textDrawer, const etk::String& _shortCutName) { TYPE* data = getDataRef(_textDrawer); if (data == null) { return false; } return onDataReceiveShortCut(_textDrawer, _shortCutName, *data); } bool onWrite(appl::TextViewer& _textDrawer, const appl::Buffer::Iterator& _pos, const etk::String& _data) { TYPE* data = getDataRef(_textDrawer); if (data == null) { return false; } return onDataWrite(_textDrawer, _pos, _data, *data); } bool onReplace(appl::TextViewer& _textDrawer, const appl::Buffer::Iterator& _pos, const etk::String& _data, const appl::Buffer::Iterator& _posEnd) { TYPE* data = getDataRef(_textDrawer); if (data == null) { return false; } return onDataReplace(_textDrawer, _pos, _data, _posEnd, *data); } bool onRemove(appl::TextViewer& _textDrawer, const appl::Buffer::Iterator& _pos, const appl::Buffer::Iterator& _posEnd) { TYPE* data = getDataRef(_textDrawer); if (data == null) { return false; } return onDataRemove(_textDrawer, _pos, _posEnd, *data); } public: virtual bool onDataReceiveShortCut(appl::TextViewer& _textDrawer, const etk::String& _shortCutName, TYPE& _data) { return false; } virtual bool onDataWrite(appl::TextViewer& _textDrawer, const appl::Buffer::Iterator& _pos, const etk::String& _strData, TYPE& _data) { return false; } virtual bool onDataReplace(appl::TextViewer& _textDrawer, const appl::Buffer::Iterator& _pos, const etk::String& _strData, const appl::Buffer::Iterator& _posEnd, TYPE& _data) { return false; } virtual bool onDataRemove(appl::TextViewer& _textDrawer, const appl::Buffer::Iterator& _pos, const appl::Buffer::Iterator& _posEnd, TYPE& _data) { return false; } virtual void remove(TYPE& _data) { return; }; }; }