Misc intermediate fixes, more work on these to follow.

--Bug fixes:
-Fix crash when using smooth interpolation in EmberGenome.
-Print error and exit EmberGenome when sequencing if times are not
sorted.

--Code changes:
-Another attempt at Singleton. This reverts the design to what it was
before with a fix to the code that was causing it not to behave like a
singleton should.
This commit is contained in:
Matt Feemster 2016-04-21 15:33:00 -07:00
parent d639921d68
commit cac7308776
9 changed files with 45 additions and 57 deletions

View File

@ -440,15 +440,16 @@ public:
Align(&embers[i1], &localEmbers[0], 2);
smoothFlag = false;
}
if (i2 == size - 1)
else if (i2 == size - 1)
{
Align(&embers[i1], &localEmbers[0], 2);
smoothFlag = false;
}
Align(&embers[i1 - 1], &localEmbers[0], 4);//Should really be doing some sort of checking here to ensure the ember vectors have 4 elements.
smoothFlag = true;
else
{
Align(&embers[i1 - 1], &localEmbers[0], 4);//Should really be doing some sort of checking here to ensure the ember vectors have 4 elements.
smoothFlag = true;
}
}
result.m_Time = time;

View File

@ -193,16 +193,13 @@ private:
/// member variables cannot be exported across module boundaries.
/// Derived classes should inherit from this using the CRTP, and declare a friend to it.
/// They also should make their constructors private and destructors public.
/// This has a severe flaw in that it cannot be used across module boundaries, else
/// every module will have its own copy. This makes it function as a per-module
/// singleton, which is unlikely to ever be desired.
/// Attribution: This class is a combination of
/// http://btorpey.github.io/blog/2014/02/12/shared-singletons/
/// and
/// http://enki-tech.blogspot.com/2012/08/c11-generic-singleton.html
/// </summary>
template <class T>
class EMBER_API Singleton
class Singleton
{
public:
/// <summary>
@ -213,7 +210,7 @@ public:
template <typename... Args>
static shared_ptr<T> Instance(Args... args)
{
static weak_ptr<T> staticInstance;
auto& staticInstance = GetStaticInstance();
auto temp = staticInstance.lock();
if (!temp)
@ -224,6 +221,18 @@ public:
return temp;
}
protected:
/// <summary>
/// Clever hack to get a static to behave like a member variable that can be seen between classes and functions in the hierarchy.
/// Also has the added benefit that it makes this work across module boundaries.
/// </summary>
/// <returns>static weak_ptr reference</returns>
static std::weak_ptr<T>& GetStaticInstance()
{
static std::weak_ptr<T> staticInstance;
return staticInstance;
}
};
//Use this if the body of the destructor will be implemented in a cpp file.
@ -231,8 +240,6 @@ public:
friend class Singleton<x>; \
public: \
~x(); \
\
private: \
x(const x& other) = delete; \
const x& operator=(const x& other) = delete//Semicolon deliberately omitted to force it on the caller.
@ -241,33 +248,9 @@ public:
friend class Singleton<x>; \
public: \
~x(){} \
\
private: \
x(const x& other) = delete; \
const x& operator=(const x& other) = delete
//Use this if not deriving from the Singleton class and are declaring Instance() in a header and implementing it in a cpp file.
#define SINGLETON_INSTANCE_DECL(x) \
static std::shared_ptr<x> Instance(); \
x(const x& other) = delete; \
const x& operator=(const x& other) = delete//Semicolon deliberately omitted to force it on the caller.
//Use this if not deriving from the Singleton class and are implementing Instance() in a cpp file.
#define SINGLETON_INSTANCE_IMPL(x) \
std::shared_ptr<x> x::Instance() \
{ \
static weak_ptr<x> staticInstance; \
auto temp = staticInstance.lock(); \
\
if (!temp) \
{ \
temp.reset(new x()); \
staticInstance = temp; \
} \
\
return temp; \
}
/// <summary>
/// Open a file in binary mode and read its entire contents into a vector of bytes. Optionally null terminate.
/// </summary>

View File

@ -13,7 +13,7 @@ namespace EmberNs
/// This class is a singleton since all of its data is shared and read-only.
/// </summary>
template <typename T>
class EMBER_API VarFuncs
class EMBER_API VarFuncs : public Singleton<VarFuncs<T>>
{
public:
/// <summary>
@ -542,7 +542,7 @@ public:
}
}
SINGLETON_INSTANCE_DECL(VarFuncs);//Implemented in VariationList.cpp
SINGLETON_DERIVED_IMPL(VarFuncs<T>);
private:
/// <summary>

View File

@ -16,14 +16,6 @@ namespace EmberNs
m_Variations.push_back(new Pre##varName##Variation<T>()); \
m_Variations.push_back(new Post##varName##Variation<T>());
/// <summary>
/// Singleton patterns, return a reference to the only instance of this object in existence.
/// </summary>
template <typename T>
SINGLETON_INSTANCE_IMPL(VarFuncs<T>)
template <typename T>
SINGLETON_INSTANCE_IMPL(VariationList<T>)
/// <summary>
/// Constructor which initializes all of the variation objects and stores them in the list.
/// </summary>

View File

@ -18,10 +18,10 @@ namespace EmberNs
/// Template argument expected to be float or double.
/// </summary>
template <typename T>
class EMBER_API VariationList
class EMBER_API VariationList: public Singleton<VariationList<T>>
{
public:
~VariationList();
//~VariationList();
const Variation<T>* GetVariation(size_t index) const;
const Variation<T>* GetVariation(size_t index, eVariationType varType) const;
Variation<T>* GetVariationCopy(size_t index, T weight = 1) const;
@ -45,7 +45,7 @@ public:
const vector<Variation<T>*>& PreVars() const;
const vector<Variation<T>*>& PostVars() const;
SINGLETON_INSTANCE_DECL(VariationList);//Implemented in VariationList.cpp
SINGLETON_DERIVED_DECL(VariationList<T>);
private:
VariationList();

View File

@ -68,8 +68,6 @@ OpenCLInfo::OpenCLInfo()
}
}
SINGLETON_INSTANCE_IMPL(OpenCLInfo)
/// <summary>
/// Get a const reference to the vector of available platforms.
/// </summary>

View File

@ -17,7 +17,7 @@ namespace EmberCLns
/// This class derives from EmberReport, so the caller is able
/// to retrieve a text dump of error information if any errors occur.
/// </summary>
class EMBERCL_API OpenCLInfo : public EmberReport
class EMBERCL_API OpenCLInfo : public EmberReport, public Singleton<OpenCLInfo>
{
public:
const vector<cl::Platform>& Platforms() const;
@ -54,9 +54,10 @@ public:
return val;
}
SINGLETON_INSTANCE_DECL(OpenCLInfo);
SINGLETON_DERIVED_IMPL(OpenCLInfo);
private:
OpenCLInfo();
bool m_Init;

View File

@ -339,7 +339,7 @@ bool EmberGenome(EmberOptions& opt)
{
if (i > 0 && embers[i].m_Time <= embers[i - 1].m_Time)
{
cerr << "Error: control points must be sorted by time, but " << embers[i].m_Time << " <= " << embers[i - 1].m_Time << ", index " << i << ".\n";
cerr << "Error: control points must be sorted by time, but time " << embers[i].m_Time << " <= " << embers[i - 1].m_Time << ", index " << i << ".\n";
return false;
}
@ -405,6 +405,15 @@ bool EmberGenome(EmberOptions& opt)
return false;
}
for (i = 0; i < embers.size(); i++)
{
if (i > 0 && embers[i].m_Time <= embers[i - 1].m_Time)
{
cerr << "Error: control points must be sorted by time, but time " << embers[i].m_Time << " <= " << embers[i - 1].m_Time << ", index " << i << ".\n";
return false;
}
}
if (opt.Enclosed())
cout << "<sequence version=\"EMBER-" << EmberVersion() << "\">\n";
@ -529,8 +538,8 @@ bool EmberGenome(EmberOptions& opt)
oldY = embers[i].m_CenterY;
embers[i].m_FinalRasH = size_t(T(embers[i].m_FinalRasH) / T(opt.Frames()));
embers[i].m_CenterY = embers[i].m_CenterY - ((opt.Frames() - 1) * embers[i].m_FinalRasH) /
(2 * embers[i].m_PixelsPerUnit * pow(T(2.0), embers[i].m_Zoom));
embers[i].m_CenterY += embers[i].m_FinalRasH * opt.Frame() / (embers[i].m_PixelsPerUnit * pow(T(2.0), embers[i].m_Zoom));
(2 * embers[i].m_PixelsPerUnit * std::pow(T(2.0), embers[i].m_Zoom));
embers[i].m_CenterY += embers[i].m_FinalRasH * opt.Frame() / (embers[i].m_PixelsPerUnit * std::pow(T(2.0), embers[i].m_Zoom));
tools.RotateOldCenterBy(embers[i].m_CenterX, embers[i].m_CenterY, oldX, oldY, embers[i].m_Rotate);
if (pTemplate)

View File

@ -27,8 +27,12 @@ int main(int argc, char* argv[])
putenv(const_cast<char*>("GPU_MAX_ALLOC_PERCENT=100"));
#endif
int rv = -1;
auto vlf = VariationList<float>::Instance();//Create two instances that will stay alive until this function exits.
auto vf = VarFuncs<float>::Instance();//Create instances that will stay alive until the program exits.
auto vlf = VariationList<float>::Instance();
#ifdef DO_DOUBLE
auto vd = VarFuncs<float>::Instance();
auto vld = VariationList<double>::Instance();//No further creations should occur after this.
#endif
try
{