Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfFIO.h
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
20#ifndef _SOURCE_IO_TFFIO_H_
21#define _SOURCE_IO_TFFIO_H_
22
23
24#include "tf_io.h"
25
26
27namespace TissueForge::io {
28
29
37 CPPAPI_FUNC(std::string) toStr(IOElement &fileElement, const MetaData &metaData);
38
47 CPPAPI_FUNC(std::string) toStr(IOElement &fileElement);
48
56 CPPAPI_FUNC(IOElement) fromStr(const std::string &str, const MetaData &metaData);
57
66 CPPAPI_FUNC(IOElement) fromStr(const std::string &str);
67
76 template <typename T>
77 std::string toString(const T &dataElement, const MetaData &metaData) {
78 IOElement fe = IOElement::create();
79 if(toFile<T>(dataElement, metaData, fe) != S_OK)
80 return "";
81 return toStr(fe, metaData);
82 }
83
93 template <typename T>
94 std::string toString(const T &dataElement) {
95 return toString(dataElement, MetaData());
96 }
97
106 template <typename T>
107 T fromString(const std::string &str, const MetaData &metaData) {
108 IOElement fe = fromStr(str, metaData);
109 T de;
110 fromFile<T>(fe, metaData, &de);
111 return de;
112 }
113
123 template <typename T>
124 T fromString(const std::string &str) {
125 return fromString<T>(str, MetaData());
126 }
127
136 struct CAPI_EXPORT FIOImportSummary {
137
139 std::unordered_map<unsigned int, unsigned int> particleIdMap;
140
142 std::unordered_map<unsigned int, unsigned int> particleTypeIdMap;
143
144 };
145
146
151 struct FIOModule {
152
158 virtual std::string moduleName() = 0;
159
167 virtual HRESULT toFile(const MetaData &metaData, IOElement &fileElement) = 0;
168
176 virtual HRESULT fromFile(const MetaData &metaData, const IOElement &fileElement) = 0;
177
183
190 void load();
191
192 };
193
194
202 struct CAPI_EXPORT FIO {
203
205 static const std::string KEY_TYPE;
206
208 static const std::string KEY_VALUE;
209
211 static const std::string KEY_ROOT;
212
214 static const std::string KEY_METADATA;
215
217 static const std::string KEY_SIMULATOR;
218
220 static const std::string KEY_UNIVERSE;
221
223 static const std::string KEY_MODULES;
224
226 inline static FIOImportSummary *importSummary = NULL;
227
234
241
249
256 static IOElement fromFile(const std::string &loadFilePath);
257
265 static HRESULT fromFile(const std::string &loadFilePath, IOElement &el);
266
273 static HRESULT toFile(const std::string &saveFilePath);
274
280 static std::string toString();
281
288 static void registerModule(const std::string moduleName, FIOModule *module);
289
297 static void loadModule(const std::string moduleName);
298
305 static bool hasImport();
306
307 private:
308
309 inline static std::unordered_map<std::string, FIOModule*> *modules = NULL;
310
311 };
312
313};
314
315#endif // _SOURCE_IO_TFFIO_H_
Tissue Forge I/O.
Definition tfThreeDFAngleMeshGenerator.h:28
std::string toStr(IOElement &fileElement, const MetaData &metaData)
Generate a JSON string representation of an intermediate I/O object.
HRESULT toFile(const T &dataElement, const MetaData &metaData, IOElement &fileElement)
Convert an object to an intermediate I/O object.
std::string toString()
Return a simulation state as a JSON string.
T fromString(const std::string &str, const MetaData &metaData)
Generate an object from a JSON string.
Definition tfFIO.h:107
HRESULT fromFile(const IOElement &fileElement, const MetaData &metaData, T *dataElement)
Instantiate an object from an intermediate I/O object.
IOElement fromStr(const std::string &str, const MetaData &metaData)
Generate an intermediate I/O object from a JSON string.
Tissue Forge data import/export interface.
Definition tfFIO.h:202
static bool hasImport()
Test whether imported data is available.
static void loadModule(const std::string moduleName)
Load a module from imported data.
static HRESULT fromFile(const std::string &loadFilePath, IOElement &el)
Load a simulation from file.
static void registerModule(const std::string moduleName, FIOModule *module)
Register a module for I/O events.
static HRESULT releaseIORootElement()
Release current root element.
static const std::string KEY_VALUE
Definition tfFIO.h:208
static const std::string KEY_METADATA
Definition tfFIO.h:214
static FIOImportSummary * importSummary
Definition tfFIO.h:226
static const std::string KEY_MODULES
Definition tfFIO.h:223
static IOElement fromFile(const std::string &loadFilePath)
Load a simulation from file.
static IOElement generateIORootElement()
Generate root element from current simulation state.
static HRESULT getCurrentIORootElement(IOElement *el)
Get the current root element, if any.
static const std::string KEY_ROOT
Definition tfFIO.h:211
static const std::string KEY_UNIVERSE
Definition tfFIO.h:220
static const std::string KEY_TYPE
Definition tfFIO.h:205
static HRESULT toFile(const std::string &saveFilePath)
Save a simulation to file.
static const std::string KEY_SIMULATOR
Definition tfFIO.h:217
static std::string toString()
Return a simulation state as a JSON string.
Tissue Forge data import summary.
Definition tfFIO.h:136
std::unordered_map< unsigned int, unsigned int > particleTypeIdMap
Definition tfFIO.h:142
std::unordered_map< unsigned int, unsigned int > particleIdMap
Definition tfFIO.h:139
Interface for Tissue Forge peripheral module I/O (e.g., models)
Definition tfFIO.h:151
void load()
User-facing function to load module data from main import.
virtual HRESULT toFile(const MetaData &metaData, IOElement &fileElement)=0
Export module data.
void registerIOModule()
Register this module for I/O events.
virtual std::string moduleName()=0
Name of module. Used as a storage key.
virtual HRESULT fromFile(const MetaData &metaData, const IOElement &fileElement)=0
Import module data.
Container for _IOElement.
Definition tf_io.h:71
Tissue Forge meta data.
Definition tf_io.h:131
int32_t HRESULT
Definition tf_port.h:255