Differences

This shows you the differences between two versions of the page.

Link to this comparison view

extdef.h [2017/12/06 11:18] (current)
Line 1: Line 1:
 +====== extdef.h ======
 +<​code>​
 +/**
 + * extdef.h - A header of macros to augment the C standard headers.
 + *
 + * v1.0 - 10/06/2012 - Initial release
 + ​* ​
 + * Copyright (C) 2012 - Joel Holdsworth
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification,​ are permitted provided that the following conditions
 + * are met:
 + *
 + * 1. Redistributions of source code must retain the above copyright
 + ​* ​   notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + ​* ​   notice, this list of conditions and the following disclaimer in the
 + ​* ​   documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''​ AND ANY EXPRESS OR
 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 + **/
 +
 +#ifndef _EXTDEF_H
 +#define _EXTDEF_H
 +
 +#define countof(x) (sizeof(x)/​sizeof(x[0]))
 +
 +#define begin_element(x) (&x[0])
 +#define end_element(x) (&​x[countof(x)])
 +
 +#endif /* _EXTDEF_H */
 +</​code>​
 +
 +===== Overview =====
 +extdef.h is a companion header to add extra macros to augment the C/C++ standard set. The header is released under the BSD license.
 +===== Reference =====
 +==== countof() ====
 +This macro is a a companion to the ''​sizeof()''​ keyword and the [[http://​cplusplus.com/​reference/​clibrary/​cstddef/​offsetof/​|offsetof()]] macro in stddef.h. ''​countof''​ returns the number of elements in a C style array. The value returned is an unsigned integral value of type ''​size_t''​ with the amount of bytes between the specified member and the beginning of its structure.
 +==== begin_element() & end_element() ====
 +''​begin_element()''​ returns a pointer to the first element of an array. ''​end_element()''​ returns a pointer to the first address after the end of the array.
 +
 +These two macros are provided so that C style array can neatly integrate with [[http://​cplusplus.com/​reference/​algorithm/​|STL algorithms]]. This is possible because STL iterators have the same semantic properties. These provide an elegant way to easily search and transform sequence data types.
 +
 +=== Example ===
 +Finding the maximum value in an array.
 +<​code>​
 +const int a[] = {1, 3, 2, 5, 4};
 +const int max = max_element(begin_element(a),​ end_element(a));​
 +</​code>​
  
extdef.h.txt ยท Last modified: 2017/12/06 11:18 (external edit)