123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805 |
- /***************************************************************************
- *
- * 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_type_Primitive_hpp
- #define oatpp_data_type_Primitive_hpp
- #include "./Type.hpp"
- #include "oatpp/core/base/Countable.hpp"
- #include <algorithm>
- #include <cctype>
- #include <iterator>
- namespace oatpp { namespace data { namespace mapping { namespace type {
- namespace __class {
- class String; // FWD
- class Int8; // FWD
- class UInt8; // FWD
- class Int16; // FWD
- class UInt16; // FWD
- class Int32; // FWD
- class UInt32; // FWD
- class Int64; // FWD
- class UInt64; // FWD
- class Float32; // FWD
- class Float64; // FWD
- class Boolean; // FWD
- }
- /**
- * Mapping-enables String is &id:type::ObjectWrapper; over `std::string`;
- */
- class String : public type::ObjectWrapper<std::string, __class::String> {
- public:
- String(const std::shared_ptr<std::string>& ptr, const type::Type* const valueType);
- public:
- String() {}
- explicit String(v_buff_size size)
- : type::ObjectWrapper<std::string, __class::String>(std::make_shared<std::string>(size, '\0'))
- {}
- String(const char* data, v_buff_size size)
- : type::ObjectWrapper<std::string, __class::String>(std::make_shared<std::string>(data, size))
- {}
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
- >
- String(T) {}
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, char>::value, void>::type
- >
- String(const T* data)
- : type::ObjectWrapper<std::string, __class::String>(
- data == nullptr ? nullptr : std::make_shared<std::string>(data)
- )
- {}
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::string>::value, void>::type
- >
- String(const T& str)
- : type::ObjectWrapper<std::string, __class::String>(std::make_shared<std::string>(str))
- {}
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::string>::value, void>::type
- >
- String(T&& str)
- : type::ObjectWrapper<std::string, __class::String>(
- std::make_shared<std::string>(std::forward<std::string>(str))
- )
- {}
- String(const std::shared_ptr<std::string>& ptr)
- : type::ObjectWrapper<std::string, __class::String>(ptr)
- {}
- String(std::shared_ptr<std::string>&& ptr)
- : type::ObjectWrapper<std::string, __class::String>(std::forward<std::shared_ptr<std::string>>(ptr))
- {}
- String(const String& other)
- : type::ObjectWrapper<std::string, __class::String>(other)
- {}
- String(String&& other)
- : type::ObjectWrapper<std::string, __class::String>(std::forward<String>(other))
- {}
- /**
- * Load data from file and store in &id:oatpp::String;.
- * @param filename - name of the file.
- * @return - &id:oatpp::String;.
- */
- static String loadFromFile(const char* filename);
- /**
- * Save content of the buffer to file.
- * @param filename - name of the file.
- */
- void saveToFile(const char* filename) const;
- const std::string& operator*() const;
- operator std::string() const {
- if (this->m_ptr == nullptr) {
- throw std::runtime_error("[oatpp::data::mapping::type::String::operator std::string() const]: "
- "Error. Null pointer.");
- }
- return this->m_ptr.operator*();
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
- >
- inline String& operator = (std::nullptr_t) {
- m_ptr.reset();
- return *this;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, char>::value, void>::type
- >
- inline String& operator = (const T* str) {
- if (str) {
- m_ptr = std::make_shared<std::string>(str);
- } else {
- m_ptr.reset();
- }
- return *this;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::string>::value, void>::type
- >
- inline String& operator = (const T& str) {
- m_ptr = std::make_shared<std::string>(str);
- return *this;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::string>::value, void>::type
- >
- inline String& operator = (T&& str) {
- m_ptr = std::make_shared<std::string>(std::forward<std::string>(str));
- return *this;
- }
- inline String& operator = (const String& other){
- m_ptr = other.m_ptr;
- return *this;
- }
- inline String& operator = (String&& other) noexcept {
- m_ptr = std::move(other.m_ptr);
- return *this;
- }
- /**
- * Case insensitive compare (ASCII only).
- * @param other
- * @return
- */
- bool equalsCI_ASCII(const std::string& other);
- /**
- * Case insensitive compare (ASCII only).
- * @param other
- * @return
- */
- bool equalsCI_ASCII(const String& other);
- /**
- * Case insensitive compare (ASCII only).
- * @param other
- * @return
- */
- bool equalsCI_ASCII(const char* str);
- /**
- * Get underlying value.
- * @param defaultValue - value to return in case stored value is `nullptr`.
- * @return - value or `defaultValue` if stored value is `nullptr`.
- */
- std::string getValue(const std::string& defaultValue) const;
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
- >
- inline bool operator == (T) const {
- return m_ptr.get() == nullptr;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
- >
- inline bool operator != (T) const {
- return m_ptr.get() != nullptr;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, char>::value, void>::type
- >
- inline bool operator == (const T* str) const {
- if(!m_ptr) return str == nullptr;
- if(str == nullptr) return false;
- return *m_ptr == str;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, char>::value, void>::type
- >
- inline bool operator != (const T* str) const {
- return !operator == (str);
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::string>::value, void>::type
- >
- inline bool operator == (const T& str) const {
- if(!m_ptr) return false;
- return *m_ptr == str;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::string>::value, void>::type
- >
- inline bool operator != (const T& str) const {
- return !operator == (str);
- }
- inline bool operator == (const String &other) const {
- if(!m_ptr) return !other.m_ptr;
- if(!other.m_ptr) return false;
- return *m_ptr == *other.m_ptr;
- }
- inline bool operator != (const String &other) const {
- return !operator == (other);
- }
- };
- String operator + (const char* a, const String& b);
- String operator + (const String& a, const char* b);
- String operator + (const String& a, const String& b);
- /**
- * Template for primitive mapping-enabled types.
- * @tparam TValueType - type of the value ex.: v_int64.
- * @tparam Clazz - Class holding static class information.
- */
- template<typename TValueType, class Clazz>
- class Primitive : public type::ObjectWrapper<TValueType, Clazz> {
- public:
- typedef TValueType UnderlyingType;
- public:
- OATPP_DEFINE_OBJECT_WRAPPER_DEFAULTS(Primitive, TValueType, Clazz)
- Primitive(TValueType value)
- : type::ObjectWrapper<TValueType, Clazz>(std::make_shared<TValueType>(value))
- {}
- Primitive& operator = (TValueType value) {
- this->m_ptr = std::make_shared<TValueType>(value);
- return *this;
- }
- TValueType operator*() const {
- return this->m_ptr.operator*();
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
- >
- inline bool operator == (T){
- return this->m_ptr.get() == nullptr;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
- >
- inline bool operator != (T){
- return this->m_ptr.get() != nullptr;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, TValueType>::value, void>::type
- >
- inline bool operator == (T value) const {
- if(!this->m_ptr) return false;
- return *this->m_ptr == value;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, TValueType>::value, void>::type
- >
- inline bool operator != (T value) const {
- if(!this->m_ptr) return true;
- return *this->m_ptr != value;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, Primitive>::value, void>::type
- >
- inline bool operator == (const T &other) const {
- if(this->m_ptr.get() == other.m_ptr.get()) return true;
- if(!this->m_ptr || !other.m_ptr) return false;
- return *this->m_ptr == *other.m_ptr;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, Primitive>::value, void>::type
- >
- inline bool operator != (const T &other) const {
- return !operator == (other);
- }
- inline operator TValueType() const {
- return *this->m_ptr;
- }
- TValueType getValue(const TValueType& defaultValue) const {
- if(this->m_ptr) {
- return *this->m_ptr;
- }
- return defaultValue;
- }
- };
- /**
- * ObjectWrapper for Boolean.
- */
- class Boolean : public type::ObjectWrapper<bool, __class::Boolean> {
- public:
- typedef bool UnderlyingType;
- public:
- OATPP_DEFINE_OBJECT_WRAPPER_DEFAULTS(Boolean, bool, __class::Boolean)
- Boolean(bool value)
- : type::ObjectWrapper<bool, __class::Boolean>(std::make_shared<bool>(value))
- {}
- Boolean& operator = (bool value) {
- this->m_ptr = std::make_shared<bool>(value);
- return *this;
- }
- inline bool operator*() const {
- return this->m_ptr.operator*();
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
- >
- inline bool operator == (T){
- return m_ptr.get() == nullptr;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
- >
- inline bool operator != (T){
- return m_ptr.get() != nullptr;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, bool>::value, void>::type
- >
- inline bool operator == (T value) const {
- if(!this->m_ptr) return false;
- return *this->m_ptr == value;
- }
- template<typename T,
- typename enabled = typename std::enable_if<std::is_same<T, bool>::value, void>::type
- >
- inline bool operator != (T value) const {
- if(!this->m_ptr) return true;
- return *this->m_ptr != value;
- }
- inline bool operator == (const Boolean &other) const {
- if(this->m_ptr.get() == other.m_ptr.get()) return true;
- if(!this->m_ptr || !other.m_ptr) return false;
- return *this->m_ptr == *other.m_ptr;
- }
- inline bool operator != (const Boolean &other) const {
- return !operator == (other);
- }
- inline operator bool() const {
- if(this->m_ptr) {
- return *(this->m_ptr);
- }
- return false;
- }
- bool getValue(bool defaultValue) const {
- if(this->m_ptr) {
- return *this->m_ptr;
- }
- return defaultValue;
- }
- };
- /**
- * Int8 is an ObjectWrapper over `v_int8` and __class::Int8.
- */
- typedef Primitive<v_int8, __class::Int8> Int8;
- /**
- * UInt8 is an ObjectWrapper over `v_uint8` and __class::UInt8.
- */
- typedef Primitive<v_uint8, __class::UInt8> UInt8;
- /**
- * Int16 is an ObjectWrapper over `v_int16` and __class::Int16.
- */
- typedef Primitive<v_int16, __class::Int16> Int16;
- /**
- * UInt16 is an ObjectWrapper over `v_uint16` and __class::UInt16.
- */
- typedef Primitive<v_uint16, __class::UInt16> UInt16;
- /**
- * Int32 is an ObjectWrapper over `v_int32` and __class::Int32.
- */
- typedef Primitive<v_int32, __class::Int32> Int32;
- /**
- * UInt32 is an ObjectWrapper over `v_uint32` and __class::UInt32.
- */
- typedef Primitive<v_uint32, __class::UInt32> UInt32;
- /**
- * Int64 is an ObjectWrapper over `v_int64` and __class::Int64.
- */
- typedef Primitive<v_int64, __class::Int64> Int64;
- /**
- * UInt64 is an ObjectWrapper over `v_uint64` and __class::UInt64.
- */
- typedef Primitive<v_uint64, __class::UInt64> UInt64;
- /**
- * Float32 is an ObjectWrapper over `v_float32` and __class::Float32.
- */
- typedef Primitive<v_float32, __class::Float32> Float32;
- /**
- * Float64 is an ObjectWrapper over `v_float64` and __class::Float64.
- */
- typedef Primitive<v_float64, __class::Float64> Float64;
- template<>
- struct ObjectWrapperByUnderlyingType <v_int8> {
- typedef Int8 ObjectWrapper;
- };
- template<>
- struct ObjectWrapperByUnderlyingType <v_uint8> {
- typedef UInt8 ObjectWrapper;
- };
- template<>
- struct ObjectWrapperByUnderlyingType <v_int16> {
- typedef Int16 ObjectWrapper;
- };
- template<>
- struct ObjectWrapperByUnderlyingType <v_uint16> {
- typedef UInt16 ObjectWrapper;
- };
- template<>
- struct ObjectWrapperByUnderlyingType <v_int32> {
- typedef Int32 ObjectWrapper;
- };
- template<>
- struct ObjectWrapperByUnderlyingType <v_uint32> {
- typedef UInt32 ObjectWrapper;
- };
- template<>
- struct ObjectWrapperByUnderlyingType <v_int64> {
- typedef Int64 ObjectWrapper;
- };
- template<>
- struct ObjectWrapperByUnderlyingType <v_uint64> {
- typedef UInt64 ObjectWrapper;
- };
- template<>
- struct ObjectWrapperByUnderlyingType <bool> {
- typedef Boolean ObjectWrapper;
- };
- namespace __class {
- class String {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class Int8 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class UInt8 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class Int16 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class UInt16 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class Int32 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class UInt32 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class Int64 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class UInt64 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class Float32 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class Float64 {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- class Boolean {
- public:
- static const ClassId CLASS_ID;
- static Type* getType();
- };
- }
- }}}}
- namespace std {
- template<>
- struct hash<oatpp::data::mapping::type::String> {
- typedef oatpp::data::mapping::type::String argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& s) const noexcept {
- if(s.get() == nullptr) return 0;
- return hash<std::string> {} (*s);
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::Boolean> {
- typedef oatpp::data::mapping::type::Boolean argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 2;
- return result_type(*v);
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::Int8> {
- typedef oatpp::data::mapping::type::Int8 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return (result_type) *v;
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::UInt8> {
- typedef oatpp::data::mapping::type::UInt8 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return (result_type) *v;
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::Int16> {
- typedef oatpp::data::mapping::type::Int16 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return (result_type) *v;
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::UInt16> {
- typedef oatpp::data::mapping::type::UInt16 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return (result_type) *v;
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::Int32> {
- typedef oatpp::data::mapping::type::Int32 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return (result_type) *v;
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::UInt32> {
- typedef oatpp::data::mapping::type::UInt32 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return (result_type) *v;
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::Int64> {
- typedef oatpp::data::mapping::type::Int64 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return (result_type) *v;
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::UInt64> {
- typedef oatpp::data::mapping::type::UInt64 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return (result_type) *v;
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::Float32> {
- typedef oatpp::data::mapping::type::Float32 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return *((v_uint32*) v.get());
- }
- };
- template<>
- struct hash<oatpp::data::mapping::type::Float64> {
- typedef oatpp::data::mapping::type::Float64 argument_type;
- typedef v_uint64 result_type;
- result_type operator()(argument_type const& v) const noexcept {
- if(v.get() == nullptr) return 0;
- return *((result_type*) v.get());
- }
- };
- }
- #endif /* oatpp_base_Countable_PrimitiveDataTypes_hpp */
|