模拟caffe调用命令行输入的函数


/*
 * 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;
}

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值