Skip to content

Commit

Permalink
Removed independent library build/install targets
Browse files Browse the repository at this point in the history
Added hello world example for fetching MDIO in CMake

Update header guard

Disable linting in header include

Updated hello world CMakeLists to point to mainline MDIO
  • Loading branch information
BrianMichell committed Aug 2, 2024
1 parent 7873817 commit dfc5798
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 390 deletions.
28 changes: 28 additions & 0 deletions examples/hello_mdio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.24)
project(hello_world_project VERSION 1.0.0 LANGUAGES CXX)

# Set the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Include FetchContent module
include(FetchContent)

# Fetch the mdio-cpp library from the specified branch
FetchContent_Declare(
mdio
GIT_REPOSITORY https://github.com/TGSAI/mdio-cpp.git
GIT_TAG main
)
FetchContent_MakeAvailable(mdio)

# Create an executable target
add_executable(hello_mdio src/hello_mdio.cc)

# Link the mdio library to the executable
target_link_libraries(hello_mdio PRIVATE
mdio
${mdio_INTERNAL_DEPS}
)


67 changes: 67 additions & 0 deletions examples/hello_mdio/src/hello_mdio.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 TGS

// 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.

/**
* @file Provides a hello world example of using the MDIO library.
* Goals:
* 1. Provide a simple example of including the MDIO library in a CMake project.
* 2. Provide a simple playground for users to experiment and learn the MDIO
* library.
*/

#include "hello_mdio.h" // NOLINT [build/include_subdir]

#include <string>
#include <vector>

/**
* @brief Attempts to fetch a Variable from the dataset and print it to stdout.
* @param ds The opened dataset
* @param varName The name of the Variable in the Dataset
* @return An ok() Result status if the Variable was successfully printed, or an
* error Result status otherwise.
*/
mdio::Result<void> PrintVariable(const mdio::Dataset ds,
const std::string& varName) {
MDIO_ASSIGN_OR_RETURN(mdio::Variable var, ds.variables.at(varName));
std::cout << var << std::endl;
return absl::OkStatus();
}

int main() {
nlohmann::json schema = get_dataset_schema();
std::string path = "hello.mdio";

mdio::Future<mdio::Dataset> dsRes =
mdio::Dataset::from_json(schema, path, mdio::constants::kCreateClean);

if (!dsRes.status().ok()) {
std::cerr << dsRes.status() << std::endl;
return 1;
}
mdio::Dataset ds = dsRes.value();

std::vector<std::string> sortedNames = ds.variables.get_iterable_accessor();
mdio::Result<void> printRes;

for (std::string& varName : sortedNames) {
printRes = PrintVariable(ds, varName);
if (!printRes.status().ok()) {
std::cerr << printRes.status() << std::endl;
return 1;
}
}

return 0;
}
63 changes: 63 additions & 0 deletions examples/hello_mdio/src/hello_mdio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2024 TGS

// 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.

#ifndef EXAMPLES_HELLO_MDIO_SRC_HELLO_MDIO_H_
#define EXAMPLES_HELLO_MDIO_SRC_HELLO_MDIO_H_

#include <mdio/mdio.h>

#include <iostream>
#include <string>
#include <nlohmann/json.hpp>

/**
* @brief Defines a schema for a dataset with 3 variables: X, Y, and Grid.
* @return A JSON object representing the dataset schema.
*/
nlohmann::json get_dataset_schema() {
std::string schemaStr = R"(
{
"metadata": {
"apiVersion": "1.0.0",
"name": "Demo MDIO",
"createdOn": "2024-08-01T15:50:00.000000Z"
},
"variables": [
{
"name": "X",
"dataType": "uint32",
"dimensions": [{"name": "X", "size": 10}]
},
{
"name": "Y",
"dataType": "uint32",
"dimensions": [{"name": "Y", "size": 10}]
},
{
"name": "Grid",
"dataType": "float32",
"dimensions": ["X", "Y"]
}
]
}
)";
try {
return nlohmann::json::parse(schemaStr);
} catch (nlohmann::json::parse_error& e) {
std::cerr << "Failed to parse schema: " << e.what() << std::endl;
}
return nlohmann::json();
}

#endif // EXAMPLES_HELLO_MDIO_SRC_HELLO_MDIO_H_
Loading

0 comments on commit dfc5798

Please sign in to comment.