0% found this document useful (0 votes)
61 views

Static Asserts Example

The document contains C++ code defining functions and classes for converting between different versions of an rtp_addition struct. It includes static assertions to check template types, functions to dynamically fill arrays and shared pointers from source data, and constructor functions to populate interface classes from different rtp_addition versions.

Uploaded by

SK_shivam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Static Asserts Example

The document contains C++ code defining functions and classes for converting between different versions of an rtp_addition struct. It includes static assertions to check template types, functions to dynamically fill arrays and shared pointers from source data, and constructor functions to populate interface classes from different rtp_addition versions.

Uploaded by

SK_shivam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

template<typename T1, typename T2, size_t N2>

static void dynamicfill(const T1* source, T2(&dest)[N2])


{
static_assert(std::is_copy_assignable<T1>::value, "source data
type must be assignable.");
static_assert(std::is_fundamental<T1>::value, "source data
type must not be a compound type.");
static_assert(std::is_integral<T1>::value, "source data type
must be an integral type.");
static_assert(std::is_signed<T1>::value, "source data type
must be an signed type.");

static_assert(std::is_copy_assignable<T2>::value, "dest data


type must be assignable.");
static_assert(std::is_fundamental<T2>::value, "dest data type
must not be a compound type.");
static_assert(std::is_integral<T2>::value, "dest data type
must be an integral type.");
static_assert(std::is_signed<T2>::value, "dest data type must
be an signed type.");

static_assert(std::is_convertible<T1, T2>::value, "source data


type must be convertible to dest data type.");

if (source != nullptr)
{
size_t source_i(0);
for (source_i = 0; (source[source_i] != -1 && source_i
<= N2); ++source_i) // count elements
{
// No body
}

++source_i; // to include the -1


source_i = std::min(source_i,
static_cast<size_t>(N2));
std::fill_n(dest, N2, -1);

for (size_t i(0); i < source_i; ++i)


{
dest[i] = static_cast<T2>(source[i]);
}
}
}

template<typename T1, typename T2>


static void dynamicfill(const T1* source, std::shared_ptr<T2>& dest)
{
static_assert(std::is_copy_assignable<T1>::value, "source data
type must be assignable.");
static_assert(std::is_fundamental<T1>::value, "source data
type must not be a compound type.");
static_assert(std::is_integral<T1>::value, "source data type
must be an integral type.");
static_assert(std::is_signed<T1>::value, "source data type
must be an signed type.");
static_assert(!std::is_pointer<T2>::value, "dest data type
must not be a pointer.");
static_assert(std::is_copy_assignable<T2>::value, "dest data
type must be assignable.");
static_assert(std::is_fundamental<T2>::value, "dest data type
must not be a compound type.");
static_assert(std::is_fundamental<T2>::value, "dest data type
must not be a compound type.");
static_assert(std::is_integral<T2>::value, "dest data type
must be an integral type.");
static_assert(std::is_signed<T2>::value, "dest data type must
be an signed type.");

static_assert(std::is_convertible<T1, T2>::value, "source data


type must be convertible to dest data type.");

if (source != nullptr)
{
size_t source_i(0);
for (source_i = 0; (source[source_i] != -1 && source_i
<= LINK_PROGRESSIVE); ++source_i); // count elements

++source_i;// to include the -1


source_i = std::min(source_i,
static_cast<size_t>(LINK_PROGRESSIVE));

if (dest == nullptr)
{
dest = std::shared_ptr<T2>(new T2[source_i],
std::default_delete<T2[]>());
std::fill_n(dest.get(), source_i, -1);
}

for (size_t i(0); i < source_i; ++i)


{
dest.get()[i] = static_cast<T2>(source[i]);
}
}
}
template<typename T>
bool arraycmp(T* a, T* b, bool withShortCircuit = false)
{
static_assert(std::is_integral<T>::value, "dest data type must
be an integral type.");
static_assert(std::is_signed<T>::value, "dest data type must
be an signed type.");
static const T negativeOne(-1);

bool result(true);

if (a == nullptr || b == nullptr)
{
result = false;
}
else
{
bool b_check(false);

for (T i(0); i < LINK_PROGRESSIVE; ++i)


{
if (withShortCircuit)
{
if (a[i] == negativeOne)
{
break;
}
if (!b_check && b[i] == negativeOne)
{
b_check = true;
}
if (a[i] != (b_check ? 0 : b[i]))
{
result = false;
}
}
else
{
result = (a[i] == b[i]);
}

if (!result || (a[i] == negativeOne && b[i]


== negativeOne))
{
break;
}
}
}

return result;
}
}
class rtp_addition_interface_v1 : public rtp_addition
{
private:
rtp_addition_interface_v1() {};
public:
rtp_addition_interface_v1(const rtp_addition& v1);

rtp_addition_interface_v1(const rtp_addition_v2& v2);

rtp_addition_interface_v1(const rtp_addition_v3& v3);


};

class rtp_addition_interface_v2 : public rtp_addition_v2


{
private:
std::shared_ptr<long> pStartups;
std::shared_ptr<long> pContribs;

rtp_addition_interface_v2() {};
public:
rtp_addition_interface_v2(const rtp_addition& v1);

rtp_addition_interface_v2(const rtp_addition_v2& v2);

rtp_addition_interface_v2(const rtp_addition_v3& v3);


};

class rtp_addition_interface_v3 : public rtp_addition_v3


{
private:
std::shared_ptr<int64_t> pStartups;
std::shared_ptr<int32_t> pContribs;

rtp_addition_interface_v3() {};
public:
rtp_addition_interface_v3(const rtp_addition& v1);
rtp_addition_interface_v3(const rtp_addition_v2& v2);

rtp_addition_interface_v3(const rtp_addition_v3& v3);

rtp_addition_interface_v3(char* type, char* boost, char* incrm, const


int32_t& variation, const uint32_t& turnover, const uint32_t& denomination,
int64_t* startups, int32_t* contribs);

void Update(const rtp_addition& v1);

void Update(const rtp_addition_v2& v2);

void Update(const rtp_addition_v3& v3);

public:

bool operator==(const rtp_addition_interface_v3& v3);

bool operator!=(const rtp_addition_interface_v3& v3)


{
return !(*this == v3);
}
};

/*
* $DOC
* FUNCTION: rtp_addition_interface_v1
* ARGS: v1 - version 1 rtp_addition struct
* DESCRIPTION: populate rtp_addition_interface_v1 from a version 1
rtp_addition struct
* $ENDDOC
*/
rtp_addition_interface_v1::rtp_addition_interface_v1(const rtp_addition& v1)
{
type = v1.type;
boost = v1.boost;
variation = v1.variation;
turnover = v1.turnover;
denomination = v1.denomination;

ATI::dynamicfill(v1.startups, startups);
ATI::dynamicfill(v1.contribs, contribs);
};

/*
* $DOC
* FUNCTION: rtp_addition_interface_v1
* ARGS: v2 - version 2 rtp_addition struct
* DESCRIPTION: populate rtp_addition_interface_v1 from a version 2
rtp_addition struct
* $ENDDOC
*/
rtp_addition_interface_v1::rtp_addition_interface_v1(const rtp_addition_v2&
v2)
{
type = const_cast<const char*>(v2.type);
boost = const_cast<const char*>(v2.boost);
variation = v2.variation;
turnover = v2.turnover;
denomination = v2.denomination;

ATI::dynamicfill(v2.startups, startups);
ATI::dynamicfill(v2.contribs, contribs);
};
/*
* $DOC
* FUNCTION: rtp_addition_interface_v1
* ARGS: v3 - version 3 rtp_addition struct
* DESCRIPTION: populate rtp_addition_interface_v1 from a version 3
rtp_addition struct
* $ENDDOC
*/
rtp_addition_interface_v1::rtp_addition_interface_v1(const rtp_addition_v3&
v3)
{
type = const_cast<const char*>(v3.type);
boost = const_cast<const char*>(v3.boost);
variation = v3.variation;
turnover = v3.turnover;
denomination = v3.denomination;

ATI::dynamicfill(v3.startups, startups);
ATI::dynamicfill(v3.contribs, contribs);
};

/*
* $DOC
* FUNCTION: rtp_addition_interface_v2
* ARGS: v1 - version 1 rtp_addition struct
* DESCRIPTION: populate rtp_addition_interface_v2 from a version 1
rtp_addition struct
* $ENDDOC
*/
rtp_addition_interface_v2::rtp_addition_interface_v2(const rtp_addition& v1)
: pStartups(nullptr)
, pContribs(nullptr)
{
type = const_cast<char*>(v1.type);
boost = const_cast<char*>(v1.boost);
incrm = nullptr;
variation = v1.variation;
turnover = v1.turnover;
denomination = v1.denomination;

ATI::dynamicfill(v1.startups, pStartups);
ATI::dynamicfill(v1.contribs, pContribs);

startups = (pStartups == nullptr) ? nullptr : pStartups.get();


contribs = (pContribs == nullptr) ? nullptr : pContribs.get();
};
/*
* $DOC
* FUNCTION: rtp_addition_interface_v2
* ARGS: v2 - version 2 rtp_addition struct
* DESCRIPTION: populate rtp_addition_interface_v2 from a version 2
rtp_addition struct
* $ENDDOC
*/
rtp_addition_interface_v2::rtp_addition_interface_v2(const rtp_addition_v2&
v2)
: pStartups(nullptr)
, pContribs(nullptr)
{
type = v2.type;
boost = v2.boost;
incrm = v2.incrm;
variation = v2.variation;
turnover = v2.turnover;
denomination = v2.denomination;

ATI::dynamicfill(v2.startups, pStartups);
ATI::dynamicfill(v2.contribs, pContribs);

startups = (pStartups == nullptr) ? nullptr : pStartups.get();


contribs = (pContribs == nullptr) ? nullptr : pContribs.get();
};

/*
* $DOC
* FUNCTION: rtp_addition_interface_v2
* ARGS: v3 - version 3 rtp_addition struct
* DESCRIPTION: populate rtp_addition_interface_v2 from a version 3
rtp_addition struct
* $ENDDOC
*/
rtp_addition_interface_v2::rtp_addition_interface_v2(const rtp_addition_v3&
v3)
: pStartups(nullptr)
, pContribs(nullptr)
{
type = v3.type;
boost = v3.boost;
incrm = v3.incrm;
variation = v3.variation;
turnover = v3.turnover;
denomination = v3.denomination;

ATI::dynamicfill(v3.startups, pStartups);
ATI::dynamicfill(v3.contribs, pContribs);

startups = (pStartups == nullptr) ? nullptr : pStartups.get();


contribs = (pContribs == nullptr) ? nullptr : pContribs.get();
};

You might also like