RootNode.cc

00001 /*
00002  * $Id: RootNode.cc,v 1.4 2006/03/27 19:17:47 brook Exp $
00003  */
00004 
00005 /*
00006  * ClearSilver++ Software License.
00007  *
00008  * Copyright (c) 2005,2006 Brook Milligan <brook@nmsu.edu>
00009  * All rights reserved.
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 
00015  * 1. Redistributions of source code must retain the above copyright
00016  *    notice, this list of conditions and the following disclaimer.
00017  * 2. Redistributions in binary form must reproduce the above
00018  *    copyright notice, this list of conditions and the following
00019  *    disclaimer in the documentation and/or other materials provided
00020  *    with the distribution.
00021  * 3. The name of the author may not be used to endorse or promote
00022  *    products derived from this software without specific prior
00023  *    written permission.
00024  * 
00025  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00026  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00029  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00031  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00033  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036  */
00037 
00038 #include "RootNode.h"
00039 
00040 #include "ClearSilverException.h"
00041 #include <ClearSilver/util/neo_hdf.h>
00042 
00043 using namespace std;
00044 
00045 namespace ClearSilver
00046 {
00047   class Deleter
00048   {
00049   public:
00050     Deleter (bool);
00051     void operator () (ClearSilverNode::HDF*) const;
00052 
00053   private:
00054     bool owns_memory_;
00055   };
00056 
00057 
00058                                 // constructors
00059   RootNode::RootNode ()
00060     : root_()
00061   {
00062     ClearSilverNode::HDF* root = 0;
00063     hdf_init (&root);
00064     RootNode r (root, true);
00065     swap (r);
00066   }
00067 
00068   RootNode::RootNode (ClearSilverNode::HDF* root, bool owns_memory)
00069     : root_(root, Deleter(owns_memory)) {}
00070 
00071   RootNode::RootNode (const RootNode& r)
00072     : root_(r.root_)
00073   {}
00074 
00075   RootNode::~RootNode () throw() {}
00076 
00077                                 // assignment
00078   RootNode&
00079   RootNode::operator = (const RootNode& r)
00080   {
00081     RootNode r0 (r);
00082     swap (r0);
00083     return *this;
00084   }
00085 
00086                                 // swap contents with another object.
00087   void
00088   RootNode::swap (RootNode& n) throw()
00089   {
00090     root_.swap (n.root_);
00091   }
00092 
00093   ClearSilverNode
00094   RootNode::operator () () const
00095   {
00096     return ClearSilverNode (root_.get());
00097   }
00098 
00099                                 // is the root node empty?
00100   bool
00101   RootNode::empty () const
00102   {
00103     if (!root_)
00104       {
00105         string message;
00106         message += "RootNode::empty(): ";
00107         message += "root node is a nil pointer";
00108         ClearSilverException e (message);
00109         throw e;
00110       }
00111     return ClearSilverNode (root_.get()).child().empty();
00112   }
00113 
00114                                 // debug flag
00115   bool&
00116   RootNode::debug ()
00117   {
00118     static bool debug_ = false;
00119     return debug_;
00120   }
00121 
00122   Deleter::Deleter (bool owns_memory) : owns_memory_(owns_memory) {}
00123 
00124   void 
00125   Deleter::operator () (ClearSilverNode::HDF* root) const
00126   {
00127     if (owns_memory_)
00128       {
00129         ClearSilverNode n (root);
00130         n.remove_tree ();
00131       }
00132   }
00133 };                              // namespace ClearSilver
00134 

Generated on Tue May 16 14:50:52 2006 for ClearSilver C++ Library by  doxygen 1.4.5