tvg format: code refactoring #8
authorHermet Park <chuneon.park@samsung.com>
Tue, 20 Jul 2021 11:03:45 +0000 (20:03 +0900)
committerJunsuChoi <jsuya.choi@samsung.com>
Thu, 22 Jul 2021 08:24:22 +0000 (17:24 +0900)
Hide iterator APIs, simplify it as much as it's necessary.

inc/thorvg.h
src/lib/tvgCommon.h
src/lib/tvgPaint.cpp
src/lib/tvgPaint.h
src/lib/tvgPictureImpl.h
src/lib/tvgSaverImpl.h
src/lib/tvgScene.cpp
src/lib/tvgSceneImpl.h
src/lib/tvgShapeImpl.h

index a411e63..308a109 100644 (file)
@@ -43,7 +43,9 @@ protected: \
 #define _TVG_DECLARE_ACCESSOR() \
     friend Canvas; \
     friend Scene; \
-    friend Picture
+    friend Picture; \
+    friend Saver;
+
 
 #define _TVG_DECALRE_IDENTIFIER() \
     auto id() const { return _id; } \
@@ -57,6 +59,7 @@ class RenderMethod;
 class Scene;
 class Picture;
 class Canvas;
+class Saver;
 
 /**
  * @defgroup ThorVG ThorVG
@@ -300,51 +303,6 @@ public:
     uint8_t opacity() const noexcept;
 
     /**
-     * @brief Const forward iterator-like class enabling the iteration over the children nodes of the given paint.
-     *
-     * For the Scene-type Paint the children nodes represent the paints pushed into the Scene - the order of the children nodes is the same as the order as they were pushed. For the Picture-type Paint the child node is the read image in one of the supported formats. The Shape-type Paint doesn't have any child nodes.
-     *
-     * @BETA_API
-     */
-    class Iterator
-    {
-        const Paint* parent;
-        const Paint* child;
-
-        public:
-            Iterator (Paint* p = nullptr, Paint* c = nullptr);
-            const Paint& operator*() const;
-            Iterator& operator++();
-            Iterator operator++(int);
-            friend bool operator!=(const Iterator& it1, const Iterator& it2)
-            {
-                return it1.child != it2.child;
-            };
-            friend bool operator==(const Iterator& it1, const Iterator& it2)
-            {
-                return it1.child == it2.child;
-            };
-    };
-
-    /**
-     * @brief Gets the iterator to the first child node.
-     *
-     * @return The iterator pointing to the first element of the children nodes of the given paint object.
-     *
-     * @BETA_API
-     */
-    Iterator begin() const noexcept;
-
-    /**
-     * @brief Gets the iterator to the past-the end child node.
-     *
-     * @return The iterator referring to the past-the-end element of the children nodes of the given paint object.
-     *
-     * @BETA_API
-     */
-    Iterator end() const noexcept;
-
-    /**
      * @brief Gets the composition target object and the composition method.
      *
      * @param[out] target The paint of the target object.
index 6ef28b3..2fed7b3 100644 (file)
@@ -45,4 +45,5 @@ using namespace tvg;
     #define TVG_UNUSED __attribute__ ((__unused__))
 #endif
 
-#endif //_TVG_COMMON_H_
+
+#endif //_TVG_COMMON_H_
\ No newline at end of file
index fc5c741..7c539fc 100644 (file)
@@ -330,45 +330,4 @@ Result Paint::opacity(uint8_t o) noexcept
 uint8_t Paint::opacity() const noexcept
 {
     return pImpl->opacity;
-}
-
-
-Paint::Iterator Paint::begin() const noexcept
-{
-    return pImpl->begin();
-}
-
-
-Paint::Iterator Paint::end() const noexcept
-{
-    return Paint::Iterator();
-}
-
-
-Paint::Iterator::Iterator(Paint* p, Paint* c) : parent{p}, child{c}
-{
-}
-
-
-const Paint& Paint::Iterator::operator*() const
-{
-    return *child;
-}
-
-
-Paint::Iterator& Paint::Iterator::operator++() //prefix
-{
-    const Paint* nextChild = nullptr;
-    if (parent) nextChild = parent->pImpl->next(child);
-    child = nextChild;
-
-    return *this;
-}
-
-
-Paint::Iterator Paint::Iterator::operator++(int) //postfix
-{
-    Iterator tmp = *this;
-    ++*this;
-    return tmp;
-}
+}
\ No newline at end of file
index f0b5c6c..6ce5d1f 100644 (file)
 
 namespace tvg
 {
+    struct Iterator
+    {
+        virtual ~Iterator() {}
+        virtual const Paint* next() = 0;
+    };
+
     struct StrategyMethod
     {
         virtual ~StrategyMethod() {}
@@ -37,8 +43,7 @@ namespace tvg
         virtual bool bounds(float* x, float* y, float* w, float* h) const = 0;
         virtual RenderRegion bounds(RenderMethod& renderer) const = 0;
         virtual Paint* duplicate() = 0;
-        virtual Paint::Iterator begin() = 0;
-        virtual const Paint* next(const Paint* p) = 0;
+        virtual Iterator* iterator() = 0;
     };
 
     struct Paint::Impl
@@ -98,6 +103,11 @@ namespace tvg
             return smethod->dispose(renderer);
         }
 
+        Iterator* iterator()
+        {
+            return smethod->iterator();
+        }
+
         bool composite(Paint* target, CompositeMethod method)
         {
             if ((!target && method != CompositeMethod::None) || (target && method == CompositeMethod::None)) return false;
@@ -113,17 +123,6 @@ namespace tvg
         void* update(RenderMethod& renderer, const RenderTransform* pTransform, uint32_t opacity, Array<RenderData>& clips, uint32_t pFlag);
         bool render(RenderMethod& renderer);
         Paint* duplicate();
-
-        Paint::Iterator begin()
-        {
-            return smethod->begin();
-        }
-
-        const Paint* next(const Paint* p)
-        {
-            return smethod->next(p);
-        }
-
     };
 
 
@@ -165,16 +164,10 @@ namespace tvg
             return inst->duplicate();
         }
 
-        Paint::Iterator begin() override
+        Iterator* iterator() override
         {
-            return inst->begin();
+            return inst->iterator();
         }
-
-        const Paint* next(const Paint* p) override
-        {
-            return inst->next(p);
-        }
-
     };
 }
 
index 1178afb..40e48e9 100644 (file)
 /* Internal Class Implementation                                        */
 /************************************************************************/
 
