SerializationNode.cpp 7.67 KB
Newer Older
1
2
3
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
Evan Pretti's avatar
Evan Pretti committed
4
5
 * This is part of the OpenMM molecular simulation toolkit.                   *
 * See https://openmm.org/development.                                        *
6
 *                                                                            *
7
 * Portions copyright (c) 2010-2026 Stanford University and the Authors.      *
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

#include "openmm/serialization/SerializationNode.h"
#include "openmm/OpenMMException.h"
32
#include <cstring>
33
34
35
36
37
#include <sstream>

using namespace OpenMM;
using namespace std;

38
extern "C" char* g_fmt(char*, double);
Peter Eastman's avatar
Peter Eastman committed
39
extern "C" double strtod2(const char* s00, char** se);
40

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const string& SerializationNode::getName() const {
    return name;
}

void SerializationNode::setName(const string& name) {
    this->name = name;
}

const vector<SerializationNode>& SerializationNode::getChildren() const {
    return children;
}

vector<SerializationNode>& SerializationNode::getChildren() {
    return children;
}

const SerializationNode& SerializationNode::getChildNode(const std::string& name) const {
peastman's avatar
peastman committed
58
59
60
    for (auto& child : children)
        if (child.name == name)
            return child;
61
62
63
64
        throw OpenMMException("Unknown child '"+name+"' for node '"+getName()+"'");
}

SerializationNode& SerializationNode::getChildNode(const std::string& name) {
peastman's avatar
peastman committed
65
66
67
    for (auto& child : children)
        if (child.name == name)
            return child;
68
69
70
        throw OpenMMException("Unknown child '"+name+"' for node '"+getName()+"'");
}

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const map<string, string> SerializationNode::getProperties() const {
    map<string, string> result;
    int start = 0;
    while (start < properties.size()) {
        string name(&properties[start]);
        start += name.size()+1;
        string value(&properties[start]);
        start += value.size()+1;
        result[name] = value;
    }
    return result;
}

const char* SerializationNode::findPropertyValue(const std::string& name, bool required) const {
    int start = 0;
    while (start < properties.size()) {
        if (strcmp(&properties[start], name.data()) == 0)
            return &properties[start+name.size()+1];

        // This isn't the property we're looking for.  Skip over the name and value.

        start += strlen(&properties[start])+1;
        start += strlen(&properties[start])+1;
    }
    if (required)
        throw OpenMMException("Unknown property '"+name+"' in node '"+getName()+"'");

    // We didn't find it, but that's ok.  Report that it wasn't found.

    return nullptr;
101
102
103
}

bool SerializationNode::hasProperty(const string& name) const {
104
    return findPropertyValue(name, false) != nullptr;
105
106
}

107
108
const string SerializationNode::getStringProperty(const string& name) const {
    return string(findPropertyValue(name, true));
109
110
}

111
112
113
const string SerializationNode::getStringProperty(const string& name, const string& defaultValue) const {
    const char* value = findPropertyValue(name, false);
    if (value == nullptr)
114
        return defaultValue;
115
    return string(value);
116
117
118
}

SerializationNode& SerializationNode::setStringProperty(const string& name, const string& value) {
119
120
121
122
123
124
    int s = properties.size()+name.size()+value.size()+2;
    properties.reserve(properties.size()+name.size()+value.size()+2);
    properties.append(name);
    properties.push_back(0);
    properties.append(value);
    properties.push_back(0);
125
126
127
128
129
    return *this;
}

int SerializationNode::getIntProperty(const string& name) const {
    int value;
130
    stringstream(findPropertyValue(name, true)) >> value;
131
132
133
134
    return value;
}

int SerializationNode::getIntProperty(const string& name, int defaultValue) const {
135
136
    const char* value = findPropertyValue(name, false);
    if (value == nullptr)
137
        return defaultValue;
138
139
140
    int result;
    stringstream(value) >> result;
    return result;
141
142
143
144
145
}

SerializationNode& SerializationNode::setIntProperty(const string& name, int value) {
    stringstream s;
    s << value;
146
    return setStringProperty(name, s.str());
147
148
}

149
150
long long SerializationNode::getLongProperty(const string& name) const {
    long long value;
151
    stringstream(findPropertyValue(name, true)) >> value;
152
153
154
155
    return value;
}

long long SerializationNode::getLongProperty(const string& name, long long defaultValue) const {
156
157
    const char* value = findPropertyValue(name, false);
    if (value == nullptr)
158
        return defaultValue;
159
160
161
    long long result;
    stringstream(value) >> result;
    return result;
162
163
164
165
166
}

SerializationNode& SerializationNode::setLongProperty(const string& name, long long value) {
    stringstream s;
    s << value;
167
    return setStringProperty(name, s.str());
168
}
169

170
bool SerializationNode::getBoolProperty(const string& name) const {
171
    bool value;
172
    stringstream(findPropertyValue(name, true)) >> value;
173
174
175
    return value;
}

176
bool SerializationNode::getBoolProperty(const string& name, bool defaultValue) const {
177
178
    const char* value = findPropertyValue(name, false);
    if (value == nullptr)
179
        return defaultValue;
180
181
182
    bool result;
    stringstream(value) >> result;
    return result;
183
184
185
186
187
}

SerializationNode& SerializationNode::setBoolProperty(const string& name, bool value) {
    stringstream s;
    s << value;
188
    return setStringProperty(name, s.str());
189
190
191
}

double SerializationNode::getDoubleProperty(const string& name) const {
192
    return strtod2(findPropertyValue(name, true), NULL);
193
194
195
}

double SerializationNode::getDoubleProperty(const string& name, double defaultValue) const {
196
197
    const char* value = findPropertyValue(name, false);
    if (value == nullptr)
198
        return defaultValue;
199
    return strtod2(value, NULL);
200
201
202
}

SerializationNode& SerializationNode::setDoubleProperty(const string& name, double value) {
203
204
    char buffer[32];
    g_fmt(buffer, value);
205
    return setStringProperty(name, string(buffer));
206
207
208
209
210
211
212
}

SerializationNode& SerializationNode::createChildNode(const std::string& name) {
    children.push_back(SerializationNode());
    children.back().setName(name);
    return children.back();
}