Ghislain Fourny has proposed merging lp:~zorba-coders/zorba/jsoniq-static-casts 
into lp:zorba.

Requested reviews:
  Markos Zaharioudakis (markos-za)
  Matthias Brantner (matthias-brantner)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/jsoniq-static-casts/+merge/118961

Simplified JSON items class and fixed some Xml Node static casts to handle JSON 
items as well.
-- 
https://code.launchpad.net/~zorba-coders/zorba/jsoniq-static-casts/+merge/118961
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'src/store/naive/json_items.cpp'
--- src/store/naive/json_items.cpp	2012-07-11 15:38:39 +0000
+++ src/store/naive/json_items.cpp	2012-08-09 13:45:44 +0000
@@ -76,6 +76,119 @@
 
 /////////////////////////////////////////////////////////////////////////////////
 //                                                                             //
+//  JSON Item                                                                  //
+//                                                                             //
+/////////////////////////////////////////////////////////////////////////////////
+/******************************************************************************
+
+*******************************************************************************/
+JSONItem::~JSONItem()
+{
+  delete theTree;
+}
+
+/******************************************************************************
+
+*******************************************************************************/
+const simplestore::Collection* JSONItem::getCollection() const
+{
+  if (theTree == NULL)
+  {
+    return NULL;
+  }
+  return theTree->getCollection();
+}
+
+
+/******************************************************************************
+
+*******************************************************************************/
+const TreeId& JSONItem::getTreeId() const
+{
+  ZORBA_ASSERT(theTree);
+  return theTree->getTreeId();
+}
+
+
+/******************************************************************************
+
+*******************************************************************************/
+JSONItem* JSONItem::getRoot() const
+{
+  ZORBA_ASSERT(theTree);
+  return theTree->getRoot();
+}
+
+/******************************************************************************
+
+*******************************************************************************/
+void JSONItem::free()
+{
+  destroy();
+}
+
+/******************************************************************************
+
+*******************************************************************************/
+void JSONItem::destroy()
+{
+  delete this;
+}
+
+/*******************************************************************************
+
+********************************************************************************/
+void JSONItem::fix(Collection* aCollection, const TreeId& aTreeId)
+{
+  ASSERT_INVARIANT();
+  
+  assert(aCollection);
+
+  // Attach
+  assert(getTree() == NULL);
+  setTree(new JSONTree());
+  getTree()->setRoot(this);
+  getTree()->setCollection(aCollection);
+  getTree()->setTreeId(aTreeId);
+  
+  ASSERT_INVARIANT();
+}
+
+/*******************************************************************************
+
+********************************************************************************/
+void JSONItem::unfix()
+{
+  ASSERT_INVARIANT();
+  
+  JSONTree* lTree = getTree();
+  // Detach
+  assert(lTree);
+  delete lTree;
+  setTree(NULL);
+  return;
+
+  ASSERT_INVARIANT();
+}
+
+#ifndef NDEBUG
+/******************************************************************************
+
+*******************************************************************************/
+void JSONItem::assertInvariant() const
+{
+  if (theTree != NULL)
+  {
+    assert(theTree->getCollection() != NULL);
+    assert(theTree->getRoot() != NULL);
+    assert(isThisTreeOfAllDescendants(theTree));
+    assert(theTree->getRoot()->isThisJSONItemInDescendance(this));
+  }
+}
+#endif
+
+/////////////////////////////////////////////////////////////////////////////////
+//                                                                             //
 //  Object                                                                     //
 //                                                                             //
 /////////////////////////////////////////////////////////////////////////////////
@@ -93,36 +206,6 @@
 /******************************************************************************
 
 *******************************************************************************/
