Refactoring

This commit is contained in:
Oleg Korshul
2022-07-25 00:07:03 +03:00
parent 38709cbc15
commit 162b520374
5 changed files with 316 additions and 19 deletions

View File

@ -56,13 +56,14 @@ int main(int argc, char *argv[])
CValue oGlobal = oContext.GetGlobal();
CValue oApi = oGlobal[L"Api"];
CValue oDocument = oApi.Call(L"GetDocument");
CValue oParagraph = oApi.Call(L"CreateParagraph");
oParagraph.Call(L"AddText", "Hello, world!");
CValue oApi = oGlobal["Api"];
CValue oDocument = oApi.Call("GetDocument");
CValue oParagraph = oApi.Call("CreateParagraph");
oParagraph.Call("SetSpacingAfter", 1000, false);
oParagraph.Call("AddText", "Hello, world!");
CValue oContent = oContext.CreateArray(1);
oContent.Set(0, oParagraph);
oDocument.Call(L"InsertContent", oContent);
oContent[0] = oParagraph;
oDocument.Call("InsertContent", oContent);
}
std::wstring sDstPath = sProcessDirectory + L"/result.docx";

View File

@ -144,8 +144,11 @@ namespace NSDoctRenderer
*/
CDocBuilderValue GetProperty(const wchar_t* name);
CDocBuilderValue Get(const char* name);
CDocBuilderValue Get(const wchar_t* name);
CDocBuilderValue operator[](const char* name);
CDocBuilderValue operator[](const wchar_t* name);
CDocBuilderValue Get(const int& index);
CDocBuilderValue operator[](const int& index);
@ -169,6 +172,14 @@ namespace NSDoctRenderer
static CDocBuilderValue CreateNull();
public:
CDocBuilderValue Call(const char* name);
CDocBuilderValue Call(const char* name, CDocBuilderValue p1);
CDocBuilderValue Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2);
CDocBuilderValue Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3);
CDocBuilderValue Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4);
CDocBuilderValue Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4, CDocBuilderValue p5);
CDocBuilderValue Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4, CDocBuilderValue p5, CDocBuilderValue p6);
CDocBuilderValue Call(const wchar_t* name);
CDocBuilderValue Call(const wchar_t* name, CDocBuilderValue p1);
CDocBuilderValue Call(const wchar_t* name, CDocBuilderValue p1, CDocBuilderValue p2);
@ -215,6 +226,7 @@ namespace NSDoctRenderer
CDocBuilderValue CreateUndefined();
CDocBuilderValue CreateNull();
CDocBuilderValue CreateObject();
CDocBuilderValue CreateArray(const int& length);
CDocBuilderValue CreateTypedArray(unsigned char* buffer, const int& length);

View File

