morm/src/setfieldvaluehelper.h

207 lines
4.4 KiB
C++

/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2019, 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_setfieldvaluehelper
#define headerfile_morm_setfieldvaluehelper
#include <vector>
#include <typeinfo>
namespace morm
{
struct FieldValueBase
{
FieldValueBase()
{
}
virtual ~FieldValueBase()
{
}
virtual void set(void * value, const std::type_info & value_type_info) = 0;
};
template<typename FieldValue>
struct FieldValueContainer : public FieldValueBase
{
FieldValueContainer(const FieldValue & value)
{
this->value = &value;
}
void set(void * value, const std::type_info & value_type_info)
{
if( value_type_info == typeid(FieldValue) )
{
FieldValue * field_value = reinterpret_cast<FieldValue*>(value);
*field_value = *this->value;
}
}
const FieldValue * value;
};
class SetFieldValueHelper
{
public:
struct Item
{
const wchar_t * db_field_name;
const wchar_t * flat_field_name;
FieldValueBase * field_value_base;
bool auto_remove_field_value_object;
Item()
{
db_field_name = nullptr;
flat_field_name = nullptr;
field_value_base = nullptr;
auto_remove_field_value_object = false;
}
~Item()
{
}
};
std::vector<Item> item_tab;
SetFieldValueHelper()
{
clear();
}
virtual ~SetFieldValueHelper()
{
remove_field_value_base_objects();
}
virtual void remove_field_value_base_objects()
{
for(Item & item : item_tab)
{
if( item.auto_remove_field_value_object )
{
delete item.field_value_base;
item.field_value_base = nullptr;
}
}
}
virtual void clear()
{
item_tab.clear();
}
virtual void add(const wchar_t * db_field_name, const wchar_t * flat_field_name)
{
add(db_field_name, flat_field_name, nullptr, false);
}
virtual void add(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValueBase * field_value_base, bool auto_remove_field_value_object)
{
Item item;
item.db_field_name = db_field_name;
item.flat_field_name = flat_field_name;
item.field_value_base = field_value_base;
item.auto_remove_field_value_object = auto_remove_field_value_object;
item_tab.push_back(item);
}
virtual size_t size()
{
return item_tab.size();
}
virtual bool empty()
{
return item_tab.empty();
}
virtual void add_field_value_container(size_t index, FieldValueBase * field_value_base, bool auto_remove_field_value_object)
{
if( item_tab[index].field_value_base && item_tab[index].auto_remove_field_value_object )
{
delete item_tab[index].field_value_base;
}
item_tab[index].field_value_base = field_value_base;
item_tab[index].auto_remove_field_value_object = auto_remove_field_value_object;
}
template<typename FieldValue>
void set_value(size_t index, FieldValue & value)
{
if( item_tab[index].field_value_base )
{
item_tab[index].field_value_base->set(&value, typeid(value));
}
}
virtual const wchar_t * get_db_field_name(size_t index)
{
return item_tab[index].db_field_name;
}
virtual const wchar_t * get_flat_field_name(size_t index)
{
return item_tab[index].flat_field_name;
}
};
}
#endif