support field copy in different c++ data type.
c++20
- COPYPP_FIELDS_NON_INTRUSIVE(destination, source, field1, field2, ...) is to be defined inside the namespace of the class/struct to create code for.
- COPYPP_FIELDS_INTRUSIVE(field1, field2, field3, ...) is to be defined outside the namespace of class/struct to create code for. This macro can also access private members.
- copy between json string and struct
- support array copy
include the header file
#include <icurve/copypp.hh>
COPYPP_FIELDS_NON_INTRUSIVE defination the need to copy class
class A {
public:
int id;
std::string name;
bool sex;
public:
A() = default;
A(int id, std::string name, bool sex) : id(id), name(name), sex(sex) {}
};
class B {
public:
int id;
std::string name;
bool sex;
public:
B() = default;
B(int id, std::string name, bool sex) : id(id), name(name), sex(sex) {}
};
add the fields
// need to copy fields from A to B
COPYPP_FIELDS_NON_INTRUSIVE(B, A, id, name, sex)
// declare variables
A a(1, "curve", true);
B b;
// copy a.{fields} the b
icurve::copy(b, a);
COPYPP_FIELDS_INTRUSIVE:
defination the class
class AA {
private:
int id;
std::string name;
bool sex;
public:
AA() = default;
AA(int id, std::string name, bool sex) : id(id), name(name), sex(sex) {}
public:
COPYPP_FIELDS_INTRUSIVE(AA, id, name, sex)
};
class BB {
private:
int id;
std::string name;
bool sex;
public:
BB() = default;
BB(int id, std::string name, bool sex) : id(id), name(name), sex(sex) {}
public:
COPYPP_FIELDS_INTRUSIVE(BB, id, name, sex)
};
AA a(10, "curve", true);
BB b;
icurve::copy(b, a);
copypp support copy between json's string and struct, this copy depency nlohmann-json library, so you need
- install the nlohmann-json lib
- add
#include <nlohmann-json>
before the copypp header#include <icurve/copypp.hh>
file - use COPYPP_FIELDS_INTRUSIVE defination method
#include <nlohmann/json.hpp>
#include "copypp_only.h"
void func() {
BB b;
std::string s = R"({"id":5,"name":"i-curve", "age": 25})";
icurve::copy(b, s); // copy from string to struct
icurve::copy(s, b); // copy from struct to string
}
c style array, you need to delivery the array message
AA a[3];
BB b[2] = {BB(1, "first", true), BB(2, "second", false)};
icurve::copy<AA, 3, BB, 2>(a, b);
c++ style array
std::array<AA,3> a;
std::array<BB, 2> b = {BB(1, "first", true), BB(2, "second", false)};
icurve::copy(a, b);
copypp support stl copy, condition:
- the source has forward end iterator(begin, end)
- the destiontion has forward iterator(begin, and)
- if the destination has back_insert_iterator, copypp will copy the all source item. Otherwise, it only copy the minimum length between source and destination
copypp can copy it, even in a different stl type.
AA a[3];
BB b[2] = {BB(1, "first", true), BB(2, "second", false)};
std::span<AA> aa(a);
std::span<BB> bb(b);
icurve::copy(aa, bb);
If it's vector, you can directly use it.
vector<AA> a;
vector<BB> b = {BB(1, "first", true), BB(2, "second", false)};
icurve::copy(a, b);
different stl type
if the destionation can append item, it will clear destionation and append new item.
std::vector<AA> a = {AA(1, "first", true), AA(2, "second", false)};
std::list<BB> b; // copy all items
icurve::copy(b, a);
std::vector<AA> a = {AA(1, "first", true), AA(2, "second", false)};
std::array<BB, 1> b;
std::array<BB, 3> c;
icurve::copy(b, a); // will copy 1 item(min(a.size(), b.size())
icurve::copy(c, a); // will copy 2 items(min(a.size(), c.size()))
copypp_only.hh is the single required file in include/icurve or released here. You need to copy to your include directories. then use it by
#include <copypp_only.hh>
using copy = icurve::copy;
clone the project and build.
git clone https://github.com/i-curve/copypp.git
cd copypp && mkdir build && cd build
# build and shut the test.if you open the test, you must install the fmt package.
cmake -DCOPYPP_TEST=OFF .
make
install
make install
if you use the vcpkg, you can install it by
vcpkg install copypp
conanfile.txt
[requires]
copypp/[version]
{copypp} is distributed under the MIT License.