1,防止一個頭檔被重複包含
#ifndef COMDEF_H
#define COMDEF_H
//頭檔內容
#endif
2,重新定義一些類型,防止由於各種平臺和編譯器的不同,而產生的類型位元組數差異,方便移植。
typedef unsigned char boolean; /* Boolean value type. */
typedef unsigned long int uint32; /* Unsigned 32 bit value */
typedef unsigned short uint16; /* Unsigned 16 bit value */
typedef unsigned char uint8; /* Unsigned 8 bit value */
typedef signed long int int32; /* Signed 32 bit value */
typedef signed short int16; /* Signed 16 bit value */
typedef signed char int8; /* Signed 8 bit value */
//下面的不建議使用
typedef unsigned char byte; /* Unsigned 8 bit value type. */
typedef unsigned short word; /* Unsinged 16 bit value type. */
typedef unsigned long dword; /* Unsigned 32 bit value type. */
typedef unsigned char uint1; /* Unsigned 8 bit value type. */
typedef unsigned short uint2; /* Unsigned 16 bit value type. */
typedef unsigned long uint4; /* Unsigned 32 bit value type. */
typedef signed char int1; /* Signed 8 bit value type. */
typedef signed short int2; /* Signed 16 bit value type. */
typedef long int int4; /* Signed 32 bit value type. */
typedef signed long sint31; /* Signed 32 bit value */
typedef signed short sint15; /* Signed 16 bit value */
typedef signed char sint7; /* Signed 8 bit value */
3,得到指定位址上的一個位元組或字
#define MEM_B( x ) ( *( (byte *) (x) ) )
#define MEM_W( x ) ( *( (word *) (x) ) )
4,求最大值和最小值
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
5,得到一個field在結構體(struct)中的偏移量
#define FPOS( type, field ) \
/*lint -e545 */ ( (dword) &(( type *) 0)-> field ) /*lint +e545 */
6,得到一個結構體中field所佔用的位元組數
#define FSIZ( type, field ) sizeof( ((type *) 0)->field )
7,按照LSB格式把兩個位元組轉化為一個Word
#define FLIPW( ray ) ( (((word) (ray)[0]) * 256) + (ray)[1] )
8,按照LSB格式把一個Word轉化為兩個位元組
#define FLOPW( ray, val ) \
(ray)[0] = ((val) / 256); \
(ray)[1] = ((val) & 0xFF)
9,得到一個變數的位址(word寬度)
#define B_PTR( var ) ( (byte *) (void *) &(var) )
#define W_PTR( var ) ( (word *) (void *) &(var) )
10,得到一個字的高位和低位元位元組
#define WORD_LO(xxx) ((byte) ((word)(xxx) & 255))
#define WORD_HI(xxx) ((byte) ((word)(xxx) >> 8))
11,返回一個比X大的最接近的8的倍數
#define RND8( x ) ((((x) + 7) / 8 ) * 8 )
12,將一個字母轉換為大寫
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
13,判斷字元是不是10進值的數字
#define DECCHK( c ) ((c) >= '0' && (c) <= '9')
14,判斷字元是不是16進值的數字
#define HEXCHK( c ) ( ((c) >= '0' && (c) <= '9') ||\
((c) >= 'A' && (c) <= 'F') ||\
((c) >= 'a' && (c) <= 'f') )
15,防止溢出的一個方法
#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
16,返回陣列元素的個數
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
17,返回一個無符號數n尾的值MOD_BY_POWER_OF_TWO(X,n)=X%(2^n)
#define MOD_BY_POWER_OF_TWO( val, mod_by ) \
( (dword)(val) & (dword)((mod_by)-1) )
18,對於IO空間映射在存儲空間的結構,輸入輸出處理
#define inp(port) (*((volatile byte *) (port)))
#define inpw(port) (*((volatile word *) (port)))
#define inpdw(port) (*((volatile dword *)(port)))
#define outp(port, val) (*((volatile byte *) (port)) = ((byte) (val)))
#define outpw(port, val) (*((volatile word *) (port)) = ((word) (val)))
#define outpdw(port, val) (*((volatile dword *) (port)) = ((dword) (val)))
19,使用一些宏跟蹤調試
A N S I標準說明了五個預定義的宏名。它們是:
_ L I N E _
_ F I L E _
_ D A T E _
_ T I M E _
_ S T D C _
如果編譯不是標準的,則可能僅支援以上宏名中的幾個,或根本不支持。記住編譯程序
也許還提供其他預定義的宏名。
_ L I N E _及_ F I L E _巨集指令在有關# l i n e的部分中已討論,這裏討論其餘的宏名。
_ D AT E _巨集指令含有形式為月/日/年的串,表示原始檔案被翻譯到代碼時的日期。
源代碼翻譯到目標代碼的時間作為串包含在_ T I M E _中。串形式為時:分:秒。
如果實現是標準的,則宏_ S T D C _含有十進位常量1。如果它含有任何其他數,則實現是
非標準的。
可以定義宏,例如:
當定義了_DEBUG,輸出資料資訊和所在檔所在行
#ifdef _DEBUG
#define DEBUGMSG(msg,date) printf(msg);printf(“%d%d%d”,date,_LINE_,_FILE_)
#else
#define DEBUGMSG(msg,date)
#endif
20,巨集定義防止使用是錯誤
用小括弧包含。
例如:#define ADD(a,b) (a+b)
用do{}while(0)語句包含多語句防止錯誤
例如:#difne DO(a,b) a+b;\
a++;
應用時:if(….)
DO(a,b); //產生錯誤
else
解決方法: #difne DO(a,b) do{a+b;\
a++;}while(0)
宏中"#"和"##"的用法
一、一般用法
我們使用#把巨集引數變為一個字串,用##把兩個巨集引數貼合在一起.
用法:
#i nclude
#i nclude
using namespace std;
#define STR(s) #s
#define CONS(a,b) int(a##e##b)
int main()
{
printf(STR(vck)); // 輸出字串"vck"
printf("%d\n", CONS(2,3)); // 2e3 輸出:2000
return 0;
}
二、當巨集引數是另一個宏的時候
需要注意的是凡巨集定義裏有用'#'或'##'的地方巨集引數是不會再展開.
1, 非'#'和'##'的情況
#define TOW (2)
#define MUL(a,b) (a*b)
printf("%d*%d=%d\n", TOW, TOW, MUL(TOW,TOW));
這行的宏會被展開為:
printf("%d*%d=%d\n", (2), (2), ((2)*(2)));
MUL裏的參數TOW會被展開為(2).
2, 當有'#'或'##'的時候
#define A (2)
#define STR(s) #s
#define CONS(a,b) int(a##e##b)
printf("int max: %s\n", STR(INT_MAX)); // INT_MAX #i nclude
這行會被展開為:
printf("int max: %s\n", "INT_MAX");
printf("%s\n", CONS(A, A)); // compile error
這一行則是:
printf("%s\n", int(AeA));
INT_MAX和A都不會再被展開, 然而解決這個問題的方法很簡單. 加多一層中間轉換宏.
加這層巨集的用意是把所有宏的參數在這層裏全部展開, 那麼在轉換巨集裏的那一個巨集(_STR)就能得到正確的巨集引數.
#define A (2)
#define _STR(s) #s
#define STR(s) _STR(s) // 轉換宏
#define _CONS(a,b) int(a##e##b)
#define CONS(a,b) _CONS(a,b) // 轉換宏
printf("int max: %s\n", STR(INT_MAX)); // INT_MAX,int型的最大值,為一個變數 #i nclude
輸出為: int max: 0x7fffffff
STR(INT_MAX) --> _STR(0x7fffffff) 然後再轉換成字串;
printf("%d\n", CONS(A, A));
輸出為:200
CONS(A, A) --> _CONS((2), (2)) --> int((2)e(2))
三、'#'和'##'的一些應用特例
1、合併匿名變數名
#define ___ANONYMOUS1(type, var, line) type var##line
#define __ANONYMOUS0(type, line) ___ANONYMOUS1(type, _anonymous, line)
#define ANONYMOUS(type) __ANONYMOUS0(type, __LINE__)
例:ANONYMOUS(static int); 即: static int _anonymous70; 70表示該行行號;
第一層:ANONYMOUS(static int); --> __ANONYMOUS0(static int, __LINE__);
第二層: --> ___ANONYMOUS1(static int, _anonymous, 70);
第三層: --> static int _anonymous70;
即每次只能解開當前層的宏,所以__LINE__在第二層才能被解開;
2、填充結構
#define FILL(a) {a, #a}
enum IDD{OPEN, CLOSE};
typedef struct MSG{
IDD id;
const char * msg;
}MSG;
MSG _msg[] = {FILL(OPEN), FILL(CLOSE)};
相當於:
MSG _msg[] = {{OPEN, "OPEN"},
{CLOSE, "CLOSE"}};
3、記錄檔案名
#define _GET_FILE_NAME(f) #f
#define GET_FILE_NAME(f) _GET_FILE_NAME(f)
static char FILE_NAME[] = GET_FILE_NAME(__FILE__);
4、得到一個數值類型所對應的字串緩衝大小
#define _TYPE_BUF_SIZE(type) sizeof #type
#define TYPE_BUF_SIZE(type) _TYPE_BUF_SIZE(type)
char buf[TYPE_BUF_SIZE(INT_MAX)];
--> char buf[_TYPE_BUF_SIZE(0x7fffffff)];
--> char buf[sizeof "0x7fffffff"];
這裏相當於:
char buf[11];
2017年4月11日 星期二
2016年9月6日 星期二
Android spinner選單設定
private String[] getCarIDs(String tmp) { String[] IDArray;try { //JSON資料解析JSONObject json_read=new JSONObject(tmp);JSONArray CarIDsJSON = json_read.getJSONArray("CarIDsJSON");IDArray = new String[CarIDsJSON.length()]; for (int i = 0; i < IDArray.length; i++) { IDArray[i] = CarIDsJSON.getString(i);} } catch (JSONException e) { e.printStackTrace();IDArray = new String[0];} return IDArray;} private Runnable threadGetCarIDs = new Runnable() { @Override public void run() { if(httpPostProcess.ReadJSON(httpPostProcess.POST_CarIDs, null)) //從後端抓JSON資料{ if(!httpPostProcess.GetJSONData().equals("")) //有抓到資料{ CarIDsJSONData = httpPostProcess.GetJSONData(); // 取得資料 String[] carIDArry = getCarIDs(CarIDsJSONData);ArrayAdapter<String> IDs;IDs = new ArrayAdapter<String>(LoginActivity.this, android.R.layout.simple_spinner_item, carIDArry); //抓取選單內容sprCarIDs.setAdapter(IDs); //設定選單//handler.sendEmptyMessage(MSG_GET_CAR_IDs);} } } };
2016年9月2日 星期五
Android Class間用Content傳Handler
出處
在寫android遊戲或是工具軟體,常常會遇到 View之間 要怎麼切換?
Android.os底下 有一個class 叫 Handler 它能解決這個問題!
Handler能讓 兩個class之間相互傳遞訊息,來寫一段簡單的code可能會比較清楚
例如:
這段程式碼的意思是:
在MainActivity 一開始設定的View 是 ViewTest ,然後,當使用者Touch螢幕,
則讓ViewTest藉由MainActivity的Handler物件,發訊息給MainActivity,讓它切換ViewTest2。
hander.sendEmptyMessage() 這邊是使用只有一個 int 參數的這個method,
隨意傳一個自訂的int數值,讓main class 去判斷,當傳入值數字正確,則main class做動作。
使用Handler 切換View的方法大致上是這樣!
但是有一點千萬要注意,如果是使用SurfaceView的話,則要注意 Thread狀態,Thread必須要讓它在dead 狀況下,
然後,再去切換View,不然程式會出現錯誤。
在寫android遊戲或是工具軟體,常常會遇到 View之間 要怎麼切換?
Android.os底下 有一個class 叫 Handler 它能解決這個問題!
Handler能讓 兩個class之間相互傳遞訊息,來寫一段簡單的code可能會比較清楚
例如:
public class MainActivity extends Activity
{
ViewTest vt;
ViewTest2 vt2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
vt =new ViewTest(this);
vt2 =new ViewTest2(this);
setContentView(vt);
}
Handler hander = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 1: setContentView(vt2);
break;
}
}
}
}
public class ViewTest extends View
{
MainActivity context;
Paint p;
public GameOverView(MainActivity context)
{
super(context);
this.context = context;
p = new Paint();
}
public void onDraw(Canvas c)
{
c.drawText("hello! View",10,10,);
}
@Override
public boolean onTouchEvent(MotionEvent e)
{
if(e.getAction()= =MotionEvent.ACTION_DOWN)
{
context.hander.sendEmptyMessage(1);
}
return true;
}
}
public class ViewTest2 extends View
{
省略......
}
這段程式碼的意思是:
在MainActivity 一開始設定的View 是 ViewTest ,然後,當使用者Touch螢幕,
則讓ViewTest藉由MainActivity的Handler物件,發訊息給MainActivity,讓它切換ViewTest2。
hander.sendEmptyMessage() 這邊是使用只有一個 int 參數的這個method,
隨意傳一個自訂的int數值,讓main class 去判斷,當傳入值數字正確,則main class做動作。
使用Handler 切換View的方法大致上是這樣!
但是有一點千萬要注意,如果是使用SurfaceView的話,則要注意 Thread狀態,Thread必須要讓它在dead 狀況下,
然後,再去切換View,不然程式會出現錯誤。
2016年8月31日 星期三
Android 不同類別傳Activity
Still being relatively new to this I'm having issues in finding a view in a non-activity class MyLocation which I'm using in my activity class MainActivity. I'm using MyLocation to get longitude and latitude. I want to highlight a textview when either GPS or Network has been used. To do so I need to find the textviews in the non-activity class MyLocation.
Here is how I'm calling it in MainActivity:
And here what I tried in MyLocation to find the textviews:
But the views are not found. I already get a NPE @
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);. What am I doing wrong? | |||
Because
context is null in MyLocation class. use MyLocation class constructor to pass MainActivity context in MyLocation to access system services as:
and in
MainActivity create MyLocation class object by passing MainActivity context as:
Now use
context to access system services in MyLocation class
EDIT: Instead of inflating main layout again in onLocationChanged use Activity context to access view from Activity Layout as:
|
Android 多個view寫法
void initOnClickListener() {
int my_ids[] = {
R.id.button01, R.id.button02, R.id.button03, ...
};
R.id.button01, R.id.button02, R.id.button03, ...
};
Button b = null;
for( int i=0 ; i< ids.length ; ++i )
if( ( b = (Button)findViewById( my_ids[i]) ) != null )
b.setOnClickListener(this);
}
for( int i=0 ; i< ids.length ; ++i )
if( ( b = (Button)findViewById( my_ids[i]) ) != null )
b.setOnClickListener(this);
}
public void onClick(View v) {
switch( v.getId() ) {
case R.id.button01:
break;
case R.id.button02:
break;
// ...
}
}
switch( v.getId() ) {
case R.id.button01:
break;
case R.id.button02:
break;
// ...
}
}
2016年8月29日 星期一
C# 用Linq+匿名物件寫法 直接組JSON
用Linq+匿名物件寫法 直接組JSON
這招在Json.net的官方文件有範例(用JObject.FromObject的那個):http://james.newtonking.com/projects/json/help/html/CreatingLINQtoJSON.htm
但只有範例,沒寫為什麼Linq要那樣寫,誰看得懂阿XD
要用Linq直接組JSON
大概把握幾點:
Json Object:Json字串用大括號{}表示,Linq也是用大括號{}表示
Json Object的Name(Key、Property):Json字串在兩個雙引號””裡寫一個英文單字,Linq就直接寫英文單字即可
Json Array:Json字串用中括號[]表示,Linq就用from o in XXX select o,這種回傳IEnumerable的寫法(如果JSON字串是物件陣列的話就用from o in XXX select new {欄位1=YYY}這種形式)
Json Value:Linq就看Json字串填什麼值就跟著填什麼值
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//Get Method
{
//準備資料
List<string> registration_ids = new List<string>() { "4", "8", "15", "16", "23", "42" };
//用Linq直接組
var result = new
{
collapse_key = "score_update",
time_to_live = 108,
delay_while_idle = true,
data = new{
score ="4x8",
time = "15:16.2342"
},
registration_ids = from s in registration_ids
select s
};
//序列化為JSON字串並輸出結果
Response.Write(JsonConvert.SerializeObject(result));
}
}
2016年8月14日 星期日
Android map fragment 顯示按鈕
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.LocationChooser">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:text="Demo Button"
android:padding="10dp"
android:layout_marginTop="20dp"
android:paddingRight="10dp"/>
</fragment>
訂閱:
文章 (Atom)

getLocationmethod ? – Spring Breaker Sep 12 '14 at 11:27