QSapecNG
 All Classes Functions Enumerations Properties
ir_circuit.h
00001 /*
00002     SapecNG - Next Generation Symbolic Analysis Program for Electric Circuit
00003     Copyright (C) 2009, Michele Caini
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 
00020 #ifndef IR_CIRCUIT_H
00021 #define IR_CIRCUIT_H
00022 
00023 
00024 #include "parser/parser.h"
00025 #include "exception/sapecngexception.h"
00026 
00027 #include <stack>
00028 #include <iostream>
00029 #include <boost/property_tree/info_parser.hpp>
00030 #include <boost/property_tree/xml_parser.hpp>
00031 #include <boost/property_tree/ptree.hpp>
00032 
00033 
00034 namespace sapecng
00035 {
00036 
00037 
00038 class ir_parser: public abstract_parser
00039 {
00040 
00041 public:
00042   ir_parser() { }
00043 
00044 protected:
00045   void parse_internal(abstract_builder& builder);
00046 
00047 private:
00048   void parse_rec(
00049       abstract_builder& builder,
00050       boost::property_tree::ptree* head
00051     );
00052 
00053 protected:
00054   typedef
00055   boost::error_info<struct tag_what, std::string>
00056   what;
00057 
00058   boost::property_tree::ptree ptree_;
00059 
00060 };
00061 
00062 
00063 class info_parser: public ir_parser
00064 {
00065 
00066 public:
00067   info_parser(
00068       std::basic_istream<
00069           boost::property_tree::ptree::key_type::value_type
00070         >& stream
00071     ): stream_(stream) { }
00072 
00073   void parse(abstract_builder& builder)
00074   {
00075     try {
00076       boost::property_tree::info_parser::read_info(stream_, ptree_);
00077       parse_internal(builder);
00078     } catch(const boost::property_tree::info_parser::info_parser_error& ipe) {
00079       throw stream_read_error() << what(std::string(ipe.what()));
00080     } catch(const sapecng_exception& se) {
00081       se << what("info parser unknow error");
00082       throw;
00083     }
00084   }
00085 
00086 private:
00087   std::basic_istream<
00088       boost::property_tree::ptree::key_type::value_type
00089     >& stream_;
00090 
00091 };
00092 
00093 
00094 class xml_parser: public ir_parser
00095 {
00096 
00097 public:
00098   xml_parser(
00099       std::basic_istream<
00100           boost::property_tree::ptree::key_type::value_type
00101         >& stream
00102     ): stream_(stream) { }
00103 
00104   void parse(abstract_builder& builder)
00105   {
00106     try {
00107       boost::property_tree::xml_parser::read_xml(stream_, ptree_);
00108       parse_internal(builder);
00109     } catch(const boost::property_tree::xml_parser::xml_parser_error& xpe) {
00110       throw stream_read_error() << what(std::string(xpe.what()));
00111     } catch(const sapecng_exception& se) {
00112       se << what("xml parser unknow error");
00113       throw;
00114     }
00115   }
00116 
00117 private:
00118   std::basic_istream<
00119       boost::property_tree::ptree::key_type::value_type
00120     >& stream_;
00121 
00122 };
00123 
00124 
00125 
00126 class ir_builder: public abstract_builder
00127 {
00128 
00129 public:
00130   ir_builder();
00131 
00132   void add_circuit_properties(std::map<std::string,std::string> map);
00133   void add_circuit_property(std::string name, std::string value);
00134 
00135   void add_wire_component(
00136       std::map<std::string,std::string> props =
00137         std::map<std::string,std::string>()
00138     );
00139 
00140   void add_out_component(
00141       unsigned int v,
00142       std::map<std::string,std::string> props =
00143         std::map<std::string,std::string>()
00144     );
00145 
00146   void add_dual_component(
00147       abstract_builder::dual_component_type c_type,
00148       std::string name,
00149       double value,
00150       bool symbolic,
00151       unsigned int va,
00152       unsigned int vb,
00153       std::map<std::string,std::string> props =
00154         std::map<std::string,std::string>()
00155     );
00156 
00157   void add_quad_component(
00158       abstract_builder::quad_component_type c_type,
00159       std::string name,
00160       double value,
00161       bool symbolic,
00162       unsigned int va,
00163       unsigned int vb,
00164       unsigned int vac,
00165       unsigned int vbc,
00166       std::map<std::string,std::string> props =
00167         std::map<std::string,std::string>()
00168     );
00169 
00170   void add_unknow_component(
00171       std::map<std::string,std::string> props =
00172         std::map<std::string,std::string>()
00173     );
00174 
00175   void begin_userdef_component(
00176       std::string name,
00177       std::map<std::string,std::string> props =
00178         std::map<std::string,std::string>()
00179     );
00180 
00181   void end_userdef_component(
00182       std::string name,
00183       std::map<std::string,std::string> props =
00184         std::map<std::string,std::string>()
00185     );
00186 
00187 protected:
00188   typedef
00189   boost::error_info<struct tag_what, std::string>
00190   what;
00191 
00192   boost::property_tree::ptree ptree_;
00193   boost::property_tree::ptree* head_;
00194   std::stack<boost::property_tree::ptree*> stack_;
00195 
00196 private:
00197   void add_item(
00198       char id,
00199       std::string name,
00200       double value,
00201       bool symbolic,
00202       std::vector<unsigned int> nodes,
00203       std::map<std::string,std::string> props =
00204         std::map<std::string,std::string>()
00205     );
00206 
00207 };
00208 
00209 
00210 class info_builder: public ir_builder
00211 {
00212 
00213 public:
00214   info_builder(
00215       std::basic_ostream<
00216           boost::property_tree::ptree::key_type::value_type
00217         >& stream
00218     ): stream_(stream) { }
00219 
00220   void flush()
00221   {
00222     try {
00223       boost::property_tree::info_parser::write_info(stream_, ptree_);
00224     } catch(const boost::property_tree::info_parser::info_parser_error& ipe) {
00225       throw stream_write_error() << what(std::string(ipe.what()));
00226     }
00227   }
00228 
00229 private:
00230   std::basic_ostream<
00231       boost::property_tree::ptree::key_type::value_type
00232     >& stream_;
00233 
00234 };
00235 
00236 
00237 class xml_builder: public ir_builder
00238 {
00239 
00240 public:
00241   xml_builder(
00242       std::basic_ostream<
00243           boost::property_tree::ptree::key_type::value_type
00244         >& stream
00245     ): stream_(stream) { }
00246 
00247   void flush()
00248   {
00249     try {
00250       boost::property_tree::xml_parser::write_xml(stream_, ptree_);
00251     } catch(const boost::property_tree::xml_parser::xml_parser_error& xpe) {
00252       throw stream_write_error() << what(std::string(xpe.what()));
00253     }
00254   }
00255 
00256 private:
00257   std::basic_ostream<
00258       boost::property_tree::ptree::key_type::value_type
00259     >& stream_;
00260 
00261 };
00262 
00263 
00264 
00265 }
00266 
00267 
00268 #endif // IR_CIRCUIT_H
 All Classes Functions Enumerations Properties