Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 928605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:02:17+00:00 2026-05-15T20:02:17+00:00

I’ve got an interesting problem that’s cropped up in a sort of pass based

  • 0

I’ve got an interesting problem that’s cropped up in a sort of pass based compiler of mine. Each pass knows nothing of other passes, and a common object is passed down the chain as it goes, following the chain of command pattern.

The object that is being passed along is a reference to a file.

Now, during one of the stages, one might wish to associate a large chunk of data, such as that file’s SHA512 hash, which requires a reasonable amount of time to compute. However, since that chunk of data is only used in that specific case, I don’t want all file references to need to reserve space for that SHA512. However, I also don’t want other passes to have to recalculate the SHA512 hash over and over again. For example, someone might only accept files which match a given list of SHA512s, but they don’t want that value printed when the file reference gets to the end of the chain, or perhaps they want both, or… .etc.

What I need is some sort of container which contain only one of a given type. If the container does not contain that type, it needs to create an instance of that type and store it somehow. It’s basically a dictionary with the type being the thing used to look things up.

Here’s what I’ve gotten so far, the relevant bit being the FileData::Get<t> method:

class FileData;
// Cache entry interface
struct FileDataCacheEntry
{
    virtual void Initalize(FileData&)
    {
    }
    virtual ~FileDataCacheEntry()
    {
    }
};

// Cache itself
class FileData
{
    struct Entry
    {
        std::size_t identifier;
        FileDataCacheEntry * data;
        Entry(FileDataCacheEntry *dataToStore, std::size_t id)
            : data(dataToStore), identifier(id)
        {
        }
        std::size_t GetIdentifier() const
        {
            return identifier;
        }
        void DeleteData()
        {
            delete data;
        }
    };
    WindowsApi::ReferenceCounter refCount;
    std::wstring fileName_;
    std::vector<Entry> cache;
public:
    FileData(const std::wstring& fileName) : fileName_(fileName)
    {
    }
    ~FileData()
    {
        if (refCount.IsLastObject())
            for_each(cache.begin(), cache.end(), std::mem_fun_ref(&Entry::DeleteData));
    }
    const std::wstring& GetFileName() const
    {
        return fileName_;
    }

    //RELEVANT METHOD HERE
    template<typename T>
    T& Get()
    {
        std::vector<Entry>::iterator foundItem = 
            std::find_if(cache.begin(), cache.end(), boost::bind(
            std::equal_to<std::size_t>(), boost::bind(&Entry::GetIdentifier, _1), T::TypeId));
        if (foundItem == cache.end())
        {
            std::auto_ptr<T> newCacheEntry(new T);
            Entry toInsert(newCacheEntry.get(), T::TypeId);
            cache.push_back(toInsert);
            newCacheEntry.release();
            T& result = *static_cast<T*>(cache.back().data);
            result.Initalize(*this);
            return result;
        }
        else
        {
            return *static_cast<T*>(foundItem->data);
        }
    }
};

// Example item you'd put in cache
class FileBasicData : public FileDataCacheEntry
{
    DWORD    dwFileAttributes;
    FILETIME ftCreationTime;
    FILETIME ftLastAccessTime;
    FILETIME ftLastWriteTime;
    unsigned __int64 size;
public:
    enum
    {
        TypeId = 42
    }
    virtual void Initialize(FileData& input)
    {
        // Get file attributes and friends...
    }
    DWORD GetAttributes() const;
    bool IsArchive() const;
    bool IsCompressed() const;
    bool IsDevice() const;
    // More methods here
};

int main()
{
    // Example use
    FileData fd;
    FileBasicData& data = fd.Get<FileBasicData>();
    // etc
}

