mirror of
https://github.com/oatpp/oatpp.git
synced 2025-04-18 19:00:23 +08:00
oatpp::data: introduce class 'Bundle'.
This commit is contained in:
parent
de4de060cc
commit
904be7ca61
@ -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
|
||||
|
37
src/oatpp/core/data/Bundle.cpp
Normal file
37
src/oatpp/core/data/Bundle.cpp
Normal 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;
|
||||
}
|
||||
|
||||
}}
|
61
src/oatpp/core/data/Bundle.hpp
Normal file
61
src/oatpp/core/data/Bundle.hpp
Normal 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
|
Loading…
x
Reference in New Issue
Block a user