This commit is contained in:
Saturneic 2019-01-14 17:19:45 +08:00
parent 12283f5076
commit 42cae7ac2e
5 changed files with 218 additions and 66 deletions

View File

@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
9221DA1121EB5FB8007310A7 /* net.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9221DA1021EB5FB8007310A7 /* net.cpp */; };
925A13A621EC68D500CBD427 /* cpart.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 925A13A421EC67C900CBD427 /* cpart.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@ -28,6 +29,7 @@
9221DA1021EB5FB8007310A7 /* net.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = net.cpp; sourceTree = "<group>"; };
9221DA1421EB62F6007310A7 /* cpart.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpart.h; sourceTree = "<group>"; };
9221DA1621EB8C02007310A7 /* pcs.map */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; name = pcs.map; path = build/Debug/PCS/pcs.map; sourceTree = "<group>"; };
925A13A421EC67C900CBD427 /* cpart.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = cpart.cpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -64,6 +66,7 @@
9221DA0F21EB5FB8007310A7 /* net.h */,
9221DA1421EB62F6007310A7 /* cpart.h */,
9221DA1621EB8C02007310A7 /* pcs.map */,
925A13A421EC67C900CBD427 /* cpart.cpp */,
);
name = Net;
sourceTree = "<group>";
@ -125,6 +128,7 @@
buildActionMask = 2147483647;
files = (
9221DA1121EB5FB8007310A7 /* net.cpp in Sources */,
925A13A621EC68D500CBD427 /* cpart.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

85
cpart.cpp Normal file
View File

@ -0,0 +1,85 @@
//
// cpart.cpp
// Net
//
// Created by 胡一兵 on 2019/1/14.
// Copyright © 2019年 Bakantu. All rights reserved.
//
#include "cpart.h"
CPart::CPart(string src_path,string src_name,string name){
this->src_path = src_path;
this->name = name;
unsigned long qp = src_name.find(".",0);
string t_libname = "lib"+src_name.substr(0,qp)+".so";
if(!~access(("Libs/"+t_libname).data(), F_OK)){
system(("g++ -fPIC -shared -std=c++11 -o ./Libs/"+t_libname+" "+src_path+"/"+src_name).data());
}
this->libname = t_libname;
this->handle = dlopen(("Libs/"+t_libname).data(), RTLD_NOW | RTLD_GLOBAL);
this->func = (PCSFUNC) dlsym(this->handle, this->name.data());
this->libargs_in = (vector<void *> *) dlsym(this->handle, ("__"+name+"_args_in").data());
this->libargs_out = (vector<void *> *) dlsym(this->handle, ("__"+name+"_args_out").data());
}
CPart::CPart(string name){
this->name = name;
this->libname = "lib"+name+".so";
this->handle = dlopen(this->libname.data(), RTLD_LAZY | RTLD_GLOBAL);
this->func = (PCSFUNC) dlsym(this->handle, this->name.data());
}
CPart::~CPart(){
for(auto k = 0; k < args_in.size(); k++){
if(fargs_in[k] == 0) delete (int *)(args_in[k]);
else delete (double *)(args_in[k]);
}
for(auto k = 0; k < args_out.size(); k++){
if(fargs_in[k] == 0) delete (int *)(args_out[k]);
else delete (double *)(args_out[k]);
}
if(handle != nullptr)
dlclose(handle);
}
void CPart::setArgsType(vector<int> fargs_in, vector<int> fargs_out){
this->fargs_in = fargs_in;
this->fargs_out = fargs_out;
}
int CPart::Run(void){
unsigned long count = fargs_in.size()-1;
for(auto k = args_in.rbegin(); k != args_in.rend();k++,count--){
if(fargs_in[count] == INT){
CPart::addArg(libargs_in, *((int *)(*k)));
}
else if(fargs_in[count] == DOUBLE){
CPart::addArg(libargs_in, *((double *)(*k)));
}
}
if(func() == SUCCESS){
int count = 0;
for(auto k = libargs_out->begin(); k != libargs_out->end();k++,count++){
if(fargs_out[count] == INT){
CPart::addArg(&args_out, *((int *)(*k)));
}
else if(fargs_out[count] == DOUBLE){
CPart::addArg(&args_out, *((double *)(*k)));
}
}
return SUCCESS;
}
else return -1;
}
void CPart::Clear(void){
for(auto k = 0; k < args_in.size(); k++){
if(fargs_in[k] == INT) delete (int *)(args_in[k]);
else delete (double *)(args_in[k]);
}
for(auto k = 0; k < args_out.size(); k++){
if(fargs_in[k] == INT) delete (int *)(args_out[k]);
else delete (double *)(args_out[k]);
}
}

88
cpart.h
View File

@ -9,6 +9,7 @@
#ifndef cpart_h
#define cpart_h
#include <unistd.h>
#include <dlfcn.h>
#include <string>
#include <vector>
@ -16,15 +17,21 @@
using std::vector;
using std::string;
#define PCSFUNC_DEFINE(name) extern "C" int name(vector<void *> args_in,vector<void *> *args_out)
#define GET_ARGS(type) CPart::popArg<type>(args_in)
#define ADD_ARGS(type,value) CPart::addArg<type>(args_in, value);
#define ARGS_DECLAER(name) vector<void *> __##name##_args_in, __##name##_args_out
#define PCSFUNC_DEFINE(name) extern "C" int name(void)
#define GET_ARGS(name,type) CPart::popArg<type>(&__##name##_args_in)
#define ADD_ARGS(name,type,value) CPart::addArg<type>(&__##name##_args_out, value);
#define POP_ARGS(name,type) GET_ARGS( name ,type)
#define PUSH_ARGS(name,type,value) ADD_ARGS( name ,type,value)
#define INT 0
#define DOUBLE 1
#define SUCCESS 0
#define FAIL 1
typedef int(*PCSFUNC)(vector<void *>,vector<void *> *);
typedef int(*PCSFUNC)(void);
class CPart;
@ -38,72 +45,37 @@ class CPart{
public:
vector<int> fargs_in, fargs_out;
vector<void *> args_in, args_out;
vector<void *> *libargs_in,*libargs_out;
vector<Depends> depends;
int (*func)(vector<void *>,vector<void *> *);
int (*func)(void);
void *handle;
string src_path,name,libname;
CPart(PCSFUNC func):func(func),handle(nullptr){
}
CPart(PCSFUNC func):func(func),handle(nullptr){}
CPart(string src_path,string src_name,string name);
CPart(string name);
~CPart();
void setArgsType(vector<int> fargs_in, vector<int> fargs_out);
int Run(void);
void Clear(void);
CPart(string src_path,string name){
this->src_path = src_path;
this->name = name;
system(("g++ -fPIC -shared -std=c++11 -o ./Libs/lib"+name+".so "+src_path).data());
this->libname = "lib"+name+".so";
this->handle = dlopen(this->libname.data(), RTLD_LAZY | RTLD_GLOBAL);
this->func = (PCSFUNC) dlsym(this->handle, this->name.data());
}
CPart(string name){
this->name = name;
this->libname = "lib"+name+".so";
this->handle = dlopen(this->libname.data(), RTLD_LAZY | RTLD_GLOBAL);
this->func = (PCSFUNC) dlsym(this->handle, this->name.data());
}
~CPart(){
for(auto k = 0; k < args_in.size(); k++){
if(fargs_in[k] == 0) delete (int *)(args_in[k]);
else delete (double *)(args_in[k]);
}
for(auto k = 0; k < args_out.size(); k++){
if(fargs_in[k] == 0) delete (int *)(args_out[k]);
else delete (double *)(args_out[k]);
}
if(handle != nullptr)
dlclose(handle);
}
void setArgsType(vector<int> fargs_in, vector<int> fargs_out){
this->fargs_in = fargs_in;
this->fargs_out = fargs_out;
}
void addArgsInt(int value){
int *p_value = new int(value);
*p_value = value;
template<class T> void addArgsIn(T value){
T *p_value = new T(value);
args_in.push_back(p_value);
}
int Run(void){
return func(args_in,&args_out);
template<class T>
static void addArg(vector<void *> *args,T value){
T *p_value = new T(value);
*p_value = value;
args->push_back(p_value);
}
template<class T>
static void addArg(vector<void *> &args,T value){
T *p_value = new T(value);
*p_value = value;
args.push_back(p_value);
}
template<class T>
static T popArg(vector<void *> &args){
T *p_value = (T *)args.back();
args.pop_back();
static T popArg(vector<void *> *args){
T *p_value = (T *)args->back();
args->pop_back();
return *p_value;
}
};
#endif /* cpart_h */

View File

@ -14,6 +14,12 @@
int main(void){
CMap map("./PCS");
CThread thread(&map);
thread.AddArgs<int>("B", 4);
thread.AddArgs<double>("B", 8);
thread.Analyse();
thread.DoLine();
cout<<thread.rargs_out.find("B")->second.size()<<endl;
return 0;
}

101
net.h
View File

@ -16,6 +16,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <vector>
#include <list>
#include <string>
#include <map>
@ -32,6 +33,7 @@ using std::string;
using std::vector;
using std::map;
using std::pair;
using std::list;
using std::ifstream;
using std::cout;
using std::endl;
@ -176,11 +178,6 @@ public:
}
};
class CThread{
public:
};
class CMap{
public:
map<string,CPart *> cparts;
@ -213,7 +210,7 @@ public:
string src_name = line.substr(qs+1,qe-qs-1);
//std::cout<<name<<" "<<src_name<<std::endl;
CPart *ncp = new CPart(path+"/"+src_name,name);
CPart *ncp = new CPart(path,src_name,name);
this->cparts.insert(pair<string,CPart *>(name,ncp));
vector<int>fargs_in,fargs_out;
@ -222,7 +219,7 @@ public:
fargs_in = BuidArgs(line);
}
map>>line;
if(line[0] == '>'){
if(line[0] == '<'){
fargs_out = BuidArgs(line);
}
ncp->setArgsType(fargs_in, fargs_out);
@ -264,7 +261,7 @@ public:
unsigned long idx = line.find('{',qe);
unsigned long ss = line.find('}',idx);
unsigned long ts, te;
while(idx < ss){
while(idx+1 < ss){
ts = line.find('(',idx);
te = line.find(']',ts);
if(te == string::npos){
@ -306,6 +303,94 @@ public:
};
class CThread{
public:
CMap *p_map;
list<CPart *> line;
map<string,vector<void *>> rargs;
map<string,vector<void *>> rargs_out;
map<string,bool> ifsolved;
CThread(CMap *tp_map):p_map(tp_map){
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
vector<void *> args,args_out;
rargs.insert(pair<string,vector<void *>>((*k).first,args));
rargs_out.insert(pair<string,vector<void *>>((*k).first,args_out));
}
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
ifsolved.insert(pair<string,bool>((*k).first,false));
}
}
template<class T>
void AddArgs(string name, T value){
auto k = rargs.find(name);
T *p_value = new T();
*p_value = value;
(*k).second.push_back((void *)p_value);
}
void Analyse(void){
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
auto cpart_depends = (*k).second->depends;
if(cpart_depends.size()){
bool if_ok = true;
for(auto ditem = cpart_depends.begin(); ditem != cpart_depends.end(); ditem++){
string name = ditem->t_cpart->name;
if(!(ifsolved.find(name)->second)){
if_ok = false;
}
}
if(if_ok) line.push_back((*k).second);
}
else{
string name = (*k).second->name;
if(rargs.find(k->second->name)->second.size() == k->second->fargs_in.size()){
if(ifsolved.find(name)->second == false){
line.push_back(k->second);
cout<<"ADD "<<k->second->name<<endl;
}
}
}
}
}
void DoLine(void){
for(auto pcp = line.begin(); pcp != line.end(); pcp++){
string name = (*pcp)->name;
cout<<(*pcp)->name<<" "<<(*pcp)->libname<<endl;
vector<void *> args = rargs.find(name)->second;
vector<void *> &args_out = rargs_out.find(name)->second;
vector<int> fargs = (*pcp)->fargs_in;
vector<int> fargs_out = (*pcp)->fargs_out;
vector<void *> &argso = (*pcp)->args_out;
(*pcp)->Clear();
int cout = 0;
for(auto arg = args.begin(); arg != args.end(); arg++,cout++){
if(fargs[cout] == INT){
(*pcp)->addArgsIn<int>(*((int *)(*arg)));
}
else if(fargs[cout] == DOUBLE){
(*pcp)->addArgsIn<double>(*((double *)(*arg)));
}
}
if(!(*pcp)->Run()){
ifsolved.find(name)->second = true;
int cout = 0;
for(auto argo = argso.begin(); argo != argso.end(); argo++,cout++){
if(fargs_out[cout] == INT){
int *p_value = new int(*((int *)(*argo)));
args_out.push_back((void *)p_value);
}
else if(fargs_out[cout] == DOUBLE){
double *p_value = new double(*((double *)(*argo)));
args_out.push_back((double *)p_value);
}
}
}
}
}
};
struct pcs_result{
char *name[16];
uint32_t in_size;