EnvironmentNode.hpp 5.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2017 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef _CONFIG2_ENVIRONMENTNODE_HPP_
  14. #define _CONFIG2_ENVIRONMENTNODE_HPP_
  15. #include <memory>
  16. #include <map>
  17. #include "SchemaItem.hpp"
  18. #include "EnvironmentValue.hpp"
  19. #include "SchemaValue.hpp"
  20. #include "Status.hpp"
  21. #include "InsertableItem.hpp"
  22. #include "NameValue.hpp"
  23. #include "platform.h"
  24. #include "ConfigPath.hpp"
  25. class DECL_EXPORT EnvironmentNode : public std::enable_shared_from_this<EnvironmentNode>
  26. {
  27. public:
  28. EnvironmentNode(const std::shared_ptr<SchemaItem> &pCfgItem, const std::string &elemName, const std::shared_ptr<EnvironmentNode> &pParent = nullptr) :
  29. m_pSchemaItem(pCfgItem), m_name(elemName), m_pParent(pParent) { }
  30. ~EnvironmentNode() { }
  31. const std::string &getName() const { return m_name; }
  32. void addChild(std::shared_ptr<EnvironmentNode> pNode);
  33. bool removeChild(std::shared_ptr<EnvironmentNode> pNode, std::vector<std::string> &removedNodeIds);
  34. void removeAllChildren(std::vector<std::string> &removedNodeIds);
  35. void getChildren(std::vector<std::shared_ptr<EnvironmentNode>> &children, const std::string &name=std::string("")) const;
  36. bool hasChildren() const { return m_children.size() != 0; }
  37. int getNumChildren() const { return (int)m_children.size(); }
  38. std::shared_ptr<EnvironmentNode> getParent() const;
  39. void setParent(const std::shared_ptr<EnvironmentNode> &pParent) { m_pParent = pParent; }
  40. bool addAttribute(const std::string &name, std::shared_ptr<EnvironmentValue> pValue);
  41. void setAttributeValues(const std::vector<NameValue> &values, Status &status, bool allowInvalid, bool forceCreate);
  42. void setAttributeValue(const std::string &name, const std::string &value, Status &status, bool allowInvalid=false, bool forceCreate=false); // candidate for a variant?
  43. std::string getAttributeValue(const std::string &name) const; // candidate for a variant?
  44. void addMissingAttributesFromConfig();
  45. bool setLocalValue(const std::string &newValue, Status &status, bool force = false);
  46. std::string getLocalValue() const;
  47. void setLocalEnvValue(const std::shared_ptr<EnvironmentValue> &pEnvValue) { m_pLocalValue = pEnvValue; }
  48. const std::shared_ptr<EnvironmentValue> &getLocalEnvValue() const { return m_pLocalValue; }
  49. bool isLocalValueSet() const { return m_pLocalValue != nullptr; }
  50. void getAttributes(std::vector<std::shared_ptr<EnvironmentValue>> &attrs) const;
  51. const std::shared_ptr<EnvironmentValue> getAttribute(const std::string &name) const;
  52. bool hasAttribute(const std::string &name) const { return m_attributes.find(name) != m_attributes.end(); }
  53. bool hasAttributes() const { return m_attributes.size() != 0; }
  54. void setId(const std::string &id) { m_id = id; }
  55. const std::string &getId() const { return m_id; }
  56. void validate(Status &status, bool includeChildren=false, bool includeHiddenNodes=false) const;
  57. void getAttributeValueForAllSiblings(const std::string &attrName, std::vector<std::string> &result) const;
  58. const std::shared_ptr<SchemaItem> &getSchemaItem() const { return m_pSchemaItem; }
  59. void getInsertableItems(std::vector<InsertableItem> &items) const;
  60. void initialize();
  61. void fetchNodes(const std::string &path, std::vector<std::shared_ptr<EnvironmentNode>> &nodes) const;
  62. std::shared_ptr<const EnvironmentNode> getRoot() const;
  63. void addEnvironmentInsertData(const std::string &envData) { m_insertData = envData; }
  64. const std::string &getEnvironmentInsertData() const { return m_insertData; }
  65. void clearEnvironmentInsertData() { m_insertData.clear(); }
  66. protected:
  67. void doFetchNodes(ConfigPath &configPath, std::vector<std::shared_ptr<EnvironmentNode>> &nodes) const;
  68. protected:
  69. std::string m_name;
  70. std::shared_ptr<SchemaItem> m_pSchemaItem;
  71. std::weak_ptr<EnvironmentNode> m_pParent;
  72. std::multimap<std::string, std::shared_ptr<EnvironmentNode>> m_children;
  73. std::shared_ptr<EnvironmentValue> m_pLocalValue; // not normal because values usually in attributes
  74. std::map<std::string, std::shared_ptr<EnvironmentValue>> m_attributes;
  75. std::string m_id;
  76. std::string m_insertData;
  77. };
  78. #endif