PB代码测试
方法列表
导入编译好的dll,PB项目中生成cmap类,有如下方法:
//把键值对存入map
subroutine put(readonly string key, any value)
//通过键取出对应的值
function any get(readonly string key)
//删除键以及对应的值
subroutine del(readonly string key)
//清空所有键值对
subroutine reset()
//获取map中所有的键,返回字符串数组
function any get_keys()
put与get
put方法传入的键为字符串类型,值支持任意的PB类型,包括基本类型、引用类型、枚举类型
cmap map
map = create cmap
st_info lst_info
datastore lds
lst_info.code = "111"
lst_info.name = "abcd"
lds = create datastore
map.put("firstName", "Jason")
map.put("lastName", "Friday")
map.put("pi", 3.1415926)
map.put("graduated", true)
map.put("struct", lst_info)
map.put("obj", lds)
string ls_first_name, ls_last_name
dec{7} ldc_pi
boolean lb_graduated
st_info lst_struct
datastore lds_obj
ls_first_name = map.get("firstName")
ls_last_name = map.get("lastName")
ldc_pi = map.get("pi")
lb_graduated = map.get("graduated")
lst_struct = map.get("struct")
lds_obj = map.get("obj")
return 0
get_keys
cmap map
map = create cmap
st_info lst_info
datastore lds
lst_info.code = "111"
lst_info.name = "abcd"
lds = create datastore
map.put("firstName", "Jason")
map.put("lastName", "Friday")
map.put("pi", 3.1415926)
map.put("graduated", true)
map.put("struct", lst_info)
map.put("obj", lds)
string ls_keys[]
ls_keys = map.get_keys()
return 0
del与reset
cmap map
map = create cmap
st_info lst_info
datastore lds
lst_info.code = "111"
lst_info.name = "abcd"
lds = create datastore
map.put("firstName", "Jason")
map.put("lastName", "Friday")
map.put("pi", 3.1415926)
map.put("graduated", true)
map.put("struct", lst_info)
map.put("obj", lds)
string ls_keys[], ls_keys1[], ls_keys2[]
ls_keys = map.get_keys()
map.del("lastName")
ls_keys1 = map.get_keys()
map.reset()
ls_keys2 = map.get_keys()
return 0
C++代码
QQ:768310524
TEL、WX:18649713925
dllmain.cpp
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include "cmap.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
PBXEXPORT LPCTSTR PBXCALL PBX_GetDescription()
{
static const TCHAR classDesc[] = {
_T("class cmap from nonvisualobject \n") \
_T(" subroutine put(readonly string key, any value) \n") \
_T(" function any get(readonly string key) \n") \
_T(" subroutine del(readonly string key) \n") \
_T(" subroutine reset() \n") \
_T(" function any get_keys() \n") \
_T("end class \n")
};
return (LPCTSTR)classDesc;
}
PBXEXPORT PBXRESULT PBXCALL PBX_CreateNonVisualObject(
IPB_Session* session,
pbobject obj,
LPCTSTR className,
IPBX_NonVisualObject** nvobj
)
{
if (_tcscmp(className, _T("cmap")) == 0)
*nvobj = new PBNIMap(session, obj);
return PBX_OK;
}
cmap.h
#pragma once
#include "include/pbni/PBEXT.H"
#include <string>
#include <map>
using namespace std;
class PBNIMap : public IPBX_NonVisualObject
{
private:
IPB_Session* m_pSession;
pbobject m_pbobject;
map<string, IPB_Value*> m;
public:
PBNIMap(IPB_Session* pSession, pbobject obj)
:m_pSession(pSession),
m_pbobject(obj)
{
};
~PBNIMap(void) {};
PBXRESULT Invoke(IPB_Session* session, pbobject obj, pbmethodID mid, PBCallInfo* ci);
void Destroy();
enum Function_Entrys
{
mid_put = 0,
mid_get,
mid_del,
mid_reset,
mid_get_keys,
NO_MORE_METHODS
};
protected:
PBXRESULT put(PBCallInfo* ci);
PBXRESULT get(PBCallInfo* ci);
PBXRESULT del(PBCallInfo* ci);
PBXRESULT reset(PBCallInfo* ci);
PBXRESULT get_keys(PBCallInfo* ci);
};
cmap.cpp
#include "pch.h"
#include "cmap.h"
using namespace std;
PBXRESULT PBNIMap::Invoke(IPB_Session* session, pbobject obj, pbmethodID mid, PBCallInfo* ci)
{
PBXRESULT pbxr = PBX_OK;
switch (mid)
{
case mid_put:
pbxr = put(ci);
break;
case mid_get:
pbxr = get(ci);
break;
case mid_del:
pbxr = del(ci);
break;
case mid_reset:
pbxr = reset(ci);
break;
case mid_get_keys:
pbxr = get_keys(ci);
break;
default:
pbxr = PBX_E_INVOKE_METHOD_AMBIGUOUS;
}
return pbxr;
}
void PBNIMap::Destroy()
{
reset(NULL);
delete this;
}
PBXRESULT PBNIMap::put(PBCallInfo* ci)
{
LPCTSTR key = m_pSession->GetString(ci->pArgs->GetAt(0)->GetString());
m[key] = m_pSession->AcquireValue(ci->pArgs->GetAt(1));
m_pSession->ReleaseString(key);
return PBX_OK;
}
PBXRESULT PBNIMap::get(PBCallInfo* ci)
{
LPCTSTR key = m_pSession->GetString(ci->pArgs->GetAt(0)->GetString());
if (m.find(key) != m.end())
{
m_pSession->SetValue(ci->returnValue, m[key]);
}
m_pSession->ReleaseString(key);
return PBX_OK;
}
PBXRESULT PBNIMap::del(PBCallInfo* ci)
{
LPCTSTR key = m_pSession->GetString(ci->pArgs->GetAt(0)->GetString());
if (m.find(key) != m.end())
{
m_pSession->ReleaseValue(m[key]);
m.erase(key);
}
m_pSession->ReleaseString(key);
return PBX_OK;
}
PBXRESULT PBNIMap::reset(PBCallInfo* ci)
{
for (auto& it : m) {
m_pSession->ReleaseValue(it.second);
}
m.clear();
return PBX_OK;
}
PBXRESULT PBNIMap::get_keys(PBCallInfo* ci)
{
pbarray arr = NULL;
pblong dim[1];
int i = 1;
arr = m_pSession->NewUnboundedSimpleArray(pbvalue_string);
for (auto& it : m) {
dim[0] = i++;
m_pSession->SetStringArrayItem(arr, dim, it.first.c_str());
}
ci->returnValue->SetArray(arr);
return PBX_OK;
}