-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dto): add custom dto vector type
This improves performances by avoiding conversion to oatpp::Vector<oatpp::Void>
- Loading branch information
1 parent
4435540
commit 01222db
Showing
8 changed files
with
365 additions
and
128 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
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,275 @@ | ||
/** | ||
* DeepDetect | ||
* Copyright (c) 2021 Jolibrain SASU | ||
* Author: Louis Jean <louis.jean@jolibrain.com> | ||
* | ||
* This file is part of deepdetect. | ||
* | ||
* deepdetect is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* deepdetect is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef DD_DTO_TYPES_HPP | ||
#define DD_DTO_TYPES_HPP | ||
|
||
#include "oatpp/core/Types.hpp" | ||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp" | ||
|
||
namespace dd | ||
{ | ||
namespace DTO | ||
{ | ||
struct VGpuIds | ||
{ | ||
std::vector<int> _ids; | ||
|
||
VGpuIds() : _ids({ 0 }) | ||
{ | ||
} | ||
VGpuIds(const oatpp::Vector<oatpp::Int32> &vec) | ||
: _ids(vec->begin(), vec->end()) | ||
{ | ||
} | ||
VGpuIds(oatpp::Int32 i) : _ids({ i }) | ||
{ | ||
} | ||
}; | ||
|
||
template <typename T> struct VVector | ||
{ | ||
std::vector<T> _vec; | ||
|
||
VVector() = default; | ||
|
||
VVector(const std::vector<T> &vec) : _vec(vec) | ||
{ | ||
} | ||
VVector(std::vector<T> &&vec) : _vec(vec) | ||
{ | ||
} | ||
std::vector<T> &operator*() | ||
{ | ||
return _vec; | ||
} | ||
const std::vector<T> &operator*() const | ||
{ | ||
return _vec; | ||
} | ||
std::vector<T> *operator->() | ||
{ | ||
return &_vec; | ||
} | ||
const std::vector<T> operator->() const | ||
{ | ||
return &_vec; | ||
} | ||
}; | ||
|
||
namespace __class | ||
{ | ||
class GpuIdsClass; | ||
template <typename T> class DTOVectorClass; | ||
} | ||
|
||
typedef oatpp::data::mapping::type::Primitive<VGpuIds, | ||
__class::GpuIdsClass> | ||
GpuIds; | ||
template <typename T> | ||
using DTOVector | ||
= oatpp::data::mapping::type::Primitive<VVector<T>, | ||
__class::DTOVectorClass<T>>; | ||
|
||
namespace __class | ||
{ | ||
class GpuIdsClass | ||
{ | ||
public: | ||
static const oatpp::ClassId CLASS_ID; | ||
|
||
static oatpp::Type *getType() | ||
{ | ||
static oatpp::Type type(CLASS_ID, nullptr, nullptr); | ||
return &type; | ||
} | ||
}; | ||
|
||
template <typename T> class DTOVectorClass | ||
{ | ||
public: | ||
static const oatpp::ClassId CLASS_ID; | ||
|
||
static oatpp::Type *getType() | ||
{ | ||
static oatpp::Type type(CLASS_ID, nullptr, nullptr); | ||
return &type; | ||
} | ||
}; | ||
|
||
template class DTOVectorClass<double>; | ||
template class DTOVectorClass<bool>; | ||
} | ||
|
||
// ==== Serialization methods | ||
|
||
static inline oatpp::Void | ||
gpuIdsDeserialize(oatpp::parser::json::mapping::Deserializer *deserializer, | ||
oatpp::parser::Caret &caret, | ||
const oatpp::Type *const type) | ||
{ | ||
(void)type; | ||
if (caret.isAtChar('[')) | ||
{ | ||
return GpuIds(VGpuIds{ | ||
deserializer | ||
->deserialize(caret, | ||
oatpp::Vector<oatpp::Int32>::Class::getType()) | ||
.staticCast<oatpp::Vector<oatpp::Int32>>() }); | ||
} | ||
else | ||
{ | ||
return GpuIds(VGpuIds{ | ||
deserializer->deserialize(caret, oatpp::Int32::Class::getType()) | ||
.staticCast<oatpp::Int32>() }); | ||
} | ||
} | ||
|
||
static inline void | ||
gpuIdsSerialize(oatpp::parser::json::mapping::Serializer *serializer, | ||
oatpp::data::stream::ConsistentOutputStream *stream, | ||
const oatpp::Void &obj) | ||
{ | ||
auto gpuid = obj.staticCast<GpuIds>(); | ||
if (gpuid->_ids.size() == 1) | ||
{ | ||
oatpp::Int32 id = gpuid->_ids[0]; | ||
serializer->serializeToStream(stream, id); | ||
} | ||
else | ||
{ | ||
oatpp::Vector<oatpp::Int32> ids; | ||
for (auto i : gpuid->_ids) | ||
{ | ||
ids->push_back(i); | ||
} | ||
serializer->serializeToStream(stream, ids); | ||
} | ||
} | ||
|
||
// Inspired by oatpp json deserializer | ||
template <typename T> inline T readVecElement(oatpp::parser::Caret &caret); | ||
|
||
template <> | ||
inline double readVecElement<double>(oatpp::parser::Caret &caret) | ||
{ | ||
return caret.parseFloat64(); | ||
} | ||
|
||
template <> inline bool readVecElement<bool>(oatpp::parser::Caret &caret) | ||
{ | ||
if (caret.isAtText("true")) | ||
{ | ||
return true; | ||
} | ||
else if (caret.isAtText("false")) | ||
{ | ||
return false; | ||
} | ||
else | ||
{ | ||
caret.setError("[readVecElement<bool>] expected 'true' or 'false'", | ||
oatpp::parser::json::mapping::Deserializer:: | ||
ERROR_CODE_VALUE_BOOLEAN); | ||
return false; | ||
} | ||
} | ||
|
||
template <typename T> | ||
static inline oatpp::Void | ||
vectorDeserialize(oatpp::parser::json::mapping::Deserializer *deserializer, | ||
oatpp::parser::Caret &caret, | ||
const oatpp::Type *const type) | ||
{ | ||
(void)deserializer; | ||
(void)type; | ||
if (caret.isAtText("null", true)) | ||
{ | ||
return oatpp::Void(type); | ||
} | ||
|
||
if (caret.canContinueAtChar('[', 1)) | ||
{ | ||
std::vector<T> vec; | ||
|
||
while (!caret.isAtChar(']') && caret.canContinue()) | ||
{ | ||
caret.skipBlankChars(); | ||
vec.push_back(caret.parseFloat64()); | ||
if (caret.hasError()) | ||
{ | ||
return nullptr; | ||
} | ||
caret.skipBlankChars(); | ||
caret.canContinueAtChar(',', 1); | ||
} | ||
|
||
if (!caret.canContinueAtChar(']', 1)) | ||
{ | ||
if (!caret.hasError()) | ||
{ | ||
caret.setError( | ||
"[oatpp::parser::json::mapping::Deserializer::" | ||
"deserializeList()]: Error. ']' - expected", | ||
oatpp::parser::json::mapping::Deserializer:: | ||
ERROR_CODE_ARRAY_SCOPE_CLOSE); | ||
} | ||
return nullptr; | ||
}; | ||
|
||
return DTOVector<T>(std::move(vec)); | ||
} | ||
else | ||
{ | ||
caret.setError("[oatpp::parser::json::mapping::Deserializer::" | ||
"deserializeList()]: Error. '[' - expected", | ||
oatpp::parser::json::mapping::Deserializer:: | ||
ERROR_CODE_ARRAY_SCOPE_OPEN); | ||
return nullptr; | ||
} | ||
} | ||
|
||
template <typename T> | ||
static inline void | ||
vectorSerialize(oatpp::parser::json::mapping::Serializer *serializer, | ||
oatpp::data::stream::ConsistentOutputStream *stream, | ||
const oatpp::Void &obj) | ||
{ | ||
(void)serializer; | ||
auto vec = obj.staticCast<DTOVector<T>>(); | ||
stream->writeCharSimple('['); | ||
bool first = true; | ||
|
||
for (auto val : vec->_vec) | ||
{ | ||
if (first) | ||
first = false; | ||
else | ||
stream->writeCharSimple(','); | ||
stream->writeAsString(val); | ||
} | ||
|
||
stream->writeCharSimple(']'); | ||
} | ||
} | ||
} | ||
|
||
#endif // DD_DTO_TYPES_HPP |
Oops, something went wrong.