Nodes.cc

00001 /*
00002  * $Id: Nodes.cc,v 1.6 2006/03/29 05:35:58 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 "Nodes.h"
00039 
00040 #include <stdexcept>
00041 #include <string.h>
00042 #include <syslog.h>
00043 #include <boost/lexical_cast.hpp>
00044 
00045 #define CGI ClearSilverCGI
00046 #define HDF ClearSilverHDF
00047 #include <ClearSilver/ClearSilver.h>
00048 #undef CGI
00049 #undef HDF
00050 
00051 using namespace std;
00052 
00053 static string hex(void*);
00054 
00055                                 // constructors
00056 ClearSilver::Nodes::Nodes ()
00057   : nodes_(), names_() {}
00058 ClearSilver::Nodes::Nodes (const Nodes& n)
00059   : nodes_(n.nodes_), names_(n.names_) {}
00060 ClearSilver::Nodes::~Nodes () {}
00061 
00062                                 // assignment
00063 ClearSilver::Nodes&
00064 ClearSilver::Nodes::operator = (const Nodes& n)
00065 {
00066   nodes_ = n.nodes_;
00067   names_ = n.names_;
00068   return *this;
00069 }
00070 
00071                                 // is the node path empty?
00072 bool
00073 ClearSilver::Nodes::empty () const
00074 {
00075   return nodes_.empty();
00076 }
00077 
00078                                 // size of the node path.
00079 ClearSilver::Nodes::size_type
00080 ClearSilver::Nodes::size () const
00081 {
00082   return nodes_.size();
00083 }
00084 
00085                                 // string representation of the entire path.
00086 string
00087 ClearSilver::Nodes::path () const
00088 {
00089   string p;
00090   for (NameContainer::const_iterator i = names_.begin();
00091        i != names_.end(); ++i)
00092     {
00093       if (i != names_.begin())
00094         p += ".";
00095       p += string(*i);
00096     }
00097   return p;
00098 }
00099 
00100 ClearSilver::Nodes::size_type
00101 ClearSilver::Nodes::depth () const
00102 {
00103   return nodes_.size();
00104 }
00105 
00106 void
00107 ClearSilver::Nodes::push (const Name& name, const ClearSilverNode& node)
00108 {
00109   nodes_.push_back (node);
00110   names_.push_back (name);
00111 }
00112 
00113 void
00114 ClearSilver::Nodes::pop ()
00115 {
00116   if (nodes_.empty())
00117     {
00118       string message;
00119       message += "ClearSilver::Nodes::pop () const: ";
00120       message += "no nodes";
00121       throw logic_error (message);
00122     }
00123   if (names_.size() != nodes_.size())
00124     {
00125       string message;
00126       message += "ClearSilver::Nodes::pop () const: ";
00127       message += "mismatch in number of nodes (";
00128       message += boost::lexical_cast<string>(nodes_.size());
00129       message += ") and paths (";
00130       message += boost::lexical_cast<string>(names_.size()); 
00131       message += ")";
00132       throw logic_error (message);
00133     }
00134   nodes_.pop_back();
00135   names_.pop_back ();
00136 }
00137 
00138 ClearSilver::ClearSilverNode
00139 ClearSilver::Nodes::top () const
00140 {
00141   if (nodes_.empty())
00142     {
00143       string message;
00144       message += "ClearSilver::Nodes::top () const: ";
00145       message += "no nodes";
00146       throw logic_error (message);
00147     }
00148   return nodes_.back();
00149 }
00150 
00151                                 // swap contents with another object
00152 void
00153 ClearSilver::Nodes::swap (Nodes& n) throw()
00154 {
00155   nodes_.swap (n.nodes_);
00156   names_.swap (n.names_);
00157 }
00158 
00159                                 // debug flag
00160 bool&
00161 ClearSilver::Nodes::debug ()
00162 {
00163   static bool debug_ = false;
00164   return debug_;
00165 }
00166 
00167                                 // XXX
00168 void
00169 ClearSilver::Nodes::print () const
00170 {
00171   int i = 1;
00172   NodeContainer::const_iterator ni = nodes_.begin();
00173   NameContainer::const_iterator pi = names_.begin();
00174   while (ni != nodes_.end())
00175     {
00176       string message;
00177       message += boost::lexical_cast<string>(i) + ". ";
00178       message += hex(ni->get()) + "  " + string(*pi);
00179       syslog(LOG_DEBUG, "%s", message.c_str());
00180       ++i;
00181       ++ni;
00182       ++pi;
00183     }
00184 }
00185 
00186 namespace ClearSilver
00187 {
00188                                 // logical comparison operators
00189   bool
00190   operator == (const Nodes& n1, const Nodes& n2)
00191   {
00192     if (n1.size() != n2.size())
00193       return false;
00194 
00195     Nodes::NodeContainer::const_iterator i = n1.nodes_.begin();
00196     Nodes::NodeContainer::const_iterator j = n2.nodes_.begin();
00197     while (i != n1.nodes_.end())
00198       {
00199         if (*i != *j)
00200           return false;
00201         ++i;
00202         ++j;
00203       }
00204     return true;
00205   }
00206 };                              // namespace ClearSilver
00207 
00208 string
00209 hex (void* p)
00210 {
00211   ostringstream s;
00212   s << std::hex << p;
00213   return s.str();
00214 }

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