xorp

c_format.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2009 XORP, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/c_format.hh,v 1.14 2008/10/03 17:41:31 atanu Exp $
00023 
00024 #ifndef __LIBXORP_C_FORMAT_HH__
00025 #define __LIBXORP_C_FORMAT_HH__
00026 
00027 #include "libxorp/xorp.h"
00028 
00029 
00030 //
00031 // c_format is a macro that creates a string from a c-style format
00032 // string.  It takes the same arguments as printf, but %n is illegal and
00033 // will cause abort to be called.
00034 //
00035 // Pseudo prototype:
00036 //  string c_format(const char* format, ...);
00037 //
00038 // In practice c_format is a nasty macro, but by doing this we can check
00039 // the compile time arguments are sane and the run time arguments.
00040 //
00041 
00042 #define c_format(format, args...)                         \
00043     (c_format_validate(format, arg_count(args)), do_c_format(format, ## args))
00044 
00045 //
00046 // Template magic to allow us to count the number of varargs passed to
00047 // the macro c_format.  We could also count the size of the var args data
00048 // for extra protection if we were doing the formatting ourselves...
00049 //
00050 
00051 // Comment this out to find unnecessary calls to c_format()
00052 inline int arg_count() { return 0; }
00053 
00054 
00055 template <class A>
00056 inline int arg_count(A) { return 1; }
00057 
00058 template <class A, class B>
00059 inline int arg_count(A,B) { return 2; }
00060 
00061 template <class A, class B, class C>
00062 inline int arg_count(A,B,C) { return 3; }
00063 
00064 template <class A, class B, class C, class D>
00065 inline int arg_count(A,B,C,D) { return 4; }
00066 
00067 template <class A, class B, class C, class D, class E>
00068 inline int arg_count(A,B,C,D,E) { return 5; }
00069 
00070 template <class A, class B, class C,class D, class E, class F>
00071 inline int arg_count(A,B,C,D,E,F) { return 6; }
00072 
00073 template <class A, class B, class C, class D, class E, class F, class G>
00074 inline int arg_count(A,B,C,D,E,F,G) { return 7; }
00075 
00076 template <class A, class B, class C, class D, class E, class F, class G,
00077       class H>
00078 inline int arg_count(A,B,C,D,E,F,G,H) { return 8; }
00079 
00080 template <class A, class B, class C, class D, class E, class F, class G,
00081       class H, class I>
00082 inline int arg_count(A,B,C,D,E,F,G,H,I) { return 9; }
00083 
00084 template <class A, class B, class C, class D, class E, class F, class G,
00085       class H, class I, class J>
00086 inline int arg_count(A,B,C,D,E,F,G,H,I,J) { return 10; }
00087 
00088 template <class A, class B, class C, class D, class E, class F, class G,
00089       class H, class I, class J, class K>
00090 inline int arg_count(A,B,C,D,E,F,G,H,I,J,K) { return 11; }
00091 
00092 template <class A, class B, class C, class D, class E, class F, class G,
00093       class H, class I, class J, class K, class L>
00094 inline int arg_count(A,B,C,D,E,F,G,H,I,J,K,L) { return 12; }
00095 
00096 template <class A, class B, class C, class D, class E, class F, class G,
00097       class H, class I, class J, class K, class L, class M>
00098 inline int arg_count(A,B,C,D,E,F,G,H,I,J,K,L,M) { return 13; }
00099 
00100 template <class A, class B, class C, class D, class E, class F, class G,
00101       class H, class I, class J, class K, class L, class M, class N>
00102 inline int arg_count(A,B,C,D,E,F,G,H,I,J,K,L,M,N) { return 14; }
00103 
00104 template <class A, class B, class C, class D, class E, class F, class G,
00105       class H, class I, class J, class K, class L, class M, class N,
00106       class O>
00107 inline int arg_count(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O) { return 15; }
00108 
00109 template <class A, class B, class C, class D, class E, class F, class G,
00110       class H, class I, class J, class K, class L, class M, class N,
00111       class O, class P>
00112 inline int arg_count(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P) { return 16; }
00113 
00114 template <class A, class B, class C, class D, class E, class F, class G,
00115       class H, class I, class J, class K, class L, class M, class N,
00116       class O, class P, class Q>
00117 inline int arg_count(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q) { return 17; }
00118 
00119 void c_format_validate(const char* fmt, int n);
00120 
00121 #if defined(__printflike)
00122 string do_c_format(const char* fmt, ...) __printflike(1,2);
00123 #elif (defined(__GNUC__))
00124 string do_c_format(const char* fmt, ...)
00125     __attribute__((__format__(printf, 1, 2)));
00126 #else
00127 string do_c_format(const char* fmt, ...);
00128 #endif
00129 
00130 #endif // __LIBXORP_C_FORMAT_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations