diff --git a/Makefile.am b/Makefile.am index 8cf194c0..f871c352 100644 --- a/Makefile.am +++ b/Makefile.am @@ -32,6 +32,8 @@ UTIL_FILES = \ alloc/null.hpp \ alloc/stack.cpp \ alloc/stack.hpp \ + annotation.hpp \ + ascii.hpp \ backtrace.hpp \ bezier.cpp \ bezier.hpp \ @@ -44,6 +46,7 @@ UTIL_FILES = \ colour.cpp \ colour.hpp \ colour.ipp \ + coord/fwd.hpp \ coord/base.hpp \ coord.hpp \ coord/init.hpp \ @@ -162,8 +165,6 @@ UTIL_FILES = \ io.cpp \ io.hpp \ io.ipp \ - ip.cpp \ - ip.hpp \ iterator.hpp \ json/fwd.hpp \ json/except.cpp \ @@ -274,14 +275,6 @@ UTIL_FILES = \ POSIX_FILES = \ - net/address.cpp \ - net/address.hpp \ - net/except.cpp \ - net/except.hpp \ - net/socket.cpp \ - net/socket.hpp \ - net/types.cpp \ - net/types.hpp \ memory/buffer/circular.cpp \ memory/buffer/circular.hpp \ memory/buffer/paged.cpp \ @@ -348,8 +341,8 @@ BACKTRACE_FILES=$(firstword $(__BACKTRACE_FILES) backtrace_null.cpp) ############################################################################### ## Local build rules -CLEANFILES = json.cpp version.cpp ip.cpp uri.cpp -EXTRA_DIST = json/flat.cpp.rl version.cpp.rl ip.cpp.rl uri.cpp.rl +CLEANFILES = json.cpp version.cpp uri.cpp +EXTRA_DIST = json/flat.cpp.rl version.cpp.rl uri.cpp.rl RAGELFLAGS = -F1 SUFFIXES = .cpp .cpp.rl @@ -394,6 +387,7 @@ TEST_BIN = \ test/alloc/dynamic \ test/alloc/linear \ test/alloc/stack \ + test/affine \ test/backtrace \ test/bezier \ test/bitwise \ @@ -426,7 +420,6 @@ TEST_BIN = \ test/hash/sha2 \ test/hton \ test/introspection \ - test/ip \ test/json_types \ test/maths \ test/matrix \ @@ -434,6 +427,7 @@ TEST_BIN = \ test/point \ test/polynomial \ test/pool \ + test/quaternion \ test/rand/buckets \ test/random \ test/range \ diff --git a/annotation.hpp b/annotation.hpp new file mode 100644 index 00000000..7f74fe11 --- /dev/null +++ b/annotation.hpp @@ -0,0 +1,33 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2011 Danny Robson + */ + +#ifndef __UTIL_ANNOTATION_HPP +#define __UTIL_ANNOTATION_HPP + +template +constexpr inline +bool +likely (T &&t) +{ return __builtin_expect (!!t, true); } + + +template +constexpr inline +bool +unlikely (T &&t) +{ return __builtin_expect (!!t, false); } + +#endif diff --git a/ascii.hpp b/ascii.hpp new file mode 100644 index 00000000..b49cff5b --- /dev/null +++ b/ascii.hpp @@ -0,0 +1,92 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2016 Danny Robson + */ + +#ifndef __CRUFT_UTIL_ASCII_HPP +#define __CRUFT_UTIL_ASCII_HPP + +#include "./annotation.hpp" + +#include +#include + +namespace util { namespace ascii { + /////////////////////////////////////////////////////////////////////////// + constexpr inline + bool + is_digit (char c) noexcept + { + return c >= '0' && c <= '9'; + } + + //------------------------------------------------------------------------- + constexpr inline + uint8_t + to_integer (char c) + { + return likely (is_digit (c)) ? + c - '0' + : throw std::invalid_argument ("character is not a digit"); + } + + + //------------------------------------------------------------------------- + constexpr inline + bool + is_upper (char c) noexcept + { + return c >= 'A' && c <= 'Z'; + } + + + /////////////////////////////////////////////////////////////////////////// + constexpr inline + char + to_upper (char c) noexcept + { + return c - 'a' + 'A'; + } + + + //------------------------------------------------------------------------- + constexpr inline + char + to_lower (char c) noexcept + { + return c - 'A' + 'a'; + } + + + /////////////////////////////////////////////////////////////////////////// + constexpr inline + bool + is_space (char c) + { + switch (c) { + case ' ': + case '\f': + case '\n': + case '\r': + case '\t': + case '\v': + return true; + + default: + return false; + } + } +} } + +#endif diff --git a/backtrace_execinfo.cpp b/backtrace_execinfo.cpp index 5f6e1bb9..22815f33 100644 --- a/backtrace_execinfo.cpp +++ b/backtrace_execinfo.cpp @@ -41,7 +41,7 @@ debug::backtrace::backtrace (void): while ((last = ::backtrace (&m_frames[0], trunc_cast (m_frames.size ()))) == size) m_frames.resize (size = m_frames.size () * 2); - CHECK_GT (last, 0); + CHECK_GT (last, 0u); m_frames.resize (last); } @@ -81,7 +81,7 @@ debug::operator <<(std::ostream &os, const debug::backtrace &rhs) { str_t names (backtrace_symbols (frames.data (), trunc_cast (frames.size ())), ::free); for (unsigned int i = 0; i < frames.size (); ++i) - os << frames[i] << '\t' << names.get()[i] << '\t' << addr2line (frames[i]) << '\n'; + os << frames[i] << '\t' << names.get()[i] << '\t' << addr2line (frames[i]); return os; } diff --git a/bezier.hpp b/bezier.hpp index bafd612c..2253b04e 100644 --- a/bezier.hpp +++ b/bezier.hpp @@ -20,7 +20,7 @@ #include "point.hpp" #include "region.hpp" -#include +#include namespace util { template diff --git a/colour.cpp b/colour.cpp index 4d0e5c1d..2f6b7cfb 100644 --- a/colour.cpp +++ b/colour.cpp @@ -14,11 +14,12 @@ * Copyright 2010-2016 Danny Robson */ -#include "colour.hpp" +#include "./colour.hpp" -#include "range.hpp" -#include "random.hpp" -#include "stream.hpp" +#include "./debug.hpp" +#include "./range.hpp" +#include "./random.hpp" +#include "./stream.hpp" #include #include @@ -439,14 +440,38 @@ util::operator>> (std::istream &is, util::colour &c) template std::istream& util::operator>> (std::istream&, util::colour<3,uint8_t>&); /////////////////////////////////////////////////////////////////////////////// -#define INSTANTIATE_S_T(S,T) \ -template struct util::colour; \ -template std::ostream& util::operator<< (std::ostream&, util::colour); +// we need to instantiate the various type_name specialisations for colour. +// +// we provide a declaration here, before then instantiating a routine that we +// know will cause an implicit instantiation (ie util::to_string) for each +// colour specialisation we require. +template +constexpr +const char util::type_name>::value[]; + +//----------------------------------------------------------------------------- +#define INSTANTIATE_S_T(S,T) \ +template \ +struct util::colour; \ + \ +template \ +std::ostream& \ +util::operator<< (std::ostream&, util::colour); \ + \ +template \ +const char* \ +util::to_string> (void); + + +//----------------------------------------------------------------------------- #define INSTANTIATE_S(S) \ INSTANTIATE_S_T(S,uint8_t) \ INSTANTIATE_S_T(S,float) \ INSTANTIATE_S_T(S,double) + +//----------------------------------------------------------------------------- +INSTANTIATE_S(1) INSTANTIATE_S(3) INSTANTIATE_S(4) diff --git a/colour.hpp b/colour.hpp index d0df406e..28e81699 100644 --- a/colour.hpp +++ b/colour.hpp @@ -18,8 +18,9 @@ #define __UTIL_COLOUR_HPP #include "coord.hpp" +#include "introspection.hpp" -#include +#include namespace util { /// An RGBA colour POD type. @@ -28,6 +29,7 @@ namespace util { using coord::base::base; using base_t = coord::base; + // overloaded cast operator which assumes values are unit normalised template colour cast (void) const; @@ -48,6 +50,7 @@ namespace util { template using colour3 = colour<3,T>; template using colour4 = colour<4,T>; + typedef colour1 colour1u; typedef colour3 colour3u; typedef colour4 colour4u; @@ -55,15 +58,24 @@ namespace util { typedef colour3 colour3f; typedef colour4 colour4f; - // RGB <-> HSV + // RGB/HSV conversions colour3f rgb_to_hsv (colour3f); colour3f hsv_to_rgb (colour3f); + // ostream/istream operators template - std::ostream& operator<< (std::ostream&, util::colour); + std::ostream& + operator<< (std::ostream&, util::colour); template - std::istream& operator>> (std::istream&, util::colour&); + std::istream& + operator>> (std::istream&, util::colour&); + + // type name introspection specialisation + template + struct type_name> { + static constexpr const char value[] = "colour"; + }; } #include "colour.ipp" diff --git a/configure.ac b/configure.ac index de571340..787306dd 100644 --- a/configure.ac +++ b/configure.ac @@ -21,6 +21,7 @@ NC_CXX NC_PLATFORM NC_OPTIMISATION NC_WARNINGS +# NC_DEBUGGING is called further down, see notes at the call site. LT_INIT @@ -83,8 +84,6 @@ AC_SEARCH_LIBS([clock_gettime], [rt c], [], [ AC_SEARCH_LIBS([cos], [m], [], [AC_MSG_ERROR([unable to find the maths library])]) -AC_SEARCH_LIBS([htons], [ws2_32], [], [AC_MSG_ERROR([unable to find htons library])]) - ############################################################################### ## Debug features @@ -126,7 +125,7 @@ AC_SUBST(LIBS) # failure on a clean build AX_APPEND_FLAG([-include config.h]) -NC_SUBPACKAGE([util]) +NC_SUBPACKAGE([cruft-util]) AC_CONFIG_FILES([ Doxyfile diff --git a/coord/base.hpp b/coord/base.hpp index 20c50ac0..41585b8e 100644 --- a/coord/base.hpp +++ b/coord/base.hpp @@ -45,7 +45,7 @@ namespace util { namespace coord { base () = default; constexpr explicit base (T val) - { for (size_t i = 0; i < S; ++i) this->data[i] = val; } + { std::fill (begin (), end (), val); } constexpr base (const base &rhs) = default; base& operator= (const base &rhs) = default; @@ -54,6 +54,9 @@ namespace util { namespace coord { T& operator[] (size_t i) { return this->data[i]; } constexpr const T& operator[] (size_t i) const { return this->data[i]; } + auto cbegin (void) const { return std::cbegin (this->data); } + auto cend (void) const { return std::cend (this->data); } + auto begin (void) const { return std::begin (this->data); } auto end (void) const { return std::end (this->data); } diff --git a/coord/fwd.hpp b/coord/fwd.hpp new file mode 100644 index 00000000..3d144148 --- /dev/null +++ b/coord/fwd.hpp @@ -0,0 +1,28 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2016 Danny Robson + */ + +#ifndef __UTIL_COORD_FWD_HPP +#define __UTIL_COORD_FWD_HPP + +namespace util { + template struct colour; + template struct extent; + template struct point; + template struct quaternion; + template struct vector; +} + +#endif diff --git a/coord/names.hpp b/coord/names.hpp index 8b3d35e5..c8f1fda2 100644 --- a/coord/names.hpp +++ b/coord/names.hpp @@ -20,11 +20,23 @@ namespace util { namespace coord { /////////////////////////////////////////////////////////////////////// // tags for accessor names + // + // colours struct rgba { }; struct hsv { }; + + // physical positions struct xyzw { }; + + // texture coords struct stpq { }; + + // physical dimensions struct whd { }; + + // quaternions + struct wxyz { }; + struct abcd { }; } } #endif diff --git a/coord/ops.hpp b/coord/ops.hpp index 0259ff49..4ae47ad3 100644 --- a/coord/ops.hpp +++ b/coord/ops.hpp @@ -17,20 +17,16 @@ #ifndef __UTIL_COORDS_OPS #define __UTIL_COORDS_OPS +#include "./fwd.hpp" + #include "../preprocessor.hpp" #include "../maths.hpp" #include "../types/bits.hpp" -#include #include +#include namespace util { - // forward declerations for traits - template struct point; - template struct extent; - template struct vector; - template struct colour; - /////////////////////////////////////////////////////////////////////// // operation traits namespace coord { @@ -38,26 +34,58 @@ namespace util { template class A, template class B > - struct traits { }; + struct result { }; //------------------------------------------------------------------------- - template <> struct traits { template using result = colour; }; - template <> struct traits { template using result = extent; }; - template <> struct traits { template using result = extent; }; - template <> struct traits { template using result = point ; }; - template <> struct traits { template using result = point ; }; - template <> struct traits { template using result = point ; }; - template <> struct traits { template using result = vector; }; + template <> struct result { template using type = colour; }; + template <> struct result { template using type = extent; }; + template <> struct result { template using type = extent; }; + template <> struct result { template using type = point ; }; + template <> struct result { template using type = point ; }; + template <> struct result { template using type = point ; }; + template <> struct result { template using type = vector; }; + + template < + template class A, + template class B + > + using result_t = typename result::type; + + + //--------------------------------------------------------------------- + template