/*
* my_caffe_start.cpp
*
* Created on: 2018年2月25日
* Author: ZLF
*/
#include<map>
#include <string>
#include<iostream>
using namespace std;
namespace caffe
{
// Common functions and classes from std that caffe often uses.
using std::string;
class caffe
{
public:
caffe()
{
cout<<"construction caffe"<<endl;
}
~caffe()
{
cout<<"deconstruction caffe"<<endl;
}
};
}
//=======================================================================//
// A simple registry for caffe commands.
// typedef 返回类型(*新类型)(参数表)
typedef int (*BrewFunction)();
typedef std::map<caffe::string, BrewFunction> BrewMap;
BrewMap g_brew_map;
#define RegisterBrewFunction(func) \
namespace { \
class __Registerer_##func { \
public: /* NOLINT */ \
__Registerer_##func() { \
g_brew_map[#func] = &func; \
} \
}; \
__Registerer_##func g_registerer_##func; \
}
//=======================================================================//
static BrewFunction GetBrewFunction(const caffe::string& name)
{
cout<<endl<<"in GetBrewFunction()"<<endl;
cout<<"current function name is: "<<name<<endl;
// count() returns the number of elements matching a certain key
if (g_brew_map.count(name))
{
cout<<"current function address is: "<<(void *)g_brew_map[name]<<endl;
cout<<"leave GetBrewFunction()"<<endl<<endl;
return g_brew_map[name];
}
else
{
cout<< "Available caffe actions:"<<endl;
for (BrewMap::iterator it = g_brew_map.begin();it != g_brew_map.end(); ++it)
{
cout<< it->first<<endl;
}
}
}
//=======================================================================//
// Train / Finetune a model.
int train()
{
cout<<"in train()"<<endl;
return 0;
}
RegisterBrewFunction(train);
// Test: score a model.
int test()
{
cout<<"in test()"<<endl;
return 0;
}
RegisterBrewFunction(test);
//=======================================================================//
//=======================================================================//
int main(int argc, char** argv)
{
cout<<"hello main()"<<endl;
cout<<"train() addess: "<<(void *)train<<endl;
cout<<"test() address: "<<(void *)test<<endl;
BrewFunction action;
action = GetBrewFunction(caffe::string(argv[1]));
std::map<caffe::string, BrewFunction>::iterator ipointer;
for(ipointer = g_brew_map.begin(); ipointer!=g_brew_map.end(); ipointer++)
{
//Sort in alphabetical order
cout<<"g_brew_map: "<<ipointer->first<<" "<<(void *)ipointer->second<<endl;
}
//
cout<<"Call function: "<<endl;
GetBrewFunction(caffe::string(argv[1]))();
cout<<"Goodbye main()"<<endl;
return 0;
}
运行结果: