From de097c4bff4609aa124ceb4deca60435aa4eb495 Mon Sep 17 00:00:00 2001 From: Jason Hiser <jdhiser@gmail.com> Date: Fri, 8 Feb 2019 15:29:01 -0500 Subject: [PATCH] refactoring. --- README | 4 +- include/dollop.h | 154 -------- include/dollop_man.h | 128 ------- include/memory_space.h | 78 ----- include/private-include/dollop.h | 127 +++++++ include/private-include/dollop_man.h | 102 ++++++ include/private-include/memory_space.h | 46 +++ include/private-include/range.h | 46 +++ include/private-include/zipr.h | 48 +++ include/private-include/zipr_constants.h | 9 + include/private-include/zipr_options.h | 368 ++++++++++++++++++++ include/{ => private-include}/zipr_plugin.h | 39 ++- include/range.h | 81 ----- include/{zipr_sdk.h => zipr-sdk} | 18 +- include/zipr.h | 84 ----- include/zipr_constants.h | 44 --- include/zipr_options.h | 365 ------------------- 17 files changed, 778 insertions(+), 963 deletions(-) delete mode 100644 include/dollop.h delete mode 100644 include/dollop_man.h delete mode 100644 include/memory_space.h create mode 100644 include/private-include/dollop.h create mode 100644 include/private-include/dollop_man.h create mode 100644 include/private-include/memory_space.h create mode 100644 include/private-include/range.h create mode 100644 include/private-include/zipr.h create mode 100644 include/private-include/zipr_constants.h create mode 100644 include/private-include/zipr_options.h rename include/{ => private-include}/zipr_plugin.h (70%) delete mode 100644 include/range.h rename include/{zipr_sdk.h => zipr-sdk} (81%) delete mode 100644 include/zipr.h delete mode 100644 include/zipr_constants.h delete mode 100644 include/zipr_options.h diff --git a/README b/README index 5e33b5a..d4e8bf2 100644 --- a/README +++ b/README @@ -1 +1,3 @@ -Anh will provide documentation. +This is the SDK for building a Zipr plugin. + +Full documentation is provided in the include files. diff --git a/include/dollop.h b/include/dollop.h deleted file mode 100644 index 0b18450..0000000 --- a/include/dollop.h +++ /dev/null @@ -1,154 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. - * - * This software is furnished under a license and/or other restrictive - * terms and may be used and copied only in accordance with such terms - * and the inclusion of the above copyright notice. This software or - * any other copies thereof may not be provided or otherwise made - * available to any other person without the express written consent - * of an authorized representative of Zephyr Software LCC. Title to, - * ownership of, and all rights in the software is retained by - * Zephyr Software LCC. - * - * Zephyr Software LLC. Proprietary Information - * - * Unless otherwise specified, the information contained in this - * directory, following this legend, and/or referenced herein is - * Zephyr Software LLC. (Zephyr) Proprietary Information. - * - * CONTACT - * - * For technical assistance, contact Zephyr Software LCC. at: - * - * - * Zephyr Software, LLC - * 2040 Tremont Rd - * Charlottesville, VA 22911 - * - * E-mail: jwd@zephyr-software.com - **************************************************************************/ - -#ifndef dollop_h -#define dollop_h - -#include <assert.h> -#include <list> - -namespace Zipr_SDK -{ -class DollopManager_t; - -class Placeable_t { - public: - Placeable_t() : m_is_placed(false), m_placed_address(0) {} - void Place(Zipr_SDK::RangeAddress_t place) { - assert(!m_is_placed); - m_is_placed = true; - m_placed_address = place; - }; - Zipr_SDK::RangeAddress_t Place() { return m_placed_address; } - bool IsPlaced() const { return m_is_placed; } - friend std::ostream &operator<<(std::ostream &, const Placeable_t &); - protected: - bool m_is_placed; - Zipr_SDK::RangeAddress_t m_placed_address; -}; - -class Dollop_t; - -class DollopPatch_t : public Placeable_t { - public: - DollopPatch_t(Dollop_t *target) : m_target(target) {}; - DollopPatch_t() : m_target(NULL) { }; - - Dollop_t *Target() const { return m_target; } - void Target(Dollop_t *target) { m_target = target; } - - friend std::ostream &operator<<(std::ostream &, const DollopPatch_t &); - private: - Dollop_t *m_target; -}; - -class DollopEntry_t : public Placeable_t { - public: - DollopEntry_t(IRDB_SDK::Instruction_t *insn, Dollop_t *member_of); - IRDB_SDK::Instruction_t *Instruction() const { return m_instruction; } - void TargetDollop(Dollop_t *target) { m_target_dollop = target; } - Dollop_t *TargetDollop() const { return m_target_dollop; } - Dollop_t *MemberOfDollop() const { return m_member_of_dollop; } - void MemberOfDollop(Dollop_t *member_of) { m_member_of_dollop = member_of; } - - bool operator==(const DollopEntry_t &); - bool operator!=(const DollopEntry_t &); - friend std::ostream &operator<<(std::ostream &, const DollopEntry_t &); - private: - IRDB_SDK::Instruction_t *m_instruction; - Dollop_t *m_target_dollop, *m_member_of_dollop; -}; - -class Dollop_t : public Placeable_t, public std::list<DollopEntry_t*> { - public: - static Dollop_t *CreateNewDollop(IRDB_SDK::Instruction_t *start, - DollopManager_t *mgr = NULL); - - Dollop_t(IRDB_SDK::Instruction_t *start, DollopManager_t *); - Dollop_t() : - m_size(0), - m_fallthrough_dollop(NULL), - m_fallback_dollop(NULL), - m_fallthrough_patched(false), - m_coalesced(false), - m_was_truncated(false), - m_dollop_mgr(NULL) {} - - void setDollopManager(DollopManager_t *mgr) { m_dollop_mgr = mgr; } - - size_t getSize() const { - return m_size; - } - void SetSize(size_t size) { - m_size = size; - } - - size_t getDollopEntryCount() const { - return size(); - } - - Dollop_t *Split(IRDB_SDK::Instruction_t *split_point); - void RemoveDollopEntries(std::list<DollopEntry_t*>::iterator, - std::list<DollopEntry_t*>::iterator); - - friend std::ostream &operator<<(std::ostream &, const Dollop_t &); - - void FallbackDollop(Dollop_t *fallback) { - m_fallback_dollop = fallback; - } - Dollop_t *FallbackDollop(void) const { return m_fallback_dollop; } - - void FallthroughDollop(Dollop_t *fallthrough) { - m_fallthrough_dollop = fallthrough; - } - Dollop_t *FallthroughDollop(void) const { return m_fallthrough_dollop; } - bool FallthroughPatched(void) const { return m_fallthrough_patched; } - void FallthroughPatched(bool patched); - - DollopEntry_t *FallthroughDollopEntry(DollopEntry_t *) const; - - void WasTruncated(bool truncated) { m_was_truncated = truncated; } - bool WasTruncated(void) const { return m_was_truncated; } - - void WasCoalesced(bool coalesced); - bool WasCoalesced(void) const { return m_coalesced; } - - void ReCalculateSize(); - private: - size_t CalculateSize(); - size_t m_size; - Dollop_t *m_fallthrough_dollop, *m_fallback_dollop; - bool m_fallthrough_patched; - bool m_coalesced; - bool m_was_truncated; - DollopManager_t *m_dollop_mgr; -}; -} -#endif diff --git a/include/dollop_man.h b/include/dollop_man.h deleted file mode 100644 index d9acf54..0000000 --- a/include/dollop_man.h +++ /dev/null @@ -1,128 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. - * - * This software is furnished under a license and/or other restrictive - * terms and may be used and copied only in accordance with such terms - * and the inclusion of the above copyright notice. This software or - * any other copies thereof may not be provided or otherwise made - * available to any other person without the express written consent - * of an authorized representative of Zephyr Software LCC. Title to, - * ownership of, and all rights in the software is retained by - * Zephyr Software LCC. - * - * Zephyr Software LLC. Proprietary Information - * - * Unless otherwise specified, the information contained in this - * directory, following this legend, and/or referenced herein is - * Zephyr Software LLC. (Zephyr) Proprietary Information. - * - * CONTACT - * - * For technical assistance, contact Zephyr Software LCC. at: - * - * - * Zephyr Software, LLC - * 2040 Tremont Rd - * Charlottesville, VA 22911 - * - * E-mail: jwd@zephyr-software.com - **************************************************************************/ - -#ifndef dollop_man_h -#define dollop_man_h - -#include <dollop.h> -#include <set> -#include <map> -#include <list> - - -namespace Zipr_SDK { - - -typedef std::set<Dollop_t*> DollopList_t; -typedef std::map<IRDB_SDK::Instruction_t*,Dollop_t*> InsnToDollopMap_t; -typedef std::list<DollopPatch_t*> DollopPatchList_t; -typedef std::map<Dollop_t*, DollopPatchList_t > DollopToDollopPatchListMap_t; - - -class DollopManager_t { - public: - DollopManager_t() {}; - - virtual size_t DetermineDollopEntrySize(DollopEntry_t *entry)=0; - - /* - * Add a dollop and all its fallthrough dollops - * to the dollop manager. - */ - virtual void AddDollops(Zipr_SDK::Dollop_t *dollop_head)=0; - /* - * Create new dollops from a particular start instruction. - */ - virtual Zipr_SDK::Dollop_t *AddNewDollops(IRDB_SDK::Instruction_t *start)=0; - - /* - * Return the number of dollops under management. - */ - virtual size_t Size()=0; - - /* - * Return the dollop containing an instruction. - */ - virtual Zipr_SDK::Dollop_t *getContainingDollop( - IRDB_SDK::Instruction_t *insn)=0; - - /* - * Add a dollop patch to the dollop manager. - */ - virtual void AddDollopPatch(Zipr_SDK::DollopPatch_t *new_patch) = 0; - /* - * Return all the patches that point to a particular - * dollop. - */ - virtual std::list<Zipr_SDK::DollopPatch_t*> PatchesToDollop( - Zipr_SDK::Dollop_t *target)=0; - - /* - * Update the dollops under management based on the - * targetted dollops in a particular dollop. - */ - virtual bool UpdateTargets(Zipr_SDK::Dollop_t *) = 0; - /* - * Update all the dollops under management based on - * all the dollops of which the manager knows. - */ - virtual void UpdateAllTargets() = 0; - - /* - * Return beginning and ending iterators for all the - * dollops under management. - */ - virtual DollopList_t::iterator dollops_begin() = 0; - virtual DollopList_t::iterator dollops_end() = 0; - - /* - * Print (for debugging) all the dollop patches - * under management. - */ - virtual void PrintDollopPatches(const std::ostream &) = 0; - /* - * print (for debugging) information about the dollop - * manager. - */ - friend std::ostream &operator<<(std::ostream &out, const DollopManager_t &dollop_man); - /* - * Print (useful output for end user) information about - * statistics of the dollop manager and the job it is - * doing. - */ - virtual void PrintStats(std::ostream &out) = 0; - /* - * Generate a placement map file for all the dollops under management. - */ - virtual void PrintPlacementMap(const Zipr_SDK::MemorySpace_t &memory_space, - const std::string &map_filename) = 0; -}; -} -#endif diff --git a/include/memory_space.h b/include/memory_space.h deleted file mode 100644 index c317624..0000000 --- a/include/memory_space.h +++ /dev/null @@ -1,78 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. - * - * This software is furnished under a license and/or other restrictive - * terms and may be used and copied only in accordance with such terms - * and the inclusion of the above copyright notice. This software or - * any other copies thereof may not be provided or otherwise made - * available to any other person without the express written consent - * of an authorized representative of Zephyr Software LCC. Title to, - * ownership of, and all rights in the software is retained by - * Zephyr Software LCC. - * - * Zephyr Software LLC. Proprietary Information - * - * Unless otherwise specified, the information contained in this - * directory, following this legend, and/or referenced herein is - * Zephyr Software LLC. (Zephyr) Proprietary Information. - * - * CONTACT - * - * For technical assistance, contact Zephyr Software LCC. at: - * - * - * Zephyr Software, LLC - * 2040 Tremont Rd - * Charlottesville, VA 22911 - * - * E-mail: jwd@zephyr-software.com - **************************************************************************/ - -#ifndef zipr_sdk_memory_space_h -#define zipr_sdk_memory_space_h - - -namespace Zipr_SDK -{ - using namespace std; - - using RangeSet_t = set<Range_t, Range_tCompare> ; - - // a memory space _is_ a map of range addres to char, with additional functionality. - class MemorySpace_t : public map<RangeAddress_t,char> - { - public: - - // range operatations - virtual void SplitFreeRange(RangeAddress_t addr)=0; - virtual void SplitFreeRange(Range_t range)=0; - virtual void MergeFreeRange(RangeAddress_t addr)=0; - virtual void MergeFreeRange(Range_t range)=0; - virtual RangeSet_t::iterator FindFreeRange(RangeAddress_t addr)=0; - virtual Range_t getFreeRange(int size)=0; - virtual Range_t getInfiniteFreeRange()=0; - virtual list<Range_t> getFreeRanges(size_t size = 0) = 0; - virtual pair<RangeSet_t::const_iterator,RangeSet_t::const_iterator> - getNearbyFreeRanges(const RangeAddress_t hint, size_t count = 0) = 0; - virtual void AddFreeRange(Range_t newRange)=0; - virtual void RemoveFreeRange(Range_t newRange)=0; - virtual Range_t GetLargeRange(void)=0; - - // queries about free areas. - virtual bool AreBytesFree(RangeAddress_t addr, int num_bytes)=0; - virtual bool IsByteFree(RangeAddress_t addr)=0; - virtual bool IsValidRange(RangeSet_t::iterator it)=0; - virtual int GetRangeCount()=0; - virtual void PrintMemorySpace(ostream &out)=0; - - virtual void PlopBytes(RangeAddress_t addr, const char the_byte[], int num)=0; - virtual void PlopByte(RangeAddress_t addr, char the_byte)=0; - virtual void PlopJump(RangeAddress_t addr)=0; - virtual RangeAddress_t GetMinPlopped() const =0; - virtual RangeAddress_t GetMaxPlopped() const =0; - - }; - -} -#endif - diff --git a/include/private-include/dollop.h b/include/private-include/dollop.h new file mode 100644 index 0000000..0dc77b5 --- /dev/null +++ b/include/private-include/dollop.h @@ -0,0 +1,127 @@ +#include <assert.h> +#include <list> + +namespace Zipr_SDK +{ + using namespace std; + using namespace IRDB_SDK; + + class Dollop_t; + class DollopManager_t; + + class Placeable_t + { + public: + Placeable_t() : m_is_placed(false), m_placed_address(0) {} + void Place(RangeAddress_t place) { + assert(!m_is_placed); + m_is_placed = true; + m_placed_address = place; + }; + RangeAddress_t getPlace() { return m_placed_address; } + bool isPlaced() const { return m_is_placed; } + protected: + bool m_is_placed; + RangeAddress_t m_placed_address; + + friend ostream &operator<<(ostream &, const Placeable_t &); + }; + + + class DollopPatch_t : public Placeable_t + { + public: + DollopPatch_t(Dollop_t *target) : m_target(target) {}; + DollopPatch_t() : m_target(NULL) { }; + + Dollop_t *getTarget() const { return m_target; } + void setTarget(Dollop_t *target) { m_target = target; } + + friend ostream &operator<<(ostream &, const DollopPatch_t &); + private: + Dollop_t *m_target; + }; + + class DollopEntry_t : public Placeable_t + { + public: + DollopEntry_t(IRDB_SDK::Instruction_t *insn, Dollop_t *member_of); + IRDB_SDK::Instruction_t *getInstruction() const { return m_instruction; } + void setTargetDollop(Dollop_t *target) { m_target_dollop = target; } + Dollop_t *getTargetDollop() const { return m_target_dollop; } + Dollop_t *getMemberOfDollop() const { return m_member_of_dollop; } + void MemberOfDollop(Dollop_t *member_of) { m_member_of_dollop = member_of; } + + bool operator==(const DollopEntry_t &); + bool operator!=(const DollopEntry_t &); + private: + IRDB_SDK::Instruction_t *m_instruction; + Dollop_t *m_target_dollop, *m_member_of_dollop; + + friend ostream &operator<<(ostream &, const DollopEntry_t &); + }; + + class Dollop_t : public Placeable_t, public list<DollopEntry_t*> + { + public: + static Dollop_t *createNewDollop(IRDB_SDK::Instruction_t *start, + DollopManager_t *mgr = NULL); + + Dollop_t(IRDB_SDK::Instruction_t *start, DollopManager_t *); + Dollop_t() : + m_size(0), + m_fallthrough_dollop(NULL), + m_fallback_dollop(NULL), + m_fallthrough_patched(false), + m_coalesced(false), + m_was_truncated(false), + m_dollop_mgr(NULL) {} + + void setDollopManager(DollopManager_t *mgr) { m_dollop_mgr = mgr; } + + size_t getSize() const { + return m_size; + } + void setSize(size_t size) { + m_size = size; + } + + size_t getDollopEntryCount() const { + return size(); + } + + Dollop_t *split(IRDB_SDK::Instruction_t *split_point); + void removeDollopEntries(list<DollopEntry_t*>::iterator, + list<DollopEntry_t*>::iterator); + + + void setFallbackDollop(Dollop_t *fallback) { m_fallback_dollop = fallback; } + Dollop_t *getFallbackDollop(void) const { return m_fallback_dollop; } + + void setFallthroughDollop(Dollop_t *fallthrough) { m_fallthrough_dollop = fallthrough; } + Dollop_t *getFallthroughDollop(void) const { return m_fallthrough_dollop; } + + bool isFallthroughPatched(void) const { return m_fallthrough_patched; } + void setFallthroughPatched(bool patched); + + DollopEntry_t *setFallthroughDollopEntry(DollopEntry_t *) const; + + void wasTruncated(bool truncated) { m_was_truncated = truncated; } + bool wasTruncated(void) const { return m_was_truncated; } + + void setCoalesced(bool coalesced); + bool wasCoalesced(void) const { return m_coalesced; } + + void reCalculateSize(); + private: + size_t CalculateSize(); + size_t m_size; + Dollop_t *m_fallthrough_dollop, *m_fallback_dollop; + bool m_fallthrough_patched; + bool m_coalesced; + bool m_was_truncated; + DollopManager_t *m_dollop_mgr; + + friend ostream &operator<<(ostream &, const Dollop_t &); + }; +} diff --git a/include/private-include/dollop_man.h b/include/private-include/dollop_man.h new file mode 100644 index 0000000..a15c6d7 --- /dev/null +++ b/include/private-include/dollop_man.h @@ -0,0 +1,102 @@ +#include <set> +#include <map> +#include <list> + + +namespace Zipr_SDK +{ + using namespace std; + using namespace IRDB_SDK; + + + using DollopList_t = set<Dollop_t*>; + using InsnToDollopMap_t = map<Instruction_t*,Dollop_t*>; + using DollopPatchList_t = list<DollopPatch_t*>; + using DollopToDollopPatchListMap_t = map<Dollop_t*, DollopPatchList_t >; + + + class DollopManager_t + { + public: + DollopManager_t() {}; + + + /* + * Add a dollop and all its fallthrough dollops + * to the dollop manager. + */ + virtual void addDollops(Dollop_t *dollop_head)=0; + /* + * Create new dollops from a particular start instruction. + */ + virtual Dollop_t *addNewDollops(Instruction_t *start)=0; + + /* + * Return the number of dollops under management. + */ + virtual size_t getSize()=0; + + /* + * Return the dollop containing an instruction. + */ + virtual Dollop_t *getContainingDollop( Instruction_t *insn)=0; + + /* + * Add a dollop patch to the dollop manager. + */ + virtual void addDollopPatch(DollopPatch_t *new_patch) = 0; + /* + * Return all the patches that point to a particular + * dollop. + */ + virtual DollopPatchList_t getPatchesToDollop( Dollop_t *target)=0; + + /* + * Update the dollops under management based on the + * targetted dollops in a particular dollop. + */ + virtual bool updateTargets(Dollop_t *) = 0; + /* + * Update all the dollops under management based on + * all the dollops of which the manager knows. + */ + virtual void updateAllTargets() = 0; + + /* + * Return beginning and ending iterators for all the + * dollops under management. + */ + virtual DollopList_t::iterator begin() = 0; + virtual DollopList_t::iterator end() = 0; + virtual DollopList_t::iterator dollops_begin() = 0; + virtual DollopList_t::iterator dollops_end() = 0; + + /* + * Print (for debugging) all the dollop patches + * under management. + */ + virtual void printDollopPatches(const ostream &) = 0; + + /* + * Print (useful output for end user) information about + * statistics of the dollop manager and the job it is + * doing. + */ + virtual void printStats(ostream &out) = 0; + + /* + * Generate a placement map file for all the dollops under management. + */ + virtual void printPlacementMap(const MemorySpace_t &memory_space, + const string &map_filename) = 0; + + /* helper */ + virtual size_t determineDollopEntrySize(DollopEntry_t *entry)=0; + + /* + * print (for debugging) information about the dollop + * manager. + */ + friend ostream &operator<<(ostream &out, const DollopManager_t &dollop_man); + }; +} diff --git a/include/private-include/memory_space.h b/include/private-include/memory_space.h new file mode 100644 index 0000000..e7cb315 --- /dev/null +++ b/include/private-include/memory_space.h @@ -0,0 +1,46 @@ + +namespace Zipr_SDK +{ + using namespace std; + + using RangeSet_t = set<Range_t, Range_tCompare> ; + 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> + { + public: + + // range operatations + virtual void splitFreeRange(RangeAddress_t addr)=0; + virtual void splitFreeRange(Range_t range)=0; + virtual void mergeFreeRange(RangeAddress_t addr)=0; + virtual void mergeFreeRange(Range_t range)=0; + virtual RangeSet_t::iterator + findFreeRange(RangeAddress_t addr)=0; + virtual Range_t getFreeRange(int size)=0; + virtual Range_t getInfiniteFreeRange()=0; + virtual list<Range_t> getFreeRanges(size_t size = 0) = 0; + virtual RangeSetBounds_t + getNearbyFreeRanges(const RangeAddress_t hint, size_t count = 0) = 0; + virtual void addFreeRange(Range_t newRange)=0; + virtual void removeFreeRange(Range_t newRange)=0; + virtual Range_t getLargeRange(void)=0; + + // queries about free areas. + virtual bool areBytesFree(RangeAddress_t addr, int num_bytes)=0; + virtual bool isByteFree(RangeAddress_t addr)=0; + virtual bool isValidRange(RangeSet_t::iterator it)=0; + virtual int getRangeCount()=0; + virtual void printMemorySpace(ostream &out)=0; + + virtual void plopBytes(RangeAddress_t addr, const char the_byte[], int num)=0; + virtual void plopByte(RangeAddress_t addr, char the_byte)=0; + virtual void plopJump(RangeAddress_t addr)=0; + virtual RangeAddress_t getMinPlopped() const =0; + virtual RangeAddress_t getMaxPlopped() const =0; + + }; + +} + diff --git a/include/private-include/range.h b/include/private-include/range.h new file mode 100644 index 0000000..95d8712 --- /dev/null +++ b/include/private-include/range.h @@ -0,0 +1,46 @@ + +namespace Zipr_SDK +{ + + using namespace std; + using RangeAddress_t = uintptr_t ; + + class Range_t + { + 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 RangeAddress_t getStart() const { return m_start; } + virtual RangeAddress_t getEnd() const { return m_end; } + virtual void setStart(RangeAddress_t s) { m_start=s; } + virtual void setEnd(RangeAddress_t e) { m_end=e; } + + virtual bool is2ByteRange() + { + return (m_end - m_start) == 2; + }; + virtual bool is5ByteRange() + { + return (m_end - m_start) == 5; + }; + + virtual bool isInfiniteRange() + { + return m_end==(RangeAddress_t)-1; + }; + + protected: + + RangeAddress_t m_start, m_end; + }; + + struct Range_tCompare + { + bool operator() (const Range_t first, const Range_t second) const + { + return first.getEnd() < second.getStart(); + } + }; + +} diff --git a/include/private-include/zipr.h b/include/private-include/zipr.h new file mode 100644 index 0000000..fe4aa63 --- /dev/null +++ b/include/private-include/zipr.h @@ -0,0 +1,48 @@ + +#include <set> +#include <utility> + + +namespace Zipr_SDK +{ + using namespace std; + + using PlacementQueue_t = set< pair<Dollop_t*,RangeAddress_t> > ; + + class Zipr_t + { + public: + /* + * These are public functions that the SDK user way want to use. + */ + virtual size_t determineDollopEntrySize( + DollopEntry_t*, + bool account_for_fallthrough) = 0; + + virtual RangeAddress_t plopDollopEntry( + DollopEntry_t *, + RangeAddress_t = 0, + RangeAddress_t = 0) = 0; + + virtual RangeAddress_t plopDollopEntryWithTarget( + DollopEntry_t *, + RangeAddress_t = 0, + RangeAddress_t = 0) = 0; + + virtual RangeAddress_t plopDollopEntryWithCallback( + DollopEntry_t *, + RangeAddress_t = 0) = 0; + + virtual MemorySpace_t *getMemorySpace()=0; + virtual IRDB_SDK::FileIR_t *getFileIR()=0; + virtual InstructionLocationMap_t *getLocationMap()=0; + virtual DollopManager_t *getDollopManager()=0; + virtual PlacementQueue_t* getPlacementQueue()=0; + virtual void applyPatch( + RangeAddress_t from_addr, + RangeAddress_t to_addr)=0; + + }; + +}; + diff --git a/include/private-include/zipr_constants.h b/include/private-include/zipr_constants.h new file mode 100644 index 0000000..4e09199 --- /dev/null +++ b/include/private-include/zipr_constants.h @@ -0,0 +1,9 @@ + +namespace Zipr_SDK +{ + enum + { + DEFAULT_TRAMPOLINE_SIZE=5 + }; +}; + diff --git a/include/private-include/zipr_options.h b/include/private-include/zipr_options.h new file mode 100644 index 0000000..8bb2833 --- /dev/null +++ b/include/private-include/zipr_options.h @@ -0,0 +1,368 @@ + +#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; + }; + +} diff --git a/include/zipr_plugin.h b/include/private-include/zipr_plugin.h similarity index 70% rename from include/zipr_plugin.h rename to include/private-include/zipr_plugin.h index 785184d..0c87c76 100644 --- a/include/zipr_plugin.h +++ b/include/private-include/zipr_plugin.h @@ -2,8 +2,9 @@ namespace Zipr_SDK { using namespace std; + using namespace IRDB_SDK; - using InstructionLocationMap_t = map<IRDB_SDK::Instruction_t*,RangeAddress_t>; + using InstructionLocationMap_t = map<Instruction_t*,RangeAddress_t>; enum ZiprPreference { @@ -14,36 +15,36 @@ namespace Zipr_SDK class ZiprPluginInterface_t { public: - virtual void PinningBegin() + virtual void doPinningBegin() { } - virtual void PinningEnd() + virtual void doPinningEnd() { } - virtual void DollopBegin() + virtual void doDollopBegin() { } - virtual void DollopEnd() + virtual void doDollopEnd() { } - virtual void CallbackLinkingBegin() + virtual void doCallbackLinkingBegin() { } - virtual void CallbackLinkingEnd() + virtual void doCallbackLinkingEnd() { } - virtual ZiprPreference AddressDollop(const Dollop_t *dollop, const RangeAddress_t &source, Range_t &place, bool &coalesce, bool &fallthrough_ok) + virtual ZiprPreference addressDollop(const Dollop_t *dollop, const RangeAddress_t &source, Range_t &place, bool &coalesce, bool &fallthrough_ok) { return None; } - virtual bool WillPluginPlop(IRDB_SDK::Instruction_t*) + virtual bool willPluginPlop(Instruction_t*) { return false; } - virtual size_t InsnSize(IRDB_SDK::Instruction_t*, bool) + virtual size_t getInsnSize(Instruction_t*, bool) { return 0; } @@ -56,11 +57,11 @@ namespace Zipr_SDK * should return the size of that padding in * these functions. */ - virtual size_t DollopEntryOpeningSize(DollopEntry_t*) + virtual size_t getDollopEntryOpeningSize(DollopEntry_t*) { return 0; } - virtual size_t DollopEntryClosingSize(DollopEntry_t*) + virtual size_t getDollopEntryClosingSize(DollopEntry_t*) { return 0; } @@ -97,7 +98,7 @@ namespace Zipr_SDK * address of the memory that "concludes" the * dollop entry. */ - virtual RangeAddress_t PlopDollopEntry(DollopEntry_t *entry, + virtual RangeAddress_t plopDollopEntry(DollopEntry_t *entry, RangeAddress_t &instruction_address, RangeAddress_t &target_address, size_t instruction_size, @@ -105,28 +106,28 @@ namespace Zipr_SDK { return 0; } - virtual string ToString() + virtual string toString() { return "NamelessPlugin"; } - virtual ZiprOptionsNamespace_t *RegisterOptions(ZiprOptionsNamespace_t *) + virtual ZiprOptionsNamespace_t *registerOptions(ZiprOptionsNamespace_t *) { return new ZiprOptionsNamespace_t(""); } - virtual ZiprPreference RetargetPin(const RangeAddress_t &, const Dollop_t *, RangeAddress_t &) + virtual ZiprPreference retargetPin(const RangeAddress_t &, const Dollop_t *, RangeAddress_t &) { return None; } - virtual ZiprPreference RetargetCallback(const RangeAddress_t &, const DollopEntry_t *, RangeAddress_t &) + virtual ZiprPreference retargetCallback(const RangeAddress_t &, const DollopEntry_t *, RangeAddress_t &) { return None; } - virtual RangeAddress_t PlaceScoopsBegin(const RangeAddress_t max_addr) + virtual RangeAddress_t doPlaceScoopsBegin(const RangeAddress_t max_addr) { return max_addr; } - virtual RangeAddress_t PlaceScoopsEnd(const RangeAddress_t max_addr) + virtual RangeAddress_t doPlaceScoopsEnd(const RangeAddress_t max_addr) { return max_addr; } diff --git a/include/range.h b/include/range.h deleted file mode 100644 index 6ce0d31..0000000 --- a/include/range.h +++ /dev/null @@ -1,81 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. - * - * This software is furnished under a license and/or other restrictive - * terms and may be used and copied only in accordance with such terms - * and the inclusion of the above copyright notice. This software or - * any other copies thereof may not be provided or otherwise made - * available to any other person without the express written consent - * of an authorized representative of Zephyr Software LCC. Title to, - * ownership of, and all rights in the software is retained by - * Zephyr Software LCC. - * - * Zephyr Software LLC. Proprietary Information - * - * Unless otherwise specified, the information contained in this - * directory, following this legend, and/or referenced herein is - * Zephyr Software LLC. (Zephyr) Proprietary Information. - * - * CONTACT - * - * For technical assistance, contact Zephyr Software LCC. at: - * - * - * Zephyr Software, LLC - * 2040 Tremont Rd - * Charlottesville, VA 22911 - * - * E-mail: jwd@zephyr-software.com - **************************************************************************/ - -#ifndef zipr_sdk_range_h -#define zipr_sdk_range_h - - -namespace Zipr_SDK -{ - - using namespace std; - using RangeAddress_t = uintptr_t ; - - - class Range_t - { - 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 RangeAddress_t getStart() const { return m_start; } - virtual RangeAddress_t getEnd() const { return m_end; } - virtual void SetStart(RangeAddress_t s) { m_start=s; } - virtual void SetEnd(RangeAddress_t e) { m_end=e; } - - virtual bool Is2ByteRange() - { - return (m_end - m_start) == 2; - }; - virtual bool Is5ByteRange() - { - return (m_end - m_start) == 5; - }; - - virtual bool IsInfiniteRange() - { - return m_end==(RangeAddress_t)-1; - }; - - protected: - - RangeAddress_t m_start, m_end; - }; - - struct Range_tCompare - { - bool operator() (const Range_t first, const Range_t second) const - { - return first.getEnd() < second.getStart(); - } - }; - -} -#endif diff --git a/include/zipr_sdk.h b/include/zipr-sdk similarity index 81% rename from include/zipr_sdk.h rename to include/zipr-sdk index c894e74..dacfa41 100644 --- a/include/zipr_sdk.h +++ b/include/zipr-sdk @@ -43,17 +43,17 @@ #include <irdb-core> // elfio includes -#include "elfio/elfio.hpp" -#include "elfio/elfio_dump.hpp" +//#include "elfio/elfio.hpp" +//#include "elfio/elfio_dump.hpp" // SDK internals -#include <range.h> -#include <dollop.h> -#include <memory_space.h> -#include <dollop_man.h> -#include <zipr_options.h> -#include <zipr_plugin.h> -#include <zipr.h> +#include <private-include/range.h> +#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/zipr.h> extern "C" Zipr_SDK::ZiprPluginInterface_t* GetPluginInterface(Zipr_SDK::Zipr_t* zipr_main_object); diff --git a/include/zipr.h b/include/zipr.h deleted file mode 100644 index ee8493b..0000000 --- a/include/zipr.h +++ /dev/null @@ -1,84 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. - * - * This software is furnished under a license and/or other restrictive - * terms and may be used and copied only in accordance with such terms - * and the inclusion of the above copyright notice. This software or - * any other copies thereof may not be provided or otherwise made - * available to any other person without the express written consent - * of an authorized representative of Zephyr Software LCC. Title to, - * ownership of, and all rights in the software is retained by - * Zephyr Software LCC. - * - * Zephyr Software LLC. Proprietary Information - * - * Unless otherwise specified, the information contained in this - * directory, following this legend, and/or referenced herein is - * Zephyr Software LLC. (Zephyr) Proprietary Information. - * - * CONTACT - * - * For technical assistance, contact Zephyr Software LCC. at: - * - * - * Zephyr Software, LLC - * 2040 Tremont Rd - * Charlottesville, VA 22911 - * - * E-mail: jwd@zephyr-software.com - **************************************************************************/ - -#ifndef zipr_h -#define zipr_h - -#include <memory_space.h> -#include <dollop_man.h> -#include <set> -#include <utility> - - -namespace Zipr_SDK -{ - using namespace std; - - using PlacementQueue_t = set< pair<Dollop_t*,RangeAddress_t> > ; - - class Zipr_t - { - public: - /* - * These are public functions that the SDK user way want to use. - */ - virtual size_t DetermineDollopEntrySize( - DollopEntry_t*, - bool account_for_fallthrough) = 0; - - virtual RangeAddress_t PlopDollopEntry( - DollopEntry_t *, - RangeAddress_t = 0, - RangeAddress_t = 0) = 0; - - virtual RangeAddress_t PlopDollopEntryWithTarget( - DollopEntry_t *, - RangeAddress_t = 0, - RangeAddress_t = 0) = 0; - - virtual RangeAddress_t PlopDollopEntryWithCallback( - DollopEntry_t *, - RangeAddress_t = 0) = 0; - - virtual MemorySpace_t *GetMemorySpace()=0; - virtual ELFIO::elfio *getELFIO()=0; - virtual IRDB_SDK::FileIR_t *getFileIR()=0; - virtual InstructionLocationMap_t *GetLocationMap()=0; - virtual DollopManager_t *getDollopManager()=0; - virtual PlacementQueue_t* GetPlacementQueue()=0; - virtual void ApplyPatch( - RangeAddress_t from_addr, - RangeAddress_t to_addr)=0; - - }; - -}; - -#endif diff --git a/include/zipr_constants.h b/include/zipr_constants.h deleted file mode 100644 index df9f447..0000000 --- a/include/zipr_constants.h +++ /dev/null @@ -1,44 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. - * - * This software is furnished under a license and/or other restrictive - * terms and may be used and copied only in accordance with such terms - * and the inclusion of the above copyright notice. This software or - * any other copies thereof may not be provided or otherwise made - * available to any other person without the express written consent - * of an authorized representative of Zephyr Software LCC. Title to, - * ownership of, and all rights in the software is retained by - * Zephyr Software LCC. - * - * Zephyr Software LLC. Proprietary Information - * - * Unless otherwise specified, the information contained in this - * directory, following this legend, and/or referenced herein is - * Zephyr Software LLC. (Zephyr) Proprietary Information. - * - * CONTACT - * - * For technical assistance, contact Zephyr Software LCC. at: - * - * - * Zephyr Software, LLC - * 2040 Tremont Rd - * Charlottesville, VA 22911 - * - * E-mail: jwd@zephyr-software.com - **************************************************************************/ - - - -#ifndef ZIPR_CONSTANTS_H -#define ZIPR_CONSTANTS_H - -namespace Zipr_SDK -{ - enum - { - DEFAULT_TRAMPOLINE_SIZE=5 - }; -}; - -#endif diff --git a/include/zipr_options.h b/include/zipr_options.h deleted file mode 100644 index 2f2d1d9..0000000 --- a/include/zipr_options.h +++ /dev/null @@ -1,365 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. - * - * This software is furnished under a license and/or other restrictive - * terms and may be used and copied only in accordance with such terms - * and the inclusion of the above copyright notice. This software or - * any other copies thereof may not be provided or otherwise made - * available to any other person without the express written consent - * of an authorized representative of Zephyr Software LCC. Title to, - * ownership of, and all rights in the software is retained by - * Zephyr Software LCC. - * - * Zephyr Software LLC. Proprietary Information - * - * Unless otherwise specified, the information contained in this - * directory, following this legend, and/or referenced herein is - * Zephyr Software LLC. (Zephyr) Proprietary Information. - * - * CONTACT - * - * For technical assistance, contact Zephyr Software LCC. at: - * - * - * Zephyr Software, LLC - * 2040 Tremont Rd - * Charlottesville, VA 22911 - * - * E-mail: jwd@zephyr-software.com - **************************************************************************/ - -#ifndef zipr_sdk_zipr_plugin_options_h -#define zipr_sdk_zipr_plugin_options_h - -#include <string> -#include <unistd.h> -#include <irdb-core> -#include <iostream> -#include <sstream> - -namespace Zipr_SDK -{ - -enum ZiprOptionType { - ZiprUntypedOptionType, - ZiprIntegerOptionType, - ZiprDoubleOptionType, - ZiprBooleanOptionType, - ZiprStringOptionType, -}; - -class ZiprOption_t -{ - public: - /* - ZiprOption_t(ZiprOptionType type) - : m_key(""), - m_untyped_value(""), - m_observee(NULL), - m_takes_value(true), - m_needs_value(true), - m_required(false), - m_set(false), - m_option_type(type) {}; - */ - ZiprOption_t(ZiprOptionType type, std::string key, std::string value) - : m_key(key), - m_untyped_value(value), - m_observee(NULL), - m_takes_value(true), - m_needs_value(true), - m_required(false), - m_set(false), - m_option_type(type) {}; - - virtual void SetValue(const std::string &value) = 0; - virtual void Set() = 0; - - std::string StringValue() { - return m_untyped_value; - } - std::string Namespace() { - return m_namespace; - } - std::string Key() { - return m_key; - } - void SetNeedsValue(bool needs_value) { - m_needs_value = needs_value; - } - bool NeedsValue() { - return m_needs_value; - } - void setTakesValue(bool takes_value) { - m_takes_value = takes_value; - } - bool TakesValue() { - return m_takes_value; - } - - bool Required() { - return m_required; - } - void SetRequired(bool required) { - m_required = required; - } - bool RequirementMet() { - return (!m_required) || (m_set); - } - - virtual std::string Description() { - return m_description; - } - - - void setDescription(std::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: - std::string m_namespace, m_key, m_untyped_value, m_description; - std::set<ZiprOption_t*> m_observers; - ZiprOption_t *m_observee; - bool m_takes_value, m_needs_value; - bool m_required; - bool m_set; - void SetObserverValues(std::string value) { - std::set<ZiprOption_t*>::const_iterator it = m_observers.begin(); - std::set<ZiprOption_t*>::const_iterator it_end = m_observers.end(); - for (; it!=it_end; it++) - (*it)->SetValue(value); - } - ZiprOptionType m_option_type; -}; -template <typename T> -class ZiprTypedOption_t : public ZiprOption_t -{ - public: - ZiprTypedOption_t(ZiprOptionType type, - std::string key, - std::string value) - : ZiprOption_t(type, key, value) {} - void SetValue(const std::string &value) { - m_set = true; - m_untyped_value = value; - m_value = ConvertToTyped(value); - } - virtual void Set() { - } - - T Value() const { - if (m_observee!=NULL) - { - ZiprTypedOption_t<T> *typed_observee = - static_cast<ZiprTypedOption_t<T>*>(m_observee); - return typed_observee->Value(); - } - return m_value; - } - operator T() const { - return Value(); - }; - virtual std::string Description() { - return std::string( - /* + ((!m_required)?"[ ":"") - + */m_key + ": " - + ((!m_needs_value)?"[":"") - + "<" + TypeName() + ">" - + ((!m_needs_value)?"]":"") - + ((m_description.length())?":\t":"") + m_description - /*+ ((!m_required)?" ]":"")*/); - } - protected: - T m_value; - virtual T ConvertToTyped(std::string) = 0; - virtual std::string ConvertToUntyped(T) = 0; - virtual std::string TypeName() = 0; -}; - -template <class T> -class ZiprCompositeOption_t : public ZiprTypedOption_t<T>, public T -{ - public: - ZiprCompositeOption_t(ZiprOptionType type, - std::string key, - std::string value) - : ZiprTypedOption_t<T>(type, key, value) {} - using ZiprTypedOption_t<T>::ConvertToTyped; - void SetValue(const std::string &value) { - ZiprTypedOption_t<T>::SetValue(value); - T::operator=(ConvertToTyped(value)); - } -}; - -class ZiprStringOption_t : public ZiprCompositeOption_t<std::string> -{ - public: - ZiprStringOption_t(std::string key, std::string value = "") - : ZiprCompositeOption_t(ZiprStringOptionType, key, value) { - SetValue(value); - } - protected: - std::string ConvertToTyped(std::string value_to_convert) { - return std::string(value_to_convert); - } - std::string ConvertToUntyped(std::string value_to_convert) { - return std::string(value_to_convert); - } - std::string TypeName() { - return "string"; - } -}; - -class ZiprBooleanOption_t : public ZiprTypedOption_t<bool> -{ - public: - ZiprBooleanOption_t(std::string key, std::string value = "") - : ZiprTypedOption_t(ZiprBooleanOptionType, key, value) { - m_untyped_value = value; - m_needs_value = false; - } - ZiprBooleanOption_t(std::string key, bool value) - : ZiprTypedOption_t(ZiprBooleanOptionType, key, "") { - m_value = value; - m_needs_value = false; - m_untyped_value = ConvertToUntyped(value); - } - void Set() { - m_untyped_value = "true"; - m_value = true; - m_set = true; - } - - private: - bool ConvertToTyped(std::string value_to_convert) { - if (value_to_convert == "true") - return true; - else - return false; - } - std::string ConvertToUntyped(bool value_to_convert) { - return (value_to_convert ? std::string("true") : std::string("false")); - } - std::string TypeName() { - return "bool"; - } -}; - -class ZiprIntegerOption_t : public ZiprTypedOption_t<int> -{ - public: - ZiprIntegerOption_t(std::string key, std::string value = "") - : ZiprTypedOption_t(ZiprIntegerOptionType, key, value) { - m_value = ConvertToTyped(value); - } - ZiprIntegerOption_t(std::string key, int value) - : ZiprTypedOption_t(ZiprIntegerOptionType, key, "") { - m_value = value; - m_untyped_value = ConvertToUntyped(value); - } - void SetValue(int value) { - m_value = value; - m_untyped_value = ConvertToUntyped(value); - } - private: - int ConvertToTyped(std::string value_to_convert) { - int converted = 0; - char *endptr = NULL; - converted = strtol(value_to_convert.c_str(), - &endptr, 10); - if (*endptr != '\0') - { - m_set = false; - m_untyped_value = ""; - return 0; - } - return converted; - } - std::string ConvertToUntyped(int value_to_convert) { - std::stringstream ss; - ss << value_to_convert; - return ss.str(); - } - std::string TypeName() { - return "integer"; - } -}; - -class ZiprDoubleOption_t : public ZiprTypedOption_t<double> -{ - public: - ZiprDoubleOption_t(std::string key, std::string value = "") - : ZiprTypedOption_t(ZiprDoubleOptionType, key, value) { - m_value = ConvertToTyped(value); - } - ZiprDoubleOption_t(std::string key, double value) - : ZiprTypedOption_t(ZiprDoubleOptionType, key, "") { - m_value = value; - m_untyped_value = ConvertToUntyped(value); - } - void SetValue(double value) { - m_value = value; - m_untyped_value = ConvertToUntyped(value); - } - private: - double ConvertToTyped(std::string value_to_convert) { - double converted = 0.; - char *endptr = NULL; - converted = strtof(value_to_convert.c_str(), - &endptr); - if (*endptr != '\0') - { - m_set = false; - m_untyped_value = ""; - return 0; - } - return converted; - } - std::string ConvertToUntyped(double value_to_convert) { - std::stringstream ss; - ss << value_to_convert; - return ss.str(); - } - std::string TypeName() { - return "double"; - } -}; -class ZiprOptionsNamespace_t : public std::set<ZiprOption_t*> -{ - public: - ZiprOptionsNamespace_t(std::string ns) : m_namespace(ns) {} - std::string Namespace() { - return m_namespace; - }; - void PrintNamespace(); - ZiprOption_t *OptionByKey(std::string key); - void AddOption(ZiprOption_t *option); - void PrintUsage(int tabs, std::ostream &out); - bool RequirementsMet(); - void MergeNamespace(ZiprOptionsNamespace_t*); - private: - std::string m_namespace; -}; - -class ZiprOptions_t { - public: - ZiprOptions_t(int argc, char **argv); - void AddNamespace(ZiprOptionsNamespace_t *); - void PrintNamespaces(); - bool Parse(std::ostream *error=&std::cout, std::ostream *warn=&std::cerr); - ZiprOptionsNamespace_t *Namespace(std::string); - void PrintUsage(std::ostream &out); - bool RequirementsMet(); - private: - std::vector<std::string> m_arguments; - std::set<ZiprOptionsNamespace_t*> m_namespaces; -}; - -} -#endif -- GitLab