-void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot)
-{
-  if (aJSONItem->isJSONObject())
-  {
-    assert(dynamic_cast<SimpleJSONObject*>(aJSONItem));
-    SimpleJSONObject* lObject = static_cast<SimpleJSONObject*>(aJSONItem);
-
-    // Only attach or detach allowed - no direct reattach.
-    assert(aRoot == NULL || lObject->theRoot == NULL);
-    lObject->setRoot(aRoot);
-  }
-  else if (aJSONItem->isJSONArray())
-  {
-    assert(dynamic_cast<SimpleJSONArray*>(aJSONItem));
-    SimpleJSONArray* lArray = static_cast<SimpleJSONArray*>(aJSONItem);
-
-    // Only attach or detach allowed - no direct reattach.
-    assert(aRoot == NULL || lArray->theRoot == NULL);
-    lArray->setRoot(aRoot);
-  } 
-  else
-  {
-    assert(false);
-  }
-}
-
-
-/******************************************************************************
-
-*******************************************************************************/
 SimpleJSONObject::~SimpleJSONObject()
 {
   ASSERT_INVARIANT();
@@ -134,7 +217,9 @@
     store::Item* lChild = lIter->second;
     if (getCollection() != NULL && lChild->isJSONItem())
     {
-      setJSONRoot(lChild, NULL);
+      assert(dynamic_cast<JSONItem*>(lChild));
+      JSONItem* lJSONItem = static_cast<JSONItem*>(lChild);
+      lJSONItem->setTree(NULL);
     }
     lName->removeReference();
     lChild->removeReference();
@@ -212,7 +297,9 @@
 
     if (getCollection() != NULL && aValue->isJSONItem())
     {
-      setJSONRoot(lValue, theRoot);
+      assert(dynamic_cast<JSONItem*>(aValue.getp()));
+      JSONItem* lJSONItem = static_cast<JSONItem*>(aValue.getp());
+      lJSONItem->setTree(getTree());
     }
     
     csize lPosition = thePairs.size();
@@ -244,7 +331,7 @@
 
       if (getCollection() != NULL)
       {
-        setJSONRoot(array.getp(), theRoot);
+        array->setTree(getTree());
       }
 
       lValue->removeReference();
@@ -285,7 +372,9 @@
 
   if (getCollection() != NULL && lValue->isJSONItem())
   {
-    setJSONRoot(lValue.getp(), NULL);
+    assert(dynamic_cast<JSONItem*>(lValue.getp()));
+    JSONItem* lJSONItem = static_cast<JSONItem*>(lValue.getp());
+    lJSONItem->setTree(NULL);
   }
 
   lKey->removeReference();
@@ -331,7 +420,7 @@
 
       if (getCollection() != NULL && lValue->isJSONItem())
       {
-        setJSONRoot(lValue.getp(), NULL);
+        lValue->setTree(NULL);
       }
 
       // Erasing the corresponding entries.
@@ -381,12 +470,16 @@
   {
     if (lOldValue->isJSONItem())
     {
-      setJSONRoot(lOldValue.getp(), NULL);
+      assert(dynamic_cast<JSONItem*>(lOldValue.getp()));
+      JSONItem* lJSONItem = static_cast<JSONItem*>(lOldValue.getp());
+      lJSONItem->setTree(NULL);
     }
 
     if (aValue->isJSONItem())
     {
-      setJSONRoot(aValue.getp(), theRoot);
+      assert(dynamic_cast<JSONItem*>(aValue.getp()));
+      JSONItem* lJSONItem = static_cast<JSONItem*>(aValue.getp());
+      lJSONItem->setTree(getTree());
     }
   }
 
@@ -441,75 +534,21 @@
 /******************************************************************************
 
 *******************************************************************************/
-void SimpleJSONObject::setRoot(const JSONItem* aRoot)
+void SimpleJSONObject::setTree(JSONTree* aTree)
 {
-  theRoot = aRoot;
+  theTree = aTree;
 
   for (Pairs::iterator lIter = thePairs.begin();
        lIter != thePairs.end();
        ++lIter)
   {
     store::Item* lValue = lIter->second;
-    if (lValue->isJSONObject())
-    {
-      assert(dynamic_cast<SimpleJSONObject*>(lValue));
-      SimpleJSONObject* lObject = static_cast<SimpleJSONObject*>(lValue);
-
-      lObject->setRoot(aRoot);
-    }
-    else if (lValue->isJSONArray())
-    {
-      assert(dynamic_cast<SimpleJSONArray*>(lValue));
-      SimpleJSONArray* lArray = static_cast<SimpleJSONArray*>(lValue);
-      
-      lArray->setRoot(aRoot);
-    }
-  }
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void SimpleJSONObject::setCollection(SimpleCollection* collection, xs_integer /*pos*/)
-{
-  ASSERT_INVARIANT();
-  // Ensures one either detaches or attaches.
-  assert(collection == NULL || theCollection == NULL);
-
-  theCollection = collection;
-  
-  if (theCollection != NULL)
-  {
-    // Attach
-    setRoot(this);
-  }
-  else 
-  {
-    // Detach
-    setRoot(NULL);
-  }
-  
-  ASSERT_INVARIANT();
-}
-
-
-/******************************************************************************
-
-*******************************************************************************/
-const store::Collection* SimpleJSONObject::getCollection() const
-{
-  if (theRoot == this)
-  {
-    return theCollection;
-  }
-  else if (theRoot != NULL)
-  {
-    return theRoot->getCollection();
-  }
-  else
-  {
-    return NULL;
+    if (lValue->isJSONItem())
+    {
+      assert(dynamic_cast<JSONItem*>(lValue));
+      JSONItem* lJSONItem = static_cast<JSONItem*>(lValue);
+      lJSONItem->setTree(aTree);
+    }
   }
 }
 
@@ -590,34 +629,7 @@
 *******************************************************************************/
 void SimpleJSONObject::assertInvariant() const
 {
-  // Note: only root objects may point to a collection, so if theCollection ==
-  // NULL, it doesn't mean that the object does not belong to a collection.
-  assert(theCollection == NULL || theRoot == this);
-
-  if (theRoot != NULL)
-  {
-    const store::Collection* lCollection = getCollection();
-    assert(lCollection != NULL);
-
-    const SimpleJSONObject* lObject =
-        dynamic_cast<const SimpleJSONObject*>(theRoot);
-    const SimpleJSONArray* lArray =
-        dynamic_cast<const SimpleJSONArray*>(theRoot);
-
-    assert(lObject != NULL || lArray != NULL);
-
-    if (lObject != NULL) 
-    {
-      assert(lObject->isThisRootOfAllDescendants(theRoot));
-      assert(lObject->isThisJSONItemInDescendance(this));
-    }
-    else
-    {
-      assert(lArray->isThisRootOfAllDescendants(theRoot));
-      assert(lArray->isThisJSONItemInDescendance(this));
-    }
-  }
-
+  JSONItem::assertInvariant();
   assert(theKeys.size() == thePairs.size());
 
   for(Keys::iterator lIter = theKeys.begin();
@@ -637,9 +649,9 @@
 /******************************************************************************
 
 *******************************************************************************/
-bool SimpleJSONObject::isThisRootOfAllDescendants(const store::Item* aRoot) const
+bool SimpleJSONObject::isThisTreeOfAllDescendants(const JSONTree* aTree) const
 {
-  if (theRoot != aRoot)
+  if (theTree != aTree)
   {
     return false;
   }
@@ -649,16 +661,8 @@
        ++lIter)
   {
     store::Item* lValue = lIter->second;
-    const SimpleJSONObject* lObject =
-      dynamic_cast<const SimpleJSONObject*>(lValue);
-    const SimpleJSONArray* lArray =
-      dynamic_cast<const SimpleJSONArray*>(lValue);
- 
-   if (lObject != NULL && (!lObject->isThisRootOfAllDescendants(aRoot)))
-    {
-      return false;
-    }
-    else if (lArray != NULL && (!lArray->isThisRootOfAllDescendants(aRoot)))
+    const JSONItem* lJSONItem = dynamic_cast<const JSONItem*>(lValue);
+    if (lJSONItem != NULL && (!lJSONItem->isThisTreeOfAllDescendants(aTree)))
     {
       return false;
     }
@@ -783,7 +787,9 @@
   {
     if (getCollection() != NULL && (*lIter)->isJSONItem())
     {
-      setJSONRoot(*lIter, NULL);
+      assert(dynamic_cast<JSONItem*>(*lIter));
+      JSONItem* lJSONItem = static_cast<JSONItem*>(*lIter);
+      lJSONItem->setTree(NULL);
     }
     (*lIter)->removeReference();
   }
@@ -799,7 +805,9 @@
 
   if (getCollection() != NULL && aValue->isJSONItem())
   {
-    setJSONRoot(aValue.getp(), theRoot);
+    assert(dynamic_cast<JSONItem*>(aValue.getp()));
+    JSONItem* lJSONItem = static_cast<JSONItem*>(aValue.getp());
+    lJSONItem->setTree(getTree());
   }
 
   aValue->addReference();
@@ -844,7 +852,9 @@
 
   if (getCollection() != NULL && member->isJSONItem())
   {
-    setJSONRoot(member.getp(), theRoot);
+    assert(dynamic_cast<JSONItem*>(member.getp()));
+    JSONItem* lJSONItem = static_cast<JSONItem*>(member.getp());
+    lJSONItem->setTree(getTree());
   }
 
   member->addReference();
@@ -903,7 +913,9 @@
 
     if (getCollection() != NULL && lItem->isJSONItem())
     {
-      setJSONRoot(lItem, theRoot);
+      assert(dynamic_cast<JSONItem*>(lItem));
+      JSONItem* lJSONItem = static_cast<JSONItem*>(lItem);
+      lJSONItem->setTree(getTree());
     }
 
     lItem->addReference();
@@ -924,7 +936,9 @@
 
   if (getCollection() != NULL && lItem->isJSONItem())
   {
-    setJSONRoot(lItem.getp(), NULL);
+    assert(dynamic_cast<JSONItem*>(lItem.getp()));
+    JSONItem* lJSONItem = static_cast<JSONItem*>(lItem.getp());
+    lJSONItem->setTree(NULL);
   }
 
   lItem->removeReference();
@@ -948,14 +962,18 @@
 
   if (getCollection() != NULL && lItem->isJSONItem())
   {
-    setJSONRoot(lItem.getp(), NULL);
+    assert(dynamic_cast<JSONItem*>(lItem.getp()));
+    JSONItem* lJSONItem = static_cast<JSONItem*>(lItem.getp());
+    lJSONItem->setTree(NULL);
   }
 
   uint64_t pos = cast(aPos) - 1;
 
   if (getCollection() != NULL && value->isJSONItem())
   {
-    setJSONRoot(value.getp(), theRoot);
+    assert(dynamic_cast<JSONItem*>(value.getp()));
+    JSONItem* lJSONItem = static_cast<JSONItem*>(value.getp());
+    lJSONItem->setTree(getTree());
   }
 
   theContent[pos]->removeReference();
@@ -970,27 +988,20 @@
 /******************************************************************************
 
 *******************************************************************************/
-void SimpleJSONArray::setRoot(const JSONItem* aRoot)
+void SimpleJSONArray::setTree(JSONTree* aTree)
 {
-  theRoot = aRoot;
+  theTree = aTree;
 
   for (Members::const_iterator lIter = theContent.begin();
        lIter != theContent.end();
        ++lIter)
   {
-    if ((*lIter)->isJSONObject())
-    {
-      assert(dynamic_cast<SimpleJSONObject*>(*lIter));
-      SimpleJSONObject* lObject = static_cast<SimpleJSONObject*>(*lIter);
-
-      lObject->setRoot(aRoot);
-    }
-    else if ((*lIter)->isJSONArray())
-    {
-      assert(dynamic_cast<SimpleJSONArray*>(*lIter));
-      SimpleJSONArray* lArray = static_cast<SimpleJSONArray*>(*lIter);
-      
-      lArray->setRoot(aRoot);
+    store::Item* lValue = *lIter;
+    if (lValue->isJSONItem())
+    {
+      assert(dynamic_cast<JSONItem*>(lValue));
+      JSONItem* lJSONItem = static_cast<JSONItem*>(lValue);
+      lJSONItem->setTree(aTree);
     }
   }
 }
@@ -1137,100 +1148,25 @@
 }
 
 
-/*******************************************************************************
-
-********************************************************************************/
-void SimpleJSONArray::setCollection(SimpleCollection* collection, xs_integer /*pos*/)
-{
-  ASSERT_INVARIANT();
-  // Ensures one either detaches or attaches.
-  ZORBA_ASSERT(collection == NULL || theCollection == NULL);
-
-  theCollection = collection;
-  
-  if (theCollection != NULL)
-  {
-    // Attach
-    setRoot(this);
-  }
-  else
-  {
-    // Detach
-    setRoot(NULL);
-  }
-  
-  ASSERT_INVARIANT();
-}
-
-
-/******************************************************************************
-
-*******************************************************************************/
-const store::Collection* SimpleJSONArray::getCollection() const
-{
-  if (theRoot == this)
-  {
-    return theCollection;
-  }
-  else if (theRoot != NULL)
-  {
-    return theRoot->getCollection();
-  }
-  else
-  {
-    return NULL;
-  }
-}
-
-
 #ifndef NDEBUG
 
 /******************************************************************************
 
 *******************************************************************************/
-void SimpleJSONArray::assertInvariant() const
-{
-  assert(theCollection == NULL || theRoot == this);
-  if (theRoot != NULL)
-  {
-    const store::Collection* lCollection = getCollection();
-    assert(lCollection != NULL);
-    const SimpleJSONObject* lObject = dynamic_cast<const SimpleJSONObject*>(theRoot);
-    const SimpleJSONArray* lArray = dynamic_cast<const SimpleJSONArray*>(theRoot);
-    assert(lObject != NULL || lArray != NULL);
-    if (lObject != NULL) {
-      assert(lObject->isThisRootOfAllDescendants(theRoot));
-      assert(lObject->isThisJSONItemInDescendance(this));
-    } else {
-      assert(lArray->isThisRootOfAllDescendants(theRoot));
-      assert(lArray->isThisJSONItemInDescendance(this));
-    }
-  }
-}
-
-
-/******************************************************************************
-
-*******************************************************************************/
-bool SimpleJSONArray::isThisRootOfAllDescendants(const store::Item* aRoot) const
-{
-  if(theRoot != aRoot)
+bool SimpleJSONArray::isThisTreeOfAllDescendants(const JSONTree* aTree) const
+{
+  if (getTree() != aTree)
   {
     return false;
   }
+
   for (Members::const_iterator lIter = theContent.begin();
        lIter != theContent.end();
        ++lIter)
   {
-    const SimpleJSONObject* lObject =
-        dynamic_cast<const SimpleJSONObject*>(*lIter);
-    const SimpleJSONArray* lArray =
-        dynamic_cast<const SimpleJSONArray*>(*lIter);
-    if (lObject != NULL && (!lObject->isThisRootOfAllDescendants(aRoot)))
-    {
-      return false;
-    }
-    else if (lArray != NULL && (!lArray->isThisRootOfAllDescendants(aRoot)))
+    store::Item* lValue = (*lIter);
+    const JSONItem* lJSONItem = dynamic_cast<const JSONItem*>(lValue);
+    if (lJSONItem != NULL && (!lJSONItem->isThisTreeOfAllDescendants(aTree)))
     {
       return false;
     }

=== modified file 'src/store/naive/json_items.h'
--- src/store/naive/json_items.h	2012-07-11 15:38:39 +0000
+++ src/store/naive/json_items.h	2012-08-09 13:45:44 +0000
@@ -87,6 +87,52 @@
 
 *******************************************************************************/
 
+class JSONTree
+{
+private:
+  simplestore::Collection* theCollection;
+  TreeId      theId;
+  JSONItem*   theRoot;
+
+public:
+  JSONTree() : theCollection(NULL), theId(), theRoot(NULL)
+  {}
+
+  simplestore::Collection* getCollection() const
+  {
+    return theCollection;
+  }
+
+  void setCollection(simplestore::Collection* aCollection)
+  {
+    theCollection = aCollection;
+  }
+
+  const TreeId& getTreeId() const
+  {
+    return theId;
+  }
+
+  void setTreeId(const TreeId& aId)
+  {
+    theId = aId;
+  }
+
+  JSONItem* getRoot() const
+  {
+    return theRoot;
+  }
+
+  void setRoot(JSONItem* aRoot)
+  {
+    theRoot = aRoot;
+  }
+};
+
+/******************************************************************************
+
+*******************************************************************************/
+
 class JSONItem : public store::Item
 {
 protected:
@@ -95,10 +141,19 @@
 public:
   SYNC_CODE(RCLock* getRCLock() const { return &theRCLock; })
 
-  JSONItem() : store::Item(JSONIQ) {}
-
-  virtual ~JSONItem() {}
-
+  JSONItem() : store::Item(JSONIQ), theTree(NULL) {}
+
+  virtual ~JSONItem();
+  
+  // Returns NULL if in no collection.
+  const simplestore::Collection* getCollection() const;
+  // These two functions are only to be called if in a collection.
+  JSONItem* getRoot() const;
+  const TreeId& getTreeId() const;
+  
+  void fix(Collection* aCollection, const TreeId& aTreeId);
+  void unfix();
+  
   // store API
 
   virtual bool equals(
@@ -109,10 +164,27 @@
     return this == other;
   }
   
-  // store methods
-  // store methods
-
-  virtual const JSONItem* getRoot() = 0;
+  virtual void free();
+  virtual void destroy();
+  
+  // Internal tree management methods
+  virtual void setTree(JSONTree* aTree) = 0;
+  JSONTree* getTree() const
+  {
+    return theTree;
+  }
+
+protected:
+  JSONTree* theTree;
+
+public:
+#ifndef NDEBUG
+  virtual void assertInvariant() const;
+
+  virtual bool isThisTreeOfAllDescendants(const JSONTree* aTree) const = 0;
+
+  virtual bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const = 0;
+#endif
 };
 
 
@@ -156,8 +228,6 @@
   virtual bool rename(
     const store::Item_t& aName,
     const store::Item_t& aNewName) = 0;
-    
-  virtual void setCollection(SimpleCollection* collection, xs_integer pos) = 0;
 };
 
 
@@ -191,19 +261,10 @@
       virtual void close();
   };
 
-protected:
-
-  Keys                 theKeys;
-  Pairs                thePairs;
-  store::Collection  * theCollection;
-  const JSONItem     * theRoot;
-
 public:
   SimpleJSONObject()
     :
-    theKeys(64, false),
-    theCollection(NULL),
-    theRoot(NULL)
+    theKeys(64, false)
   {
   }
 
@@ -227,8 +288,6 @@
 
   virtual void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
 
-  virtual const store::Collection* getCollection() const;
-
   // updates
   
   virtual bool add(
@@ -246,25 +305,21 @@
       const store::Item_t& aName,
       const store::Item_t& aNewName);
 
-  virtual void setCollection(SimpleCollection* collection, xs_integer pos);
-  
   // root management
   
 protected:
-  friend void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
-
-  void setRoot(const JSONItem* aRoot);
-
-  const JSONItem* getRoot() { return theRoot; }
-  
+  void setTree(JSONTree* aTree);
+
+private:
+  Keys   theKeys;
+  Pairs  thePairs;
+
   // Invariant handling
-protected:
-  friend class SimpleJSONArray;
-
+public:
 #ifndef NDEBUG
   void assertInvariant() const;
-
-  bool isThisRootOfAllDescendants(const store::Item* aRoot) const;
+  
+  bool isThisTreeOfAllDescendants(const JSONTree* aTree) const;
 
   bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const;
 #endif
@@ -324,7 +379,6 @@
   virtual store::Item_t
   replace(const xs_integer& pos, const store::Item_t& value) = 0;
 
-  virtual void setCollection(SimpleCollection* collection, xs_integer pos) = 0;
 };
 
 
@@ -358,18 +412,9 @@
       virtual void close();
   };
 
-protected:
-  Members                   theContent;
-  const store::Collection * theCollection;
-  const JSONItem          * theRoot;
-
 public:
   SimpleJSONArray()
-    :
-    theCollection(NULL),
-    theRoot(NULL)
-  {
-  }
+  {}
 
   virtual ~SimpleJSONArray();
   
@@ -393,8 +438,6 @@
 
   void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
 
-  virtual const store::Collection* getCollection() const;
-  
   // updates
   
   virtual void
@@ -421,38 +464,29 @@
   virtual store::Item_t
   replace(const xs_integer& aPos, const store::Item_t& value);
 
-  void setCollection(SimpleCollection* collection, xs_integer pos);
-
   // root management
-protected:
-  friend void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
-
-  void setRoot(const JSONItem* aRoot);
-
-  const JSONItem* getRoot() { return theRoot; }
-  
+public:
+  void setTree(JSONTree* aTree);
+
 protected:
   void add(uint64_t pos, const std::vector<store::Item_t>& aNewMembers);
 
   static uint64_t cast(const xs_integer& i);
   
+private:
+  Members theContent;
+
   // Invariant handling
-protected:
-  friend class SimpleJSONObject;
-
+public:
 #ifndef NDEBUG
-  void assertInvariant() const;
-
-  bool isThisRootOfAllDescendants(const store::Item* aRoot) const;
+  bool isThisTreeOfAllDescendants(const JSONTree* aTree) const;
 
   bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const;
 #endif
 };
 
 
-void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
-    
-#if 0 // ifndef NDEBUG
+#ifndef NDEBUG
 #define ASSERT_INVARIANT() assertInvariant()
 #else
 #define ASSERT_INVARIANT()

=== modified file 'src/store/naive/pul_primitives.cpp'
--- src/store/naive/pul_primitives.cpp	2012-07-24 08:48:48 +0000
+++ src/store/naive/pul_primitives.cpp	2012-08-09 13:45:44 +0000
@@ -164,8 +164,11 @@
     theParent->deleteChild(*this);
   }
   
-  static_cast<zorba::simplestore::XmlNode*>(theTarget.getp())
+  if (theTarget->isNode())
+  {
+    static_cast<zorba::simplestore::XmlNode*>(theTarget.getp())
         ->unregisterReferencesToDeletedSubtree();
+  }
 }
 
 
@@ -993,9 +996,21 @@
 
   for (uint64_t i = 0; i < size; ++i)
   {
-    XmlNode* root = static_cast<XmlNode*>(collection->nodeAt(xs_integer(i)).getp());
-    XmlTree* tree = root->getTree();
-    if (tree->getRefCount() > 1)
+    long lRefCount = 0;
+    store::Item* lItem = collection->nodeAt(xs_integer(i)).getp();
+    if (lItem->isNode())
+    {
+      assert(dynamic_cast<XmlNode*>(lItem));
+      XmlNode* lNode = static_cast<XmlNode*>(lItem);
+      lRefCount = lNode->getTree()->getRefCount();
+#ifdef ZORBA_WITH_JSON
+    } else if (lItem->isJSONItem()) {
+      assert(dynamic_cast<json::JSONItem*>(lItem));
+      json::JSONItem* lJSONItem = static_cast<json::JSONItem*>(lItem);
+      lRefCount = lJSONItem->getRefCount();
+#endif
+    }
+    if (lRefCount > 1)
     {
       RAISE_ERROR(zerr::ZDDY0015_COLLECTION_BAD_DESTROY_NODES, theLoc,
       ERROR_PARAMS(collection->getName()->getStringValue()));

=== modified file 'src/store/naive/simple_collection.cpp'
--- src/store/naive/simple_collection.cpp	2012-07-24 08:48:48 +0000
+++ src/store/naive/simple_collection.cpp	2012-08-09 13:45:44 +0000
@@ -155,9 +155,9 @@
 
 #ifdef ZORBA_WITH_JSON
   if (object)
-    object->setCollection(this, pos);
+    object->fix(this, createTreeId());
   else if (array)
-    array->setCollection(this, pos);
+    array->fix(this, createTreeId());
   else
 #endif
     node->setCollection(this, pos);
@@ -249,9 +249,9 @@
 
 #ifdef ZORBA_WITH_JSON
     if (object)
-      object->setCollection(this, pos);
+      object->fix(this, createTreeId());
     else if (array)
-      array->setCollection(this, pos);
+      array->fix(this, createTreeId());
     else
 #endif
       node->setCollection(this, pos);
@@ -331,9 +331,9 @@
 
 #ifdef ZORBA_WITH_JSON
     if (object)
-      object->setCollection(NULL, zero);
+      object->unfix();
     else if (array)
-      array->setCollection(NULL, zero);
+      array->unfix();
     else
 #endif
       node->setCollection(NULL, zero);
@@ -381,12 +381,12 @@
     else if (item->isJSONObject())
     {
       json::SimpleJSONObject* object = static_cast<json::SimpleJSONObject*>(item);
-      object->setCollection(NULL, zero);
+      object->unfix();
     }
     else if (item->isJSONArray())
     {
       json::SimpleJSONArray* array = static_cast<json::SimpleJSONArray*>(item);
-      array->setCollection(NULL, zero);
+      array->unfix();
     }
 #endif
     else
@@ -442,12 +442,12 @@
       else if (item->isJSONObject())
       {
         json::SimpleJSONObject* object = static_cast<json::SimpleJSONObject*>(item);
-        object->setCollection(NULL, zero);
+        object->unfix();
       }
       else if (item->isJSONArray())
       {
         json::SimpleJSONArray* array = static_cast<json::SimpleJSONArray*>(item);
-        array->setCollection(NULL, zero);
+        array->unfix();
       }
 #endif
       else

=== modified file 'src/store/naive/simple_pul.cpp'
--- src/store/naive/simple_pul.cpp	2012-07-24 08:48:48 +0000
+++ src/store/naive/simple_pul.cpp	2012-08-09 13:45:44 +0000
@@ -176,18 +176,28 @@
   assert(target->isNode());
 #endif
 
-  const XmlNode* n = static_cast<const XmlNode*>(target);
-
-  const store::Collection* collection = n->getCollection();
-
-  if (collection != NULL)
-  {
-    collName = static_cast<const QNameItem*>(collection->getName())->getNormalized();
+  const store::Collection* lCollection;
+  
+  if (target->isNode())
+  {
+    assert(dynamic_cast<const XmlNode*>(target));
+    const XmlNode* lNode = static_cast<const XmlNode*>(target);
+    lCollection = lNode->getCollection();
+#ifdef ZORBA_WITH_JSON
+  } else if (target->isJSONItem()) {
+    assert(dynamic_cast<const json::JSONItem*>(target));
+    const json::JSONItem* lJSONItem = static_cast<const json::JSONItem*>(target);
+    lCollection = lJSONItem->getCollection();
+#endif
+  }
+  if (lCollection != NULL)
+  {
+    collName = static_cast<const QNameItem*>(lCollection->getName())->getNormalized();
 
     if (collName == theLastCollection)
       return theLastPul;
 
-    return getCollectionPulByName(collName, collection->isDynamic());
+    return getCollectionPulByName(collName, lCollection->isDynamic());
   }
   else if (theNoCollectionPul != NULL)
   {
@@ -2230,23 +2240,48 @@
     NodeToUpdatesMap::iterator it = pul->theNodeToUpdatesMap.begin();
     NodeToUpdatesMap::iterator end = pul->theNodeToUpdatesMap.end();
 
+    bool found = false;
+    
     for (; it != end; ++it)
     {
-      const XmlNode* targetNode = static_cast<XmlNode*>((*it).first);
-
-      bool found = false;
-
-      for (csize i = 0; i < numRoots; i++)
+      zorba::store::Item* lItem = (*it).first;
+      if (lItem->isNode())
       {
-        XmlNode* rootNode = static_cast<XmlNode*>(rootNodes[i]);
+        assert(dynamic_cast<const XmlNode*>(lItem));
+        const XmlNode* lNode = static_cast<const XmlNode*>(lItem);
+        for (csize i = 0; i < numRoots; i++)
+        {
+          if (rootNodes[i]->isNode()) {
+            assert(dynamic_cast<const XmlNode*>(rootNodes[i]));
+            XmlNode* lRootNode = static_cast<XmlNode*>(rootNodes[i]);
+            
+            if (lNode->getTree() == lRootNode->getTree())
+            {
+              found = true;
+              break;
+            }
+          }
+        }
+#ifdef ZORBA_WITH_JSON
+      } else if (lItem->isJSONItem()) {
+        assert(dynamic_cast<const json::JSONItem*>(lItem));
+        const json::JSONItem* lJSONItem = static_cast<const json::JSONItem*>(lItem);
+        for (csize i = 0; i < numRoots; i++)
+        {
+          if (rootNodes[i]->isJSONItem()) {
+            assert(dynamic_cast<const json::JSONItem*>(rootNodes[i]));
+            json::JSONItem* lRootJSONItem = static_cast<json::JSONItem*>(rootNodes[i]);
+            
+            if (lJSONItem->getTree() == lRootJSONItem->getTree())
+            {
+              found = true;
+              break;
+            }
+          }
+        }
+#endif
+      }
         
-        if (targetNode->getTree() == rootNode->getTree())
-        {
-          found = true;
-          break;
-        }
-      }
-
       if (!found)
         throw XQUERY_EXCEPTION(err::XUDY0014);
     }

-- 
Mailing list: https://launchpad.net/~zorba-coders
Post to     : zorba-coders@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zorba-coders
More help   : https://help.launchpad.net/ListHelp

Reply via email to