oatpp::data: introduce class 'Bundle'.

This commit is contained in:
lganzzzo 2021-10-14 01:09:09 +03:00
parent de4de060cc
commit 904be7ca61
3 changed files with 100 additions and 0 deletions

View File

@ -115,6 +115,8 @@ add_library(oatpp
oatpp/core/data/stream/Stream.hpp
oatpp/core/data/stream/StreamBufferedProxy.cpp
oatpp/core/data/stream/StreamBufferedProxy.hpp
oatpp/core/data/Bundle.cpp
oatpp/core/data/Bundle.hpp
oatpp/core/macro/basic.hpp
oatpp/core/macro/codegen.hpp
oatpp/core/macro/component.hpp

View File

@ -0,0 +1,37 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* 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.
*
***************************************************************************/
#include "Bundle.hpp"
namespace oatpp { namespace data {
void Bundle::put(const oatpp::String& key, const oatpp::Void& polymorph) {
m_data.insert({key, polymorph});
}
const std::unordered_map<oatpp::String, oatpp::Void>& Bundle::getAll() const {
return m_data;
}
}}

View File

@ -0,0 +1,61 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* 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 oatpp_data_Bundle_hpp
#define oatpp_data_Bundle_hpp
#include "oatpp/core/Types.hpp"
namespace oatpp { namespace data {
class Bundle {
private:
std::unordered_map<oatpp::String, oatpp::Void> m_data;
public:
void put(const oatpp::String& key, const oatpp::Void& polymorph);
template<typename WrapperType>
WrapperType get(const oatpp::String& key) const {
auto it = m_data.find(key);
if(it == m_data.end()) {
throw std::runtime_error("[oatpp::data::Bundle::get()]: "
"Error. Data not found for key '" + *key + "'.");
}
if(it->second.valueType != WrapperType::Class::getType()) {
throw std::runtime_error("[oatpp::data::Bundle::get()]: Error. Type mismatch. Stored '" +
std::string(it->second.valueType->classId.name) +
"' vs requested '" + std::string(WrapperType::Class::getType()->classId.name) + "'.");
}
return it->second.template staticCast<WrapperType>();
}
const std::unordered_map<oatpp::String, oatpp::Void>& getAll() const;
};
}}
#endif //oatpp_data_Bundle_hpp