当前位置: 首页 > news >正文

d代码做网站竞价运营是做什么的

d代码做网站,竞价运营是做什么的,vr全景网站怎么做,淄博网站建设公司三农编译环境:Ubuntu16.04 64位 交叉编译工具:arm-hisiv500-linux-gcc 文章目录 1. 项目背景2. lua开源版本选择3. 封装代码3.1 源码简介3.2 封装类3.2.1 头文件3.2.2 类的实现3.3.3 sample代码 1. 项目背景 使用lua脚本,读取key对应的值&#x…

编译环境:Ubuntu16.04 64位
交叉编译工具:arm-hisiv500-linux-gcc

文章目录

  • 1. 项目背景
  • 2. lua开源版本选择
  • 3. 封装代码
    • 3.1 源码简介
    • 3.2 封装类
      • 3.2.1 头文件
      • 3.2.2 类的实现
      • 3.3.3 sample代码

1. 项目背景

使用lua脚本,读取key对应的值,用作设备的默认配置。

2. lua开源版本选择

使用lua-5.4.6.tar.gz点击下载,早期使用lua-5.0.2.tar.gz,在部分平台上存在浮点运算错误的问题,放弃。

3. 封装代码

3.1 源码简介

源码的目录结构比较简单,只有一个src目录,Makefile略作修改即可,或者根据自己项目做简化。
lua.hpp文件内容如下,外部调用主要用到就是这三个头文件,在编译C++工程时注意extern “C”:

// lua.hpp
// Lua header files for C++
// <<extern "C">> not supplied automatically because Lua also compiles as C++extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

3.2 封装类

3.2.1 头文件

#ifndef __LUA_CONFIG_H__
#define __LUA_CONFIG_H__#include <string>
#include <pthread.h>struct lua_State;
typedef struct lua_State lua_State;class LuaConfig
{
public:static int Initialize(void);static int Invalidate(void);static LuaConfig* instance(void);
private:static LuaConfig* s_instance;LuaConfig(void);virtual ~LuaConfig(void);LuaConfig(LuaConfig &);			// 拷贝构造函数,禁止拷贝public:int Init(const char * filename);//要解析的lua文件,可以按照lua语法包含其他luavoid unInit();///< 根据传入的键值返回相应的字符串///< key为要访问的键值///< defaultValue为默认值,当访问的键值不存在时返回std::string getString(const char * key, const char * defaultValue="");///< 根据传入的键值返回相应的double值,与getString类似///< key为要访问的键值///< defaultValue为默认值,当访问的键值不存在时返回double getNumber(const char * key, double defaultValue = 0);private:int TravelTable(const char * key);private:lua_State *m_luastate;pthread_mutex_t m_Mutex;
};#endif //__LUA_CONFIG_H__

3.2.2 类的实现

