MGJ的地盘

爱拼才会赢!

MAC OSX 程序关闭控制 177

作者为 发表

苹果开发中点击关闭按钮并没有退出程序,而是在后台,如果不做处理那么再点Dock的图表没有响应
因此有如下两种方法解决

1.点击关闭按钮退出程序的方法

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{
  return YES;
}

或者实用NSApplication的terminate方法

NSButton *closeButton = [[self window] standardWindowButton:NSWindowCloseButton];
[closeButton setTarget:self];
[closeButton setAction:@selector(closeApplication)];

- (void) closeApplication {
    [[NSApplication sharedApplication] terminate:nil];
}

2.点击dock图标重新弹出窗口

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
                    hasVisibleWindows:(BOOL)flag{
  if(!flag){
    //主窗口显示
    [NSApp activateIgnoringOtherApps:NO];
    [self.window makeKeyAndOrderFront:self];
  }
  returnYES;
}

C++反射类和成员函数的实现 228

作者为 发表

C++反射类和成员函数的实现,需要用到STL标准模板库

//定义反射工厂类以及基类NSObject
#ifndef __NS_OBJECT_FACTORYE_H__
#define __NS_OBJECT_FACTORYE_H__

#include <map>
#include <string>


typedef void* (*SEL)(void);
typedef SEL (*callFuc)(void);

class NSObject{
public:
	Base(){};
	~Base(){};
};

struct NSObjectFactory {

    typedef std::map<std::string, NSObject*(*)()> map_type;
    typedef std::map<std::string, callFuc> fuc_map_type;

private:

    static map_type * m_map;
    static fuc_map_type * fuc_map;

protected:

public:

    static NSObject * createInstance(std::string const& s){
        map_type::iterator it = getMap()->find(s);
        if(it == getMap()->end())
            return 0;
        return it->second();
    }
 
    static SEL callFaction(std::string const& s) {
        fuc_map_type::iterator it = getFunctionMap()->find(s);
        if(it == getFunctionMap()->end())
            return 0;
        return it->second();
 
    }

    static map_type * getMap(){
        // never delete'ed. (exist until program termination)
        // because we can't guarantee correct destruction order 
        if(!m_map) { m_map = new map_type; } 
        return m_map;
    }

    static fuc_map_type* getFunctionMap() {
 
        if(!fuc_map) { fuc_map = new fuc_map_type(); } 
 
        return fuc_map; 
 
    }
    
};

NSObjectFactory::map_type * NSObjectFactory::m_map = 0;

NSObjectFactory::fuc_map_type * NSObjectFactory::fuc_map = 0;

template<typename T> NSObject * createT() { return (NSObject *)(new T); }


template<typename T>
struct NSObjectRegister : NSObjectFactory { 
    NSObjectRegister(std::string const& s) { 
        getMap()->insert(std::make_pair(s, &createT<T>));
    }
};

#define REFECTION_CLASS_IMPLEMENT_DEFINE(NAME) \
    virtual string toString(){return #NAME;}; \
    static NSObjectRegister<NAME> registerNSObject

#define REFECTION_CLASS_IMPLEMENT(NAME) \
    NSObjectRegister<NAME> NAME::registerNSObject(#NAME)

#define NSClassFromString(NAME) \
    NSObjectFactory::createInstance(NAME)

struct FuctionRegister : NSObjectFactory { 
    FuctionRegister(std::string const& s, callFuc f) { 
        getFunctionMap()->insert(std::make_pair(s, f));
    }
 
};
 
#define REFECTION_FUNCTION_IMPLEMENT(NAME) \
    static FuctionRegister* f_reg_##NAME = new FuctionRegister(#NAME, (callFuc)NAME)

#define NSSelectorFromString(NAME) \
    NSObjectFactory::callFaction(NAME)

#endif // __NS_OBJECT_FACTORYE_H__
//示例封装成员函数反射方法定义一个base类
#define __EJ_GET_POINTER_TO(NAME) \
	SEL _ptr_to_##NAME() { \
		return (SEL)NAME; \
	} \
	REFECTION_FUNCTION_IMPLEMENT(_ptr_to_##NAME);

#define EJ_BIND_FUNCTION_DEFINE(NAME, ARGC_NAME, ARGV_NAME) \
	int _func_##NAME(size_t ARGC_NAME, const char* ARGV_NAME)

#define EJ_BIND_FUNCTION(CLASS, NAME, ARGC_NAME, ARGV_NAME) \
	\
	static int _##CLASS##_func_##NAME( \
		Base* object, \
		size_t argc, \
		const char argv[] \
	) { \
		CLASS* instance = (CLASS*)object; \
		int ret = instance->_func_##NAME(argc, argv); \
		return ret; \
	} \
	__EJ_GET_POINTER_TO(_##CLASS##_func_##NAME)\
	\
	/* The actual implementation for this method */ \
	int CLASS::_func_##NAME(size_t ARGC_NAME, const char* ARGV_NAME)

class Base: public NSObject {
public:
	Base();
	~Base();
	REFECTION_CLASS_IMPLEMENT_DEFINE(Base);//反射类申明

	EJ_BIND_FUNCTION(EJBindingEjectaCore,include, argc, argv )//反射成员函数申明
};

REFECTION_CLASS_IMPLEMENT(Base);//反射类实现

//反射成员函数类申明
EJ_BIND_FUNCTION(EJBindingEjectaCore,include, argc, argv ) {
	if( argc < 1 ) { return -1; }
	return 1;
}
//使用方法
typedef int (*CALL_BACK)( Base* object,  size_t argc,  const char argv[] );

int main(){
 
    NSObject* object = NSClassFromString("Base");//获得反射对象
    SEL callback = NSSelectorFromString("_ptr_to__Base_func_include");//获得函数指针
    ((CALL_BACK)callback)((Base*)object, 0, NULL);//调用静态函数使其调用object对象的成员函数
    return 0;
 
}

linux实用脚本及命令 216

作者为 发表

在文件夹目录下的所有文件中查找关键字

find . -exec grep "keywords" {} \; -print

修改文件夹下所有以-fs8.png结尾的文件为.png结尾

for i in `ls -R |awk '{print i$0}' i=\`pwd\`'/' | grep '\-fs8.png'`; do mv $i `echo $i | sed 's/-fs8.png$/.png/'` ;done

移除svn中已删除文件的版本控制

for i in `svn stat | grep '^\!' | cut -c9-`;do svn remove $i;done;

删除子目录下名为.svn文件夹

find . -type d -name ".svn"|xargs rm -rf

windows下bat脚本为

for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q "%%a\.svn"


 主题设计 • skyfrit.com  MGJ的地盘 | 保留所有权利

35 queries in 0.584 seconds.