text: // Copyright (c) 2020 RISC Software GmbH
  //
  // This file was generated by CPACSGen from CPACS XML Schema (c) German Aerospace Center (DLR/SC).
  // Do not edit, all changes are lost when files are re-generated.
  //
  // Licensed under the Apache License, Version 2.0 (the "License")
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  #include <cassert>
  #include "CPACSCpacs.h"
  #include "CPACSHeader.h"
  #include "CTiglError.h"
  #include "CTiglLogging.h"
  #include "CTiglUIDObject.h"
  #include "TixiHelper.h"
  
  namespace tigl
  {
  namespace generated
  {
      CPACSHeader::CPACSHeader(CPACSCpacs* parent)
          : m_timestamp(0)
      {
          //assert(parent != NULL);
          m_parent = parent;
      }
  
      CPACSHeader::~CPACSHeader()
      {
      }
  
      const CPACSCpacs* CPACSHeader::GetParent() const
      {
          return m_parent;
      }
  
      CPACSCpacs* CPACSHeader::GetParent()
      {
          return m_parent;
      }
  
      const CTiglUIDObject* CPACSHeader::GetNextUIDParent() const
      {
          if (m_parent) {
              return m_parent->GetNextUIDParent();
          }
          return nullptr;
      }
  
      CTiglUIDObject* CPACSHeader::GetNextUIDParent()
      {
          if (m_parent) {
              return m_parent->GetNextUIDParent();
          }
          return nullptr;
      }
  
      void CPACSHeader::ReadCPACS(const TixiDocumentHandle& tixiHandle, const std::string& xpath)
      {
          // read element name
          if (tixi::TixiCheckElement(tixiHandle, xpath + "/name")) {
              m_name = tixi::TixiGetElement<std::string>(tixiHandle, xpath + "/name");
              if (m_name.empty()) {
                  LOG(WARNING) << "Required element name is empty at xpath " << xpath;
              }
          }
          else {
              LOG(ERROR) << "Required element name is missing at xpath " << xpath;
          }
  
          // read element description
          if (tixi::TixiCheckElement(tixiHandle, xpath + "/description")) {
              m_description = tixi::TixiGetElement<std::string>(tixiHandle, xpath + "/description");
              if (m_description->empty()) {
                  LOG(WARNING) << "Optional element description is present but empty at xpath " << xpath;
              }
          }
  
          // read element creator
          if (tixi::TixiCheckElement(tixiHandle, xpath + "/creator")) {
              m_creator = tixi::TixiGetElement<std::string>(tixiHandle, xpath + "/creator");
              if (m_creator.empty()) {
                  LOG(WARNING) << "Required element creator is empty at xpath " << xpath;
              }
          }
          else {
              LOG(ERROR) << "Required element creator is missing at xpath " << xpath;
          }
  
          // read element timestamp
          if (tixi::TixiCheckElement(tixiHandle, xpath + "/timestamp")) {
              m_timestamp = tixi::TixiGetElement<std::time_t>(tixiHandle, xpath + "/timestamp");
          }
          else {
              LOG(ERROR) << "Required element timestamp is missing at xpath " << xpath;
          }
  
          // read element version
          if (tixi::TixiCheckElement(tixiHandle, xpath + "/version")) {
              m_version = tixi::TixiGetElement<std::string>(tixiHandle, xpath + "/version");
              if (m_version.empty()) {
                  LOG(WARNING) << "Required element version is empty at xpath " << xpath;
              }
          }
          else {
              LOG(ERROR) << "Required element version is missing at xpath " << xpath;
          }
  
          // read element cpacsVersion
          if (tixi::TixiCheckElement(tixiHandle, xpath + "/cpacsVersion")) {
              m_cpacsVersion = tixi::TixiGetElement<std::string>(tixiHandle, xpath + "/cpacsVersion");
              if (m_cpacsVersion->empty()) {
                  LOG(WARNING) << "Optional element cpacsVersion is present but empty at xpath " << xpath;
              }
          }
  
          // read element updates
          if (tixi::TixiCheckElement(tixiHandle, xpath + "/updates")) {
              m_updates = boost::in_place(this);
              try {
                  m_updates->ReadCPACS(tixiHandle, xpath + "/updates");
              } catch(const std::exception& e) {
                  LOG(ERROR) << "Failed to read updates at xpath " << xpath << ": " << e.what();
                  m_updates = boost::none;
              }
          }
  
      }
  
      void CPACSHeader::WriteCPACS(const TixiDocumentHandle& tixiHandle, const std::string& xpath) const
      {
          // write element name
          tixi::TixiCreateElementIfNotExists(tixiHandle, xpath + "/name");
          tixi::TixiSaveElement(tixiHandle, xpath + "/name", m_name);
  
          // write element description
          if (m_description) {
              tixi::TixiCreateElementIfNotExists(tixiHandle, xpath + "/description");
              tixi::TixiSaveElement(tixiHandle, xpath + "/description", *m_description);
          }
          else {
              if (tixi::TixiCheckElement(tixiHandle, xpath + "/description")) {
                  tixi::TixiRemoveElement(tixiHandle, xpath + "/description");
              }
          }
  
          // write element creator
          tixi::TixiCreateElementIfNotExists(tixiHandle, xpath + "/creator");
          tixi::TixiSaveElement(tixiHandle, xpath + "/creator", m_creator);
  
          // write element timestamp
          tixi::TixiCreateElementIfNotExists(tixiHandle, xpath + "/timestamp");
          tixi::TixiSaveElement(tixiHandle, xpath + "/timestamp", m_timestamp);
  
          // write element version
          tixi::TixiCreateElementIfNotExists(tixiHandle, xpath + "/version");
          tixi::TixiSaveElement(tixiHandle, xpath + "/version", m_version);
  
          // write element cpacsVersion
          if (m_cpacsVersion) {
              tixi::TixiCreateElementIfNotExists(tixiHandle, xpath + "/cpacsVersion");
              tixi::TixiSaveElement(tixiHandle, xpath + "/cpacsVersion", *m_cpacsVersion);
          }
          else {
              if (tixi::TixiCheckElement(tixiHandle, xpath + "/cpacsVersion")) {
                  tixi::TixiRemoveElement(tixiHandle, xpath + "/cpacsVersion");
              }
          }
  
          // write element updates
          if (m_updates) {
              tixi::TixiCreateElementIfNotExists(tixiHandle, xpath + "/updates");
              m_updates->WriteCPACS(tixiHandle, xpath + "/updates");
          }
          else {
              if (tixi::TixiCheckElement(tixiHandle, xpath + "/updates")) {
                  tixi::TixiRemoveElement(tixiHandle, xpath + "/updates");
              }
          }
  
      }
  
      const std::string& CPACSHeader::GetName() const
      {
          return m_name;
      }
  
      void CPACSHeader::SetName(const std::string& value)
      {
          m_name = value;
      }
  
      const boost::optional<std::string>& CPACSHeader::GetDescription() const
      {
          return m_description;
      }
  
      void CPACSHeader::SetDescription(const boost::optional<std::string>& value)
      {
          m_description = value;
      }
  
      const std::string& CPACSHeader::GetCreator() const
      {
          return m_creator;
      }
  
      void CPACSHeader::SetCreator(const std::string& value)
      {
          m_creator = value;
      }
  
      const std::time_t& CPACSHeader::GetTimestamp() const
      {
          return m_timestamp;
      }
  
      void CPACSHeader::SetTimestamp(const std::time_t& value)
      {
          m_timestamp = value;
      }
  
      const std::string& CPACSHeader::GetVersion() const
      {
          return m_version;
      }
  
      void CPACSHeader::SetVersion(const std::string& value)
      {
          m_version = value;
      }
  
      const boost::optional<std::string>& CPACSHeader::GetCpacsVersion() const
      {
          return m_cpacsVersion;
      }
  
      void CPACSHeader::SetCpacsVersion(const boost::optional<std::string>& value)
      {
          m_cpacsVersion = value;
      }
  
      const boost::optional<CPACSUpdates>& CPACSHeader::GetUpdates() const
      {
          return m_updates;
      }
  
      boost::optional<CPACSUpdates>& CPACSHeader::GetUpdates()
      {
          return m_updates;
      }
  
      CPACSUpdates& CPACSHeader::GetUpdates(CreateIfNotExistsTag)
      {
          if (!m_updates)
              m_updates = boost::in_place(this);
          return *m_updates;
      }
  
      void CPACSHeader::RemoveUpdates()
      {
          m_updates = boost::none;
      }
  
  } // namespace generated
  } // namespace tigl
  