#include "LuaConfig.h"
#include <string.h>
#include <stdlib.h>extern "C"
{#include "lua/lua.h"#include "lua/lauxlib.h"#include "lua/lualib.h"
};int LuaConfig::Initialize(void)
{if(s_instance != NULL)return -1;s_instance = new LuaConfig;return 0;
}
int LuaConfig::Invalidate(void)
{if(s_instance == NULL)return 0;delete s_instance;return 0;
}
LuaConfig* LuaConfig::instance(void)
{return s_instance;
}LuaConfig* LuaConfig::s_instance = NULL;LuaConfig::LuaConfig()
{m_luastate = NULL;pthread_mutex_init(&m_Mutex, NULL);
}LuaConfig::~LuaConfig()
{unInit();pthread_mutex_destroy(&m_Mutex);
}int LuaConfig::Init(const char * filename)
{if (m_luastate != NULL)return -1;if (filename == NULL)return -2;
#if 0 // 5.0.2的封装m_luastate = lua_open();if (m_luastate == NULL)return -3;luaopen_base(m_luastate);luaopen_table(m_luastate);luaopen_io(m_luastate);luaopen_string(m_luastate);luaopen_math(m_luastate);luaopen_debug(m_luastate);//luaopen_lfs(m_luastate);//luaopen_bitlib(m_luastate);if (lua_dofile(m_luastate, filename) != 0)return -4;
#else//5.4.6m_luastate = luaL_newstate();if (m_luastate == NULL)return -3;luaL_openlibs(m_luastate);if (luaL_dofile(m_luastate, filename) != 0)return -4;
#endifreturn 0;
}void LuaConfig::unInit()
{if (m_luastate != NULL){lua_close(m_luastate);m_luastate = NULL;}return;
}std::string LuaConfig::getString(const char * key, const char * defaultValue)
{pthread_mutex_lock(&m_Mutex);int nTop   = lua_gettop(m_luastate);int status = TravelTable(key);std::string ret = defaultValue;if( (status == 0) && (lua_isstring(m_luastate, -1))){ret = lua_tostring(m_luastate, -1);}lua_settop(m_luastate, nTop);pthread_mutex_unlock(&m_Mutex);return ret;
}double LuaConfig::getNumber(const char * key, double defaultValue)
{pthread_mutex_lock(&m_Mutex);int nTop   = lua_gettop(m_luastate);int status = TravelTable(key);double ret = defaultValue;if( (status == 0) && (lua_isnumber(m_luastate, -1))){ret = lua_tonumber(m_luastate, -1);}lua_settop(m_luastate, nTop);pthread_mutex_unlock(&m_Mutex);return ret;
}int LuaConfig::TravelTable(const char * key)
{// 创建匿名函数int len = strlen(key) + 16;char* szFunc = (char*)malloc(len);memset(szFunc, 0, len);sprintf(szFunc, "return %s", key);int status = luaL_loadbuffer(m_luastate, szFunc, strlen(szFunc), "table_travel");if(status == 0){status = lua_pcall(m_luastate, 0, LUA_MULTRET, 0);}free(szFunc);return status;
}

3.3.3 sample代码

LuaConfig::Initialize();
LuaConfig* pCfg = LuaConfig::instance();
pCfg->Init("./test.lua");int testA = (int)LuaConfig::instance()->getNumber("testA", 0);
std::string testB = LuaConfig::instance()->getString("testB", "123456");if (pCfg != NULL)
{pCfg->unInit();LuaConfig::Invalidate();
}

以上。
转载请注明出处,如有错漏之处,敬请指正。

http://www.tj-hxxt.cn/news/68673.html

相关文章:

  • 外贸网页制作公司seo搜索引擎优化总结报告
  • 宁波市建设厅网站首页泉州百度首页优化
  • 做装修行业营销型网站百度刷排名百度快速排名
  • bc网站怎么做排名长春网站优化团队
  • 如何做网站pptseo营销专员
  • eclipse与jsp网站开发搜索引擎seo是什么
  • 住房和城乡建设部科技网站提升seo排名平台
  • 网站开发好公司建站为应用技术
  • 手机建站程序免费下载拼多多运营
  • 网站开发后所有权成品影视app开发
  • 深圳南山企业网站建设报价深圳最新疫情
  • WordPress搭建社区网站技术培训
  • 动态网页网站苏州seo关键词排名
  • 怎样注册一个自己的网站爱站seo查询软件
  • 阳泉哪里做网站培训心得体会1000字
  • 通过网站做国际贸易的成本百度推广账号登录
  • 便宜做网站的公司靠谱吗seo推广排名平台有哪些
  • app要有网站做基础知识市场营销产品推广策划方案
  • 会员管理网站ASP建设2022今日最新军事新闻
  • 石家庄百成网络烟台seo
  • 001做淘宝代码的网站网页seo搜索引擎优化
  • 郑州做网站的论坛手机百度2020
  • 品牌型网站建设公司微信营销软件排行榜
  • 做传感器交易的网站武汉大学人民医院光谷院区
  • 婺源做网站软文大全
  • wordpress document郑州seo推广优化
  • 赶集网2022年最新招聘关于seo如何优化
  • 阿里云建立wordpressseo知识是什么意思
  • bt搜索引擎下载北京优化seo
  • wordpress 豆瓣fm宁波seo推广服务电话