2006-01-16 22:02:54 +01:00
|
|
|
// Copyright (c) 2005, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * 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.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// 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 OWNER 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.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file variable_data.cpp
|
|
|
|
/// @brief A variant-type implementation
|
|
|
|
/// @ingroup utility subs_storage
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
////////////
|
|
|
|
// Includes
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_style.h"
|
|
|
|
#include "utils.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "variable_data.h"
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Constructor
|
2010-06-24 03:24:21 +02:00
|
|
|
VariableData::VariableData() {
|
2006-01-16 22:02:54 +01:00
|
|
|
type = VARDATA_NONE;
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Destructor
|
2010-06-24 03:24:21 +02:00
|
|
|
VariableData::~VariableData() {
|
2006-01-16 22:02:54 +01:00
|
|
|
DeleteValue ();
|
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Deletes the stored value
|
|
|
|
/// @return
|
2006-01-16 22:02:54 +01:00
|
|
|
void VariableData::DeleteValue () {
|
|
|
|
if (!value) return;
|
|
|
|
if (type == VARDATA_NONE) return;
|
|
|
|
switch (type) {
|
|
|
|
case VARDATA_INT: delete value_int; break;
|
|
|
|
case VARDATA_FLOAT: delete value_float; break;
|
|
|
|
case VARDATA_TEXT: delete value_text; break;
|
|
|
|
case VARDATA_BOOL: delete value_bool; break;
|
|
|
|
case VARDATA_COLOUR: delete value_colour; break;
|
2010-06-24 03:24:21 +02:00
|
|
|
case VARDATA_BLOCK: delete *value_block; delete value_block; break;
|
2007-01-11 04:18:14 +01:00
|
|
|
default: break;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
type = VARDATA_NONE;
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-16 08:20:06 +02:00
|
|
|
template<class T> static inline VariableDataType get_type();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> inline VariableDataType get_type<int>() {
|
|
|
|
return VARDATA_INT;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2010-06-27 09:53:31 +02:00
|
|
|
template<> inline VariableDataType get_type<float>() {
|
|
|
|
return VARDATA_FLOAT;
|
|
|
|
}
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> inline VariableDataType get_type<double>() {
|
|
|
|
return VARDATA_FLOAT;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> inline VariableDataType get_type<bool>() {
|
|
|
|
return VARDATA_BOOL;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> inline VariableDataType get_type<wxString>() {
|
|
|
|
return VARDATA_TEXT;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> inline VariableDataType get_type<wxColour>() {
|
|
|
|
return VARDATA_COLOUR;
|
|
|
|
}
|
|
|
|
template<> inline VariableDataType get_type<AssDialogueBlockOverride *>() {
|
|
|
|
return VARDATA_BLOCK;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2010-06-16 08:20:06 +02:00
|
|
|
template<class T>
|
|
|
|
void VariableData::Set(T param) {
|
2006-01-16 22:02:54 +01:00
|
|
|
DeleteValue();
|
2010-06-16 08:20:06 +02:00
|
|
|
type = get_type<T>();
|
|
|
|
value = new T(param);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2010-06-16 08:20:06 +02:00
|
|
|
template void VariableData::Set<int>(int param);
|
2010-06-27 09:53:31 +02:00
|
|
|
template void VariableData::Set<float>(float param);
|
2010-06-16 08:20:06 +02:00
|
|
|
template void VariableData::Set<double>(double param);
|
|
|
|
template void VariableData::Set<bool>(bool param);
|
|
|
|
template void VariableData::Set(wxString param);
|
|
|
|
template void VariableData::Set<wxColour>(wxColour param);
|
|
|
|
template void VariableData::Set<AssDialogueBlockOverride *>(AssDialogueBlockOverride * param);
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Resets a value with a string, preserving current type
|
|
|
|
/// @param value
|
2006-01-16 22:02:54 +01:00
|
|
|
void VariableData::ResetWith(wxString value) {
|
|
|
|
switch (type) {
|
|
|
|
case VARDATA_INT: {
|
|
|
|
long temp = 0;
|
|
|
|
value.ToLong(&temp);
|
2010-06-16 08:20:06 +02:00
|
|
|
Set<int>(temp);
|
2006-01-16 22:02:54 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VARDATA_FLOAT: {
|
|
|
|
double temp = 0;
|
|
|
|
value.ToDouble(&temp);
|
2010-06-16 08:20:06 +02:00
|
|
|
Set(temp);
|
2006-01-16 22:02:54 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VARDATA_BOOL:
|
2011-09-28 21:43:11 +02:00
|
|
|
if (value == "1") Set(true);
|
2010-06-16 08:20:06 +02:00
|
|
|
else Set(false);
|
2006-01-16 22:02:54 +01:00
|
|
|
break;
|
|
|
|
case VARDATA_COLOUR: {
|
|
|
|
long r=0,g=0,b=0;
|
|
|
|
value.Mid(1,2).ToLong(&r,16);
|
|
|
|
value.Mid(3,2).ToLong(&g,16);
|
|
|
|
value.Mid(5,2).ToLong(&b,16);
|
2010-06-16 08:20:06 +02:00
|
|
|
Set(wxColour(r,g,b));
|
2006-01-16 22:02:54 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2010-06-16 08:20:06 +02:00
|
|
|
Set(value);
|
2006-01-16 22:02:54 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Reads as an int
|
|
|
|
/// @return
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> int VariableData::Get<int>() const {
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!value) throw "Null parameter";
|
2010-06-16 08:20:06 +02:00
|
|
|
if (type == VARDATA_BOOL) return !!(*value_bool);
|
2006-07-09 00:11:42 +02:00
|
|
|
if (type == VARDATA_INT) return *value_int;
|
|
|
|
if (type == VARDATA_FLOAT) return (int)(*value_float);
|
2008-01-13 20:36:05 +01:00
|
|
|
if (type == VARDATA_TEXT) return 0;
|
2011-09-28 21:43:11 +02:00
|
|
|
throw "Wrong parameter type, should be int";
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Reads as a float
|
2010-06-27 09:53:31 +02:00
|
|
|
/// @return
|
|
|
|
template<> float VariableData::Get<float>() const {
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!value) throw "Null parameter";
|
2010-06-27 09:53:31 +02:00
|
|
|
if (type == VARDATA_FLOAT) return (float)*value_float;
|
|
|
|
if (type == VARDATA_INT) return (float)(*value_int);
|
|
|
|
if (type == VARDATA_TEXT) return 0.0f;
|
2011-09-28 21:43:11 +02:00
|
|
|
throw "Wrong parameter type, should be float";
|
2010-06-27 09:53:31 +02:00
|
|
|
}
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> double VariableData::Get<double>() const {
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!value) throw "Null parameter";
|
2006-07-09 00:11:42 +02:00
|
|
|
if (type == VARDATA_FLOAT) return *value_float;
|
|
|
|
if (type == VARDATA_INT) return (float)(*value_int);
|
2010-06-27 09:53:31 +02:00
|
|
|
if (type == VARDATA_TEXT) return 0.0;
|
2011-09-28 21:43:11 +02:00
|
|
|
throw "Wrong parameter type, should be float";
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Reads as a bool
|
|
|
|
/// @return
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> bool VariableData::Get<bool>() const {
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!value) throw "Null parameter";
|
2006-01-16 22:02:54 +01:00
|
|
|
if (type == VARDATA_BOOL) return *value_bool;
|
2006-07-09 00:11:42 +02:00
|
|
|
if (type == VARDATA_INT) return ((*value_int)!=0);
|
|
|
|
if (type == VARDATA_FLOAT) return ((*value_float)!=0);
|
2008-01-13 20:36:05 +01:00
|
|
|
if (type == VARDATA_TEXT) return false;
|
2011-09-28 21:43:11 +02:00
|
|
|
throw "Wrong parameter type, should be bool";
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Reads as a colour
|
|
|
|
/// @return
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> wxColour VariableData::Get<wxColour>() const {
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!value) throw "Null parameter";
|
2006-01-16 22:02:54 +01:00
|
|
|
if (type == VARDATA_COLOUR) return *value_colour;
|
|
|
|
else if (type == VARDATA_TEXT) {
|
|
|
|
AssColor color;
|
2007-01-18 07:45:55 +01:00
|
|
|
color.Parse(*value_text);
|
2006-01-16 22:02:54 +01:00
|
|
|
return color.GetWXColor();
|
|
|
|
}
|
2011-09-28 21:43:11 +02:00
|
|
|
else throw "Wrong parameter type, should be colour";
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Reads as a block
|
|
|
|
/// @return
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> AssDialogueBlockOverride *VariableData::Get<AssDialogueBlockOverride *>() const {
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!value) throw "Null parameter";
|
|
|
|
if (type != VARDATA_BLOCK) throw "Wrong parameter type, should be block";
|
2010-06-16 08:20:06 +02:00
|
|
|
return *value_block;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Reads as a string
|
|
|
|
/// @return
|
2010-06-16 08:20:06 +02:00
|
|
|
template<> wxString VariableData::Get<wxString>() const {
|
2011-09-28 21:43:11 +02:00
|
|
|
if (!value) throw "Null parameter";
|
2006-01-16 22:02:54 +01:00
|
|
|
if (type != VARDATA_TEXT) {
|
2010-06-16 08:20:06 +02:00
|
|
|
if (type == VARDATA_INT) return wxString::Format("%i",*value_int);
|
|
|
|
else if (type == VARDATA_FLOAT) return wxString::Format("%g",*value_float);
|
|
|
|
else if (type == VARDATA_COLOUR) return wxString::Format("#%02X%02X%02X",value_colour->Red(),value_colour->Green(),value_colour->Blue());
|
|
|
|
else if (type == VARDATA_BOOL) return *value_bool ? "1" : "0";
|
|
|
|
else if (type == VARDATA_BLOCK) return (*value_block)->GetText();
|
2011-09-28 21:43:11 +02:00
|
|
|
else throw "Wrong parameter type, should be text";
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
return *value_text;
|
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Gets type
|
|
|
|
/// @return
|
2006-01-16 22:02:54 +01:00
|
|
|
VariableDataType VariableData::GetType() const {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Copy
|
|
|
|
/// @param param
|
2006-01-16 22:02:54 +01:00
|
|
|
void VariableData::operator= (const VariableData ¶m) {
|
|
|
|
switch(param.GetType()) {
|
2010-06-16 08:20:06 +02:00
|
|
|
case VARDATA_INT: Set(param.Get<int>()); break;
|
|
|
|
case VARDATA_FLOAT: Set(param.Get<double>()); break;
|
|
|
|
case VARDATA_TEXT: Set(param.Get<wxString>()); break;
|
|
|
|
case VARDATA_BOOL: Set(param.Get<bool>()); break;
|
|
|
|
case VARDATA_COLOUR: Set(param.Get<wxColor>()); break;
|
|
|
|
case VARDATA_BLOCK: Set(param.Get<AssDialogueBlockOverride*>()); break;
|
2006-01-16 22:02:54 +01:00
|
|
|
default: DeleteValue();
|
|
|
|
}
|
|
|
|
}
|