예제 #1
0
void functionAttributeTest()
{
    // Function Attribute test

    TestClass<double> test1;
    test1.value = 432.1;
    TestClass<double> test2;
    test2.value = 2.1;
    coca::FunctionInputAttribute<double,coca::Function<void,const double&> > iAttribute( coca::makeFunction( test1, &TestClass<double>::setValue ) );
    coca::FunctionOutputAttribute<double,coca::Function<const double&> > oAttribute( coca::makeFunction( test2, &TestClass<double>::getValue ) );

    COCA_REQUIRE_EQUAL( iAttribute.connectSource( oAttribute ), true );
    COCA_REQUIRE_EQUAL( test1.value, 2.1 );
    COCA_REQUIRE_EQUAL( test1.value, test2.value );
}
예제 #2
0
void pointerFunctionAttributeTest()
{
    // Function Attribute pointer test

    double value = 123345.6789;
    TestClass<double*> test1;
    test1.value = 0;
    TestClass<double*> test2;
    test2.value = &value;
    coca::FunctionInputAttribute<double*,coca::Function<void,double* const&> > iAttribute( coca::makeFunction( test1, &TestClass<double*>::setValue ) );
    coca::FunctionOutputAttribute<double*,coca::Function<double* const&> > oAttribute( coca::makeFunction( test2, &TestClass<double*>::getValue ) );

    COCA_REQUIRE_EQUAL( iAttribute.connectSource( oAttribute ), true );
    COCA_REQUIRE_EQUAL( test1.value, &value );
    COCA_REQUIRE_EQUAL( test1.value, test2.value );
    iAttribute.disconnectSources(); // should reset input pointer to 0!
    COCA_REQUIRE_NULL( test1.value );

    TestClass<double*> test3;
    test3.value = 0;

    coca::AccessorInputOutputAttribute<double*,coca::Accessor<double* const&> > ioAttribute( coca::makeAccessor( test3, &TestClass<double*>::getValue, &TestClass<double*>::setValue ) );
    COCA_REQUIRE_EQUAL( oAttribute.connectSink( ioAttribute ), true );
    COCA_REQUIRE_EQUAL( ioAttribute.connectSink( iAttribute ), true );
    COCA_REQUIRE_EQUAL( oAttribute.getSinks()->size(), (size_t)1 );
    COCA_REQUIRE_EQUAL( ioAttribute.getSources()->size(), (size_t)1 );
    COCA_REQUIRE_EQUAL( ioAttribute.getSinks()->size(), (size_t)1 );
    COCA_REQUIRE_EQUAL( iAttribute.getSources()->size(), (size_t)1 );
    COCA_REQUIRE_EQUAL( test1.value, &value );
    COCA_REQUIRE_EQUAL( test2.value, &value );
    COCA_REQUIRE_EQUAL( test3.value, &value );

    ioAttribute.setEnabled( false );
    COCA_REQUIRE_EQUAL( ioAttribute.isEnabled(), false );
    COCA_REQUIRE_NULL( test3.value );
    COCA_REQUIRE_NULL( test1.value );
    ioAttribute.setEnabled( true );
    COCA_REQUIRE_EQUAL( test3.value, &value );
    COCA_REQUIRE_EQUAL( test1.value, &value );
    ioAttribute.disconnectSources();
    COCA_REQUIRE_NULL( test3.value );
    COCA_REQUIRE_NULL( test1.value );

}
예제 #3
0
nsresult
nsDOMAttributeMap::SetNamedItemInternal(nsIDOMNode *aNode,
                                        nsIDOMNode **aReturn,
                                        bool aWithNS)
{
  NS_ENSURE_ARG_POINTER(aNode);
  NS_ENSURE_ARG_POINTER(aReturn);

  nsresult rv = NS_OK;
  *aReturn = nsnull;
  nsCOMPtr<nsIDOMNode> tmpReturn;

  if (mContent) {
    // XXX should check same-origin between mContent and aNode however
    // nsContentUtils::CheckSameOrigin can't deal with attributenodes yet
    
    nsCOMPtr<nsIAttribute> iAttribute(do_QueryInterface(aNode));
    if (!iAttribute) {
      return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
    }

    nsDOMAttribute *attribute = static_cast<nsDOMAttribute*>(iAttribute.get());

    // Check that attribute is not owned by somebody else
    nsDOMAttributeMap* owner = iAttribute->GetMap();
    if (owner) {
      if (owner != this) {
        return NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR;
      }

      // setting a preexisting attribute is a no-op, just return the same
      // node.
      NS_ADDREF(*aReturn = aNode);
      
      return NS_OK;
    }

    if (!mContent->HasSameOwnerDoc(iAttribute)) {
      nsCOMPtr<nsIDOMDocument> domDoc =
        do_QueryInterface(mContent->OwnerDoc(), &rv);
      NS_ENSURE_SUCCESS(rv, rv);

      nsCOMPtr<nsIDOMNode> adoptedNode;
      rv = domDoc->AdoptNode(aNode, getter_AddRefs(adoptedNode));
      NS_ENSURE_SUCCESS(rv, rv);

      NS_ASSERTION(adoptedNode == aNode, "Uh, adopt node changed nodes?");
    }

    // Get nodeinfo and preexisting attribute (if it exists)
    nsAutoString name;
    nsCOMPtr<nsINodeInfo> ni;

    // SetNamedItemNS()
    if (aWithNS) {
      // Return existing attribute, if present
      ni = iAttribute->NodeInfo();

      if (mContent->HasAttr(ni->NamespaceID(), ni->NameAtom())) {
        rv = RemoveAttribute(ni, getter_AddRefs(tmpReturn));
        NS_ENSURE_SUCCESS(rv, rv);
      }
    }
    else { // SetNamedItem()
      attribute->GetName(name);

      // get node-info of old attribute
      ni = mContent->GetExistingAttrNameFromQName(name);
      if (ni) {
        rv = RemoveAttribute(ni, getter_AddRefs(tmpReturn));
        NS_ENSURE_SUCCESS(rv, rv);
      }
      else {
        if (mContent->IsInHTMLDocument() &&
            mContent->IsHTML()) {
          nsContentUtils::ASCIIToLower(name);
        }

        rv = mContent->NodeInfo()->NodeInfoManager()->
          GetNodeInfo(name, nsnull, kNameSpaceID_None,
                      nsIDOMNode::ATTRIBUTE_NODE, getter_AddRefs(ni));
        NS_ENSURE_SUCCESS(rv, rv);
        // value is already empty
      }
    }

    nsAutoString value;
    attribute->GetValue(value);

    // Add the new attribute to the attribute map before updating
    // its value in the element. @see bug 364413.
    nsAttrKey attrkey(ni->NamespaceID(), ni->NameAtom());
    mAttributeCache.Put(attrkey, attribute);
    iAttribute->SetMap(this);

    rv = mContent->SetAttr(ni->NamespaceID(), ni->NameAtom(),
                           ni->GetPrefixAtom(), value, true);
    if (NS_FAILED(rv)) {
      DropAttribute(ni->NamespaceID(), ni->NameAtom());
    }
  }

  tmpReturn.swap(*aReturn); // transfers ref.

  return rv;
}