I have general header, which I wanna include in different contracts:

ent.h:

//@abi table entities i64
struct entity {
    entity() {};
    uint64_t id;    
    uint64_t primary_key() const { return id; }
    EOSLIB_SERIALIZE( entity, (id))
};

I need table "entities" in abi only in one contract, and don't need in second. I've tried macros:

#define GEN_TABLE
#ifdef GEN_TABLE
//@abi table entities i64
#endif //that's not work. Table isn't generate
struct entity {
    entity() {};
    uint64_t id;    
    uint64_t primary_key() const { return id; }
    EOSLIB_SERIALIZE( entity, (id))
};

and tried post struct definition:

struct entity {
    entity() {};
    uint64_t id;    
    uint64_t primary_key() const { return id; }
    EOSLIB_SERIALIZE( entity, (id))
};
...
cpp:
//@abi table entities i64
struct entity; //doesn't generate table

It didn't help.

Is it possible to do it without copy-paste code?

Macros should work, but you'd have to edit the eosiocpp script in order to be able to use them when you compile the code. You can't just do eosiocpp -DGEN_TABLE like in normal c++.

  • They don't work. If I have some code after comment with abi, it wouldn't generate... Look example: #define tst #ifdef tst //@abi table entities i64 #endif // tst – Philipp Ponomarev Sep 17 at 5:58
  • Can you be more specific about why they don't work? What exactly did you try to do to use precompilation defines? – Phillip Hamnett - EOS42 Sep 17 at 6:49
  • I didn't modify eosiocpp, but I predefined in code #define GEN_TABLE before ifdef, so //abi... was included to build, but abi table didn't generate. Or is it some difference between predefine in code and predefine in build? Look at second codesinppet in question, I modified it. Thank u anyway! – Philipp Ponomarev Sep 17 at 7:18
  • Ah I get what you mean now. I'm not sure, but it could be that the //@abi part has to be directly above the table, and if you use these preprocessor directives, then it appears 2 lines above with a blank line inbetween. Trydoing the //@abi... part followed on the same line by #endif? – Phillip Hamnett - EOS42 Sep 17 at 8:19
  • I don't see a way to do it, abi comment starts with //, so it cannot ends with #ifdef. U cannot write abi comment after #endif cause it will not have any sense... Or am I missing your idea? – Philipp Ponomarev Sep 17 at 8:28
up vote 0 down vote accepted

Ok. There's no way to do it with eosiocpp, but it will be removed in feature releases.

To do that trick with eosio.cdt just use macroses:

#define INC_TO_BUILD
#ifdef INC_TO_BUILD
#define ABI_TABLE [[eosio::table]]
#define ABI_ACTION [[eosio::action]]
#else
#define ABI_TABLE
#define ABI_ACTION
#endif // INC_TO_BUILD
...
struct ABI_TABLE state {...}

If u need to use name parameter, u can use that:

#define INC_TO_BUILD
#ifdef INC_TO_BUILD
#define ABI_TABLE(...) [[eosio::table(#__VA_ARGS__)]]
#define ABI_ACTION [[eosio::action]]
#else
#define ABI_TABLE(...)
#define ABI_ACTION
#endif // INC_TO_BUILD
...
struct ABI_TABLE(entities) entity {...}

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.