13#include "sparta/utils/SpartaExpBackoff.hpp"
16#define NULL_TO_EMPTY_STR(s) (((s)==nullptr)?"":s)
22#define DEMANGLE_BUF_LENGTH 4096
30constexpr inline uint64_t
operator ""_u64(
unsigned long long i) {
37constexpr inline uint32_t
operator ""_u32(
unsigned long long i) {
44constexpr inline uint16_t
operator ""_u16(
unsigned long long i) {
51constexpr inline uint8_t
operator ""_u8(
unsigned long long i) {
58constexpr inline int64_t
operator ""_64(
unsigned long long i) {
65constexpr inline int32_t
operator ""_32(
unsigned long long i) {
72constexpr inline int16_t
operator ""_16(
unsigned long long i) {
79constexpr inline int8_t
operator ""_8(
unsigned long long i) {
92template<
typename K,
typename V,
typename... Args,
template<
typename...>
class MapType>
93MapType<V, K>
flipMap(
const MapType<K, V, Args...>& map){
94 auto inverted_map {MapType<V, K>{}};
95 for(
const auto& [key, value] : map){
96 inverted_map.insert({value, key});
124 typename ConstT=
typename std::add_pointer<
125 typename std::add_const<
126 typename std::remove_pointer<T>::type
143 return ((x==0) || !(x & (x-1)));
163 double tmp = log2(size);
164 if(tmp != floor(tmp)){
165 throw SpartaException(
"For computeMask, size must be a power of 2, is ")
169 mask = ~((1 << lsb_pos) - 1);
173 lsb_pos =
sizeof(T) * 8;
203inline std::string
demangle(
const std::string& name)
noexcept {
204 char buf[DEMANGLE_BUF_LENGTH];
205 size_t buf_size = DEMANGLE_BUF_LENGTH;
207 char* out = __cxxabiv1::__cxa_demangle(name.c_str(), buf, &buf_size, &status);
211 return std::string(out);
246template <
typename T>
struct is_vector<std::vector<T> > {
247 static const bool value =
true;
265inline uint32_t
replaceSubstring(std::string& s,
const std::string& from,
const std::string& to){
266 uint32_t num_replacements = 0;
268 while((pos = s.find(from, pos)) != std::string::npos) {
269 s.replace(pos, from.size(), to);
273 return num_replacements;
282inline std::string
copyWithReplace(
const std::string& s,
char from,
const std::string& to){
284 result.reserve(s.size()*2);
286 const char* p = s.c_str();
290 }
else if(*p ==
'\0'){
309 std::string result = s;
310 const char * cpp_stuff =
"<>:";
311 const char replacement =
'_';
313 std::string::size_type pos;
314 while(pos = result.find_first_of(cpp_stuff), pos != std::string::npos) {
315 result[pos] = replacement;
319 while(pos = result.find_first_of(
"()"), pos != std::string::npos) {
320 std::string newstr = result.substr(0, pos);
321 newstr += result.substr(pos + 1);
332inline void writeNChars(std::ostream& out, uint32_t num,
char chr=
' ') {
333 for(uint32_t i=0; i<num; ++i){
341inline std::string
toLower(
const std::string& s){
342 std::string out(s.size(),
' ');
343 std::transform(s.begin(), s.end(), out.begin(), (
int(*)(
int))std::tolower);
353template <
bool Default>
356 bool value_ = Default;
372 "OneWayBool<" << Default <<
"> can only be explicitly initalized to " << !Default
373 <<
". Otherwise, it must be default-constructed which will provide a value of "
380 operator bool ()
const {
return value_; }
390 template <
bool ArgDefault>
398 template <
bool ArgDefault>
401 "OneWayBool<" << Default <<
"> can never be set to " << Default
402 <<
" except at initialization");
414 "OneWayBool<" << Default <<
"> can never be set to " << Default
415 <<
" except at initialization");
441 bool defaulted_ =
false;
445 const char * name_ =
nullptr;
454 defaulted_(rhp.defaulted_),
464 defaulted_(rhp.defaulted_),
494 operator const T& ()
const {
502 sparta_assert(set_ || defaulted_, getPrintableQuotedName_() <<
" must be set before reading");
510 sparta_assert(set_ || defaulted_, getPrintableQuotedName_() <<
" must be set before comparing");
511 return value_ == b.
get();
519 getPrintableQuotedName_() <<
" has already been assigned once. It cannot be re-assigned");
521 getPrintableQuotedName_() <<
" cannot be assigned with another AssignOnceObject which is not defaulted or set");
533 getPrintableQuotedName_() <<
" has already been assigned once. It cannot be re-assigned");
543 bool assigned()
const {
549 template <
typename X>
550 friend inline std::ostream& operator<<(std::ostream& o,
const AssignOnceObject<X>& v);
557 std::string getPrintableQuotedName_()
const {
558 if(name_ !=
nullptr){
559 return std::string(
"\"") + name_ +
"\"";
561 return std::string(
"\"AssignOnceObject<") +
typeid(T).name() +
">";
567inline std::ostream&
operator<<(std::ostream& o,
const AssignOnceObject<T>& v) {
568 if(v.set_ || v.defaulted_){
571 o <<
"<uninitialized>";
582template <typename T, T min_bound=std::numeric_limits<T>::min(), T max_bound=std::numeric_limits<T>::max()>
592 template <
typename ArgT=T>
593 bounded_int(ArgT,
typename std::enable_if<std::is_integral<ArgT>::value==
false
594 && std::is_convertible<ArgT,T>::value==
false>::type* = 0)
597 static_assert(std::is_integral<ArgT>::value,
598 "Cannot store a non-integral value in a bounded integer type");
601 template <
typename ArgT=T>
602 bounded_int(ArgT val,
typename std::enable_if<std::is_integral<ArgT>::value==
false
603 && std::is_convertible<ArgT,T>::value==
true>::type* = 0)
604 : val_(
static_cast<T
>(val))
606 rangeCheck_(
static_cast<T
>(val));
610 template <
typename ArgT=T>
611 bounded_int(ArgT val,
typename std::enable_if<std::is_integral<ArgT>::value
612 && std::is_signed<ArgT>::value!=std::is_signed<T>::value>::type* = 0)
619 template <
typename ArgT=T>
620 bounded_int(ArgT val,
typename std::enable_if<std::is_integral<ArgT>::value
621 && (
sizeof(ArgT) <=
sizeof(T))
622 && std::is_signed<ArgT>::value==std::is_signed<T>::value>::type* = 0)
634 template <
typename ArgT=T>
635 bounded_int(ArgT val,
typename std::enable_if<std::is_integral<ArgT>::value
636 && (
sizeof(ArgT) >
sizeof(T))
637 && std::is_signed<ArgT>::value==std::is_signed<T>::value>::type* = 0)
644 template <
typename ArgT>
645 void rangeCheck_(ArgT val){
646 lowerRangeCheck_(val);
647 upperRangeCheck_(val);
650 template <
typename ArgT=T>
651 typename std::enable_if<std::numeric_limits<ArgT>::min()!=min_bound,
void>::type
652 lowerRangeCheck_(ArgT val)
const {
654 "Tried to store a " <<
typeid(ArgT).name() <<
" into a bounded " <<
typeid(T).name() <<
". Not safe to "
655 "cast " << val <<
" into a type with a minimum of " << min_bound);
658 template <
typename ArgT=T>
659 typename std::enable_if<std::numeric_limits<ArgT>::min()==min_bound,
void>::type
660 lowerRangeCheck_(ArgT val)
const {
664 template <
typename ArgT=T>
665 typename std::enable_if<std::numeric_limits<ArgT>::max()!=max_bound,
void>::type
666 upperRangeCheck_(ArgT val)
const {
668 "Tried to store a " <<
typeid(ArgT).name() <<
" into a bounded " <<
typeid(T).name() <<
". Not safe to "
669 "cast " << val <<
" into a type with a maximum of " << max_bound);
672 template <
typename ArgT=T>
673 typename std::enable_if<std::numeric_limits<ArgT>::max()==max_bound,
void>::type
674 upperRangeCheck_(ArgT val)
const {
692 operator T()
const {
return val_; }
724#define GENERATE_HAS_ATTR(name) \
725 template <typename T> \
726 struct has_attr_##name { \
727 typedef char one[1]; \
728 typedef char two[2]; \
730 template <typename C> static one& test( decltype(&C::name) ) ; \
731 template <typename C> static two& test(...); \
733 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....