@ -357,13 +357,52 @@ namespace NSDoctRenderer
CDocBuilderValue::CDocBuilderValue(const CDocBuilderValue& src)
{
m_internal = new CDocBuilderValue_Private();
m_internal->m_context = src.m_internal->m_context;
m_internal->m_value = src.m_internal->m_value;
*this = src;
// only for constructor
if (src.m_internal->m_parent.is_init())
m_internal->m_parent = src.m_internal->m_parent;
}
CDocBuilderValue& CDocBuilderValue::operator=(const CDocBuilderValue& src)
{
m_internal->m_context = src.m_internal->m_context;
m_internal->m_value = src.m_internal->m_value;
m_internal->m_nativeType = src.m_internal->m_nativeType;
m_internal->m_nativeValue = src.m_internal->m_nativeValue;
switch (m_internal->m_nativeType)
{
case CDocBuilderValue_Private::ptString:
{
size_t len = wcslen(m_internal->m_nativeValue.sValue);
wchar_t* copy_ptr = new wchar_t[len + 1];
memcpy(copy_ptr, m_internal->m_nativeValue.sValue, (len + 1) * sizeof(wchar_t));
m_internal->m_nativeValue.sValue = copy_ptr;
break;
}
default:
break;
}
if (m_internal->m_parent.is_init())
{
m_internal->CheckNative();
JSSmart<CJSValue> oParent = m_internal->m_parent->m_parent;
if (oParent->isArray())
{
JSSmart<CJSArray> oParentArray = oParent->toArray();
oParentArray->set(m_internal->m_parent->m_parent_index, m_internal->m_value.GetPointer());
}
else if (oParent->isObject() && !m_internal->m_parent->m_parent_prop_name.empty())
{
JSSmart<CJSObject> oParentObject = oParent->toObject();
oParentObject->set(m_internal->m_parent->m_parent_prop_name.c_str(), m_internal->m_value.GetPointer());
}
}
return *this;
}
CDocBuilderValue::~CDocBuilderValue()
@ -469,24 +508,36 @@ namespace NSDoctRenderer
return ret;
}
CDocBuilderValue CDocBuilderValue::Get(const wchar_t* name)
CDocBuilderValue CDocBuilderValue::Get(const char* name)
{
CDocBuilderValue ret;
if (IsEmpty() || !m_internal->m_value->isObject())
return ret;
std::wstring sProp(name);
std::string sPropA = U_TO_UTF8(sProp);
ret.m_internal->m_context = m_internal->m_context;
ret.m_internal->m_value = m_internal->m_value->toObjectSmart()->get(sPropA.c_str());
ret.m_internal->m_value = m_internal->m_value->toObjectSmart()->get(name);
ret.m_internal->m_parent = new CDocBuilderValue_Private::CParentValueInfo();
ret.m_internal->m_parent->m_parent = m_internal->m_value;
ret.m_internal->m_parent->m_parent_prop_name = std::string(name);
ret.m_internal->m_parent->m_parent_index = -1;
return ret;
}
CDocBuilderValue CDocBuilderValue::Get(const wchar_t* name)
{
std::wstring sProp(name);
std::string sPropA = U_TO_UTF8(sProp);
return Get(sPropA.c_str());
}
CDocBuilderValue CDocBuilderValue::GetProperty(const wchar_t* name)
{
return Get(name);
}
CDocBuilderValue CDocBuilderValue::operator[](const char* name)
{
return Get(name);
}
CDocBuilderValue CDocBuilderValue::operator[](const wchar_t *name)
{
return Get(name);
@ -501,6 +552,11 @@ namespace NSDoctRenderer
ret.m_internal->m_context = m_internal->m_context;
JSSmart<CJSArray> array = m_internal->m_value->toArray();
ret.m_internal->m_value = array->get(index);
ret.m_internal->m_parent = new CDocBuilderValue_Private::CParentValueInfo();
ret.m_internal->m_parent->m_parent = m_internal->m_value;
ret.m_internal->m_parent->m_parent_index = index;
return ret;
}
CDocBuilderValue CDocBuilderValue::operator[](const int &index)
@ -579,6 +635,130 @@ namespace NSDoctRenderer
}
// Functions
CDocBuilderValue CDocBuilderValue::Call(const char* name)
{
CDocBuilderValue ret;
if (IsEmpty() || !m_internal->m_value->isObject())
return ret;
ret.m_internal->m_context = m_internal->m_context;
ret.m_internal->m_value = m_internal->m_value->toObjectSmart()->call_func(name);
return ret;
}
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1)
{
CDocBuilderValue ret;
if (IsEmpty() || !m_internal->m_value->isObject())
return ret;
p1.m_internal->CheckNative();
JSSmart<CJSValue> argv[1];
argv[0] = p1.m_internal->m_value;
ret.m_internal->m_context = m_internal->m_context;
ret.m_internal->m_value = m_internal->m_value->toObjectSmart()->call_func(name, 1, argv);
return ret;
}
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2)
{
CDocBuilderValue ret;
if (IsEmpty() || !m_internal->m_value->isObject())
return ret;
p1.m_internal->CheckNative();
p2.m_internal->CheckNative();
JSSmart<CJSValue> argv[2];
argv[0] = p1.m_internal->m_value;
argv[1] = p2.m_internal->m_value;
ret.m_internal->m_context = m_internal->m_context;
ret.m_internal->m_value = m_internal->m_value->toObjectSmart()->call_func(name, 2, argv);
return ret;
}
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3)
{
CDocBuilderValue ret;
if (IsEmpty() || !m_internal->m_value->isObject())
return ret;
p1.m_internal->CheckNative();
p2.m_internal->CheckNative();
p3.m_internal->CheckNative();
JSSmart<CJSValue> argv[3];
argv[0] = p1.m_internal->m_value;
argv[1] = p2.m_internal->m_value;
argv[2] = p3.m_internal->m_value;
ret.m_internal->m_context = m_internal->m_context;
ret.m_internal->m_value = m_internal->m_value->toObjectSmart()->call_func(name, 3, argv);
return ret;
}
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4)
{
CDocBuilderValue ret;
if (IsEmpty() || !m_internal->m_value->isObject())
return ret;
p1.m_internal->CheckNative();
p2.m_internal->CheckNative();
p3.m_internal->CheckNative();
p4.m_internal->CheckNative();
JSSmart<CJSValue> argv[4];
argv[0] = p1.m_internal->m_value;
argv[1] = p2.m_internal->m_value;
argv[2] = p3.m_internal->m_value;
argv[3] = p4.m_internal->m_value;
ret.m_internal->m_context = m_internal->m_context;
ret.m_internal->m_value = m_internal->m_value->toObjectSmart()->call_func(name, 4, argv);
return ret;
}
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4, CDocBuilderValue p5)
{
CDocBuilderValue ret;
if (IsEmpty() || !m_internal->m_value->isObject())
return ret;
p1.m_internal->CheckNative();
p2.m_internal->CheckNative();
p3.m_internal->CheckNative();
p4.m_internal->CheckNative();
p5.m_internal->CheckNative();
JSSmart<CJSValue> argv[5];
argv[0] = p1.m_internal->m_value;
argv[1] = p2.m_internal->m_value;
argv[2] = p3.m_internal->m_value;
argv[3] = p4.m_internal->m_value;
argv[4] = p5.m_internal->m_value;
ret.m_internal->m_context = m_internal->m_context;
ret.m_internal->m_value = m_internal->m_value->toObjectSmart()->call_func(name, 5, argv);
return ret;
}
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4, CDocBuilderValue p5, CDocBuilderValue p6)
{
CDocBuilderValue ret;
if (IsEmpty() || !m_internal->m_value->isObject())
return ret;
p1.m_internal->CheckNative();
p2.m_internal->CheckNative();
p3.m_internal->CheckNative();
p4.m_internal->CheckNative();
p5.m_internal->CheckNative();
p6.m_internal->CheckNative();
JSSmart<CJSValue> argv[6];
argv[0] = p1.m_internal->m_value;
argv[1] = p2.m_internal->m_value;
argv[2] = p3.m_internal->m_value;
argv[3] = p4.m_internal->m_value;
argv[4] = p5.m_internal->m_value;
argv[5] = p6.m_internal->m_value;
ret.m_internal->m_context = m_internal->m_context;
ret.m_internal->m_value = m_internal->m_value->toObjectSmart()->call_func(name, 6, argv);
return ret;
}
CDocBuilderValue CDocBuilderValue::Call(const wchar_t* name)
{
CDocBuilderValue ret;
@ -712,7 +892,7 @@ namespace NSDoctRenderer
p4.m_internal->CheckNative();
p5.m_internal->CheckNative();
p6.m_internal->CheckNative();
JSSmart<CJSValue> argv[5];
JSSmart<CJSValue> argv[6];
argv[0] = p1.m_internal->m_value;
argv[1] = p2.m_internal->m_value;
argv[2] = p3.m_internal->m_value;
@ -758,6 +938,13 @@ namespace NSDoctRenderer
ret.m_internal->m_value = NSJSBase::CJSContext::createNull();
return ret;
}
CDocBuilderValue CDocBuilderContext::CreateObject()
{
CDocBuilderValue ret;
ret.m_internal->m_context = m_internal->m_context;
ret.m_internal->m_value = NSJSBase::CJSContext::createObject();
return ret;
}
CDocBuilderValue CDocBuilderContext::CreateArray(const int& length)
{
CDocBuilderValue ret;

View File

@ -159,10 +159,26 @@ namespace NSDoctRenderer
{
class CDocBuilderValue_Private
{
public:
class CParentValueInfo
{
public:
JSSmart<CJSValue> m_parent;
int m_parent_index;
std::string m_parent_prop_name;
public:
CParentValueInfo() : m_parent(), m_parent_index(-1), m_parent_prop_name("")
{
}
};
public:
JSSmart<CJSContext> m_context;
JSSmart<CJSValue> m_value;
// for operator [index]/["name"] and setter without references
JSSmart<CParentValueInfo> m_parent;
enum PrimitiveType
{
ptUndefined = 0,

View File

@ -40,6 +40,11 @@
namespace NSDoctRenderer
{
CString::CString() {}
CString::CString(const CString& src) {}
CString& CString::operator=(const CString& src) { return *this; }
wchar_t* CString::c_str() const { return NULL; }
CDocBuilderValue::CDocBuilderValue() {}
CDocBuilderValue::CDocBuilderValue(const CDocBuilderValue& src) {}
CDocBuilderValue& CDocBuilderValue::operator=(const CDocBuilderValue& src) { return *this; }
@ -49,12 +54,83 @@ namespace NSDoctRenderer
bool CDocBuilderValue::IsEmpty() { return true; }
void CDocBuilderValue::Clear() {}
bool IsNull() { return false; }
bool IsUndefined() { return false; }
int ToInt() { return 0; }
double ToDouble() { return 0; }
wchar_t* ToString() { return NULL; }
bool CDocBuilderValue::IsNull() { return false; }
bool CDocBuilderValue::IsUndefined() { return false; }
bool CDocBuilderValue::IsBool() { return false; }
bool CDocBuilderValue::IsInt() { return false; }
bool CDocBuilderValue::IsDouble() { return false; }
bool CDocBuilderValue::IsString() { return false; }
bool CDocBuilderValue::IsFunction() { return false; }
bool CDocBuilderValue::IsObject() { return false; }
bool CDocBuilderValue::IsArray() { return false; }
bool CDocBuilderValue::IsTypedArray() { return false; }
unsigned int CDocBuilderValue::GetLength() { return 0; }
bool CDocBuilderValue::ToBool() { return false; }
int CDocBuilderValue::ToInt() { return 0; }
double CDocBuilderValue::ToDouble() { return 0; }
CString CDocBuilderValue::ToString() { return CString(); }
CDocBuilderValue CDocBuilderValue::GetProperty(const wchar_t* name) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Get(const char* name) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Get(const wchar_t* name) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::operator[](const char* name) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::operator[](const wchar_t* name) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Get(const int& index) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::operator[](const int& index) { CDocBuilderValue ret; return ret; }
void CDocBuilderValue::SetProperty(const wchar_t* name, CDocBuilderValue value) {}
void CDocBuilderValue::Set(const wchar_t* name, CDocBuilderValue value) {}
void CDocBuilderValue::Set(const int& index, CDocBuilderValue value) {}
// primitives
CDocBuilderValue::CDocBuilderValue(const bool& value) {}
CDocBuilderValue::CDocBuilderValue(const int& value) {}
CDocBuilderValue::CDocBuilderValue(const unsigned int& value) {}
CDocBuilderValue::CDocBuilderValue(const double& value) {}
CDocBuilderValue::CDocBuilderValue(const char* value) {}
CDocBuilderValue::CDocBuilderValue(const wchar_t* value) {}
CDocBuilderValue CDocBuilderValue::CreateUndefined() { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::CreateNull() { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const char* name) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4, CDocBuilderValue p5) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const char* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4, CDocBuilderValue p5, CDocBuilderValue p6) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const wchar_t* name) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const wchar_t* name, CDocBuilderValue p1) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const wchar_t* name, CDocBuilderValue p1, CDocBuilderValue p2) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const wchar_t* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const wchar_t* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const wchar_t* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4, CDocBuilderValue p5) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderValue::Call(const wchar_t* name, CDocBuilderValue p1, CDocBuilderValue p2, CDocBuilderValue p3, CDocBuilderValue p4, CDocBuilderValue p5, CDocBuilderValue p6) { CDocBuilderValue ret; return ret; }
CDocBuilderContextScope::CDocBuilderContextScope() {}
CDocBuilderContextScope::CDocBuilderContextScope(const CDocBuilderContextScope& src) {}
CDocBuilderContextScope& CDocBuilderContextScope::operator=(const CDocBuilderContextScope& src) { return *this; }
CDocBuilderContextScope::~CDocBuilderContextScope() {}
CDocBuilderContext::CDocBuilderContext() {}
CDocBuilderContext::CDocBuilderContext(const CDocBuilderContext& src) {}
CDocBuilderContext& CDocBuilderContext::operator=(const CDocBuilderContext& src) { return *this; }
CDocBuilderContext::~CDocBuilderContext() {}
CDocBuilderValue CDocBuilderContext::CreateUndefined() { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderContext::CreateNull() { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderContext::CreateObject() { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderContext::CreateArray(const int& length) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderContext::CreateTypedArray(unsigned char* buffer, const int& length) { CDocBuilderValue ret; return ret; }
CDocBuilderValue CDocBuilderContext::GetGlobal() { CDocBuilderValue ret; return ret; }
CDocBuilderContextScope CDocBuilderContext::CreateScope() { CDocBuilderContextScope ret; return ret; }
bool CDocBuilderContext::IsError() { return true; }
}
namespace NSDoctRenderer
@ -82,6 +158,11 @@ namespace NSDoctRenderer
char* CDocBuilder::GetVersion() { return "0.0"; }
CDocBuilderContext CDocBuilder::GetContext()
{
return CDocBuilderContext();
}
void CDocBuilder::Initialize() {}
void CDocBuilder::Dispose() {}
}