LogCabin
Core/Util.h
Go to the documentation of this file.
00001 /* Copyright (c) 2011-2012 Stanford University
00002  * Copyright (c) 2015 Diego Ongaro
00003  *
00004  * Permission to use, copy, modify, and distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
00009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
00011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /**
00018  * \file
00019  * Common utilities and definitions.
00020  * See also Core::STLUtil and Core::StringUtil.
00021  */
00022 
00023 #include <cassert>
00024 #include <cinttypes>
00025 #include <functional>
00026 #include <stdexcept>
00027 
00028 #ifndef LOGCABIN_CORE_UTIL_H
00029 #define LOGCABIN_CORE_UTIL_H
00030 
00031 namespace LogCabin {
00032 namespace Core {
00033 namespace Util {
00034 
00035 /**
00036  * Cast a bigger int down to a smaller one.
00037  * Asserts that no precision is lost at runtime.
00038  */
00039 // This was taken from the RAMCloud project.
00040 template<typename Small, typename Large>
00041 Small
00042 downCast(const Large& large)
00043 {
00044     Small small = static_cast<Small>(large);
00045     // The following comparison (rather than "large==small") allows
00046     // this method to convert between signed and unsigned values.
00047     assert(large - static_cast<Large>(small) == 0);
00048     return small;
00049 }
00050 
00051 /// Like sizeof but returns a uint32_t.
00052 #define sizeof32(x) LogCabin::Core::Util::downCast<uint32_t>(sizeof(x))
00053 
00054 /**
00055  * Calls a function when this object goes out of scope.
00056  * This is useful for deferring execution of something until the end of the
00057  * scope without creating a full-blown RAII class to wrap it. It's named after
00058  * the try { .. } finally { ... } control structure found in many languages.
00059  * See also http://www.stroustrup.com/bs_faq2.html#finally
00060  */
00061 class Finally {
00062   public:
00063     explicit Finally(std::function<void()> onDestroy)
00064         : onDestroy(onDestroy) {
00065     }
00066     ~Finally() {
00067         onDestroy();
00068     }
00069     std::function<void()> onDestroy;
00070 };
00071 
00072 /**
00073  * Return true if the log of x in base 2 is a whole number.
00074  */
00075 bool
00076 isPowerOfTwo(uint64_t x);
00077 
00078 /**
00079  * Copy some noncontiguous chunks of data into a contiguous chunk.
00080  * \param dest
00081  *      Where the new copy should be written.
00082  * \param src
00083  *      A list of (pointer, length) pairs describing where to copy from.
00084  * \return
00085  *      dest (as in memcpy).
00086  */
00087 void*
00088 memcpy(void* dest,
00089        std::initializer_list<std::pair<const void*, size_t>> src);
00090 
00091 /**
00092  * The thread could not complete its task because it was asked to exit.
00093  */
00094 class ThreadInterruptedException : public std::runtime_error {
00095   public:
00096     ThreadInterruptedException();
00097 };
00098 
00099 } // namespace LogCabin::Core::Util
00100 } // namespace LogCabin::Core
00101 } // namespace LogCabin
00102 
00103 #endif /* LOGCABIN_CORE_UTIL_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines