Skip to content

Commit 4a2bcf2

Browse files
committed
Export ValueSet base classes for use in DLL
Mark friend operator in Enum class as inline to prevent duplication Make STL ValueSet members private/protected Fix Condition copy assignment operator where existing children where discarded (leaked)
1 parent a778308 commit 4a2bcf2

File tree

6 files changed

+53
-28
lines changed

6 files changed

+53
-28
lines changed

include/ValueSet.hpp

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ namespace ePhotosynthesis {
4444
#define DO_VALUE_SET_CHILD_CLASSES_MACRO(macro, iter, ...) \
4545
if (!noChildren) { \
4646
size_t iChild = 0; \
47-
for (std::vector<ValueSetClassWrapper>::iter it = child_classes.begin(); \
48-
it != child_classes.end(); it++, iChild++) { \
47+
for (std::vector<ValueSetClassWrapper>::iter it = BaseClass::child_classes.begin(); \
48+
it != BaseClass::child_classes.end(); it++, iChild++) { \
4949
if (*it) { \
5050
macro((*it), __VA_ARGS__); \
5151
} \
@@ -714,7 +714,7 @@ namespace ePhotosynthesis {
714714
Untemplated base class to allow storage of mixed value sets in
715715
containers.
716716
*/
717-
class ValueSet_t {
717+
class EPHOTO_API ValueSet_t {
718718
public:
719719

720720
virtual ~ValueSet_t() {}
@@ -889,7 +889,7 @@ namespace ePhotosynthesis {
889889
will be used by.
890890
*/
891891
template<class T, class U, MODULE ID, PARAM_TYPE PT>
892-
class ValueSetBase : public ValueSetEnum<ID, PT> {
892+
class EPHOTO_API ValueSetBase : public ValueSetEnum<ID, PT> {
893893
public:
894894
typedef T ThisClass; /** Inheriting class */
895895
typedef U ThisParent; /** Parent class */
@@ -1097,6 +1097,14 @@ namespace ePhotosynthesis {
10971097
print_value(val, out, show_pointers);
10981098
return std::move(out).str();
10991099
}
1100+
/**
1101+
Get a string containing a certain number of tabs.
1102+
\param[in] tab Number of tabs to include.
1103+
\returns String containing tabs.
1104+
*/
1105+
static std::string getTabs(const uint tab = 0) {
1106+
return std::string(tab * tab_size, ' ');
1107+
}
11001108
/**
11011109
Display a single value from a value map.
11021110
\tparam V Type of value in value map.
@@ -1129,7 +1137,7 @@ namespace ePhotosynthesis {
11291137
if ((!includeSkipped) && (isSkipped(key) ||
11301138
isConstant(key)))
11311139
return out;
1132-
const std::string space(tab * tab_size, ' ');
1140+
const std::string space = getTabs(tab);
11331141
std::string iname;
11341142
if (includePrefixes)
11351143
iname = getNameWithPrefix(key);
@@ -2401,12 +2409,14 @@ namespace ePhotosynthesis {
24012409
child_classes.emplace_back(new ValueSetClassType<C>());
24022410
}
24032411

2412+
protected:
24042413
static int static_flags; /**< Bitwise flags describing the value set static state */
24052414
static std::map<EnumType, double> defaults; /**< Default values */
24062415
static std::map<EnumType, double*> static_values; /**< Static values */
24072416
static std::vector<ValueSetClassWrapper> child_classes; /**< Child classes */
24082417
static const uint tab_size = 2;
24092418

2419+
public:
24102420
// Methods that call inheriting class methods
24112421
/**
24122422
Initialize the child classes.
@@ -2460,6 +2470,13 @@ namespace ePhotosynthesis {
24602470
if (!(static_flags & VS_FLAG_SELECTED))
24612471
ERROR_VALUE_SET("Value set has not been selected");
24622472
}
2473+
/**
2474+
Check if the value set has children.
2475+
\returns true if the value set has children, false otherwise.
2476+
*/
2477+
static bool hasChildren() {
2478+
return (child_classes.size() > 0);
2479+
}
24632480
/**
24642481
Check if the value set uses the C3 defaults.
24652482
\returns true if the C3 defaults are being used, false otherwise.
@@ -3220,6 +3237,7 @@ namespace ePhotosynthesis {
32203237
children.push_back(C);
32213238
}
32223239

3240+
private:
32233241
int flags = 0; /**< Bitwise flags describing the value set */
32243242
std::map<EnumType, double*> values; /**< Values in the set. */
32253243
std::map<EnumType, double> preinit_values; /**< Value set pre-initialization */
@@ -3266,7 +3284,7 @@ namespace ePhotosynthesis {
32663284
ENSURE_VALUE_POINTERS_CONST;
32673285
uint child_tab = tab;
32683286
if (!(noChildren || includePrefixes)) {
3269-
const std::string space(tab * BaseClass::tab_size, ' ');
3287+
const std::string space = BaseClass::getTabs(tab);
32703288
out << space << error_prefix() << std::endl;
32713289
child_tab++;
32723290
}
@@ -3355,7 +3373,7 @@ namespace ePhotosynthesis {
33553373
}
33563374
BaseClass::import_value_map(values, map, CONTEXT_VALUE_SET);
33573375
if (setinit)
3358-
static_flags |= BaseClass::VS_FLAG_INIT_VALUES;
3376+
BaseClass::static_flags |= BaseClass::VS_FLAG_INIT_VALUES;
33593377
}
33603378

33613379
/** \copydoc ValueSet::initMembers */
@@ -3371,11 +3389,11 @@ namespace ePhotosynthesis {
33713389
reset_value_map(values);
33723390
T::_resetValues();
33733391
DO_VALUE_SET_CHILD_CLASSES(resetValues, ());
3374-
static_flags &= ~BaseClass::VS_FLAG_INIT_VALUES;
3392+
BaseClass::static_flags &= ~BaseClass::VS_FLAG_INIT_VALUES;
33753393
}
33763394
/** \copydoc ValueSet::initialized */
33773395
static bool initialized() {
3378-
return (static_flags & BaseClass::VS_FLAG_INIT_VALUES);
3396+
return (BaseClass::static_flags & BaseClass::VS_FLAG_INIT_VALUES);
33793397
}
33803398
/** \copydoc ValueSet::initValues */
33813399
static void initValues(const bool noDefaults=false,
@@ -3386,21 +3404,25 @@ namespace ePhotosynthesis {
33863404
if (!noDefaults)
33873405
init_value_map(values, initialized());
33883406
T::_initValues();
3407+
// if (!noChildren) {
3408+
// ERROR_VALUE_SET("recursion for child classes disabled.");
3409+
// }
33893410
DO_VALUE_SET_CHILD_CLASSES(initValues, (noDefaults, force));
33903411
if (!preinit_values.empty()) {
33913412
copy_value_map(values, preinit_values, "initValues: ",
33923413
false, true, true, false, true);
33933414
}
3394-
static_flags |= BaseClass::VS_FLAG_INIT_VALUES;
3415+
BaseClass::static_flags |= BaseClass::VS_FLAG_INIT_VALUES;
33953416
}
33963417

33973418
/** \copydoc ValueSet::initMemberPointers */
33983419
static void initMemberPointers(const bool noChildren=false) {
3399-
if (static_flags & BaseClass::VS_FLAG_INIT_POINTERS) return;
3420+
if (BaseClass::static_flags & BaseClass::VS_FLAG_INIT_POINTERS)
3421+
return;
34003422
BaseClass::initDefaults();
34013423
T::_initMemberPointers();
34023424
DO_VALUE_SET_CHILD_CLASSES(initMemberPointers, ());
3403-
static_flags |= BaseClass::VS_FLAG_INIT_POINTERS;
3425+
BaseClass::static_flags |= BaseClass::VS_FLAG_INIT_POINTERS;
34043426
}
34053427
/** \copydoc ValueSetBase::initStaticMembers */
34063428
static void initStaticMembers(const bool noChildren = false) {
@@ -3670,9 +3692,10 @@ namespace ePhotosynthesis {
36703692
*/
36713693
template<typename C>
36723694
static void addChildClass() {
3673-
child_classes.push_back(new ValueSetStaticClassType<C>());
3695+
BaseClass::child_classes.push_back(new ValueSetStaticClassType<C>());
36743696
}
36753697

3698+
private:
36763699
static std::map<EnumType, double*> values; /**< Values in the set. */
36773700
static std::map<EnumType, double> preinit_values; /**< Value set pre-initialization */
36783701

@@ -3701,7 +3724,7 @@ namespace ePhotosynthesis {
37013724
ENSURE_VALUE_POINTERS;
37023725
uint child_tab = tab;
37033726
if (!(noChildren || includePrefixes)) {
3704-
const std::string space(tab * BaseClass::tab_size, ' ');
3727+
const std::string space = BaseClass::getTabs(tab);
37053728
out << space << error_prefix() << std::endl;
37063729
child_tab++;
37073730
}

include/conditions/ConditionBase.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class ConditionBase : public VALUE_SET_PARENT(T, U, ID, PARAM_TYPE_COND) {
120120
static void _initStaticMembers() {
121121
ParentClass::_initStaticMembers();
122122
#ifdef INCDEBUG
123-
if (ParentClass::child_classes.size() > 0)
123+
if (ParentClass::hasChildren())
124124
T::_dlevel = Debug::Middle;
125125
#endif // INCDEBUG
126126
}
@@ -421,8 +421,12 @@ private: \
421421
\param name Name of the module the child condition is associated with.
422422
*/
423423
#define DEFINE_CONDITION_COMPOSITE_CHILD_CREATE_OTHER(name) \
424-
name ## _con = new VARS_CLASS_VAR_LOCAL(name, COND)(other.name ## _con); \
425-
name ## _con->setParent(this)
424+
if (name ## _con) { \
425+
(*name ## _con) = other.name ## _con; \
426+
} else { \
427+
name ## _con = new VARS_CLASS_VAR_LOCAL(name, COND)(other.name ## _con); \
428+
name ## _con->setParent(this); \
429+
}
426430
/**
427431
Utility to initialize a child condition from another child condition.
428432
\param name Name of the module the child condition is associated with.

include/enums/enums.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// modified directly
33
#pragma once
44

5+
#include "ePhotosynthesis_export.h"
56
#include <string>
67
#include <map>
78
#include <vector>
@@ -243,7 +244,7 @@ namespace ePhotosynthesis {
243244
};
244245
#endif // EPHOTO_USE_SCOPED_ENUM
245246
template<MODULE M, PARAM_TYPE PT>
246-
class ValueSetEnum {
247+
class EPHOTO_API ValueSetEnum {
247248
public:
248249
#ifdef EPHOTO_USE_SCOPED_ENUM
249250
typedef typename enum_helper<M, PT>::type Type;
@@ -282,7 +283,7 @@ namespace ePhotosynthesis {
282283
\param[in] x Key to serialize
283284
\return Updated stream
284285
*/
285-
friend std::ostream& operator<<(std::ostream& out, const Type& x) {
286+
friend inline std::ostream& operator<<(std::ostream& out, const Type& x) {
286287
out << getName(x);
287288
return out;
288289
}
@@ -292,7 +293,7 @@ namespace ePhotosynthesis {
292293
\param[in] x Collection to serialize
293294
\return Updated stream
294295
*/
295-
friend std::ostream& operator<<(std::ostream& out, const std::map<Type, std::string>& x) {
296+
friend inline std::ostream& operator<<(std::ostream& out, const std::map<Type, std::string>& x) {
296297
print_map(x, out);
297298
return out;
298299
}
@@ -607,7 +608,7 @@ namespace ePhotosynthesis {
607608
\param[in] x Collection to serialize
608609
\return Updated stream
609610
*/
610-
friend std::ostream& operator<<(std::ostream& out, const std::vector<Type>& x) {
611+
friend inline std::ostream& operator<<(std::ostream& out, const std::vector<Type>& x) {
611612
print_vector(x, out);
612613
return out;
613614
}

include/macros.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,6 @@
197197

198198
#define INHERIT_METHODS_VALUE_SET_BASE(...) \
199199
INHERIT_METHODS_VALUE_SET_TYPES(__VA_ARGS__) \
200-
using __VA_ARGS__::static_flags; \
201-
using __VA_ARGS__::defaults; \
202-
using __VA_ARGS__::static_values; \
203-
using __VA_ARGS__::child_classes; \
204200
using __VA_ARGS__::getValueSetClass; \
205201
using __VA_ARGS__::inArrays; \
206202
using __VA_ARGS__::remove_skipped; \

src/Variables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Variables::Variables(SUNContext* ctx, const int flags) :
6868
}
6969
select();
7070
useC3 = usesC3();
71-
initValues();
71+
initValues(false, false, true);
7272
}
7373
Variables::Variables() : Variables((SUNContext*)nullptr) {}
7474
Variables::Variables(const Variables& other) :

utils/generate_enum.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ def generate_additional_method(self, function_type, enum_name='Type',
14771477
lines += ['*/']
14781478
static = 'static'
14791479
if function_type in ['operator<<']:
1480-
static = 'friend'
1480+
static = 'friend inline'
14811481
lines += [f"{static} {return_type} {function_name}"
14821482
f"({', '.join(args)}) {{"]
14831483
lines += [" " + x for x in body]
@@ -1779,6 +1779,7 @@ class CEnumGeneratorGlobalHeader(CEnumGeneratorBase):
17791779
name = 'global'
17801780
file_suffix = ''
17811781
prefix = CEnumGeneratorBase.prefix + [
1782+
'#include "ePhotosynthesis_export.h"',
17821783
'#include <string>',
17831784
'#include <map>',
17841785
'#include <vector>',
@@ -2424,7 +2425,7 @@ def generate_declaration(self, name, members, enum_name=None,
24242425
lines += ['#endif // EPHOTO_USE_SCOPED_ENUM']
24252426
lines += template_lines
24262427
lines += [
2427-
f'class {class_name}{specialization} {{',
2428+
f'class EPHOTO_API {class_name}{specialization} {{',
24282429
'public:',
24292430
]
24302431
lines += [

0 commit comments

Comments
 (0)