Skip to content
Snippets Groups Projects
Commit 523c7662 authored by Jason Hiser's avatar Jason Hiser :tractor:
Browse files

refactored to deal with options better

parent e4259da0
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,7 @@ namespace Zipr_SDK
Placeable_t() {}
Placeable_t(const Placeable_t& copy) = delete;
public:
virtual ~Placeable_t() {}
virtual void Place(RangeAddress_t place) = 0;
virtual RangeAddress_t getPlace() const = 0;
virtual bool isPlaced() const = 0;
......@@ -33,6 +34,7 @@ namespace Zipr_SDK
DollopPatch_t() { }
DollopPatch_t(const DollopPatch_t& copy) = delete;
public:
virtual ~DollopPatch_t() { }
virtual Dollop_t* getTarget() const = 0 ;
virtual void setTarget(Dollop_t *target) = 0 ;
......@@ -46,6 +48,8 @@ namespace Zipr_SDK
DollopEntry_t() { }
DollopEntry_t(const DollopEntry_t& copy) = delete;
public:
virtual ~DollopEntry_t() { }
virtual void setTargetDollop(Dollop_t *target) = 0 ;
virtual void MemberOfDollop(Dollop_t *member_of) = 0 ;
......@@ -67,6 +71,7 @@ namespace Zipr_SDK
Dollop_t(const Dollop_t& copy) = delete;
public:
virtual ~Dollop_t() {}
virtual size_t getSize() const = 0;
virtual size_t getDollopEntryCount() const = 0;
......
......@@ -17,9 +17,12 @@ namespace Zipr_SDK
class DollopManager_t
{
public:
protected:
DollopManager_t() {};
DollopManager_t(const DollopManager_t& copy) = delete;
public:
virtual ~DollopManager_t() {};
/*
* Add a dollop and all its fallthrough dollops
......
......@@ -7,9 +7,14 @@ namespace Zipr_SDK
using RangeSetBounds_t = pair<RangeSet_t::const_iterator,RangeSet_t::const_iterator>;
// a memory space _is_ a map of range addres to char, with additional functionality.
class MemorySpace_t : public map<RangeAddress_t,char>
class MemorySpace_t : virtual public map<RangeAddress_t,char>
{
protected:
MemorySpace_t(){}
MemorySpace_t(const MemorySpace_t& copy) = delete;
public:
virtual ~MemorySpace_t(){}
// range operatations
virtual void splitFreeRange(RangeAddress_t addr) = 0;
......
#include <string>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <set>
namespace Zipr_SDK
{
using namespace std;
class ZiprOption_t
{
protected:
ZiprOption_t() { }
ZiprOption_t(const ZiprOption_t& copy) = delete;
public:
virtual ~ZiprOption_t() { }
virtual string getValueString() const = 0;
virtual string getNamespace() const = 0;
virtual string getKey() const = 0;
virtual string getDescription() const = 0;
virtual bool areRequirementMet() const = 0;
virtual void setRequired(bool req=true) = 0;
};
template <class T>
class ZiprTypedOption_t : virtual public ZiprOption_t
{
protected:
ZiprTypedOption_t() { }
ZiprTypedOption_t(const ZiprTypedOption_t& copy) = delete;
public:
virtual ~ZiprTypedOption_t() { }
virtual T getValue() const = 0;
virtual operator T() const = 0;
};
class ZiprStringOption_t : virtual public ZiprTypedOption_t<string>
{
protected:
ZiprStringOption_t() { }
ZiprStringOption_t(const ZiprStringOption_t& copy) = delete;
public:
virtual ~ZiprStringOption_t() { }
virtual void setValue(const string& ) = 0;
};
class ZiprBooleanOption_t : virtual public ZiprTypedOption_t<bool>
{
protected:
ZiprBooleanOption_t() { }
ZiprBooleanOption_t(const ZiprBooleanOption_t& copy) = delete;
public:
virtual ~ZiprBooleanOption_t() { }
virtual void setValue(const bool& ) = 0;
};
class ZiprIntegerOption_t : virtual public ZiprTypedOption_t<size_t>
{
protected:
ZiprIntegerOption_t() { }
ZiprIntegerOption_t(const ZiprIntegerOption_t& copy) = delete;
public:
virtual ~ZiprIntegerOption_t() { }
virtual void setValue(const size_t& ) = 0;
};
class ZiprDoubleOption_t : public ZiprTypedOption_t<double>
{
protected:
ZiprDoubleOption_t() { }
ZiprDoubleOption_t(const ZiprDoubleOption_t& copy) = delete;
public:
virtual ~ZiprDoubleOption_t() { }
virtual void setValue(const double& ) = 0;
};
class ZiprOptionsNamespace_t
{
protected:
ZiprOptionsNamespace_t() { }
ZiprOptionsNamespace_t(const ZiprOptionsNamespace_t& copy) = delete;
public:
virtual ~ZiprOptionsNamespace_t() { }
virtual string getNamespaceString() const = 0 ;
virtual ZiprStringOption_t* getStringOption (const string& name, const string &description="", const string& default_value="") = 0;
virtual ZiprIntegerOption_t* getIntegerOption(const string& name, const string &description="", const size_t& default_value=0) = 0;
virtual ZiprBooleanOption_t* getBooleanOption(const string& name, const string &description="", const bool & default_value=false) = 0;
virtual ZiprDoubleOption_t* getDoubleOption (const string& name, const string &description="", const double& default_value=0.0) = 0;
};
class ZiprOptionsManager_t
{
protected:
ZiprOptionsManager_t() { }
ZiprOptionsManager_t(const ZiprOptionsManager_t& copy) = delete;
public:
virtual ~ZiprOptionsManager_t() { }
virtual bool areRequirementsMet() const = 0;
virtual ZiprOptionsNamespace_t* getNamespace(const string& name) = 0;
};
}
......@@ -85,9 +85,6 @@ namespace Zipr_SDK
virtual string toString()
{ return "NamelessPlugin"; }
virtual ZiprOptionsNamespace_t *registerOptions(ZiprOptionsNamespace_t *)
{ return new ZiprOptionsNamespace_t(""); }
virtual ZiprPreference_t retargetPin(const RangeAddress_t &, const Dollop_t *, RangeAddress_t &)
{ return None; }
......
......@@ -10,6 +10,7 @@ namespace Zipr_SDK
public:
Range_t(RangeAddress_t p_s, RangeAddress_t p_e) : m_start(p_s), m_end(p_e) { }
Range_t() : m_start(0), m_end(0) { }
virtual ~Range_t() {}
virtual RangeAddress_t getStart() const { return m_start; }
virtual RangeAddress_t getEnd() const { return m_end; }
......
......@@ -9,12 +9,16 @@ namespace Zipr_SDK
using PlacementQueue_t = set< pair<Dollop_t*,RangeAddress_t> > ;
class ZiprOptionsManager_t;
class Zipr_t
{
protected:
Zipr_t() { }
Zipr_t(const Zipr_t& copy) = delete;
public:
virtual ~Zipr_t() { }
/*
* These are public functions that the SDK user way want to use.
*/
......@@ -44,6 +48,7 @@ namespace Zipr_SDK
virtual InstructionLocationMap_t* getLocationMap() = 0;
virtual DollopManager_t* getDollopManager() = 0;
virtual PlacementQueue_t* getPlacementQueue() = 0;
virtual ZiprOptionsManager_t* getOptionsManager() = 0;
};
......
#include <string>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <set>
namespace Zipr_SDK
{
using namespace std;
using ZiprOptionType_t = enum ZiprOptionType
{
zotZiprUntypedOptionType,
zotZiprIntegerOptionType,
zotZiprDoubleOptionType,
zotZiprBooleanOptionType,
zotZiprStringOptionType,
};
class ZiprOption_t
{
public:
ZiprOption_t(ZiprOptionType_t type, const string& key, const string& value)
: m_key(key),
m_untyped_value(value),
m_observee(nullptr),
m_takes_value(true),
m_needs_value(true),
m_required(false),
m_set(false),
m_option_type(type) {};
virtual void setValue(const string &value) = 0;
virtual void setOption() = 0;
string getStringValue()
{
return m_untyped_value;
}
string getNamespace()
{
return m_namespace;
}
string getKey()
{
return m_key;
}
void setNeedsValue(bool needs_value)
{
m_needs_value = needs_value;
}
bool getNeedsValue()
{
return m_needs_value;
}
void setTakesValue(bool takes_value)
{
m_takes_value = takes_value;
}
bool getTakesValue()
{
return m_takes_value;
}
bool isRequired()
{
return m_required;
}
void setRequired(bool required)
{
m_required = required;
}
bool areRequirementMet()
{
return (!m_required) || (m_set);
}
virtual string getDescription()
{
return m_description;
}
void setDescription(string description)
{
m_description = description;
}
void addObserver(ZiprOption_t *observer)
{
assert(observer->m_option_type == m_option_type);
//observer->setValue(m_untyped_value);
observer->m_observee = this;
m_observers.insert(observer);
}
protected:
string m_namespace, m_key, m_untyped_value, m_description;
set<ZiprOption_t*> m_observers;
ZiprOption_t *m_observee;
bool m_takes_value, m_needs_value;
bool m_required;
bool m_set;
void SetObserverValues(string value)
{
set<ZiprOption_t*>::const_iterator it = m_observers.begin();
set<ZiprOption_t*>::const_iterator it_end = m_observers.end();
for (; it!=it_end; it++)
(*it)->setValue(value);
}
ZiprOptionType_t m_option_type;
};
template <typename T>
class ZiprTypedOption_t : public ZiprOption_t
{
public:
ZiprTypedOption_t(ZiprOptionType_t type,
const string& key,
const string& value)
: ZiprOption_t(type, key, value) {}
void setValue(const string &value)
{
m_set = true;
m_untyped_value = value;
m_value = convertToTyped(value);
}
virtual void setOption()
{
}
T getValue() const
{
if (m_observee!=nullptr)
{
ZiprTypedOption_t<T> *typed_observee =
static_cast<ZiprTypedOption_t<T>*>(m_observee);
return typed_observee->getValue();
}
return m_value;
}
operator T() const
{
return getValue();
};
virtual string getDescription()
{
return string(m_key + ": "
+ ((!m_needs_value)?"[":"")
+ "<" + getTypeName() + ">"
+ ((!m_needs_value)?"]":"")
+ ((m_description.length())?":\t":"") + m_description
);
}
protected:
T m_value;
virtual T convertToTyped(const string& ) = 0;
virtual string convertToUntyped(const T&) = 0;
virtual string getTypeName() = 0;
};
template <class T>
class ZiprCompositeOption_t : public ZiprTypedOption_t<T>, public T
{
public:
ZiprCompositeOption_t(ZiprOptionType_t type,
string key,
string value)
: ZiprTypedOption_t<T>(type, key, value) {}
using ZiprTypedOption_t<T>::convertToTyped;
void setValue(const string &value)
{
ZiprTypedOption_t<T>::setValue(value);
T::operator=(convertToTyped(value));
}
};
class ZiprStringOption_t : public ZiprCompositeOption_t<string>
{
public:
ZiprStringOption_t(string key, string value = "")
: ZiprCompositeOption_t(zotZiprStringOptionType, key, value)
{
setValue(value);
}
protected:
string convertToTyped(const string& value_to_convert)
{
return string(value_to_convert);
}
string convertToUntyped(const string& value_to_convert)
{
return string(value_to_convert);
}
string getTypeName()
{
return "string";
}
};
class ZiprBooleanOption_t : public ZiprTypedOption_t<bool>
{
public:
ZiprBooleanOption_t(const string& key, const string& value = "")
: ZiprTypedOption_t(zotZiprBooleanOptionType, key, value)
{
m_untyped_value = value;
m_needs_value = false;
}
ZiprBooleanOption_t(const string& key, bool value)
: ZiprTypedOption_t(zotZiprBooleanOptionType, key, "")
{
m_value = value;
m_needs_value = false;
m_untyped_value = convertToUntyped(value);
}
void setOption()
{
m_untyped_value = "true";
m_value = true;
m_set = true;
}
private:
bool convertToTyped(const string& value_to_convert)
{
if (value_to_convert == "true")
return true;
else
return false;
}
string convertToUntyped(const bool& value_to_convert)
{
return (value_to_convert ? string("true") : string("false"));
}
string getTypeName()
{
return "bool";
}
};
class ZiprIntegerOption_t : public ZiprTypedOption_t<int>
{
public:
ZiprIntegerOption_t(const string& key, const string& value = "")
: ZiprTypedOption_t(zotZiprIntegerOptionType, key, value)
{
m_value = convertToTyped(value);
}
ZiprIntegerOption_t(const string& key, int value)
: ZiprTypedOption_t(zotZiprIntegerOptionType, key, "")
{
m_value = value;
m_untyped_value = convertToUntyped(value);
}
void setValue(int value)
{
m_value = value;
m_untyped_value = convertToUntyped(value);
}
private:
int convertToTyped(const string& value_to_convert)
{
int converted = 0;
char *endptr = nullptr;
converted = strtol(value_to_convert.c_str(),
&endptr, 10);
if (*endptr != '\0')
{
m_set = false;
m_untyped_value = "";
return 0;
}
return converted;
}
string convertToUntyped(const int& value_to_convert)
{
stringstream ss;
ss << value_to_convert;
return ss.str();
}
string getTypeName()
{
return "integer";
}
};
class ZiprDoubleOption_t : public ZiprTypedOption_t<double>
{
public:
ZiprDoubleOption_t(const string& key, const string& value = "")
: ZiprTypedOption_t(zotZiprDoubleOptionType, key, value)
{
m_value = convertToTyped(value);
}
ZiprDoubleOption_t(const string& key, double value)
: ZiprTypedOption_t(zotZiprDoubleOptionType, key, "")
{
m_value = value;
m_untyped_value = convertToUntyped(value);
}
void setValue(double value)
{
m_value = value;
m_untyped_value = convertToUntyped(value);
}
private:
double convertToTyped(const string& value_to_convert)
{
double converted = 0.;
char *endptr = nullptr;
converted = strtof(value_to_convert.c_str(),
&endptr);
if (*endptr != '\0')
{
m_set = false;
m_untyped_value = "";
return 0;
}
return converted;
}
string convertToUntyped(const double& value_to_convert)
{
stringstream ss;
ss << value_to_convert;
return ss.str();
}
string getTypeName() {
return "double";
}
};
class ZiprOptionsNamespace_t : public set<ZiprOption_t*>
{
public:
ZiprOptionsNamespace_t(const string& ns) : m_namespace(ns) {}
string getNamespace()
{
return m_namespace;
};
void printNamespace();
ZiprOption_t *optionByKey(const string& key);
void addOption(ZiprOption_t *option);
void printUsage(int tabs, ostream &out);
bool areRequirementsMet();
void mergeNamespace(ZiprOptionsNamespace_t*);
private:
string m_namespace;
};
class ZiprOptions_t
{
public:
ZiprOptions_t(int argc, char **argv);
void addNamespace(ZiprOptionsNamespace_t *);
void printNamespaces();
bool parse(ostream *error=&cout, ostream *warn=&cerr);
ZiprOptionsNamespace_t *getNamespace(const string& );
void printUsage(ostream &out);
bool areRequirementsMet();
private:
vector<string> m_arguments;
set<ZiprOptionsNamespace_t*> m_namespaces;
};
}
......@@ -17,8 +17,8 @@
#include <private-include/dollop.h>
#include <private-include/memory_space.h>
#include <private-include/dollop_man.h>
#include <private-include/zipr_options.h>
#include <private-include/zipr_plugin.h>
#include <private-include/options.h>
#include <private-include/plugin.h>
#include <private-include/zipr.h>
//
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment