external_node.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* external_node.cpp - import node from external file
  2. * Copyright (C) 2018 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <core/node_info/macros.h>
  18. #include <core/node_info/default_node.h>
  19. #include <core/node/new_node.h>
  20. #include <core/node/proxy_node.h>
  21. #include <core/node_tree/path.h>
  22. #include <core/fs/document_loader.h>
  23. #include <core/fs/from_context.h>
  24. #include <core/util/nullptr.h>
  25. #include <core/all_types.h>
  26. namespace rainynite::core::nodes {
  27. template <typename T>
  28. class ExternalNode :
  29. public NewProxyNode<
  30. ExternalNode<T>,
  31. T,
  32. types::Only<fs::Path::path_t>,
  33. types::Only<NodeTreePath>
  34. >
  35. {
  36. DOC_STRING(
  37. "Load a node from external file"
  38. )
  39. NODE_PROPERTIES("file_name", "node_path")
  40. COMPLEX_DEFAULT_VALUES(make_value<fs::Path::path_t>(), make_default_node<NodeTreePath>())
  41. PROPERTY(file_name)
  42. PROPERTY(node_path)
  43. protected:
  44. NodeInContext get_proxy(shared_ptr<Context> ctx) const override {
  45. auto fname = file_name_value<fs::Path::path_t>(ctx);
  46. auto fpath = fs::context_path(*ctx, fname);
  47. auto doc = no_null(DocumentLoader::instance()->get_document(fpath));
  48. auto tree = doc->get_tree();
  49. auto npath = node_path_value<NodeTreePath>(ctx);
  50. auto idx = tree_path_to_index(*tree, npath);
  51. // TODO: ctx?..
  52. return {tree->get_node(idx), ctx};
  53. }
  54. };
  55. NODE_INFO_INSTANCES(ExternalNode, ExternalNode<T>, T)
  56. } // namespace rainynite::core::nodes