GME  13
CMBinaryOp.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: CMBinaryOp.cpp 677396 2008-07-16 19:36:20Z amassari $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #include <xercesc/util/XercesDefs.hpp>
00027 #include <xercesc/util/RuntimeException.hpp>
00028 #include <xercesc/validators/common/ContentSpecNode.hpp>
00029 #include <xercesc/validators/common/CMBinaryOp.hpp>
00030 #include <xercesc/validators/common/CMStateSet.hpp>
00031 
00032 XERCES_CPP_NAMESPACE_BEGIN
00033 
00034 // ---------------------------------------------------------------------------
00035 //  CMBinaryOp: Constructors
00036 // ---------------------------------------------------------------------------
00037 CMBinaryOp::CMBinaryOp( ContentSpecNode::NodeTypes  type
00038                         , CMNode* const             leftToAdopt
00039                         , CMNode* const             rightToAdopt
00040                         , unsigned int              maxStates
00041                         , MemoryManager* const      manager) :
00042     CMNode(type, maxStates, manager)
00043     , fLeftChild(leftToAdopt)
00044     , fRightChild(rightToAdopt)
00045 {
00046     // Insure that its one of the types we require
00047     if (((type & 0x0f) != ContentSpecNode::Choice)
00048     &&  ((type & 0x0f) != ContentSpecNode::Sequence))
00049     {
00050         ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::CM_BinOpHadUnaryType, manager);
00051     }
00052     //
00053     //  If its an alternation, then if either child is nullable then
00054     //  this node is nullable. If its a concatenation, then both of
00055     //  them have to be nullable.
00056     //
00057     if ((type & 0x0f) == ContentSpecNode::Choice)
00058         fIsNullable=(fLeftChild->isNullable() || fRightChild->isNullable());
00059     else
00060         fIsNullable=(fLeftChild->isNullable() && fRightChild->isNullable());
00061 }
00062 
00063 CMBinaryOp::~CMBinaryOp()
00064 {
00065     delete fLeftChild;
00066     delete fRightChild;
00067 }
00068 
00069 
00070 // ---------------------------------------------------------------------------
00071 //  CMBinaryOp: Getter methods
00072 // ---------------------------------------------------------------------------
00073 const CMNode* CMBinaryOp::getLeft() const
00074 {
00075     return fLeftChild;
00076 }
00077 
00078 CMNode* CMBinaryOp::getLeft()
00079 {
00080     return fLeftChild;
00081 }
00082 
00083 const CMNode* CMBinaryOp::getRight() const
00084 {
00085     return fRightChild;
00086 }
00087 
00088 CMNode* CMBinaryOp::getRight()
00089 {
00090     return fRightChild;
00091 }
00092 
00093 
00094 // ---------------------------------------------------------------------------
00095 //  CMBinaryOp: Implementation of the public CMNode virtual interface
00096 // ---------------------------------------------------------------------------
00097 void CMBinaryOp::orphanChild()
00098 {
00099     delete fLeftChild;
00100     fLeftChild=0;
00101     delete fRightChild;
00102     fRightChild=0;
00103 }
00104 
00105 // ---------------------------------------------------------------------------
00106 //  CMBinaryOp: Implementation of the protected CMNode virtual interface
00107 // ---------------------------------------------------------------------------
00108 void CMBinaryOp::calcFirstPos(CMStateSet& toSet) const
00109 {
00110     if ((getType() & 0x0f) == ContentSpecNode::Choice)
00111     {
00112         // Its the the union of the first positions of our children.
00113         toSet = fLeftChild->getFirstPos();
00114         toSet |= fRightChild->getFirstPos();
00115     }
00116     else if ((getType() & 0x0f) == ContentSpecNode::Sequence)
00117     {
00118         //
00119         //  If our left child is nullable, then its the union of our
00120         //  children's first positions. Else is our left child's first
00121         //  positions.
00122         //
00123         toSet = fLeftChild->getFirstPos();
00124         if (fLeftChild->isNullable())
00125             toSet |= fRightChild->getFirstPos();
00126     }
00127 }
00128 
00129 void CMBinaryOp::calcLastPos(CMStateSet& toSet) const
00130 {
00131     if ((getType() & 0x0f) == ContentSpecNode::Choice)
00132     {
00133         // Its the the union of the first positions of our children.
00134         toSet = fLeftChild->getLastPos();
00135         toSet |= fRightChild->getLastPos();
00136     }
00137     else if ((getType() & 0x0f) == ContentSpecNode::Sequence)
00138     {
00139         //
00140         //  If our right child is nullable, then its the union of our
00141         //  children's last positions. Else is our right child's last
00142         //  positions.
00143         //
00144         toSet = fRightChild->getLastPos();
00145         if (fRightChild->isNullable())
00146             toSet |= fLeftChild->getLastPos();
00147     }
00148 }
00149 
00150 XERCES_CPP_NAMESPACE_END