ConstHDF.cc

00001 /*
00002  * $Id: ConstHDF.cc,v 1.4 2006/03/27 19:17:43 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 "ConstHDF.h"
00039 
00040 #include <stdexcept>
00041 #include <string.h>
00042 #include "HDF.h"
00043 
00044 using namespace std;
00045 
00046                                 // constructors
00047 ClearSilver::ConstHDF::ConstHDF ()
00048   : ConstHDFNode(), root_(), nodes_() {}
00049 ClearSilver::ConstHDF::ConstHDF (const char * path)
00050   : ConstHDFNode(), root_(), nodes_() { root_().read_file(path); }
00051 ClearSilver::ConstHDF::ConstHDF (const string& path)
00052   : ConstHDFNode(), root_(), nodes_() { root_().read_file(path); }
00053 ClearSilver::ConstHDF::ConstHDF (const RootNode& root)
00054   : ConstHDFNode(), root_(root), nodes_() {}
00055 ClearSilver::ConstHDF::ConstHDF (const HDF& hdf)
00056   : ConstHDFNode(), root_(hdf.root_), nodes_(hdf.nodes_) {}
00057 ClearSilver::ConstHDF::ConstHDF (const ConstHDF& hdf)
00058   : ConstHDFNode(), root_(hdf.root_), nodes_(hdf.nodes_) {}
00059 ClearSilver::ConstHDF::~ConstHDF () throw () {}
00060 
00061                                 // assignment
00062 ClearSilver::ConstHDF&
00063 ClearSilver::ConstHDF::operator = (const ConstHDF& hdf)
00064 {
00065   ConstHDF hdf0 (hdf);
00066   swap (hdf0);
00067   return *this;
00068 }
00069 
00070                                 // inspectors
00071                                 // nesting depth
00072 ClearSilver::ConstHDF::size_type
00073 ClearSilver::ConstHDF::depth () const
00074 {
00075   return nodes_.depth();
00076 }
00077 
00078                                 // path to the current node.
00079 std::string
00080 ClearSilver::ConstHDF::path () const
00081 {
00082   return nodes_.path();
00083 }
00084 
00085                                 // associative array-like interface
00086 ClearSilver::ConstHDF::const_reference
00087 ClearSilver::ConstHDF::operator [] (const char * key) const
00088 {
00089   return operator [] (std::string(key));
00090 }
00091 
00092 ClearSilver::ConstHDF::const_reference
00093 ClearSilver::ConstHDF::operator [] (const string& key) const
00094 {
00095   return *find(key);
00096 }
00097 
00098                                 // STL iterators
00099 ClearSilver::ConstHDF::const_iterator
00100 ClearSilver::ConstHDF::begin () const
00101 {
00102   if (empty())                  // XXX
00103     return end();
00104 
00105   const_iterator i (*this);
00106   i.is_iterator() = true;
00107   ClearSilverNode child (i->node().child());
00108   string name;
00109   if (child)
00110     name = child.name();
00111   i->push(name, child);
00112   return i;
00113 }
00114 
00115 ClearSilver::ConstHDF::const_iterator
00116 ClearSilver::ConstHDF::end () const
00117 {
00118   const_iterator i (*this);
00119   i.is_iterator() = true;
00120   i->push (string(), ClearSilverNode());
00121   return i;
00122 }
00123 
00124 ClearSilver::ConstHDF::const_iterator
00125 ClearSilver::ConstHDF::find (const char * key) const
00126 {
00127   return find(std::string(key));
00128 }
00129 
00130 ClearSilver::ConstHDF::const_iterator
00131 ClearSilver::ConstHDF::find (const string& key) const
00132 {
00133   Nodes nodes (find_node (key));
00134   const_iterator i (*this);
00135   i.is_iterator() = true;
00136   i->nodes() = nodes;
00137   return i;
00138 }
00139 
00140                                 // iterator methods
00141 #if XXX
00142 ClearSilver::ConstHDF::const_iterator&
00143 ClearSilver::ConstHDF::operator ++ ();
00144 
00145 ClearSilver::ConstHDF::const_iterator
00146 ClearSilver::ConstHDF::operator ++ (int);
00147     // XXX - no longer an iterator
00148 #endif
00149 
00150 ClearSilver::ConstHDF::value_type
00151 ClearSilver::ConstHDF::operator * ()
00152 {
00153   value_type v(*this);
00154   v.is_iterator() = false;
00155   return v;
00156 }
00157 
00158 ClearSilver::ConstHDF::pointer
00159 ClearSilver::ConstHDF::operator -> ()
00160 {
00161   return this;
00162 }
00163 
00164 ClearSilver::Nodes
00165 ClearSilver::ConstHDF::nodes () const
00166 {
00167   return nodes_;
00168 }
00169 
00170 ClearSilver::Nodes&
00171 ClearSilver::ConstHDF::nodes ()
00172 {
00173   return nodes_;
00174 }
00175 
00176 ClearSilver::ClearSilverNode
00177 ClearSilver::ConstHDF::node () const
00178 {
00179   if (nodes_.empty())
00180     return root_();
00181   return nodes_.top();
00182 }
00183 
00184                                 // access the root node
00185 ClearSilver::ClearSilverNode
00186 ClearSilver::ConstHDF::root () const
00187 {
00188   return root_();
00189 }
00190 
00191 void
00192 ClearSilver::ConstHDF::push (const Name& name,
00193                              const ClearSilverNode& node)
00194 {
00195   nodes_.push(name, node);
00196 }
00197 
00198 void
00199 ClearSilver::ConstHDF::pop ()
00200 {
00201   if (nodes_.empty())
00202     {
00203       string message;
00204       message += "ClearSilver::ConstHDF::pop (): ";
00205       message += "empty stack";
00206       throw invalid_argument (message);
00207     }
00208   nodes_.pop();
00209 }
00210 
00211 ClearSilver::ClearSilverNode
00212 ClearSilver::ConstHDF::top () const
00213 {
00214   if (nodes_.empty())
00215     return ClearSilverNode();
00216   return nodes_.top();
00217 }
00218 
00219                                 // swap contents with another object.
00220 void
00221 ClearSilver::ConstHDF::swap (ConstHDF& hdf) throw()
00222 {
00223   ConstHDFNode::swap (hdf);
00224   root_.swap (hdf.root_);
00225   nodes_.swap (hdf.nodes_);
00226 }
00227 
00228                                 // debug flag
00229 bool&
00230 ClearSilver::ConstHDF::debug ()
00231 {
00232   static bool debug_ = false;
00233   return debug_;
00234 }
00235 
00236 namespace ClearSilver
00237 {
00238                                 // equality comparison operator
00239   bool
00240   operator == (const ConstHDF::reference& r1, const ConstHDF::reference& r2)
00241   {
00242     return r1.root_() == r2.root_() && r1.nodes_ == r2.nodes_;
00243   }
00244 };                              // namespace ClearSilver

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