jeudi 17 décembre 2020

conditionally integrate functionality from one header into another if it's included (is this even necessary?)

Maybe I'm thinking about this the wrong way because I spent so many years on C# away from C++ and I'm a bit rusty. I forget how good selective linking is.

This is really a multi-part question, and I'll start by describing the big picture.

I am building a JSON parser and query engine for low memory environments like IoT devices. They don't have a ton of space for code so I want the end developer to be able to only include parts of my library they intend to use. The pull parser is the code of the library so that's a given, but you might not need the in-memory trees.

Currently, lacking a better way, I have a #define HTCW_JSONTREE which i define in one header. It gets picked up by a 2nd header and a function is included in the 2nd header that depends on code in the first header.

#include "JsonTree.h" // optional
#include "JsonReader.h" // if JsonTree.h above is included
// extra functionality will be available from JsonReader

basically a parseSubtree() function that returns an in-memory tree will be available if you include both headers, but won't if you don't include JsonTree.h. It smells.

First of all, is this necessary, or can I just unconditionally include all the functionality and expect that parseSubtree() will never get linked in if its never used?

Second, if it is necessary, what is a better way to do it? Right now the includes are order dependent and the code reeks. I want to change it. Basically it's just in there now until I figure out something better because it's easier to remove than it would have been to add later if it turns out i need it.

Thanks in advance.

Here's more of what the code looks like:

From JsonTree.hpp by way of JsonTree.h:

#ifndef HTCW_JSONTREE_HPP
#define HTCW_JSONTREE_HPP
#define HTCW_JSONTREE
#include <cinttypes>
...

from JsonReader.hpp by way of JsonReader.h:

#ifdef HTCW_JSONTREE
JsonElement* parseSubtree(mem::MemoryPool& pool,JsonParseFilter* pfilter = nullptr,mem::MemoryPool* pstringPool=nullptr,bool poolValues=false) {
...
#endif

Where JsonElement comes from JsonTree.h as well. parseSubtree is my integration point between the two areas of functionality

Aucun commentaire:

Enregistrer un commentaire