For some reason though, this design feels wrong to me, namely because it’s doing a whole bunch of things with untyped pointers. Am I severely off base here? Are there preexisting libraries (boost or otherwise) which would make this clearer/easier to understand?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:02:17+00:00Added an answer on May 15, 2026 at 8:02 pm

    As ergosys said already, std::map is the obvious solution to your problem. But I can see you concerns with RTTI (and the associated bloat). As a matter of fact, an “any” value container does not need RTTI to work. It is sufficient to provide a mapping between a type and an unique identifier. Here is a simple class that provides this mapping:

    #include <stdexcept>
    #include <boost/shared_ptr.hpp>
    class typeinfo
    {
        private:
            typeinfo(const typeinfo&); 
            void operator = (const typeinfo&);
        protected:
            typeinfo(){}
        public:
            bool operator != (const typeinfo &o) const { return this != &o; }
            bool operator == (const typeinfo &o) const { return this == &o; }
            template<class T>
            static const typeinfo & get()
            {
                static struct _ti : public typeinfo {} _inst;
                return _inst;
            }
    };
    

    typeinfo::get<T>() returns a reference to a simple, stateless singleton which allows comparisions.

    This singleton is created only for types T where typeinfo::get< T >() is issued anywhere in the program.

    Now we are using this to implement a top type we call value. value is a holder for a value_box which actually contains the data:

    class value_box
    {
        public:
            // returns the typeinfo of the most derived object
            virtual const typeinfo& type() const =0;
            virtual ~value_box(){}
    };
    
    template<class T>
    class value_box_impl : public value_box
    {
        private:
            friend class value;
            T m_val; 
            value_box_impl(const T &t) : m_val(t) {}
            virtual const typeinfo& type() const
            {
                return typeinfo::get< T >();
            }
    };
    // specialization for void.
    template<>
    class value_box_impl<void> : public value_box
    {
        private:
            friend class value_box;
            virtual const typeinfo& type() const
            {
                return typeinfo::get< void >();
            }
        // This is an optimization to avoid heap pressure for the 
        // allocation of stateless value_box_impl<void> instances:
        void* operator new(size_t) 
        {
            static value_box_impl<void> inst;
            return &inst;
        }
        void operator delete(void* d) 
        {
        }
    
    };
    

    Here’s the bad_value_cast exception:

    class bad_value_cast : public std::runtime_error
    {
        public:
            bad_value_cast(const char *w="") : std::runtime_error(w) {}
    };
    

    And here’s value:

    class value
    {
        private:
            boost::shared_ptr<value_box> m_value_box;       
        public:
            // a default value contains 'void'
            value() : m_value_box( new value_box_impl<void>() ) {}          
                // embedd an object of type T.
            template<class T> 
            value(const T &t) : m_value_box( new value_box_impl<T>(t) ) {}
            // get the typeinfo of the embedded object
            const typeinfo & type() const {  return m_value_box->type(); }
            // convenience type to simplify overloading on return values
            template<class T> struct arg{};
            template<class T>
            T convert(arg<T>) const
            {
                if (type() != typeinfo::get<T>())
                    throw bad_value_cast(); 
                // this is safe now
                value_box_impl<T> *impl=
                          static_cast<value_box_impl<T>*>(m_value_box.get());
                return impl->m_val;
            }
            void convert(arg<void>) const
            {
                if (type() != typeinfo::get<void>())
                    throw bad_value_cast(); 
            }
    };
    

    The convenient casting syntax:

    template<class T>
    T value_cast(const value &v) 
    {
        return v.convert(value::arg<T>());
    }
    

    And that’s it. Here is how it looks like:

    #include <string>
    #include <map>
    #include <iostream>
    int main()
    {
        std::map<std::string,value> v;
        v["zero"]=0;
        v["pi"]=3.14159;
        v["password"]=std::string("swordfish");
        std::cout << value_cast<int>(v["zero"]) << std::endl;
        std::cout << value_cast<double>(v["pi"]) << std::endl;
        std::cout << value_cast<std::string>(v["password"]) << std::endl;   
    }
    

    The nice thing about having you own implementation of any is, that you can very easily tailor it to the features you actually need, which is quite tedious with boost::any. For example, there are few requirements on the types that value can store: they need to be copy-constructible and have a public destructor. What if all types you use have an operator<<(ostream&,T) and you want a way to print your dictionaries? Just add a to_stream method to box and overload operator<< for value and you can write:

    std::cout << v["zero"] << std::endl;
    std::cout << v["pi"] << std::endl;
    std::cout << v["password"] << std::endl;
    

    Here’s a pastebin with the above, should compile out of the box with g++/boost: http://pastebin.com/v0nJwVLW

    EDIT: Added an optimization to avoid the allocation of box_impl< void > from the heap:
    http://pastebin.com/pqA5JXhA

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.