11#include <boost/uuid/uuid.hpp>
12#include <boost/uuid/uuid_generators.hpp>
13#include <boost/uuid/uuid_io.hpp>
17#include "sparta/utils/SpartaExpBackoff.hpp"
20#define NULL_TO_EMPTY_STR(s) (((s)==nullptr)?"":s)
26#define DEMANGLE_BUF_LENGTH 4096
34constexpr inline uint64_t
operator ""_u64(
unsigned long long i) {
41constexpr inline uint32_t
operator ""_u32(
unsigned long long i) {
48constexpr inline uint16_t
operator ""_u16(
unsigned long long i) {
55constexpr inline uint8_t
operator ""_u8(
unsigned long long i) {
62constexpr inline int64_t
operator ""_64(
unsigned long long i) {
69constexpr inline int32_t
operator ""_32(
unsigned long long i) {
76constexpr inline int16_t
operator ""_16(
unsigned long long i) {
83constexpr inline int8_t
operator ""_8(
unsigned long long i) {
90inline std::string generateUUID()
92 boost::uuids::uuid uuid = boost::uuids::random_generator()();
93 std::string uuid_str = boost::uuids::to_string(uuid);
103template<
typename K,
typename V,
typename... Args,
template<
typename...>
class MapType>
104MapType<V, K>
flipMap(
const MapType<K, V, Args...>& map){
105 auto inverted_map {MapType<V, K>{}};
106 for(
const auto& [key, value] : map){
107 inverted_map.insert({value, key});
135 typename ConstT=
typename std::add_pointer<
136 typename std::add_const<
137 typename std::remove_pointer<T>::type
154 return ((x==0) || !(x & (x-1)));
174 double tmp = log2(size);
175 if(tmp != floor(tmp)){
176 throw SpartaException(
"For computeMask, size must be a power of 2, is ")
180 mask = ~((1 << lsb_pos) - 1);
184 lsb_pos =
sizeof(T) * 8;
214inline std::string
demangle(
const std::string& name)
noexcept {
215 char buf[DEMANGLE_BUF_LENGTH];
216 size_t buf_size = DEMANGLE_BUF_LENGTH;
218 char* out = __cxxabiv1::__cxa_demangle(name.c_str(), buf, &buf_size, &status);
222 return std::string(out);
257template <
typename T>
struct is_vector<std::vector<T> > {
258 static const bool value =
true;
276inline uint32_t
replaceSubstring(std::string& s,
const std::string& from,
const std::string& to){
277 uint32_t num_replacements = 0;
279 while((pos = s.find(from, pos)) != std::string::npos) {
280 s.replace(pos, from.size(), to);
284 return num_replacements;
293inline std::string
copyWithReplace(
const std::string& s,
char from,
const std::string& to){
295 result.reserve(s.size()*2);
297 const char* p = s.c_str();
301 }
else if(*p ==
'\0'){
320 std::string result = s;
321 const char * cpp_stuff =
"<>:";
322 const char replacement =
'_';
324 std::string::size_type pos;
325 while(pos = result.find_first_of(cpp_stuff), pos != std::string::npos) {
326 result[pos] = replacement;
330 while(pos = result.find_first_of(
"()"), pos != std::string::npos) {
331 std::string newstr = result.substr(0, pos);
332 newstr += result.substr(pos + 1);
343inline void writeNChars(std::ostream& out, uint32_t num,
char chr=
' ') {
344 for(uint32_t i=0; i<num; ++i){
352inline std::string
toLower(
const std::string& s){
353 std::string out(s.size(),
' ');
354 std::transform(s.begin(), s.end(), out.begin(), (
int(*)(
int))std::tolower);
364template <
bool Default>
367 bool value_ = Default;
383 "OneWayBool<" << Default <<
"> can only be explicitly initalized to " << !Default
384 <<
". Otherwise, it must be default-constructed which will provide a value of "
391 operator bool ()
const {
return value_; }
401 template <
bool ArgDefault>
409 template <
bool ArgDefault>
412 "OneWayBool<" << Default <<
"> can never be set to " << Default
413 <<
" except at initialization");
425 "OneWayBool<" << Default <<
"> can never be set to " << Default
426 <<
" except at initialization");
452 bool defaulted_ =
false;
456 const char * name_ =
nullptr;
465 defaulted_(rhp.defaulted_),
475 defaulted_(rhp.defaulted_),
505 operator const T& ()
const {
513 sparta_assert(set_ || defaulted_, getPrintableQuotedName_() <<
" must be set before reading");
521 sparta_assert(set_ || defaulted_, getPrintableQuotedName_() <<
" must be set before comparing");
522 return value_ == b.
get();
530 getPrintableQuotedName_() <<
" has already been assigned once. It cannot be re-assigned");
532 getPrintableQuotedName_() <<
" cannot be assigned with another AssignOnceObject which is not defaulted or set");
544 getPrintableQuotedName_() <<
" has already been assigned once. It cannot be re-assigned");
554 bool assigned()
const {
560 template <
typename X>
568 std::string getPrintableQuotedName_()
const {
569 if(name_ !=
nullptr){
570 return std::string(
"\"") + name_ +
"\"";
572 return std::string(
"\"AssignOnceObject<") +
typeid(T).name() +
">";
578inline std::ostream&
operator<<(std::ostream& o,
const AssignOnceObject<T>& v) {
579 if(v.set_ || v.defaulted_){
582 o <<
"<uninitialized>";
593template <typename T, T min_bound=std::numeric_limits<T>::min(), T max_bound=std::numeric_limits<T>::max()>
603 template <
typename ArgT=T>
604 bounded_int(ArgT,
typename std::enable_if<std::is_integral<ArgT>::value==
false
605 && std::is_convertible<ArgT,T>::value==
false>::type* = 0)
608 static_assert(std::is_integral<ArgT>::value,
609 "Cannot store a non-integral value in a bounded integer type");
612 template <
typename ArgT=T>
613 bounded_int(ArgT val,
typename std::enable_if<std::is_integral<ArgT>::value==
false
614 && std::is_convertible<ArgT,T>::value==
true>::type* = 0)
615 : val_(
static_cast<T
>(val))
617 rangeCheck_(
static_cast<T
>(val));
621 template <
typename ArgT=T>
622 bounded_int(ArgT val,
typename std::enable_if<std::is_integral<ArgT>::value
623 && std::is_signed<ArgT>::value!=std::is_signed<T>::value>::type* = 0)
630 template <
typename ArgT=T>
631 bounded_int(ArgT val,
typename std::enable_if<std::is_integral<ArgT>::value
632 && (
sizeof(ArgT) <=
sizeof(T))
633 && std::is_signed<ArgT>::value==std::is_signed<T>::value>::type* = 0)
645 template <
typename ArgT=T>
646 bounded_int(ArgT val,
typename std::enable_if<std::is_integral<ArgT>::value
647 && (
sizeof(ArgT) >
sizeof(T))
648 && std::is_signed<ArgT>::value==std::is_signed<T>::value>::type* = 0)
655 template <
typename ArgT>
656 void rangeCheck_(ArgT val){
657 lowerRangeCheck_(val);
658 upperRangeCheck_(val);
661 template <
typename ArgT=T>
662 typename std::enable_if<std::numeric_limits<ArgT>::min()!=min_bound,
void>::type
663 lowerRangeCheck_(ArgT val)
const {
665 "Tried to store a " <<
typeid(ArgT).name() <<
" into a bounded " <<
typeid(T).name() <<
". Not safe to "
666 "cast " << val <<
" into a type with a minimum of " << min_bound);
669 template <
typename ArgT=T>
670 typename std::enable_if<std::numeric_limits<ArgT>::min()==min_bound,
void>::type
671 lowerRangeCheck_(ArgT val)
const {
675 template <
typename ArgT=T>
676 typename std::enable_if<std::numeric_limits<ArgT>::max()!=max_bound,
void>::type
677 upperRangeCheck_(ArgT val)
const {
679 "Tried to store a " <<
typeid(ArgT).name() <<
" into a bounded " <<
typeid(T).name() <<
". Not safe to "
680 "cast " << val <<
" into a type with a maximum of " << max_bound);
683 template <
typename ArgT=T>
684 typename std::enable_if<std::numeric_limits<ArgT>::max()==max_bound,
void>::type
685 upperRangeCheck_(ArgT val)
const {
703 operator T()
const {
return val_; }
735#define GENERATE_HAS_ATTR(name) \
736 template <typename T> \
737 struct has_attr_##name { \
738 typedef char one[1]; \
739 typedef char two[2]; \
741 template <typename C> static one& test( decltype(&C::name) ) ; \
742 template <typename C> static two& test(...); \
744 enum { value = sizeof(test<T>(nullptr)) == sizeof(one) }; \
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Exception class for all of Sparta.
Object which can only have it's value set once. Throws exception if being set more than once.
const T & operator=(const T &v)
Assign using a given value of type T.
AssignOnceObject()
Construct with no name.
AssignOnceObject(const AssignOnceObject &rhp)
Copy Constructor.
const T & operator=(const AssignOnceObject< T > &b)
Assign using another AssignOnceObject of the same contained type T.
const T & get() const
Get contained value of type T.
AssignOnceObject(AssignOnceObject &&rhp)
Move-Construction disabled until needed.
AssignOnceObject(const T &def_value, const char *name)
Construct with a default and name for use during errors. This constructor starts the object in unassi...
bool operator==(const AssignOnceObject< T > &b) const
Compare against another AssignOnceObject of the same contained type T.
Boolean with a default capable of being changed to the opposite value only. it can never set to the d...
OneWayBool()=default
Default construction. Defaults to template argument 'Default'.
bool operator=(bool b)
Assign value from a bool.
bool operator=(const OneWayBool< ArgDefault > &b)
Assign value from another OneWayBool.
bool operator==(const OneWayBool< ArgDefault > &b) const
Compare value with another OneWayBool.
void set_DEPRECATED(bool b)
set this object arbitrarily
bool operator==(bool b) const
Compare value with a bool.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Bounded integer type with range-checking.
bool operator=(const bounded_int &)=delete
Deleted comaprison with bounded in (until implemented fully)
bool operator==(const bounded_int &)=delete
Deleted comaprison with bounded in (until implemented fully)
Macros for handling exponential backoff.
std::string convertCPPStringToPython(const std::string &s)
Take all of the C++-isms out of a string (like parens, angle brackets, colons, etc) and replace them ...
std::string copyWithReplace(const std::string &s, char from, const std::string &to)
Copies from 1 string to another with replacement of a single character with a string.
T * notNull(T *p)
Ensures that a pointer is not null.
void writeNChars(std::ostream &out, uint32_t num, char chr=' ')
Insert a number of some character into an ostream.
uint32_t replaceSubstring(std::string &s, const std::string &from, const std::string &to)
Replaces within a string 's' all instances of some string 'from' with 'to'.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo
std::string demangle(const std::string &name) noexcept
Demangles a C++ symbol.
T computeMask(T size, T &lsb_pos)
Computes a maks and the lsb-position given a block size. The mask generated can be AND-ed with any nu...
MapType< V, K > flipMap(const MapType< K, V, Args... > &map)
Function to invert a maps or an unordered_map, or any type of class that has key/value semantics....
std::string toLower(const std::string &s)
Makes a copy of an input string s in lower-case.
T computeMaskShift(T size)
Convenience wrapper around computeMask to get the shift value.
bool isPowerOf2(uint64_t x)
Determines if input is 0 or a power of 2.
Templated for determining if ValueType is std::vector for use in metaprogramming constructs....
static const bool value
'value' field used by enable_if construct.
Template type helper that removes a pointer, adds a const, and then re-adds the pointer....