diff --git a/src/Makefile.dep b/src/Makefile.dep index 9345e1c..a63305e 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -2,50 +2,62 @@ baseexpression.o: baseexpression.h morm_types.h modelenv.h modeldata.h baseexpression.o: cursorhelper.h queryresult.h finderhelper.h -baseexpression.o: fieldvaluehelper.h ft.h model.h modelconnector.h clearer.h -baseexpression.o: dbconnector.h flatconnector.h dbexpression.h -baseexpression.o: flatexpression.h modelwrapper.h +baseexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h +baseexpression.o: baseobjectwrapper.h modelcontainerwrapper.h ft.h model.h +baseexpression.o: modelconnector.h clearer.h dbconnector.h flatconnector.h +baseexpression.o: dbexpression.h flatexpression.h clearer.o: clearer.h model.h modelconnector.h dbconnector.h queryresult.h clearer.o: ft.h flatconnector.h dbexpression.h baseexpression.h morm_types.h clearer.o: modelenv.h modeldata.h cursorhelper.h finderhelper.h -clearer.o: fieldvaluehelper.h flatexpression.h modelwrapper.h +clearer.o: fieldvaluehelper.h wrapper.h spacewrapper.h baseobjectwrapper.h +clearer.o: modelcontainerwrapper.h flatexpression.h dbconnector.o: dbconnector.h queryresult.h ft.h dbexpression.h dbconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h -dbconnector.o: cursorhelper.h finderhelper.h fieldvaluehelper.h model.h -dbconnector.o: modelconnector.h clearer.h flatconnector.h flatexpression.h -dbconnector.o: modelwrapper.h +dbconnector.o: cursorhelper.h finderhelper.h fieldvaluehelper.h wrapper.h +dbconnector.o: spacewrapper.h baseobjectwrapper.h modelcontainerwrapper.h +dbconnector.o: model.h modelconnector.h clearer.h flatconnector.h +dbconnector.o: flatexpression.h dbexpression.o: dbexpression.h baseexpression.h morm_types.h modelenv.h dbexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h -dbexpression.o: fieldvaluehelper.h ft.h +dbexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h +dbexpression.o: baseobjectwrapper.h modelcontainerwrapper.h ft.h flatconnector.o: flatconnector.h flatexpression.h baseexpression.h flatconnector.o: morm_types.h modelenv.h modeldata.h cursorhelper.h -flatconnector.o: queryresult.h finderhelper.h fieldvaluehelper.h ft.h model.h -flatconnector.o: modelconnector.h clearer.h dbconnector.h dbexpression.h -flatconnector.o: modelwrapper.h +flatconnector.o: queryresult.h finderhelper.h fieldvaluehelper.h wrapper.h +flatconnector.o: spacewrapper.h baseobjectwrapper.h modelcontainerwrapper.h +flatconnector.o: ft.h model.h modelconnector.h clearer.h dbconnector.h +flatconnector.o: dbexpression.h flatexpression.o: flatexpression.h baseexpression.h morm_types.h modelenv.h flatexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h -flatexpression.o: fieldvaluehelper.h ft.h +flatexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h +flatexpression.o: baseobjectwrapper.h modelcontainerwrapper.h ft.h jsonconnector.o: jsonconnector.h flatconnector.h jsonexpression.h jsonconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h jsonconnector.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h -jsonconnector.o: fieldvaluehelper.h ft.h +jsonconnector.o: fieldvaluehelper.h wrapper.h spacewrapper.h +jsonconnector.o: baseobjectwrapper.h modelcontainerwrapper.h ft.h jsonexpression.o: jsonexpression.h flatexpression.h baseexpression.h jsonexpression.o: morm_types.h modelenv.h modeldata.h cursorhelper.h -jsonexpression.o: queryresult.h finderhelper.h fieldvaluehelper.h ft.h +jsonexpression.o: queryresult.h finderhelper.h fieldvaluehelper.h wrapper.h +jsonexpression.o: spacewrapper.h baseobjectwrapper.h modelcontainerwrapper.h +jsonexpression.o: ft.h model.o: model.h modelconnector.h clearer.h dbconnector.h queryresult.h ft.h model.o: flatconnector.h dbexpression.h baseexpression.h morm_types.h model.o: modelenv.h modeldata.h cursorhelper.h finderhelper.h -model.o: fieldvaluehelper.h flatexpression.h modelwrapper.h +model.o: fieldvaluehelper.h wrapper.h spacewrapper.h baseobjectwrapper.h +model.o: modelcontainerwrapper.h flatexpression.h modelconnector.o: modelconnector.h clearer.h dbconnector.h queryresult.h ft.h modelconnector.o: flatconnector.h postgresqlconnector.o: postgresqlconnector.h dbconnector.h queryresult.h ft.h postgresqlconnector.o: postgresqlqueryresult.h postgresqlexpression.h postgresqlconnector.o: dbexpression.h baseexpression.h morm_types.h postgresqlconnector.o: modelenv.h modeldata.h cursorhelper.h finderhelper.h -postgresqlconnector.o: fieldvaluehelper.h +postgresqlconnector.o: fieldvaluehelper.h wrapper.h spacewrapper.h +postgresqlconnector.o: baseobjectwrapper.h modelcontainerwrapper.h postgresqlexpression.o: postgresqlexpression.h dbexpression.h postgresqlexpression.o: baseexpression.h morm_types.h modelenv.h modeldata.h postgresqlexpression.o: cursorhelper.h queryresult.h finderhelper.h -postgresqlexpression.o: fieldvaluehelper.h ft.h +postgresqlexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h +postgresqlexpression.o: baseobjectwrapper.h modelcontainerwrapper.h ft.h postgresqlqueryresult.o: postgresqlqueryresult.h queryresult.h queryresult.o: queryresult.h diff --git a/src/baseobjectwrapper.h b/src/baseobjectwrapper.h new file mode 100644 index 0000000..669a7b0 --- /dev/null +++ b/src/baseobjectwrapper.h @@ -0,0 +1,115 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2021, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef headerfile_morm_baseobjectwrapper +#define headerfile_morm_baseobjectwrapper + + + +namespace morm +{ + + +class BaseObjectWrapper +{ +public: + + BaseObjectWrapper() + { + auto_remove = true; + reference_counter = 1; + } + + virtual ~BaseObjectWrapper() + { + } + + virtual bool should_be_auto_removed() + { + return auto_remove; + } + + virtual void increment_reference_counter() + { + reference_counter += 1; + } + + virtual void decrement_reference_counter() + { + if( reference_counter > 0 ) + reference_counter -= 1; + } + + virtual size_t get_reference_counter() + { + return reference_counter; + } + + +protected: + + bool auto_remove; + size_t reference_counter; + + +private: + + /* + * don't copy these wrappers + */ + BaseObjectWrapper(const BaseObjectWrapper & wrapper) + { + operator=(wrapper); + } + + BaseObjectWrapper(BaseObjectWrapper && wrapper) + { + auto_remove = wrapper.auto_remove; + reference_counter = wrapper.reference_counter; + wrapper.auto_remove = false; + wrapper.reference_counter = 0; + } + + BaseObjectWrapper & operator=(const BaseObjectWrapper & wrapper) + { + auto_remove = wrapper.auto_remove; + reference_counter = 1; + return *this; + } + +}; + +} + +#endif diff --git a/src/model.cpp b/src/model.cpp index eca681b..2d4e1d0 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -240,7 +240,7 @@ void Model::get_table_name(std::string & str, bool with_schema_name, ModelData * } -Model * Model::get_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, bool put_log_if_not_found) +Model * Model::get_model(const wchar_t * db_field_name, const wchar_t * flat_field_name) { ModelEnv model_env_local; model_env = &model_env_local; @@ -261,24 +261,12 @@ Model * Model::get_model(const wchar_t * db_field_name, const wchar_t * flat_fie throw; } - if( !model_env->model && model_connector && put_log_if_not_found ) - { - pt::Log * plog = model_connector->get_logger(); - - if( plog ) - { - (*plog) << pt::Log::log1 << "Morm: I cannot find such a property: "; - put_fields_to_log(*plog, db_field_name, flat_field_name); - (*plog) << pt::Log::logend; - } - } - model_env = nullptr; return model; } -ModelWrapper * Model::get_model_wrapper(const wchar_t * db_field_name, const wchar_t * flat_field_name, bool put_log_if_not_found) +Wrapper Model::get_wrapper(const wchar_t * db_field_name, const wchar_t * flat_field_name) { ModelEnv model_env_local; model_env = &model_env_local; @@ -286,12 +274,10 @@ ModelWrapper * Model::get_model_wrapper(const wchar_t * db_field_name, const wch model_env->db_field_name = db_field_name; model_env->flat_field_name = flat_field_name; model_env->model = this; - ModelWrapper * model_wrapper = nullptr; try { fields(); - model_wrapper = model_env->model_wrapper; } catch(...) { @@ -299,31 +285,19 @@ ModelWrapper * Model::get_model_wrapper(const wchar_t * db_field_name, const wch throw; } - if( !model_env->model_wrapper && model_connector && put_log_if_not_found ) - { - pt::Log * plog = model_connector->get_logger(); - - if( plog ) - { - (*plog) << pt::Log::log1 << "Morm: I cannot find such a property: "; - put_fields_to_log(*plog, db_field_name, flat_field_name); - (*plog) << pt::Log::logend; - } - } - model_env = nullptr; - return model_wrapper; + return model_env_local.wrapper; } -bool Model::get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Stream & stream, bool clear_stream, bool put_log_if_not_found) +bool Model::get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Stream & stream, bool clear_stream) { - return get_raw_value(db_field_name, flat_field_name, nullptr, stream, clear_stream, put_log_if_not_found); + return get_raw_value(db_field_name, flat_field_name, nullptr, stream, clear_stream); } -bool Model::get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelData * model_data, pt::Stream & stream, bool clear_stream, bool put_log_if_not_found) +bool Model::get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelData * model_data, pt::Stream & stream, bool clear_stream) { if( clear_stream ) { @@ -350,18 +324,6 @@ bool Model::get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_fi throw; } - if( !model_env->model_wrapper && model_connector && put_log_if_not_found ) - { - pt::Log * plog = model_connector->get_logger(); - - if( plog ) - { - (*plog) << pt::Log::log1 << "Morm: I cannot find such a property: "; - put_fields_to_log(*plog, db_field_name, flat_field_name); - (*plog) << pt::Log::logend; - } - } - model_env = nullptr; return model_env_local.was_field_found; } @@ -1608,9 +1570,9 @@ void Model::field_model(const wchar_t * db_field_name, const wchar_t * flat_fiel { if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) && (is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) && - !model_env->model_wrapper ) + !model_env->wrapper.model ) { - model_env->model_wrapper = new ModelWrapperModel(&field_model); + model_env->wrapper.model = &field_model; } } diff --git a/src/model.h b/src/model.h index 3f6383e..7c4de9d 100644 --- a/src/model.h +++ b/src/model.h @@ -48,7 +48,7 @@ #include "flatexpression.h" #include "modelenv.h" #include "ft.h" -#include "modelwrapper.h" +#include "wrapper.h" #ifdef MORM_HAS_EZC_LIBRARY #include "funinfo.h" @@ -113,13 +113,13 @@ ModelMember model_member = static_cast(method); \ field_member_ezc(db_field_name, flat_field_name, model_member, field_type); \ } \ - void field(const wchar_t * field_name, void (ClassName::*method)(morm::ModelWrapper **), const morm::FT & field_type = morm::FT::default_type) \ + void field(const wchar_t * field_name, void (ClassName::*method)(morm::Wrapper &), const morm::FT & field_type = morm::FT::default_type) \ { \ field(field_name, field_name, method, field_type); \ } \ - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, void (ClassName::*method)(morm::ModelWrapper **), const morm::FT & field_type = morm::FT::default_type) \ + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, void (ClassName::*method)(morm::Wrapper &), const morm::FT & field_type = morm::FT::default_type) \ { \ - typedef void (Model::*ModelMember)(morm::ModelWrapper **); \ + typedef void (Model::*ModelMember)(morm::Wrapper &); \ ModelMember model_member = static_cast(method); \ field_member_ezc(db_field_name, flat_field_name, model_member, field_type); \ } @@ -252,11 +252,11 @@ public: } - Model * get_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, bool put_log_if_not_found = true); - ModelWrapper * get_model_wrapper(const wchar_t * db_field_name, const wchar_t * flat_field_name, bool put_log_if_not_found = true); + Model * get_model(const wchar_t * db_field_name, const wchar_t * flat_field_name); + Wrapper get_wrapper(const wchar_t * db_field_name, const wchar_t * flat_field_name); - bool get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Stream & stream, bool clear_stream = true, bool put_log_if_not_found = true); - bool get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelData * model_data, pt::Stream & stream, bool clear_stream = true, bool put_log_if_not_found = true); + bool get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Stream & stream, bool clear_stream = true); + bool get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelData * model_data, pt::Stream & stream, bool clear_stream = true); @@ -453,7 +453,7 @@ protected: void field(const wchar_t * field_name, pt::Date & field_value, const FT & field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, field_type); + field_date(field_name, field_name, field_value, field_type); } void field(const wchar_t * field_name, pt::Space & field_value, const FT & field_type = FT::default_type) @@ -583,7 +583,7 @@ protected: void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Date & field_value, const FT & field_type = FT::default_type) { - field_generic(db_field_name, flat_field_name, field_value, field_type); + field_date(db_field_name, flat_field_name, field_value, field_type); } void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Space & field_value, const FT & field_type = FT::default_type) @@ -824,6 +824,27 @@ protected: } + void field_date(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Date & field_value, const FT & field_type) + { + if( model_connector && model_env ) + { + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GET_MODEL_WRAPPER ) + { + if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) && + (is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) && + !model_env->wrapper.date ) + { + model_env->wrapper.date = &field_value; + } + } + else + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + } + } + + void field_space(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Space & field_value, const FT & field_type) { if( model_connector && model_env ) @@ -832,9 +853,9 @@ protected: { if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) && (is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) && - !model_env->model_wrapper ) + !model_env->wrapper.space_wrapper ) { - model_env->model_wrapper = new ModelWrapperSpace(&field_value); + model_env->wrapper.space_wrapper = new SpaceWrapper(&field_value); } } else @@ -989,7 +1010,7 @@ protected: } - void field_member_ezc(const wchar_t * db_field_name, const wchar_t * flat_field_name, void (Model::*method)(ModelWrapper ** model_wrapper), const FT & field_type) + void field_member_ezc(const wchar_t * db_field_name, const wchar_t * flat_field_name, void (Model::*method)(Wrapper & wrapper), const FT & field_type) { if( model_connector && model_env && model_env->model ) { @@ -1008,7 +1029,7 @@ protected: if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) && (is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) ) { - (model_env->model->*method)(&model_env->model_wrapper); + (model_env->model->*method)(model_env->wrapper); } } } @@ -1178,9 +1199,9 @@ protected: { if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) && (is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) && - !model_env->model_wrapper ) + !model_env->wrapper.model_container_wrapper ) { - model_env->model_wrapper = new ModelWrapperVector(&field_value); + model_env->wrapper.model_container_wrapper = new ModelWrapperVector(&field_value); } } } @@ -1203,9 +1224,9 @@ protected: { if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) && (is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) && - !model_env->model_wrapper ) + !model_env->wrapper.model_container_wrapper ) { - model_env->model_wrapper = new ModelWrapperList(&field_value); + model_env->wrapper.model_container_wrapper = new ModelWrapperList(&field_value); } } } @@ -1228,9 +1249,9 @@ protected: { if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) && (is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) && - !model_env->model_wrapper ) + !model_env->wrapper.model_container_wrapper ) { - model_env->model_wrapper = new ModelWrapperVectorPointer(&field_value); + model_env->wrapper.model_container_wrapper = new ModelWrapperVectorPointer(&field_value); } } } @@ -1253,9 +1274,9 @@ protected: { if( (is_empty_field(model_env->db_field_name) || is_the_same_field(db_field_name, model_env->db_field_name)) && (is_empty_field(model_env->flat_field_name) || is_the_same_field(flat_field_name, model_env->flat_field_name)) && - !model_env->model_wrapper ) + !model_env->wrapper.model_container_wrapper ) { - model_env->model_wrapper = new ModelWrapperListPointer(&field_value); + model_env->wrapper.model_container_wrapper = new ModelWrapperListPointer(&field_value); } } } diff --git a/src/modelcontainerwrapper.h b/src/modelcontainerwrapper.h index b7656b2..2a6762b 100644 --- a/src/modelcontainerwrapper.h +++ b/src/modelcontainerwrapper.h @@ -32,16 +32,13 @@ * */ -#ifndef headerfile_morm_modelwrapper -#define headerfile_morm_modelwrapper +#ifndef headerfile_morm_modelcontainerwrapper +#define headerfile_morm_modelcontainerwrapper -#include #include #include -#include #include -#include "space/space.h" - +#include "baseobjectwrapper.h" namespace morm @@ -49,41 +46,15 @@ namespace morm class Model; - -class ModelWrapper +class ModelContainerWrapper : public BaseObjectWrapper { public: - ModelWrapper() - { - auto_remove = true; - } - - virtual ~ModelWrapper() - { - clear_childs(); - } - - virtual bool should_be_auto_removed() - { - return auto_remove; - } - virtual Model * get_model() { return nullptr; } - virtual pt::Space * get_space() - { - return nullptr; - } - - virtual bool is_container() - { - return false; - } - virtual bool is_container_empty() { return true; @@ -93,174 +64,17 @@ public: { } - virtual void increment_iterator(size_t space_index, size_t table_size) - { - } - - virtual size_t space_indices_table_size() - { - return 0; - } - virtual bool is_iterator_correct() { return false; } - virtual size_t get_space_iterator_value(size_t space_index) - { - return std::numeric_limits::max(); - } - - - virtual void clear_childs() - { - for(auto & map_item : childs_map) - { - if( map_item.second->should_be_auto_removed() ) - { - delete map_item.second; - map_item.second = nullptr; - } - } - - childs_map.clear(); - } - - virtual void add_child(const std::wstring & child_name, ModelWrapper * models_base) - { - childs_map[child_name] = models_base; - } - - virtual ModelWrapper * find_child(const std::wstring & child_name) - { - auto i = childs_map.find(child_name); - - if( i != childs_map.end() ) - { - return i->second; - } - - return nullptr; - } - -protected: - - bool auto_remove; - std::map childs_map; - - -private: - - ModelWrapper(const ModelWrapper &) - { - auto_remove = false; - } - - ModelWrapper & operator=(const ModelWrapper &) - { - return *this; - } - - }; - - -class ModelWrapperModel : public ModelWrapper -{ -public: - - ModelWrapperModel(Model * model) - { - this->model = model; - } - - Model * get_model() - { - return model; - } - - -protected: - - Model * model; - -}; - - - -class ModelWrapperSpace : public ModelWrapper -{ -public: - - ModelWrapperSpace(pt::Space * space) - { - this->space = space; - initialize_indices(); - } - - pt::Space * get_space() - { - return space; - } - - size_t space_indices_table_size() - { - return MODEL_WRAPPER_SPACE_INDICES_TABLE_SIZE; - } - - - void increment_iterator(size_t space_index, size_t table_size) - { - if( space_index < indices.size() ) - { - if( indices[space_index] >= table_size ) - { - indices[space_index] = 0; - } - else - { - indices[space_index] += 1; - } - } - } - - size_t get_space_iterator_value(size_t space_index) - { - if( space_index < indices.size() ) - { - return indices[space_index]; - } - - return std::numeric_limits::max(); - } - -protected: - - const size_t MODEL_WRAPPER_SPACE_INDICES_TABLE_SIZE = 32; - pt::Space * space; - std::vector indices; - - void initialize_indices() - { - indices.resize(MODEL_WRAPPER_SPACE_INDICES_TABLE_SIZE); - - for(size_t & val : indices) - { - val = std::numeric_limits::max(); - } - } - -}; - - - - template -class ModelWrapperBaseContainer : public ModelWrapper +class ModelWrapperBaseContainer : public ModelContainerWrapper { public: @@ -270,11 +84,6 @@ public: iterator = container->end(); } - bool is_container() - { - return true; - } - bool is_container_empty() { return container->empty(); diff --git a/src/modelenv.h b/src/modelenv.h index 6124832..a4f35c6 100644 --- a/src/modelenv.h +++ b/src/modelenv.h @@ -40,6 +40,8 @@ #include "finderhelper.h" #include "fieldvaluehelper.h" #include "morm_types.h" +#include "wrapper.h" + #ifdef MORM_HAS_EZC_LIBRARY #include "funinfo.h" @@ -49,7 +51,7 @@ namespace morm { class Model; -class ModelWrapper; +class ModelContainerWrapper; class ModelEnv @@ -92,9 +94,9 @@ public: const wchar_t * flat_field_name; Model * model; Model * child_model; - ModelWrapper * model_wrapper; pt::Stream * stream; bool was_field_found; // used only in MORM_MODEL_WORK_MODE_PUT_FIELD_RAW_VALUE_TO_STREAM + Wrapper wrapper; #ifdef MORM_HAS_EZC_LIBRARY @@ -133,9 +135,9 @@ public: flat_field_name = e.flat_field_name; model = e.model; child_model = e.child_model; - model_wrapper = e.model_wrapper; stream = e.stream; was_field_found = e.was_field_found; + wrapper = e.wrapper; #ifdef MORM_HAS_EZC_LIBRARY ezc_fun_info = e.ezc_fun_info; @@ -184,9 +186,9 @@ public: flat_field_name = nullptr; model = nullptr; child_model = nullptr; - model_wrapper = nullptr; stream = nullptr; was_field_found = false; + wrapper.clear(); #ifdef MORM_HAS_EZC_LIBRARY ezc_fun_info = nullptr; diff --git a/src/spacewrapper.h b/src/spacewrapper.h new file mode 100644 index 0000000..0dcbb24 --- /dev/null +++ b/src/spacewrapper.h @@ -0,0 +1,116 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2021, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef headerfile_morm_spacewrapper +#define headerfile_morm_spacewrapper + +#include +#include +#include "space/space.h" +#include "baseobjectwrapper.h" + + +namespace morm +{ + + +class SpaceWrapper : public BaseObjectWrapper +{ +public: + + SpaceWrapper(pt::Space * space) + { + this->space = space; + initialize_indices(); + } + + pt::Space * get_space() + { + return space; + } + + size_t space_indices_table_size() + { + return SPACE_INDICES_TABLE_SIZE; + } + + + void increment_iterator(size_t space_index, size_t table_size) + { + if( space_index < indices.size() ) + { + if( indices[space_index] >= table_size ) + { + indices[space_index] = 0; + } + else + { + indices[space_index] += 1; + } + } + } + + size_t get_space_iterator_value(size_t space_index) + { + if( space_index < indices.size() ) + { + return indices[space_index]; + } + + return std::numeric_limits::max(); + } + +protected: + + const size_t SPACE_INDICES_TABLE_SIZE = 32; + pt::Space * space; + std::vector indices; + + + void initialize_indices() + { + indices.resize(SPACE_INDICES_TABLE_SIZE); + + for(size_t & val : indices) + { + val = std::numeric_limits::max(); + } + } + +}; + + +} + +#endif diff --git a/src/wrapper.h b/src/wrapper.h new file mode 100644 index 0000000..5933e09 --- /dev/null +++ b/src/wrapper.h @@ -0,0 +1,183 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2021, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef headerfile_morm_wrapper +#define headerfile_morm_wrapper + +#include "spacewrapper.h" +#include "date/date.h" +#include "modelcontainerwrapper.h" + + +namespace morm +{ + + +class Wrapper +{ +public: + + Model * model; + ModelContainerWrapper * model_container_wrapper; + pt::Date * date; + SpaceWrapper * space_wrapper; + + + Wrapper() + { + model = nullptr; + model_container_wrapper = nullptr; + date = nullptr; + space_wrapper = nullptr; + } + + + Wrapper(const Wrapper & wrapper) + { + operator=(wrapper); + } + + + Wrapper & operator=(const Wrapper & wrapper) + { + model = wrapper.model; + model_container_wrapper = wrapper.model_container_wrapper; + date = wrapper.date; + space_wrapper = wrapper.space_wrapper; + + if( model_container_wrapper ) + { + model_container_wrapper->increment_reference_counter(); + } + + if( space_wrapper ) + { + space_wrapper->increment_reference_counter(); + } + + // childs_map don't need to be copied + + return *this; + } + + + virtual ~Wrapper() + { + clear(); + } + + + virtual void clear() + { + if( model_container_wrapper ) + { + model_container_wrapper->decrement_reference_counter(); + + if( model_container_wrapper->get_reference_counter() == 0 && model_container_wrapper->should_be_auto_removed() ) + { + delete model_container_wrapper; + } + } + + if( space_wrapper ) + { + space_wrapper->decrement_reference_counter(); + + if( space_wrapper->get_reference_counter() == 0 && space_wrapper->should_be_auto_removed() ) + { + delete space_wrapper; + } + } + + model = nullptr; + model_container_wrapper = nullptr; + date = nullptr; + space_wrapper = nullptr; + + clear_childs(); + } + + + + virtual void clear_childs() + { + childs_map.clear(); + } + + + virtual Wrapper * add_child(const std::wstring & child_name, Wrapper & wrapper) + { + Wrapper & w = childs_map[child_name]; + w.clear(); + w = wrapper; + return &w; + } + + + virtual Wrapper * find_child(const std::wstring & child_name) + { + auto i = childs_map.find(child_name); + + if( i != childs_map.end() ) + { + return &i->second; + } + + return nullptr; + } + + + bool has_object() + { + return model || model_container_wrapper || date || space_wrapper; + } + + + bool has_model_object() + { + return model || model_container_wrapper; + } + + +private: + + std::map childs_map; + +}; + + + +} + +#endif