際際滷

際際滷Share a Scribd company logo
API Design for C++
Ch.5 API 螳覦 覦覯
蠖 覦覩手
Styles


Flat C



Object Orient



Template Based



Data Driven

ex) FMOD => Flat C, OO, DD http://www.fmod.org/download
Flat C


譴 C Lib(stdio.h, stdlib.h, string.h, etc), Win32 API,
Linux Kernel API, GNOME GLib, NetScape PR, libtiff,
libpng, libz, libungif, etc



ANSI C 豢豌



char, const, continue, default, do, double else, enum,
extern, 鍖oat, for, goto, if, int, long, register, short, signed,
sizeof, static, struct, switch, typedef, union, unsigned, void,
volatile, while



C襦 燕螻 C++ 貉危朱襦 る慨語, る襯  襷 れ
Flat C Style
struct Stack	
{	
int *mStack;	
int mCurSize;	
};	
!
typedef struct Stack *StackPtr;	
!
void StackPush(StackPtr stack, int val);	
!
int StackPop(StackPtr stack);	
!
bool StackIsEmpty(const StackPtr stack);
Flat C Style 語
StackPtr stack = StackCreate();	
if (stack)	
{	
StackPush(stack, 10);	
StackPush(stack, 3);	
while (!StackIsEmpty(stack))	
{	
StackPop(stack);	
}	
StackDestroy(stack);	
}
Flat C Style レ


牛伎狩 蠍一ヾ ろ C襦  




) 襴 貉る API襯 伎 る

伎 誤煙 讌伎
Flat C, C++ 螳 郁鍵


class 螳 れ 蠍讌



extern c襦 螳瑚鍵
#ifdef __cplusplus	
extern C {	
#endif	

!
	

!
#ifdef _cplusplus	
}	
#endif
OO Style
class Stack	
{	
public:	
void Push(int val);	
int Pop();	
bool IsEmpty() const;	
private:	
int *mStack;	
int mCurSize;	
};
OO Style 語
Stack *stack = new Stack();	
if (stack)	
{	
stack->Push(10);	
stack->Push(3);	
while (!stack->IsEmpty())	
{	
stack->Pop();	
}	
delete stack;	
}
OO Style レ


谿 貊  郁屋 覈



譟郁  朱Μ朱 蠏



企るゼ 伎 一危一 覃 貂′


) namespace, public, protected, private
OO Style 


 覲旧′, 襷る誤



る



伎 誤 讌 企れ
Template Based Style
#include <vector>	
!
template <typename T>	
class Stack	
{	
public:	
void Push(T val);	
T Pop();	
bool IsEmpty() const;	
!
private:	
std::vector<T> mStack;	
};
Template Based Style 語
typedef Stack<int> IntStack;	
!
IntStack *stack = new IntStack();	
if (stack)	
{	
stack->Push(10);	
stack->Push(3);	
while (!stack->IsEmpty())	
{	
stack->Pop();	
}	
delete stack;	
}
Template Based Style レ



譴覲旧蟇



貉危   ろ



螳覃襯 狩   焔 レ
Template Based Style 


螳覦 企 襴酔 る , 覈 語ろ伎ろ
襦 .cpp襦 蟲 蠍郁鍵



貉危 螳 讀螳



覦企襴 讀螳



螻豺 る 覃語
Data Driven API Style
// flat c-style	
func(obj, a, b, c);	
!
// object-orient style	
obj.func(a, b, c);	
!
// data-driven function with parameters	
send(func, a, b, c);	
!
//data-driven function with a dictionary	
send(func, dict(arg1 = a, arg2 = b, arg3 = c));
Data Driven API Style
class Stack	
{	
public:	
Stack();	
Result Command(const std::string &command, const
ArgList *args);	
};
Data Driven API Style 語
s = new Stack();	
if (s)	
{	
s->Command(Push, ArgList().Add(value, 10));	
s->Command(Push, ArgList().Add(value, 3));	
Stack::Result r = s->Command(IsEmpty);	
while (!r.convertToBool())	
{	
s->Command(Pop);	
r = s->Command(IsEmpty);	
}	
delete s;	
}	

Tip: QT QVariant, Boost any, Union, void*,  煙 伎
螳覲 朱誤磯ゼ 伎
Data Driven API Style ろ 
危 

# Input file for data driven Stack API	
Push value:10	
Push value:3	
Pop	
Pop
Data Driven Style レ


觜讀 襦讌 碁襦 覿襴蠍 , 貉危 
觜讀 襦讌 



API  覯 誤 襴




 ろ 貉るЖ 豌襴 覃 蠏碁

ろ  襴
Data Driven Style 


壱 焔



