Antkeeper  0.0.1
body-size-loader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
25 #include <stdexcept>
26 
27 using namespace ::ant;
28 
29 static void deserialize_body_size_phene(phene::body_size& phene, const json& phene_element, resource_manager* resource_manager)
30 {
31  phene.min_mesosoma_length = 1.0f;
32  phene.max_mesosoma_length = 1.0f;
33  phene.mean_mesosoma_length = 1.0f;
34 
35  // Parse min mesosoma length
36  if (auto element = phene_element.find("min_mesosoma_length"); element != phene_element.end())
37  phene.min_mesosoma_length = element->get<float>();
38 
39  // Parse max mesosoma length
40  if (auto element = phene_element.find("max_mesosoma_length"); element != phene_element.end())
41  phene.max_mesosoma_length = element->get<float>();
42 
43  // Parse mean mesosoma length
44  if (auto element = phene_element.find("mean_mesosoma_length"); element != phene_element.end())
45  phene.mean_mesosoma_length = element->get<float>();
46 }
47 
48 template <>
49 gene::body_size* resource_loader<gene::body_size>::load(resource_manager* resource_manager, PHYSFS_File* file, const std::filesystem::path& path)
50 {
51  // Load JSON data
53 
54  // Validate gene file
55  auto body_size_element = data->find("body_size");
56  if (body_size_element == data->end())
57  throw std::runtime_error("Invalid body size gene.");
58 
59  // Allocate gene
61 
62  // Deserialize gene
63  gene::deserialize_gene(*body_size, &deserialize_body_size_phene, *body_size_element, resource_manager);
64 
65  // Free JSON data
66  delete data;
67 
68  return body_size;
69 }
static T * load(resource_manager *resourceManager, PHYSFS_File *file, const std::filesystem::path &path)
Loads resource data.
Loads resources.
nlohmann::json json
JSON data.
Definition: json.hpp:26
polyphenic_gene< phene::body_size > body_size
Polyphenic body size gene.
void deserialize_gene(monophenic_gene< T > &gene, void(*deserialize_phene)(T &, const json &, resource_manager *), const json &gene_element, resource_manager *resource_manager)
Deserializes a gene.
Definition: gene-loader.hpp:43
Definition: caste.hpp:25