View as source file or XML.
// NOTE: THIS IS NOT THE ACTUAL API -- IT'S A "WHAT IF" API class Stemmer { public: virtual ~Stemmer(); // ... }; class StemmerProvider { public: // ... Stemmer* getStemmer( locale::iso639_1::type lang ) const = 0; };and that you implemented it like this:
class MyStemmerProvider : public StemmerProvider { public: Stemmer* getStemmer( locale::iso639_1::type lang ) const; }; Stemmer* MyStemmerProvider::getStemmer( locale::iso639_1::type lang ) const { MyStemmer stemmer; return &stemmer; }Assume that your stemmer is used for all languages and that it maintains no state. For efficiency, you could therefore use a singleton instance of it. The problem is that Zorba can't tell the difference between a non-singleton (dynamically allocated) instance and a singleton (statically allocated) instance, i.e., whether to call delete on it or not. (In C++, there is no built-in way to determine whether a pointer points to an object that is statically allocated or was dynamically allocated.)
class Stemmer { public: typedef /* implementation-defined */ ptr; virtual void destroy() const = 0; // ... protected: virtual ~Stemmer(); }; class StemmerProvider { public: // ... Stemmer::ptr getStemmer( locale::iso639_1::type lang ) const = 0; };The changes are:
class MyStemmer { public: void destroy(); // ... private: MyStemmer(); friend class MyStemmerProvider; // only it can create instances }; void MyStemmer::destroy() const { // Do nothing since our stemmer is statically allocated. } class MyStemmerProvider : public StemmerProvider { public: Stemmer::ptr getStemmer( locale::iso639_1::type lang ) const; }; Stemmer::ptr MyStemmerProvider::getStemmer( locale::iso639_1::type lang ) const { static MyStemmer stemmer; return Stemmer::ptr( &stemmer ); }then it would work for a statically allocated instance of your stemmer. On the other hand, if your StemmerProvider dynamically allocates instances, then your implementation should be like this:
void MyStemmer::destroy() const { delete this; } Stemmer::ptr MyStemmerProvider::getStemmer( locale::iso639_1::type lang ) const { return Stemmer::ptr( new MyStemmer ); }