It really depends on what you are going to achieve.
If you are just trying to reuse the old code in a hurry, I would suggest:
SOLUTION 1
header.hpp
struct [[ eosio::table]] test {
uint64_t prim_key;
uint64_t timestamp;
auto primary_key() const { return prim_key; }
};
one.cpp
#include <eosiolib/asset.hpp>
#include <eosiolib/eosio.hpp>
using namespace eosio;
CONTRACT one : public contract {
private:
#include "../header.hpp"
typedef multi_index<name("one"), test> test_table;
public:
using contract::contract;
ACTION hi(name user) { print_f("Hello % from one", user); }
};
EOSIO_DISPATCH(one, (hi))
If you think it is ugly and want to spend some effort extract an macro, try:
SOLUTION 2
header.hpp
#include <eosiolib/eosio.hpp>
struct [[ eosio::table, eosio::contract(__MY__CONTRACT__)]] test {
uint64_t prim_key;
uint64_t timestamp;
auto primary_key() const { return prim_key; }
};
one.cpp
#define __MY__CONTRACT__ "one"
#include "../header.hpp"
#include <eosiolib/asset.hpp>
#include <eosiolib/eosio.hpp>
using namespace eosio;
CONTRACT one : public contract {
private:
typedef multi_index<name("one"), test> test_table;
public:
using contract::contract;
ACTION hi(name user) { print_f("Hello % from one", user); }
};
EOSIO_DISPATCH(one, (hi))
The last solution may not fit your needs as you need the table have same name with contract. However, we use it in our own projects so I would like show it here:
SOLUTION 3
header.hpp
#include <eosiolib/eosio.hpp>
struct test {
uint64_t prim_key;
uint64_t timestamp;
auto primary_key() const { return prim_key; }
};
one.cpp
#include "../header.hpp"
#include <eosiolib/asset.hpp>
#include <eosiolib/eosio.hpp>
using namespace eosio;
CONTRACT one : public contract {
private:
TABLE tableone : test{};
typedef multi_index<name("one"), test> test_table;
public:
using contract::contract;
ACTION hi(name user) { print_f("Hello % from one", user); }
};
EOSIO_DISPATCH(one, (hi))
Conclusion
This can be also done by many other macro tricks and you can pick which ever you like.
typedef eosio::multi_index< "accounts"_n, account > accounts;
This may also be a good resource to look at: developers.eos.io/eosio-home/docs/data-persistencetypedef eosio::multi_index<name("test"), test> t_test
. Btw, I will check, thanks for sharing. But I need to define a general table in the .hpp file, .hpp which includes also the table typedef.