SchemaItem.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_CONFIGITEM_HPP_
  14. #define _CONFIG2_CONFIGITEM_HPP_
  15. #include <string>
  16. #include <memory>
  17. #include <set>
  18. #include <map>
  19. #include "SchemaType.hpp"
  20. #include "SchemaValue.hpp"
  21. #include "platform.h"
  22. class DECL_EXPORT SchemaItem : public std::enable_shared_from_this<SchemaItem>
  23. {
  24. public:
  25. SchemaItem(const std::string &name, const std::string &className = "category", const std::shared_ptr<SchemaItem> &pParent = nullptr);
  26. ~SchemaItem() { }
  27. std::string getItemType() const;
  28. void setMinInstances(unsigned num) { m_minInstances = num; }
  29. unsigned getMinInstances() const { return m_minInstances; }
  30. void setMaxInstances(unsigned num) { m_maxInstances = num; }
  31. unsigned getMaxInstances() const { return m_maxInstances; }
  32. void addSchemaValueType(const std::shared_ptr<SchemaType> &pType);
  33. std::shared_ptr<SchemaType> getSchemaValueType(const std::string &typeName, bool throwIfNotPresent = true) const;
  34. void addSchemaType(const std::shared_ptr<SchemaItem> &pItem, const std::string &typeName);
  35. std::shared_ptr<SchemaItem> getSchemaType(const std::string &name, bool throwIfNotPresent=true) const;
  36. void insertSchemaType(const std::shared_ptr<SchemaItem> pTypeItem);
  37. void addChild(const std::shared_ptr<SchemaItem> &pItem) { m_children.insert({ pItem->getProperty("name"), pItem }); }
  38. void addChild(const std::shared_ptr<SchemaItem> &pItem, const std::string &name) { m_children.insert({ name, pItem }); }
  39. void getChildren(std::vector<std::shared_ptr<SchemaItem>> &children);
  40. std::shared_ptr<SchemaItem> getChild(const std::string &name);
  41. std::shared_ptr<SchemaItem> getChildByComponent(const std::string &name, std::string &componentName);
  42. void setItemSchemaValue(const std::shared_ptr<SchemaValue> &pValue) { m_pItemValue = pValue; }
  43. std::shared_ptr<SchemaValue> getItemSchemaValue() const { return m_pItemValue; }
  44. bool isItemValueDefined() { return m_pItemValue != nullptr; }
  45. void findSchemaValues(const std::string &path, std::vector<std::shared_ptr<SchemaValue>> &schemaValues);
  46. void addAttribute(const std::shared_ptr<SchemaValue> &pCfgValue);
  47. void addAttribute(const std::vector<std::shared_ptr<SchemaValue>> &attributes);
  48. void addAttribute(const std::map<std::string, std::shared_ptr<SchemaValue>> &attributes);
  49. std::shared_ptr<SchemaValue> getAttribute(const std::string &name, bool createIfDoesNotExist=true) const;
  50. void getAttributes(std::vector<std::shared_ptr<SchemaValue>> &attributes) const;
  51. bool addUniqueName(const std::string keyName);
  52. void addUniqueAttributeValueSetDefinition(const std::string &setName, const std::string &elementPath, const std::string &attributeName, bool duplicateOk = false);
  53. void addReferenceToUniqueAttributeValueSet(const std::string &setName, const std::string &elementPath, const std::string &attributeName);
  54. void processDefinedUniqueAttributeValueSets(std::map<std::string, std::vector<std::shared_ptr<SchemaValue>>> &uniqueAttributeValueSets);
  55. void processUniqueAttributeValueSetReferences(const std::map<std::string, std::vector<std::shared_ptr<SchemaValue>>> &uniqueAttributeValueSets);
  56. void resetEnvironment();
  57. void postProcessConfig(const std::map<std::string, std::vector<std::shared_ptr<SchemaValue>>> &uniqueAttributeValueSets);
  58. bool isInsertable() const { return (m_minInstances == 0) || (m_maxInstances > m_minInstances); }
  59. bool isRequired() const { return m_minInstances > 0; }
  60. std::string getProperty(const std::string &name, const std::string &dfault = std::string("")) const;
  61. void setProperty(const std::string &name, const std::string &value) { m_properties[name] = value; }
  62. protected:
  63. SchemaItem() { };
  64. protected:
  65. std::map<std::string, std::string> m_properties;
  66. unsigned m_minInstances;
  67. unsigned m_maxInstances;
  68. std::multimap<std::string, std::shared_ptr<SchemaItem>> m_children;
  69. std::shared_ptr<SchemaValue> m_pItemValue; // value for this item (think of it as the VALUE for an element <xx attr= att1>VALUE</xx>)
  70. std::map<std::string, std::shared_ptr<SchemaValue>> m_attributes; // attributes for this item (think in xml terms <m_name attr1="val" attr2="val" .../> where attrN is in this vector
  71. std::set<std::string> m_keys; // generic set of key values for use by any component to prevent duplicate operations
  72. std::weak_ptr<SchemaItem> m_pParent;
  73. std::map<std::string, std::shared_ptr<SchemaType>> m_types;
  74. std::map<std::string, std::shared_ptr<SchemaItem>> m_schemaTypes; // reusable types
  75. // This struct handles both unique attribute sets and references to same
  76. struct SetInfo {
  77. SetInfo(const std::string &setName, const std::string &elementPath, const std::string &attributeName) :
  78. m_setName(setName), m_elementPath(elementPath), m_attributeName(attributeName), m_duplicateOk(false) { }
  79. SetInfo(const std::string &setName, const std::string &elementPath, const std::string &attributeName, bool duplicateOk) :
  80. m_setName(setName), m_elementPath(elementPath), m_attributeName(attributeName), m_duplicateOk(duplicateOk) { }
  81. std::string m_setName;
  82. std::string m_elementPath;
  83. std::string m_attributeName;
  84. bool m_duplicateOk;
  85. };
  86. // Attribute unique sets and references to unique sets are stored during parsing and post processed
  87. std::map<std::string, SetInfo> m_uniqueAttributeValueSetReferences;
  88. std::map<std::string, SetInfo> m_uniqueAttributeValueSetDefs;
  89. // These are the attribute value sets whose members must be unique
  90. //static std::map<std::string, std::vector<std::shared_ptr<SchemaValue>>> m_uniqueAttributeValueSets;
  91. };
  92. #endif // _CONFIG2_CONFIGITEM_HPP_