+struct PictureIterator : Iterator
+{
+    Paint* paint = nullptr;
+
+    PictureIterator(Paint* p) : paint(p)
+    {
+    }
+
+    const Paint* next() override
+    {
+        auto ret = paint;
+        paint = nullptr;
+        return ret;
+    }
+};
+
+
 struct Picture::Impl
 {
     shared_ptr<Loader> loader = nullptr;
@@ -219,15 +236,10 @@ struct Picture::Impl
         return ret.release();
     }
 
-    Paint::Iterator begin()
+    Iterator* iterator()
     {
         reload();
-        return Paint::Iterator(picture, paint);
-    }
-
-    const Paint* next(const Paint* p)
-    {
-        return nullptr;
+        return new PictureIterator(paint);
     }
 };
 
index eef0830..eafd19f 100644 (file)
@@ -347,13 +347,16 @@ struct Saver::Impl
 
     ByteCounter serializeChildren(const Paint* paint)
     {
-        if (!paint) return 0;
         ByteCounter dataByteCnt = 0;
 
-        for (auto it = paint->begin(); it != paint->end(); ++it) {
-            dataByteCnt += serialize(&(*it));
+        auto it = paint->pImpl->iterator();
+
+        while (auto p = it->next()) {
+            dataByteCnt += serialize(p);
         }
 
+        delete(it);
+
         return dataByteCnt;
     }
 
index 08cc743..798f164 100644 (file)
@@ -25,7 +25,7 @@
 /* External Class Implementation                                        */
 /************************************************************************/
 
-Scene::Scene() : pImpl(new Impl(this))
+Scene::Scene() : pImpl(new Impl())
 {
     _id = TVG_CLASS_ID_SCENE;
     Paint::pImpl->method(new PaintMethod<Scene::Impl>(pImpl));
@@ -67,4 +67,4 @@ Result Scene::clear(bool free) noexcept
     pImpl->clear(free);
 
     return Result::Success;
-}
+}
\ No newline at end of file
index eb062be..9b14db8 100644 (file)
 /* Internal Class Implementation                                        */
 /************************************************************************/
 
+struct SceneIterator : Iterator
+{
+    Array<Paint*>* paints;
+    uint32_t idx = 0;
+
+    SceneIterator(Array<Paint*>* p) : paints(p)
+    {
+    }
+
+    const Paint* next() override
+    {
+        if (idx >= paints->count) return nullptr;
+        return paints->data[idx++];
+    }
+};
+
 struct Scene::Impl
 {
     Array<Paint*> paints;
-    Scene* scene = nullptr;
-    uint8_t opacity;            //for composition
+    uint8_t opacity;                     //for composition
     RenderMethod* renderer = nullptr;    //keep it for explicit clear
 
     ~Impl()
@@ -43,10 +58,6 @@ struct Scene::Impl
         }
     }
 
-    Impl(Scene* s) : scene(s)
-    {
-    }
-
     bool dispose(RenderMethod& renderer)
     {
         for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
@@ -187,21 +198,9 @@ struct Scene::Impl
         renderer = nullptr;
     }
 
-    Paint::Iterator begin()
+    Iterator* iterator()
     {
-        if (paints.count > 0) return Paint::Iterator(scene, *(paints.data));
-        return Paint::Iterator();
-    }
-
-    const Paint* next(const Paint* p)
-    {
-        for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
-            auto tmp = paint;
-            if (*paint == p && ++tmp < (paints.data + paints.count)) {
-                return *tmp;
-            }
-        }
-        return nullptr;
+        return new SceneIterator(&paints);
     }
 };
 
index f0ea0c9..fca452b 100644 (file)
@@ -391,12 +391,7 @@ struct Shape::Impl
         return ret.release();
     }
 
-    Paint::Iterator begin()
-    {
-        return Paint::Iterator();
-    }
-
-    const Paint* next(const Paint* p)
+    Iterator* iterator()
     {
         return nullptr;
     }