る 朱 誤壱伎 覩碁ゼ  蠍 企れ 覓語




豌危,  豌危 企れ
Flat C, OO, Template
Based, Data-Driven

More Related Content

API.Design.for.CPlusPlus.Ch5

  • 1. API Design for C++ Ch.5 API 螳覦 覦覯 蠖 覦覩手
  • 2. Styles Flat C Object Orient Template Based Data Driven ex) FMOD => Flat C, OO, DD http://www.fmod.org/download
  • 3. Flat C 譴 C Lib(stdio.h, stdlib.h, string.h, etc), Win32 API, Linux Kernel API, GNOME GLib, NetScape PR, libtiff, libpng, libz, libungif, etc ANSI C 豢豌 char, const, continue, default, do, double else, enum, extern, 鍖oat, for, goto, if, int, long, register, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while C襦 燕螻 C++ 貉危朱襦 る慨語, る襯 襷 れ
  • 4. Flat C Style struct Stack { int *mStack; int mCurSize; }; ! typedef struct Stack *StackPtr; ! void StackPush(StackPtr stack, int val); ! int StackPop(StackPtr stack); ! bool StackIsEmpty(const StackPtr stack);
  • 5. Flat C Style 語 StackPtr stack = StackCreate(); if (stack) { StackPush(stack, 10); StackPush(stack, 3); while (!StackIsEmpty(stack)) { StackPop(stack); } StackDestroy(stack); }
  • 6. Flat C Style レ 牛伎狩 蠍一ヾ ろ C襦 ) 襴 貉る API襯 伎 る 伎 誤煙 讌伎
  • 7. Flat C, C++ 螳 郁鍵 class 螳 れ 蠍讌 extern c襦 螳瑚鍵 #ifdef __cplusplus extern C { #endif ! ! #ifdef _cplusplus } #endif
  • 8. OO Style class Stack { public: void Push(int val); int Pop(); bool IsEmpty() const; private: int *mStack; int mCurSize; };
  • 9. OO Style 語 Stack *stack = new Stack(); if (stack) { stack->Push(10); stack->Push(3); while (!stack->IsEmpty()) { stack->Pop(); } delete stack; }
  • 10. OO Style レ 谿 貊 郁屋 覈 譟郁 朱Μ朱 蠏 企るゼ 伎 一危一 覃 貂′ ) namespace, public, protected, private
  • 11. OO Style 覲旧′, 襷る誤 る 伎 誤 讌 企れ
  • 12. Template Based Style #include <vector> ! template <typename T> class Stack { public: void Push(T val); T Pop(); bool IsEmpty() const; ! private: std::vector<T> mStack; };
  • 13. Template Based Style 語 typedef Stack<int> IntStack; ! IntStack *stack = new IntStack(); if (stack) { stack->Push(10); stack->Push(3); while (!stack->IsEmpty()) { stack->Pop(); } delete stack; }
  • 14. Template Based Style レ 譴覲旧蟇 貉危 ろ 螳覃襯 狩 焔 レ
  • 15. Template Based Style 螳覦 企 襴酔 る , 覈 語ろ伎ろ 襦 .cpp襦 蟲 蠍郁鍵 貉危 螳 讀螳 覦企襴 讀螳 螻豺 る 覃語
  • 16. Data Driven API Style // flat c-style func(obj, a, b, c); ! // object-orient style obj.func(a, b, c); ! // data-driven function with parameters send(func, a, b, c); ! //data-driven function with a dictionary send(func, dict(arg1 = a, arg2 = b, arg3 = c));
  • 17. Data Driven API Style class Stack { public: Stack(); Result Command(const std::string &command, const ArgList *args); };
  • 18. Data Driven API Style 語 s = new Stack(); if (s) { s->Command(Push, ArgList().Add(value, 10)); s->Command(Push, ArgList().Add(value, 3)); Stack::Result r = s->Command(IsEmpty); while (!r.convertToBool()) { s->Command(Pop); r = s->Command(IsEmpty); } delete s; } Tip: QT QVariant, Boost any, Union, void*, 煙 伎 螳覲 朱誤磯ゼ 伎
  • 19. Data Driven API Style ろ 危 # Input file for data driven Stack API Push value:10 Push value:3 Pop Pop
  • 20. Data Driven Style レ 觜讀 襦讌 碁襦 覿襴蠍 , 貉危 觜讀 襦讌 API 覯 誤 襴 ろ 貉るЖ 豌襴 覃 蠏碁 ろ 襴
  • 21. Data Driven Style 壱 焔 る 朱 誤壱伎 覩碁ゼ 蠍 企れ 覓語 豌危, 豌危 企れ
  • 22. Flat C, OO, Template Based, Data-Driven
  • 23.