-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from LBNL-ETA/BaseLoaderRoutines
Base loader routines
- Loading branch information
Showing
12 changed files
with
630 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
cmake_minimum_required(VERSION 3.11) | ||
|
||
include(FetchContent) | ||
if(NOT TARGET xmlParser) | ||
|
||
FetchContent_Declare( | ||
XMLParser | ||
GIT_REPOSITORY https://github.com/LBNL-ETA/XMLParser.git | ||
GIT_TAG v1.0.0 | ||
) | ||
include(FetchContent) | ||
|
||
FetchContent_MakeAvailable(XMLParser) | ||
FetchContent_Declare( | ||
XMLParser | ||
GIT_REPOSITORY https://github.com/LBNL-ETA/XMLParser.git | ||
GIT_TAG v1.0.1 | ||
) | ||
|
||
FetchContent_MakeAvailable(XMLParser) | ||
endif() | ||
|
||
# Assuming the target name is xmlParser | ||
target_include_directories(xmlParser SYSTEM PUBLIC ${xmlParser_INCLUDE_DIRS}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,60 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(FileParse CXX) | ||
|
||
# Set the runtime library to dynamic for all configurations in MSVC | ||
if (MSVC) | ||
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") | ||
endif() | ||
|
||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | ||
|
||
set(LIB_MAJOR_VERSION "1") | ||
set(LIB_MINOR_VERSION "1") | ||
set(LIB_PATCH_VERSION "0") | ||
set(LIB_VERSION_STRING "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_PATCH_VERSION}") | ||
|
||
cmake_minimum_required(VERSION 3.5) | ||
|
||
set(LIB_NAME ${PROJECT_NAME}) | ||
|
||
if(NOT "${CMAKE_CXX_STANDARD}") | ||
set(CMAKE_CXX_STANDARD 17) | ||
endif() | ||
|
||
if(NOT "${CMAKE_CXX_EXTENSIONS}") | ||
set(CMAKE_CXX_EXTENSIONS OFF) | ||
endif() | ||
# Set C++ standard and extensions | ||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
set(CMAKE_CXX_EXTENSIONS OFF) | ||
|
||
include(GNUInstallDirs) | ||
if(NOT "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}") | ||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) | ||
endif() | ||
|
||
if(NOT "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) | ||
endif() | ||
|
||
if(NOT "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) | ||
endif() | ||
# Set output directories | ||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) | ||
|
||
# Include additional CMake files | ||
include(CMakeLists-xmlParser.txt) | ||
|
||
add_subdirectory(include/fileParse) | ||
|
||
# Include directories for targets | ||
target_include_directories(${LIB_NAME} | ||
PUBLIC | ||
PUBLIC | ||
$<INSTALL_INTERFACE:include> | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
PRIVATE | ||
PRIVATE | ||
${CMAKE_CURRENT_SOURCE_DIR}/include | ||
) | ||
|
||
option(BUILD_FileParse_tests "Build FileParse tests." ON) | ||
|
||
if(BUILD_FileParse_tests) | ||
enable_testing() | ||
add_subdirectory(test) | ||
endif() | ||
enable_testing() | ||
add_subdirectory(test) | ||
endif() | ||
|
||
# Explicitly set the runtime library for each target | ||
if (MSVC) | ||
set_target_properties(${LIB_NAME} PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") | ||
|
||
# Set runtime for tests if enabled | ||
if(TARGET FileParse-test) | ||
set_target_properties(FileParse-test PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") | ||
endif() | ||
|
||
# Repeat for other targets, including any third-party or external targets | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <fstream> | ||
#include <sstream> | ||
#include <optional> | ||
|
||
#include "XMLNodeAdapter.hxx" | ||
|
||
namespace Common | ||
{ | ||
namespace | ||
{ | ||
void createFileFromString(std::string_view fileName, std::string_view fileContent) | ||
{ | ||
std::ofstream out(fileName.data()); | ||
out << fileContent; | ||
out.close(); | ||
} | ||
} // namespace | ||
|
||
template<typename T> | ||
std::optional<T> loadFromXMLString(const std::string & data, const std::string & nodeTypeName) | ||
{ | ||
// Attempt to load the top node for the given type | ||
const auto xmlNode = getTopNodeFromString(data, nodeTypeName); | ||
|
||
// Create an instance of the type | ||
if(xmlNode.has_value()) | ||
{ | ||
T model; | ||
// Assume that `operator>>` is overloaded for T and xmlNode type | ||
xmlNode.value() >> model; | ||
return model; | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
template<typename T> | ||
std::optional<T> loadFromXMLFile(std::string_view fileName, const std::string & nodeTypeName) | ||
{ | ||
// Convert std::string_view to std::string for file operations | ||
std::string fileNameStr(fileName); | ||
|
||
// Check if file exists and is accessible; if not, create it with default content | ||
if(std::ifstream f(fileNameStr.c_str()); !f.good()) | ||
{ | ||
const std::string fileContent = "<" + nodeTypeName + ">\n</" + nodeTypeName + ">"; | ||
createFileFromString(fileNameStr, fileContent); | ||
} | ||
|
||
// Attempt to load the top node for the given type | ||
const auto xmlNode = getTopNodeFromFile(fileNameStr, nodeTypeName); | ||
|
||
|
||
if(xmlNode.has_value()) | ||
{ | ||
T model; | ||
// Assume that `operator>>` is overloaded for T and xmlNode type | ||
xmlNode.value() >> model; | ||
return model; | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
template<typename T> | ||
int saveToXMLFile(const T & object, std::string_view fileName, const std::string & nodeName) | ||
|
||
{ | ||
auto node = createTopNode(nodeName); | ||
|
||
node << object; | ||
|
||
return node.writeToFile(fileName.data()); | ||
} | ||
|
||
template<typename T> | ||
std::string saveToXMLString(const T & object, const std::string & nodeName) | ||
{ | ||
auto node = createTopNode(nodeName); | ||
|
||
node << object; | ||
|
||
return node.getContent(); | ||
} | ||
} // namespace Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.