プロジェクトでС言語を使用しています。結局、私はコードの大部分がチェックであることに気付き始めました。プロジェクトでは、関数boolの戻り値の型。我々はメディアでの作業や機能を読み取ったり、我々はプログラムの開発を中断しなければなりませんfalseを返す記述する場合、 されているのでいくつかの時間前、私はでC言語でVisual Studio例外を使用
__try
__finally
(使用のVisual Studio 2008)のVisual Studioに組み込まれていることを除い学びましたプロジェクトでは、ログを使用します。すべての関数はログを使用します。 ログと例外を組み合わせることにしました。プリプロセッサを使用する
#define try do \
{ \
Int32_T __FUNCTION__##ReturnValue = -1; \
LOG_BEGIN(); \
__try \
{
#define finally } \
__finally \
{
#define end_try } \
if(__FUNCTION__##ReturnValue != -1) \
{ \
return LOG_END_WITH(__FUNCTION__##ReturnValue); \
} \
else \
{ \
LOG_END(); \
return; \
} \
}while(0)
#define raise(e) RaiseException(e, EXCEPTION_NONCONTINUABLE, 0, NULL)
#define return_false __FUNCTION__##ReturnValue = 0; \
__leave
#define return_true ##__FUNCTION__##ReturnValue = 1; \
__leave
#define return_void ##__FUNCTION__##ReturnValue = -1; \
__leave
使用例関数はブール値を返します。
Bool_T Node_Insert(IN OUT Tree_T * tree, IN Text_T fullPath, ...)
{
Text_T itemName = NULL;
int * delimiterPos = NULL;
try
{
// if tree is empty we should add ROOT_PATH
//
if(Tree_IsEmpty(IN *tree))
{
if(failed Tree_CreateRoot(IN OUT *tree, IN datafield))
{
LOG_ERROR("Node_Insert FAILURE --> Tree_CreateRoot failed");
return_false;
}
}
if(Text_Length(IN fullPath) > 1)
{
...
// Allocate memory for Text
//
item = Text_New();
// Allocate for delimeterPos array
//
delimiterPos = NEW(n, sizeof(elem));
...
while(i < nDelimiter)
{
...
...
...
}
}
else if (Text_Length(IN fullPath) == 1 )
{
// we have already added ROOT
//
return_true;
}else{
LOG_ERROR("Node_Insert FAILURE --> Path length is not correct");
return_false;
}
}
// In the end we need free allocated memory
//
finally
{
Text_Delete(IN OUT &itemName);
free(delimiterPos);
}
// in end_try preprocessor we write log and return true or false
//
end_try;
}
使用例値を返さないメソッド
void Ifo_ReadBufferHeader(IN Uint8_T * buffer, ... IN OUT Int32_T * position)
{
Text_T fullPath = NULL;
try
{
// getting buffer size
//
Uint32_T size = Buffer_GetSize(IN buffer);
// getting mode
//
Uint8_T writeMode = Buffer_GetWritingMode(IN buffer);
if(Ifo_IsAddMode(IN static_cast(DataIfoMode_T, writeMode)))
{
Uint32_T pos = 1 + Ifo_GetLabelLength() + 23;
// getting path length
//
fullPath = Text_New(IN "", IN mp);
Buffer_GetNameField(IN buffer, OUT &fullPath, IN OUT &pos);
*parentFolderPath = fullPath;
*position = pos;
} else if(Ifo_IsRenameMode(IN static_cast(DataIfoMode_T, writeMode))
{
*position = 1 + Ifo_GetLabelLength() + 23;
}
*bufferSize = size;
*mode = static_cast(DataIfoMode_T, writeMode);
// return_void preprocessor that use
//
return_void;
}
finally
{
// After run the main code you must free allocated memory
//
Text_Delete(IN OUT &fullPath);
}
end_try;
}
私の質問があります。どのように値を返すのでしょうか?つまり、この関数は、バッファCRC CRCの戻り値を値として返すなどの値を返します。これらのマクロを使用してこれを行うにはどうすればよいですか?
int GetCrcBuffer(IN Uint32_T buffer, IN Tree_T tree)
{
Text_T itemName = NULL;
int crc = 0;
try
{
...
...
itemName = Text_New();
if(crc != 12)
{
return_value(crc); // ???? return value of crc and delete text??? how???
}
...
...
}
finally
{
Text_Delete(IN OUT itemName);
}
end_try;
}
ブロックを実行して値を返す方法は? プリプロセッサの書き方return_value()???
Bあなたは関数がintだけでなく返すことができるという事実を忘れていました。プロジェクトは、さまざまな種類の多くの機能があります。 – alexander
"関数はintだけでなく"何を意味するのかよくわかりません - それは 'int'を返すと宣言されています。非整数型を返す関数を記述したい場合について質問していますか?それは元の質問ではなかった:)その場合は更新を参照してください – Attila