github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/ir_conf_reader.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #include "common/ir_conf_reader.h"
    21  
    22  #include <boost/property_tree/ptree.hpp>
    23  #include <boost/property_tree/json_parser.hpp>
    24  #include <boost/foreach.hpp>
    25  
    26  namespace neb {
    27  
    28    template<typename T>
    29      void check_exception(T lambda) {
    30        try {
    31          lambda();
    32        } catch (const boost::property_tree::ptree_error &e) {
    33          throw json_general_failure(e.what());
    34        }
    35      }
    36  
    37    ir_conf_reader::ir_conf_reader(const std::string &conf_fp) {
    38      // TODO
    39      boost::property_tree::ptree json_root;
    40      read_json_file(conf_fp, json_root);
    41      get_self_ref(json_root);
    42      get_depends(json_root);
    43      get_available_height(json_root);
    44  
    45      get_clang_arguments(json_root, "cpp_files", m_cpp_files);
    46      get_clang_arguments(json_root, "include_header_files", m_include_header_files);
    47      get_clang_arguments(json_root, "link_files", m_link_files);
    48      get_clang_arguments(json_root, "link_path", m_link_path);
    49      get_clang_arguments(json_root, "flags", m_flags);
    50    }
    51  
    52    ir_conf_reader::~ir_conf_reader() = default;
    53  
    54    void ir_conf_reader::read_json_file(const std::string &conf_fp, boost::property_tree::ptree &json_root){
    55      auto lambda_fun = [&]() {
    56       boost::property_tree::read_json(conf_fp, json_root);
    57      };
    58  
    59      check_exception(lambda_fun);
    60    }
    61  
    62    void ir_conf_reader::set_ir_ref_by_ptree(ir_ref &ir, const boost::property_tree::ptree &ptree) {
    63        ir.name() = ptree.get<std::string>("name");
    64        ir.version().major_version() = ptree.get<uint32_t>("version_major");
    65        ir.version().minor_version() = ptree.get<uint16_t>("version_minor");
    66        ir.version().patch_version() = ptree.get<uint16_t>("version_patch");
    67    }
    68  
    69    void ir_conf_reader::get_self_ref(const boost::property_tree::ptree &json_root){
    70      auto lambda_fun = [this, json_root]() {
    71        set_ir_ref_by_ptree(m_self_ref, json_root);
    72      };
    73  
    74      check_exception(lambda_fun);
    75    }
    76  
    77    void ir_conf_reader::get_depends(const boost::property_tree::ptree &json_root){
    78      auto lambda_fun = [this, json_root]() {
    79        boost::property_tree::ptree depends_node = json_root.get_child("depends");
    80  
    81        BOOST_FOREACH(boost::property_tree::ptree::value_type &child_node, depends_node) {
    82          ir_ref ir;
    83          set_ir_ref_by_ptree(ir, child_node.second);
    84  
    85          m_depends.push_back(ir);
    86        }
    87      };
    88  
    89      check_exception(lambda_fun);
    90    }
    91  
    92    void ir_conf_reader::get_clang_arguments(const boost::property_tree::ptree &json_root,
    93                                             const std::string &key,
    94                                             std::vector<std::string> &container) {
    95  
    96      auto lambda_fun = [this, &json_root, &key, &container]() {
    97        boost::property_tree::ptree node = json_root.get_child(key);
    98        BOOST_FOREACH(boost::property_tree::ptree::value_type &child_node, node) {
    99          std::string value = child_node.second.get_value<std::string>();
   100          container.push_back(value);
   101        }
   102      };
   103  
   104      check_exception(lambda_fun);
   105    }
   106  
   107    void ir_conf_reader::get_available_height(const boost::property_tree::ptree &json_root){
   108      auto lambda_fun = [this, json_root]() {
   109        m_available_height = json_root.get<block_height_t>("available_height");
   110      };
   111  
   112      check_exception(lambda_fun);
   113    }
   114  
   115  } // end namespace neb